IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
IkvmReflectionModuleSymbol.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Immutable;
3using System.Diagnostics;
4using System.Linq;
5using System.Reflection.Metadata.Ecma335;
6using System.Threading;
7
9
10using Module = IKVM.Reflection.Module;
11using Type = IKVM.Reflection.Type;
12
14{
15
20 {
21
27 static bool IsTypeDefinition(Type type)
28 {
29 return type.HasElementType == false && type.IsConstructedGenericType == false && type.IsGenericParameter == false;
30 }
31
32 readonly Module _underlyingModule;
33
34 Type[]? _typesSource;
35 int _typesBaseRow;
36 IkvmReflectionTypeSymbol?[]? _types;
37
44 public IkvmReflectionModuleSymbol(IkvmReflectionSymbolContext context, Module underlyingModule) :
45 base(context)
46 {
47 _underlyingModule = underlyingModule ?? throw new ArgumentNullException(nameof(underlyingModule));
48 }
49
53 internal Module UnderlyingModule => _underlyingModule;
54
61 internal IkvmReflectionTypeSymbol GetOrCreateTypeSymbol(Type type)
62 {
63 if (type is null)
64 throw new ArgumentNullException(nameof(type));
65
66 Debug.Assert(type.Module == _underlyingModule);
67
68 // type is not a definition, but is substituted
69 if (IsTypeDefinition(type) == false)
70 return GetOrCreateTypeSymbolForSpecification(type);
71
72 // look up handle and row
73 var hnd = MetadataTokens.TypeDefinitionHandle(type.MetadataToken);
74 var row = MetadataTokens.GetRowNumber(hnd);
75
76 // initialize source table
77 if (_typesSource == null)
78 {
79 _typesSource = _underlyingModule.GetTypes().OrderBy(i => i.MetadataToken).ToArray();
80 _typesBaseRow = _typesSource.Length != 0 ? MetadataTokens.GetRowNumber(MetadataTokens.MethodDefinitionHandle(_typesSource[0].MetadataToken)) : 0;
81 }
82
83 // initialize cache table
84 _types ??= new IkvmReflectionTypeSymbol?[_typesSource.Length];
85
86 // index of current record is specified row - base
87 var idx = row - _typesBaseRow;
88 if (idx < 0)
89 throw new Exception();
90
91 Debug.Assert(idx >= 0);
92 Debug.Assert(idx < _typesSource.Length);
93
94 // check that our type list is long enough to contain the entire table
95 if (_types.Length < idx)
96 throw new IndexOutOfRangeException();
97
98 // if not yet created, create, allow multiple instances, but only one is eventually inserted
99 if (_types[idx] == null)
100 Interlocked.CompareExchange(ref _types[idx], new IkvmReflectionTypeSymbol(Context, this, type), null);
101
102 // this should never happen
103 if (_types[idx] is not IkvmReflectionTypeSymbol sym)
104 throw new InvalidOperationException();
105
106 return sym;
107 }
108
114 IkvmReflectionTypeSymbol GetOrCreateTypeSymbolForSpecification(Type type)
115 {
116 if (type is null)
117 throw new ArgumentNullException(nameof(type));
118
119 Debug.Assert(type.Module == _underlyingModule);
120
121 if (type.GetElementType() is { } elementType)
122 {
123 var elementTypeSymbol = GetOrCreateTypeSymbol(elementType);
124
125 // handles both SZ arrays and normal arrays
126 if (type.IsArray)
127 return (IkvmReflectionTypeSymbol)elementTypeSymbol.MakeArrayType(type.GetArrayRank());
128
129 if (type.IsPointer)
130 return (IkvmReflectionTypeSymbol)elementTypeSymbol.MakePointerType();
131
132 if (type.IsByRef)
133 return (IkvmReflectionTypeSymbol)elementTypeSymbol.MakeByRefType();
134
135 throw new InvalidOperationException();
136 }
137
138 if (type.IsGenericType)
139 {
140 var definitionType = type.GetGenericTypeDefinition();
141 var definitionTypeSymbol = GetOrCreateTypeSymbol(definitionType);
142 return definitionTypeSymbol.GetOrCreateGenericTypeSymbol(type.GetGenericArguments());
143 }
144
145 // generic type parameter
146 if (type.IsGenericParameter && type.DeclaringMethod is null && type.DeclaringType is not null)
147 {
148 var declaringType = GetOrCreateTypeSymbol(type.DeclaringType);
149 return declaringType.GetOrCreateGenericParameterSymbol(type);
150 }
151
152 // generic method parameter
153 if (type.IsGenericParameter && type.DeclaringMethod is not null && type.DeclaringMethod.DeclaringType is not null)
154 {
155 var declaringMethod = GetOrCreateTypeSymbol(type.DeclaringMethod.DeclaringType);
156 return declaringMethod.GetOrCreateGenericParameterSymbol(type);
157 }
158
159 throw new InvalidOperationException();
160 }
161
163 public IAssemblySymbol Assembly => Context.GetOrCreateAssemblySymbol(_underlyingModule.Assembly);
164
166 public string FullyQualifiedName => _underlyingModule.FullyQualifiedName;
167
169 public int MetadataToken => _underlyingModule.MetadataToken;
170
172 public Guid ModuleVersionId => _underlyingModule.ModuleVersionId;
173
175 public string Name => _underlyingModule.Name;
176
178 public string ScopeName => _underlyingModule.ScopeName;
179
181 public override bool IsMissing => _underlyingModule.__IsMissing;
182
184 public IFieldSymbol? GetField(string name)
185 {
186 return _underlyingModule.GetField(name) is { } f ? ResolveFieldSymbol(f) : null;
187 }
188
190 public IFieldSymbol? GetField(string name, global::System.Reflection.BindingFlags bindingAttr)
191 {
192 return _underlyingModule.GetField(name, (BindingFlags)bindingAttr) is { } f ? ResolveFieldSymbol(f) : null;
193 }
194
196 public IFieldSymbol[] GetFields(global::System.Reflection.BindingFlags bindingFlags)
197 {
198 return ResolveFieldSymbols(_underlyingModule.GetFields((BindingFlags)bindingFlags));
199 }
200
203 {
204 return ResolveFieldSymbols(_underlyingModule.GetFields());
205 }
206
208 public IMethodSymbol? GetMethod(string name)
209 {
210 return _underlyingModule.GetMethod(name) is { } m ? ResolveMethodSymbol(m) : null;
211 }
212
214 public IMethodSymbol? GetMethod(string name, ITypeSymbol[] types)
215 {
216 return _underlyingModule.GetMethod(name, UnpackTypeSymbols(types)) is { } m ? ResolveMethodSymbol(m) : null;
217 }
218
220 public IMethodSymbol? GetMethod(string name, global::System.Reflection.BindingFlags bindingAttr, global::System.Reflection.CallingConventions callConvention, ITypeSymbol[] types, global::System.Reflection.ParameterModifier[]? modifiers)
221 {
222 if (modifiers != null)
223 throw new NotImplementedException();
224
225 return _underlyingModule.GetMethod(name, (BindingFlags)bindingAttr, null, (CallingConventions)callConvention, UnpackTypeSymbols(types), null) is { } m ? ResolveMethodSymbol(m) : null;
226 }
227
230 {
231 return ResolveMethodSymbols(_underlyingModule.GetMethods());
232 }
233
235 public IMethodSymbol[] GetMethods(global::System.Reflection.BindingFlags bindingFlags)
236 {
237 return ResolveMethodSymbols(_underlyingModule.GetMethods((BindingFlags)bindingFlags));
238 }
240
241 public ITypeSymbol? GetType(string className)
242 {
243 return _underlyingModule.GetType(className) is { } t ? ResolveTypeSymbol(t) : null;
244 }
245
247 public ITypeSymbol? GetType(string className, bool ignoreCase)
248 {
249 return _underlyingModule.GetType(className, ignoreCase) is { } t ? ResolveTypeSymbol(t) : null;
250 }
251
253 public ITypeSymbol? GetType(string className, bool throwOnError, bool ignoreCase)
254 {
255 return _underlyingModule.GetType(className, throwOnError, ignoreCase) is { } t ? ResolveTypeSymbol(t) : null;
256 }
257
260 {
261 return ResolveTypeSymbols(_underlyingModule.GetTypes());
262 }
263
265 public bool IsResource()
266 {
267 return _underlyingModule.IsResource();
268 }
269
271 public IFieldSymbol? ResolveField(int metadataToken)
272 {
273 return _underlyingModule.ResolveField(metadataToken) is { } f ? ResolveFieldSymbol(f) : null;
274 }
275
277 public IFieldSymbol? ResolveField(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
278 {
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;
282 }
283
285 public IMemberSymbol? ResolveMember(int metadataToken)
286 {
287 return _underlyingModule.ResolveMember(metadataToken) is { } m ? ResolveMemberSymbol(m) : null;
288 }
289
291 public IMemberSymbol? ResolveMember(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
292 {
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;
296 }
297
299 public IMethodBaseSymbol? ResolveMethod(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
300 {
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;
304 }
305
307 public IMethodBaseSymbol? ResolveMethod(int metadataToken)
308 {
309 return _underlyingModule.ResolveMethod(metadataToken) is { } m ? ResolveMethodBaseSymbol(m) : null;
310 }
311
313 public byte[] ResolveSignature(int metadataToken)
314 {
315 return _underlyingModule.ResolveSignature(metadataToken);
316 }
317
319 public string ResolveString(int metadataToken)
320 {
321 return _underlyingModule.ResolveString(metadataToken);
322 }
323
325 public ITypeSymbol ResolveType(int metadataToken)
326 {
327 return ResolveTypeSymbol(_underlyingModule.ResolveType(metadataToken));
328 }
329
331 public ITypeSymbol ResolveType(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
332 {
333 var _genericTypeArguments = genericTypeArguments != null ? UnpackTypeSymbols(genericTypeArguments) : null;
334 var _genericMethodArguments = genericMethodArguments != null ? UnpackTypeSymbols(genericMethodArguments) : null;
335 return ResolveTypeSymbol(_underlyingModule.ResolveType(metadataToken, _genericTypeArguments, _genericMethodArguments));
336 }
337
339 public ImmutableArray<CustomAttributeSymbol> GetCustomAttributes()
340 {
341 return ResolveCustomAttributes(_underlyingModule.GetCustomAttributesData());
342 }
343
345 public ImmutableArray<CustomAttributeSymbol> GetCustomAttributes(ITypeSymbol attributeType)
346 {
347 return ResolveCustomAttributes(_underlyingModule.__GetCustomAttributes(((IkvmReflectionTypeSymbol)attributeType).UnderlyingType, false));
348 }
349
351 public bool IsDefined(ITypeSymbol attributeType)
352 {
353 return _underlyingModule.IsDefined(((IkvmReflectionTypeSymbol)attributeType).UnderlyingType, false);
354 }
355
356 }
357
358}
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,...