mirror of https://github.com/icsharpcode/ILSpy.git
20 changed files with 45 additions and 311 deletions
@ -1,223 +0,0 @@
@@ -1,223 +0,0 @@
|
||||
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
// software and associated documentation files (the "Software"), to deal in the Software
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using ICSharpCode.Decompiler.TypeSystem; |
||||
using Mono.Cecil; |
||||
using Mono.Cecil.Cil; |
||||
using ArrayType = Mono.Cecil.ArrayType; |
||||
|
||||
namespace ICSharpCode.Decompiler |
||||
{ |
||||
/// <summary>
|
||||
/// Cecil helper methods.
|
||||
/// </summary>
|
||||
public static class CecilExtensions |
||||
{ |
||||
public static string OffsetToString(int offset) |
||||
{ |
||||
return string.Format("IL_{0:x4}", offset); |
||||
} |
||||
|
||||
public static string OffsetToString(long offset) |
||||
{ |
||||
return string.Format("IL_{0:x4}", offset); |
||||
} |
||||
|
||||
public static FieldDefinition ResolveWithinSameModule(this FieldReference field) |
||||
{ |
||||
if (field != null && field.DeclaringType.GetElementType().Module == field.Module) |
||||
return field.Resolve(); |
||||
else |
||||
return null; |
||||
} |
||||
|
||||
public static MethodDefinition ResolveWithinSameModule(this MethodReference method) |
||||
{ |
||||
if (method != null && method.DeclaringType.GetElementType().Module == method.Module) |
||||
return method.Resolve(); |
||||
else |
||||
return null; |
||||
} |
||||
|
||||
public static TypeDefinition ResolveOrThrow(this TypeReference typeReference) |
||||
{ |
||||
var resolved = typeReference.Resolve(); |
||||
if (resolved == null) |
||||
throw new ReferenceResolvingException(); |
||||
return resolved; |
||||
} |
||||
|
||||
public static bool IsCompilerGenerated(this ICustomAttributeProvider provider) |
||||
{ |
||||
if (provider != null && provider.HasCustomAttributes) { |
||||
foreach (CustomAttribute a in provider.CustomAttributes) { |
||||
if (a.AttributeType.FullName == "System.Runtime.CompilerServices.CompilerGeneratedAttribute") |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static bool IsCompilerGeneratedOrIsInCompilerGeneratedClass(this IMemberDefinition member) |
||||
{ |
||||
if (member == null) |
||||
return false; |
||||
if (member.IsCompilerGenerated()) |
||||
return true; |
||||
return IsCompilerGeneratedOrIsInCompilerGeneratedClass(member.DeclaringType); |
||||
} |
||||
|
||||
public static TypeReference GetEnumUnderlyingType(this TypeDefinition type) |
||||
{ |
||||
if (!type.IsEnum) |
||||
throw new ArgumentException("Type must be an enum", nameof(type)); |
||||
|
||||
var fields = type.Fields; |
||||
|
||||
for (int i = 0; i < fields.Count; i++) |
||||
{ |
||||
var field = fields[i]; |
||||
if (!field.IsStatic) |
||||
return field.FieldType; |
||||
} |
||||
|
||||
throw new NotSupportedException(); |
||||
} |
||||
|
||||
public static bool IsAnonymousType(this TypeReference type) |
||||
{ |
||||
if (type == null) |
||||
return false; |
||||
if (string.IsNullOrEmpty(type.Namespace) && type.HasGeneratedName() && (type.Name.Contains("AnonType") || type.Name.Contains("AnonymousType"))) { |
||||
TypeDefinition td = type.Resolve(); |
||||
return td != null && td.IsCompilerGenerated(); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static bool HasGeneratedName(this MemberReference member) |
||||
{ |
||||
return member.Name.StartsWith("<", StringComparison.Ordinal); |
||||
} |
||||
|
||||
public static bool ContainsAnonymousType(this TypeReference type) |
||||
{ |
||||
GenericInstanceType git = type as GenericInstanceType; |
||||
if (git != null) { |
||||
if (IsAnonymousType(git)) |
||||
return true; |
||||
for (int i = 0; i < git.GenericArguments.Count; i++) { |
||||
if (git.GenericArguments[i].ContainsAnonymousType()) |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
TypeSpecification typeSpec = type as TypeSpecification; |
||||
if (typeSpec != null) |
||||
return typeSpec.ElementType.ContainsAnonymousType(); |
||||
else |
||||
return false; |
||||
} |
||||
|
||||
public static string GetDefaultMemberName(this TypeDefinition type) |
||||
{ |
||||
CustomAttribute attr; |
||||
return type.GetDefaultMemberName(out attr); |
||||
} |
||||
|
||||
public static string GetDefaultMemberName(this TypeDefinition type, out CustomAttribute defaultMemberAttribute) |
||||
{ |
||||
if (type.HasCustomAttributes) |
||||
foreach (CustomAttribute ca in type.CustomAttributes) |
||||
if (ca.Constructor.DeclaringType.Name == "DefaultMemberAttribute" && ca.Constructor.DeclaringType.Namespace == "System.Reflection" |
||||
&& ca.Constructor.FullName == @"System.Void System.Reflection.DefaultMemberAttribute::.ctor(System.String)") { |
||||
defaultMemberAttribute = ca; |
||||
return ca.ConstructorArguments[0].Value as string; |
||||
} |
||||
defaultMemberAttribute = null; |
||||
return null; |
||||
} |
||||
|
||||
public static bool IsIndexer(this PropertyDefinition property) |
||||
{ |
||||
CustomAttribute attr; |
||||
return property.IsIndexer(out attr); |
||||
} |
||||
|
||||
public static bool IsIndexer(this PropertyDefinition property, out CustomAttribute defaultMemberAttribute) |
||||
{ |
||||
defaultMemberAttribute = null; |
||||
if (property.HasParameters) { |
||||
var accessor = property.GetMethod ?? property.SetMethod; |
||||
PropertyDefinition basePropDef = property; |
||||
if (accessor.HasOverrides) { |
||||
// if the property is explicitly implementing an interface, look up the property in the interface:
|
||||
MethodDefinition baseAccessor = accessor.Overrides.First().Resolve(); |
||||
if (baseAccessor != null) { |
||||
foreach (PropertyDefinition baseProp in baseAccessor.DeclaringType.Properties) { |
||||
if (baseProp.GetMethod == baseAccessor || baseProp.SetMethod == baseAccessor) { |
||||
basePropDef = baseProp; |
||||
break; |
||||
} |
||||
} |
||||
} else |
||||
return false; |
||||
} |
||||
CustomAttribute attr; |
||||
var defaultMemberName = basePropDef.DeclaringType.GetDefaultMemberName(out attr); |
||||
if (defaultMemberName == basePropDef.Name) { |
||||
defaultMemberAttribute = attr; |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static bool IsUnconditionalBranch(this OpCode opcode) |
||||
{ |
||||
if (opcode.OpCodeType == OpCodeType.Prefix) |
||||
return false; |
||||
switch (opcode.FlowControl) { |
||||
case FlowControl.Branch: |
||||
case FlowControl.Throw: |
||||
case FlowControl.Return: |
||||
return true; |
||||
case FlowControl.Next: |
||||
case FlowControl.Call: |
||||
case FlowControl.Cond_Branch: |
||||
return false; |
||||
default: |
||||
throw new NotSupportedException(opcode.FlowControl.ToString()); |
||||
} |
||||
} |
||||
|
||||
public static bool IsDelegate(this TypeDefinition type) |
||||
{ |
||||
if (type.BaseType != null && type.BaseType.Namespace == "System") { |
||||
if (type.BaseType.Name == "MulticastDelegate") |
||||
return true; |
||||
if (type.BaseType.Name == "Delegate" && type.Name != "MulticastDelegate") |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue