Browse Source

Rename DynamicAwareTypeReference to DynamicTypeReference

pull/1198/head
Siegfried Pammer 7 years ago
parent
commit
cbb1f204e1
  1. 2
      ICSharpCode.Decompiler/IL/ILReader.cs
  2. 17
      ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs
  3. 18
      ICSharpCode.Decompiler/TypeSystem/Implementation/TypeSpecification.cs
  4. 24
      ICSharpCode.Decompiler/TypeSystem/MetadataLoader.cs
  5. 2
      ILSpy/Languages/CSharpLanguage.cs

2
ICSharpCode.Decompiler/IL/ILReader.cs

@ -86,7 +86,7 @@ namespace ICSharpCode.Decompiler.IL
this.currentStack = ImmutableStack<ILVariable>.Empty; this.currentStack = ImmutableStack<ILVariable>.Empty;
this.unionFind = new UnionFind<ILVariable>(); this.unionFind = new UnionFind<ILVariable>();
this.stackMismatchPairs = new List<(ILVariable, ILVariable)>(); this.stackMismatchPairs = new List<(ILVariable, ILVariable)>();
this.methodReturnStackType = typeSystem.ResolveFromSignature(TypeSystem.Implementation.DynamicAwareTypeReference.Create(methodSignature.ReturnType, methodDefinition.GetCustomAttributes(), metadata)).GetStackType(); this.methodReturnStackType = typeSystem.ResolveFromSignature(TypeSystem.Implementation.DynamicTypeReference.Create(methodSignature.ReturnType, methodDefinition.GetCustomAttributes(), metadata)).GetStackType();
InitParameterVariables(); InitParameterVariables();
localVariables = InitLocalVariables(); localVariables = InitLocalVariables();
if (body.LocalVariablesInitialized) { if (body.LocalVariablesInitialized) {

17
ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs

@ -28,12 +28,6 @@ namespace ICSharpCode.Decompiler.TypeSystem
/// </summary> /// </summary>
readonly MetadataLoader typeReferenceCecilLoader = new MetadataLoader(); readonly MetadataLoader typeReferenceCecilLoader = new MetadataLoader();
/// <summary>
/// Dictionary for NRefactory->Cecil lookup.
/// May only be accessed within lock(entityDict)
/// </summary>
Dictionary<IUnresolvedEntity, SRM.EntityHandle> entityDict = new Dictionary<IUnresolvedEntity, SRM.EntityHandle>();
Dictionary<SRM.EntityHandle, IField> fieldLookupCache = new Dictionary<SRM.EntityHandle, IField>(); Dictionary<SRM.EntityHandle, IField> fieldLookupCache = new Dictionary<SRM.EntityHandle, IField>();
Dictionary<SRM.EntityHandle, IProperty> propertyLookupCache = new Dictionary<SRM.EntityHandle, IProperty>(); Dictionary<SRM.EntityHandle, IProperty> propertyLookupCache = new Dictionary<SRM.EntityHandle, IProperty>();
Dictionary<SRM.EntityHandle, IMethod> methodLookupCache = new Dictionary<SRM.EntityHandle, IMethod>(); Dictionary<SRM.EntityHandle, IMethod> methodLookupCache = new Dictionary<SRM.EntityHandle, IMethod>();
@ -44,7 +38,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
if (moduleDefinition == null) if (moduleDefinition == null)
throw new ArgumentNullException(nameof(moduleDefinition)); throw new ArgumentNullException(nameof(moduleDefinition));
this.moduleDefinition = moduleDefinition; this.moduleDefinition = moduleDefinition;
MetadataLoader cecilLoader = new MetadataLoader { IncludeInternalMembers = true, LazyLoad = true, OnEntityLoaded = StoreMemberReference, ShortenInterfaceImplNames = false }; MetadataLoader cecilLoader = new MetadataLoader { IncludeInternalMembers = true, LazyLoad = true, ShortenInterfaceImplNames = false };
typeReferenceCecilLoader.SetCurrentModule(moduleDefinition); typeReferenceCecilLoader.SetCurrentModule(moduleDefinition);
IUnresolvedAssembly mainAssembly = cecilLoader.LoadModule(moduleDefinition); IUnresolvedAssembly mainAssembly = cecilLoader.LoadModule(moduleDefinition);
// Load referenced assemblies and type-forwarder references. // Load referenced assemblies and type-forwarder references.
@ -91,13 +85,6 @@ namespace ICSharpCode.Decompiler.TypeSystem
public SRM.MetadataReader GetMetadata() => moduleDefinition.GetMetadataReader(); public SRM.MetadataReader GetMetadata() => moduleDefinition.GetMetadataReader();
void StoreMemberReference(IUnresolvedEntity entity, SRM.EntityHandle mr)
{
// This is a callback from the type system, which is multi-threaded and may be accessed externally
lock (entityDict)
entityDict[entity] = mr;
}
public IType ResolveFromSignature(ITypeReference typeReference) public IType ResolveFromSignature(ITypeReference typeReference)
{ {
return typeReference.Resolve(context); return typeReference.Resolve(context);
@ -159,7 +146,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
case SRM.HandleKind.FieldDefinition: case SRM.HandleKind.FieldDefinition:
var fieldDef = metadata.GetFieldDefinition((SRM.FieldDefinitionHandle)fieldReference); var fieldDef = metadata.GetFieldDefinition((SRM.FieldDefinitionHandle)fieldReference);
declaringType = ResolveAsType(fieldDef.GetDeclaringType()); declaringType = ResolveAsType(fieldDef.GetDeclaringType());
returnType = DynamicAwareTypeReference.Create(fieldDef.DecodeSignature(TypeReferenceSignatureDecoder.Instance, default), fieldDef.GetCustomAttributes(), metadata); returnType = DynamicTypeReference.Create(fieldDef.DecodeSignature(TypeReferenceSignatureDecoder.Instance, default), fieldDef.GetCustomAttributes(), metadata);
var declaringTypeDefinition = declaringType.GetDefinition(); var declaringTypeDefinition = declaringType.GetDefinition();
if (declaringTypeDefinition == null) if (declaringTypeDefinition == null)
field = CreateFakeField(declaringType, metadata.GetString(fieldDef.Name), returnType); field = CreateFakeField(declaringType, metadata.GetString(fieldDef.Name), returnType);

18
ICSharpCode.Decompiler/TypeSystem/Implementation/TypeSpecification.cs

@ -87,19 +87,19 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
} }
} }
public sealed class DynamicAwareTypeReference : TypeVisitor, ITypeReference public sealed class DynamicTypeReference : TypeVisitor, ITypeReference
{ {
readonly ITypeReference reference; readonly ITypeReference reference;
readonly bool isDynamic;
readonly bool[] dynamicInfo; readonly bool[] dynamicInfo;
int typeIndex; int typeIndex;
static readonly ITypeResolveContext minimalCorlibContext = new SimpleTypeResolveContext(MinimalCorlib.Instance.CreateCompilation()); static readonly ITypeResolveContext minimalCorlibContext = new SimpleTypeResolveContext(MinimalCorlib.Instance.CreateCompilation());
public static DynamicAwareTypeReference Create(ITypeReference reference, SRM.CustomAttributeHandleCollection? customAttributes, SRM.MetadataReader metadata) public static ITypeReference Create(ITypeReference reference, SRM.CustomAttributeHandleCollection? customAttributes, SRM.MetadataReader metadata)
{ {
bool isDynamic = HasDynamicAttribute(customAttributes, metadata, out var dynamicInfo); if (HasDynamicAttribute(customAttributes, metadata, out var dynamicInfo))
return new DynamicAwareTypeReference(reference, isDynamic, dynamicInfo); return new DynamicTypeReference(reference, dynamicInfo);
return reference;
} }
public static bool HasDynamicAttribute(SRM.CustomAttributeHandleCollection? attributes, SRM.MetadataReader metadata, out bool[] mapping) public static bool HasDynamicAttribute(SRM.CustomAttributeHandleCollection? attributes, SRM.MetadataReader metadata, out bool[] mapping)
@ -126,19 +126,15 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
return false; return false;
} }
DynamicAwareTypeReference(ITypeReference reference, bool isDynamic, bool[] dynamicInfo) DynamicTypeReference(ITypeReference reference, bool[] dynamicInfo)
{ {
this.reference = reference; this.reference = reference;
this.isDynamic = isDynamic;
this.dynamicInfo = dynamicInfo; this.dynamicInfo = dynamicInfo;
} }
public IType Resolve(ITypeResolveContext context) public IType Resolve(ITypeResolveContext context)
{ {
if (isDynamic)
return reference.Resolve(context).AcceptVisitor(this); return reference.Resolve(context).AcceptVisitor(this);
else
return reference.Resolve(context);
} }
public override IType VisitPointerType(PointerType type) public override IType VisitPointerType(PointerType type)
@ -176,8 +172,6 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
public override IType VisitTypeDefinition(ITypeDefinition type) public override IType VisitTypeDefinition(ITypeDefinition type)
{ {
if (!isDynamic)
return type;
if (type.KnownTypeCode == KnownTypeCode.Object) { if (type.KnownTypeCode == KnownTypeCode.Object) {
if (dynamicInfo == null || typeIndex >= dynamicInfo.Length) if (dynamicInfo == null || typeIndex >= dynamicInfo.Length)
return SpecialType.Dynamic; return SpecialType.Dynamic;

24
ICSharpCode.Decompiler/TypeSystem/MetadataLoader.cs

@ -92,7 +92,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
/// Warning: if delay-loading is used and the type system is accessed by multiple threads, /// Warning: if delay-loading is used and the type system is accessed by multiple threads,
/// the callback may be invoked concurrently on multiple threads. /// the callback may be invoked concurrently on multiple threads.
/// </remarks> /// </remarks>
public Action<IUnresolvedEntity, EntityHandle> OnEntityLoaded { get; set; } public Action<IUnresolvedEntity> OnEntityLoaded { get; set; }
bool shortenInterfaceImplNames = true; bool shortenInterfaceImplNames = true;
@ -212,7 +212,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
if (this.LazyLoad) { if (this.LazyLoad) {
var t = new LazySRMTypeDefinition(cecilLoaderCloneForLazyLoading, module, h); var t = new LazySRMTypeDefinition(cecilLoaderCloneForLazyLoading, module, h);
currentAssembly.AddTypeDefinition(t); currentAssembly.AddTypeDefinition(t);
RegisterCecilObject(t, h); RegisterCecilObject(t);
} else { } else {
var t = CreateTopLevelTypeDefinition(h, td); var t = CreateTopLevelTypeDefinition(h, td);
cecilTypeDefs.Add(h); cecilTypeDefs.Add(h);
@ -303,7 +303,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
switch (type.Kind) { switch (type.Kind) {
case HandleKind.TypeSpecification: case HandleKind.TypeSpecification:
return DynamicAwareTypeReference.Create(currentMetadata.GetTypeSpecification((TypeSpecificationHandle)type) return DynamicTypeReference.Create(currentMetadata.GetTypeSpecification((TypeSpecificationHandle)type)
.DecodeSignature(TypeReferenceSignatureDecoder.Instance, default), typeAttributes, currentMetadata); .DecodeSignature(TypeReferenceSignatureDecoder.Instance, default), typeAttributes, currentMetadata);
case HandleKind.TypeReference: case HandleKind.TypeReference:
return CreateTypeReference((TypeReferenceHandle)type); return CreateTypeReference((TypeReferenceHandle)type);
@ -798,7 +798,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
InitMembers(typeDefinition, td, td.Members); InitMembers(typeDefinition, td, td.Members);
td.ApplyInterningProvider(interningProvider); td.ApplyInterningProvider(interningProvider);
td.Freeze(); td.Freeze();
RegisterCecilObject(td, handle); RegisterCecilObject(td);
} }
void InitBaseTypes(TypeDefinitionHandle handle, IList<ITypeReference> baseTypes) void InitBaseTypes(TypeDefinitionHandle handle, IList<ITypeReference> baseTypes)
@ -1224,7 +1224,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
int j = 0; int j = 0;
if (signature.RequiredParameterCount > parameters.Length) { if (signature.RequiredParameterCount > parameters.Length) {
foreach (var parameterType in signature.ParameterTypes) { foreach (var parameterType in signature.ParameterTypes) {
m.Parameters.Add(new DefaultUnresolvedParameter(DynamicAwareTypeReference.Create(parameterType, null, currentMetadata), string.Empty)); m.Parameters.Add(new DefaultUnresolvedParameter(DynamicTypeReference.Create(parameterType, null, currentMetadata), string.Empty));
} }
} else { } else {
foreach (var p in parameters) { foreach (var p in parameters) {
@ -1296,7 +1296,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
attributes = par.GetCustomAttributes(); attributes = par.GetCustomAttributes();
} }
} }
return DynamicAwareTypeReference.Create(returnType, attributes, currentMetadata); return DynamicTypeReference.Create(returnType, attributes, currentMetadata);
} }
static bool HasExtensionAttribute(MetadataReader currentModule, CustomAttributeHandleCollection attributes) static bool HasExtensionAttribute(MetadataReader currentModule, CustomAttributeHandleCollection attributes)
@ -1368,7 +1368,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
#region Read Parameter #region Read Parameter
public IUnresolvedParameter ReadParameter(Parameter parameter, ITypeReference type) public IUnresolvedParameter ReadParameter(Parameter parameter, ITypeReference type)
{ {
var p = new DefaultUnresolvedParameter(DynamicAwareTypeReference.Create(type, parameter.GetCustomAttributes(), currentMetadata), interningProvider.Intern(currentMetadata.GetString(parameter.Name))); var p = new DefaultUnresolvedParameter(DynamicTypeReference.Create(type, parameter.GetCustomAttributes(), currentMetadata), interningProvider.Intern(currentMetadata.GetString(parameter.Name)));
if (type is ByReferenceTypeReference) { if (type is ByReferenceTypeReference) {
if ((parameter.Attributes & ParameterAttributes.In) == 0 && (parameter.Attributes & ParameterAttributes.Out) != 0) if ((parameter.Attributes & ParameterAttributes.In) == 0 && (parameter.Attributes & ParameterAttributes.Out) != 0)
@ -1479,7 +1479,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
f.Accessibility = GetAccessibility(field.Attributes); f.Accessibility = GetAccessibility(field.Attributes);
f.IsReadOnly = (field.Attributes & FieldAttributes.InitOnly) == FieldAttributes.InitOnly; f.IsReadOnly = (field.Attributes & FieldAttributes.InitOnly) == FieldAttributes.InitOnly;
f.IsStatic = (field.Attributes & FieldAttributes.Static) == FieldAttributes.Static; f.IsStatic = (field.Attributes & FieldAttributes.Static) == FieldAttributes.Static;
f.ReturnType = DynamicAwareTypeReference.Create(field.DecodeSignature(TypeReferenceSignatureDecoder.Instance, default), field.GetCustomAttributes(), currentMetadata); f.ReturnType = DynamicTypeReference.Create(field.DecodeSignature(TypeReferenceSignatureDecoder.Instance, default), field.GetCustomAttributes(), currentMetadata);
var constantHandle = field.GetDefaultValue(); var constantHandle = field.GetDefaultValue();
if (!constantHandle.IsNil) { if (!constantHandle.IsNil) {
var constant = currentMetadata.GetConstant(constantHandle); var constant = currentMetadata.GetConstant(constantHandle);
@ -1609,7 +1609,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
int i = 0; int i = 0;
if (signature.RequiredParameterCount > parameterHandles.Length) { if (signature.RequiredParameterCount > parameterHandles.Length) {
foreach (var parameterType in signature.ParameterTypes) { foreach (var parameterType in signature.ParameterTypes) {
p.Parameters.Add(new DefaultUnresolvedParameter(DynamicAwareTypeReference.Create(parameterType, null, currentMetadata), string.Empty)); p.Parameters.Add(new DefaultUnresolvedParameter(DynamicTypeReference.Create(parameterType, null, currentMetadata), string.Empty));
} }
} else { } else {
foreach (var h in parameterHandles) { foreach (var h in parameterHandles) {
@ -1681,14 +1681,14 @@ namespace ICSharpCode.Decompiler.TypeSystem
member.MetadataToken = cecilDefinition; member.MetadataToken = cecilDefinition;
member.ApplyInterningProvider(interningProvider); member.ApplyInterningProvider(interningProvider);
member.Freeze(); member.Freeze();
RegisterCecilObject(member, cecilDefinition); RegisterCecilObject(member);
} }
#endregion #endregion
#region Type system translation table #region Type system translation table
void RegisterCecilObject(IUnresolvedEntity typeSystemObject, EntityHandle cecilObject) void RegisterCecilObject(IUnresolvedEntity typeSystemObject)
{ {
OnEntityLoaded?.Invoke(typeSystemObject, cecilObject); OnEntityLoaded?.Invoke(typeSystemObject);
} }
#endregion #endregion
} }

2
ILSpy/Languages/CSharpLanguage.cs

@ -683,7 +683,7 @@ namespace ICSharpCode.ILSpy
public InsertDynamicTypeVisitor(MetadataReader metadata, CustomAttributeHandleCollection? customAttributes) public InsertDynamicTypeVisitor(MetadataReader metadata, CustomAttributeHandleCollection? customAttributes)
{ {
isDynamic = DynamicAwareTypeReference.HasDynamicAttribute(customAttributes, metadata, out mapping); isDynamic = DynamicTypeReference.HasDynamicAttribute(customAttributes, metadata, out mapping);
} }
public override void VisitComposedType(ComposedType composedType) public override void VisitComposedType(ComposedType composedType)

Loading…
Cancel
Save