2using System.Collections.Immutable;
3using System.Diagnostics;
5using System.Reflection.Metadata.Ecma335;
27 static bool IsTypeDefinition(Type type)
29 return type.HasElementType ==
false && type.IsConstructedGenericType ==
false && type.IsGenericParameter ==
false;
32 readonly Module _underlyingModule;
47 _underlyingModule = underlyingModule ??
throw new ArgumentNullException(nameof(underlyingModule));
53 internal Module UnderlyingModule => _underlyingModule;
64 throw new ArgumentNullException(nameof(type));
66 Debug.Assert(type.Module == _underlyingModule);
69 if (IsTypeDefinition(type) ==
false)
70 return GetOrCreateTypeSymbolForSpecification(type);
73 var hnd = MetadataTokens.TypeDefinitionHandle(type.MetadataToken);
74 var row = MetadataTokens.GetRowNumber(hnd);
77 if (_typesSource ==
null)
79 _typesSource = _underlyingModule.GetTypes().OrderBy(i => i.MetadataToken).ToArray();
80 _typesBaseRow = _typesSource.Length != 0 ? MetadataTokens.GetRowNumber(MetadataTokens.MethodDefinitionHandle(_typesSource[0].MetadataToken)) : 0;
84 _types ??=
new IkvmReflectionTypeSymbol?[_typesSource.Length];
87 var idx = row - _typesBaseRow;
89 throw new Exception();
91 Debug.Assert(idx >= 0);
92 Debug.Assert(idx < _typesSource.Length);
95 if (_types.Length < idx)
96 throw new IndexOutOfRangeException();
99 if (_types[idx] ==
null)
100 Interlocked.CompareExchange(
ref _types[idx],
new IkvmReflectionTypeSymbol(
Context,
this, type),
null);
103 if (_types[idx] is not IkvmReflectionTypeSymbol sym)
104 throw new InvalidOperationException();
114 IkvmReflectionTypeSymbol GetOrCreateTypeSymbolForSpecification(Type type)
117 throw new ArgumentNullException(nameof(type));
119 Debug.Assert(type.Module == _underlyingModule);
121 if (type.GetElementType() is { } elementType)
123 var elementTypeSymbol = GetOrCreateTypeSymbol(elementType);
127 return (IkvmReflectionTypeSymbol)elementTypeSymbol.MakeArrayType(type.GetArrayRank());
130 return (IkvmReflectionTypeSymbol)elementTypeSymbol.MakePointerType();
133 return (IkvmReflectionTypeSymbol)elementTypeSymbol.MakeByRefType();
135 throw new InvalidOperationException();
138 if (type.IsGenericType)
140 var definitionType = type.GetGenericTypeDefinition();
141 var definitionTypeSymbol = GetOrCreateTypeSymbol(definitionType);
142 return definitionTypeSymbol.GetOrCreateGenericTypeSymbol(type.GetGenericArguments());
146 if (type.IsGenericParameter && type.DeclaringMethod is
null && type.DeclaringType is not
null)
148 var declaringType = GetOrCreateTypeSymbol(type.DeclaringType);
149 return declaringType.GetOrCreateGenericParameterSymbol(type);
153 if (type.IsGenericParameter && type.DeclaringMethod is not
null && type.DeclaringMethod.DeclaringType is not
null)
155 var declaringMethod = GetOrCreateTypeSymbol(type.DeclaringMethod.DeclaringType);
156 return declaringMethod.GetOrCreateGenericParameterSymbol(type);
159 throw new InvalidOperationException();
175 public string Name => _underlyingModule.Name;
181 public override bool IsMissing => _underlyingModule.__IsMissing;
186 return _underlyingModule.GetField(name) is { } f ? ResolveFieldSymbol(f) :
null;
192 return _underlyingModule.GetField(name, (BindingFlags)bindingAttr) is { } f ? ResolveFieldSymbol(f) :
null;
198 return ResolveFieldSymbols(_underlyingModule.GetFields((BindingFlags)bindingFlags));
204 return ResolveFieldSymbols(_underlyingModule.GetFields());
210 return _underlyingModule.GetMethod(name) is { } m ? ResolveMethodSymbol(m) :
null;
216 return _underlyingModule.GetMethod(name, UnpackTypeSymbols(types)) is { } m ? ResolveMethodSymbol(m) :
null;
220 public IMethodSymbol?
GetMethod(
string name, global::System.Reflection.BindingFlags bindingAttr, global::System.Reflection.CallingConventions callConvention,
ITypeSymbol[] types, global::System.Reflection.ParameterModifier[]? modifiers)
222 if (modifiers !=
null)
223 throw new NotImplementedException();
225 return _underlyingModule.GetMethod(name, (BindingFlags)bindingAttr,
null, (CallingConventions)callConvention, UnpackTypeSymbols(types),
null) is { } m ? ResolveMethodSymbol(m) :
null;
231 return ResolveMethodSymbols(_underlyingModule.GetMethods());
237 return ResolveMethodSymbols(_underlyingModule.GetMethods((BindingFlags)bindingFlags));
243 return _underlyingModule.GetType(className) is { } t ? ResolveTypeSymbol(t) :
null;
249 return _underlyingModule.GetType(className, ignoreCase) is { } t ? ResolveTypeSymbol(t) :
null;
255 return _underlyingModule.GetType(className, throwOnError, ignoreCase) is { } t ? ResolveTypeSymbol(t) :
null;
261 return ResolveTypeSymbols(_underlyingModule.GetTypes());
267 return _underlyingModule.IsResource();
273 return _underlyingModule.ResolveField(metadataToken) is { } f ? ResolveFieldSymbol(f) :
null;
279 var _genericTypeArguments = genericTypeArguments !=
null ? UnpackTypeSymbols(genericTypeArguments) :
null;
280 var _genericMethodArguments = genericMethodArguments !=
null ? UnpackTypeSymbols(genericMethodArguments) :
null;
281 return _underlyingModule.ResolveField(metadataToken, _genericTypeArguments, _genericMethodArguments) is { } f ? ResolveFieldSymbol(f) :
null;
287 return _underlyingModule.ResolveMember(metadataToken) is { } m ?
ResolveMemberSymbol(m) :
null;
293 var _genericTypeArguments = genericTypeArguments !=
null ? UnpackTypeSymbols(genericTypeArguments) :
null;
294 var _genericMethodArguments = genericMethodArguments !=
null ? UnpackTypeSymbols(genericMethodArguments) :
null;
295 return _underlyingModule.ResolveMember(metadataToken, _genericTypeArguments, _genericMethodArguments) is { } m ?
ResolveMemberSymbol(m) :
null;
301 var _genericTypeArguments = genericTypeArguments !=
null ? UnpackTypeSymbols(genericTypeArguments) :
null;
302 var _genericMethodArguments = genericMethodArguments !=
null ? UnpackTypeSymbols(genericMethodArguments) :
null;
303 return _underlyingModule.ResolveMethod(metadataToken, _genericTypeArguments, _genericMethodArguments) is { } m ? ResolveMethodBaseSymbol(m) :
null;
309 return _underlyingModule.ResolveMethod(metadataToken) is { } m ? ResolveMethodBaseSymbol(m) :
null;
315 return _underlyingModule.ResolveSignature(metadataToken);
321 return _underlyingModule.ResolveString(metadataToken);
327 return ResolveTypeSymbol(_underlyingModule.ResolveType(metadataToken));
333 var _genericTypeArguments = genericTypeArguments !=
null ? UnpackTypeSymbols(genericTypeArguments) :
null;
334 var _genericMethodArguments = genericMethodArguments !=
null ? UnpackTypeSymbols(genericMethodArguments) :
null;
335 return ResolveTypeSymbol(_underlyingModule.ResolveType(metadataToken, _genericTypeArguments, _genericMethodArguments));
341 return ResolveCustomAttributes(_underlyingModule.GetCustomAttributesData());
347 return ResolveCustomAttributes(_underlyingModule.__GetCustomAttributes(((
IkvmReflectionTypeSymbol)attributeType).UnderlyingType,
false));
System.Threading.Interlocked Interlocked
global::java.lang.invoke.LambdaForm.Name Name
Implementation of IModuleSymbol derived from System.Reflection.
ITypeSymbol ResolveType(int metadataToken)
Returns the type identified by the specified metadata token.
IMethodSymbol? GetMethod(string name, ITypeSymbol[] types)
Returns a method having the specified name and parameter types.
IMethodSymbol? GetMethod(string name, global::System.Reflection.BindingFlags bindingAttr, global::System.Reflection.CallingConventions callConvention, ITypeSymbol[] types, global::System.Reflection.ParameterModifier[]? modifiers)
IFieldSymbol[] GetFields(global::System.Reflection.BindingFlags bindingFlags)
IFieldSymbol? ResolveField(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the field identified by the specified metadata token, in the context defined by the specified...
IMemberSymbol? ResolveMember(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the type or member identified by the specified metadata token, in the context defined by the ...
string ResolveString(int metadataToken)
Returns the string identified by the specified metadata token.
ITypeSymbol? GetType(string className, bool throwOnError, bool ignoreCase)
Returns the specified type, specifying whether to make a case-sensitive search of the module and whet...
IMethodBaseSymbol? ResolveMethod(int metadataToken)
Returns the method or constructor identified by the specified metadata token.
byte[] ResolveSignature(int metadataToken)
Returns the signature blob identified by a metadata token.
IkvmReflectionModuleSymbol(IkvmReflectionSymbolContext context, Module underlyingModule)
Initializes a new instance.
ITypeSymbol? GetType(string className, bool ignoreCase)
Returns the specified type, searching the module with the specified case sensitivity.
IMemberSymbol? ResolveMember(int metadataToken)
Returns the type or member identified by a metadata token.
IMethodSymbol[] GetMethods()
Returns the global methods defined on the module.
IMethodSymbol? GetMethod(string name)
Returns a method having the specified name.
IMethodSymbol[] GetMethods(global::System.Reflection.BindingFlags bindingFlags)
IFieldSymbol? GetField(string name)
Returns a field having the specified name.
ImmutableArray< CustomAttributeSymbol > GetCustomAttributes(ITypeSymbol attributeType)
Returns an array of custom attributes defined on this member, identified by type, or an empty array i...
ITypeSymbol ResolveType(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the type identified by the specified metadata token, in the context defined by the specified ...
IFieldSymbol? GetField(string name, global::System.Reflection.BindingFlags bindingAttr)
IFieldSymbol? ResolveField(int metadataToken)
Returns the field identified by a metadata token.
string ScopeName
Gets a string representing the name of the module.
string FullyQualifiedName
Gets a string representing the fully qualified name and path to this module.
bool IsResource()
Gets a value indicating whether the object is a resource.
ITypeSymbol[] GetTypes()
Returns all the types defined within this module.
bool IsDefined(ITypeSymbol attributeType)
Indicates whether one or more instance of attributeType is defined on this member.
ITypeSymbol? GetType(string className)
Returns the specified type, performing a case-sensitive search.
IFieldSymbol[] GetFields()
Returns the global fields defined on the module.
ImmutableArray< CustomAttributeSymbol > GetCustomAttributes()
Returns an array of all of the custom attributes defined on this member, excluding named attributes,...
int MetadataToken
Gets a token that identifies the module in metadata.
Guid ModuleVersionId
Gets a universally unique identifier (UUID) that can be used to distinguish between two versions of a...
override bool IsMissing
Returns true if the symbol is missing.
IMethodBaseSymbol? ResolveMethod(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the method or constructor identified by the specified metadata token, in the context defined ...
Holds references to symbols derived from System.Reflection.
IkvmReflectionAssemblySymbol GetOrCreateAssemblySymbol(Assembly assembly)
Gets or creates a IkvmReflectionAssemblySymbol for the specified Assembly.
virtual IkvmReflectionMemberSymbol ResolveMemberSymbol(MemberInfo member)
Resolves the symbol for the specified type.
IkvmReflectionSymbolContext Context
Gets the associated IkvmReflectionSymbolContext.
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Discovers the attributes of a field and provides access to field metadata.
Obtains information about the attributes of a member and provides access to member metadata.
Provides information about methods and constructors.
Discovers the attributes of a method and provides access to method metadata.
Performs reflection on a module.
Represents type declarations: class types, interface types, array types, value types,...