@@ -12,27 +12,31 @@ public readonly struct ContextResolver
1212 private readonly AssemblyAnalysisContext referencedFrom ;
1313 private readonly TypeAnalysisContext ? referencingType ;
1414 private readonly MethodAnalysisContext ? referencingMethod ;
15+ private readonly RuntimeContext runtimeContext ;
1516
16- public ContextResolver ( AssemblyAnalysisContext referencedFrom )
17+ public ContextResolver ( AssemblyAnalysisContext referencedFrom , RuntimeContext runtimeContext )
1718 {
1819 this . referencedFrom = referencedFrom ;
20+ this . runtimeContext = runtimeContext ;
1921 }
2022
21- public ContextResolver ( TypeAnalysisContext referencingType )
23+ public ContextResolver ( TypeAnalysisContext referencingType , RuntimeContext runtimeContext )
2224 {
2325 if ( referencingType is ReferencedTypeAnalysisContext )
2426 throw new ArgumentException ( "Must be a simple type" , nameof ( referencingType ) ) ;
2527 referencedFrom = referencingType . DeclaringAssembly ;
2628 this . referencingType = referencingType ;
29+ this . runtimeContext = runtimeContext ;
2730 }
2831
29- public ContextResolver ( MethodAnalysisContext referencingMethod )
32+ public ContextResolver ( MethodAnalysisContext referencingMethod , RuntimeContext runtimeContext )
3033 {
3134 if ( referencingMethod is ConcreteGenericMethodAnalysisContext )
3235 throw new ArgumentException ( "Must be a simple method" , nameof ( referencingMethod ) ) ;
3336 referencedFrom = referencingMethod . CustomAttributeAssembly ;
3437 referencingType = referencingMethod . DeclaringType ;
3538 this . referencingMethod = referencingMethod ;
39+ this . runtimeContext = runtimeContext ;
3640 }
3741
3842 public TypeAnalysisContext ? Resolve ( TypeSignature ? type ) => type switch
@@ -86,6 +90,16 @@ public bool TryResolve(TypeSignature? type, [NotNullWhen(true)] out TypeAnalysis
8690
8791 private TypeAnalysisContext ? Resolve ( TypeDefOrRefSignature typeDefOrRef )
8892 {
93+ return Resolve ( typeDefOrRef . Type ) ;
94+ }
95+
96+ private TypeAnalysisContext ? Resolve ( ITypeDefOrRef typeDefOrRef )
97+ {
98+ if ( typeDefOrRef is TypeSpecification typeSpecification )
99+ {
100+ return Resolve ( typeSpecification . Signature ) ;
101+ }
102+
89103 if ( typeDefOrRef . DeclaringType is not null )
90104 {
91105 if ( ! TryResolve ( typeDefOrRef . DeclaringType , out var declaringType ) )
@@ -102,9 +116,9 @@ public bool TryResolve(TypeSignature? type, [NotNullWhen(true)] out TypeAnalysis
102116 return null ;
103117 }
104118
105- if ( typeDefOrRef . Type is not TypeDefinition )
119+ if ( typeDefOrRef is not TypeDefinition )
106120 {
107- typeDefOrRef = ( TypeDefOrRefSignature ? ) typeDefOrRef . Resolve ( ) ? . ToTypeSignature ( ) ?? typeDefOrRef ;
121+ typeDefOrRef = typeDefOrRef . TryResolve ( runtimeContext ) ?? typeDefOrRef ;
108122 }
109123
110124 var assemblyName = GetName ( typeDefOrRef . Scope ) ;
@@ -132,7 +146,12 @@ public bool TryResolve(TypeSignature? type, [NotNullWhen(true)] out TypeAnalysis
132146
133147 public TypeAnalysisContext ? Resolve ( ITypeDescriptor ? type )
134148 {
135- return Resolve ( type ? . ToTypeSignature ( ) ) ;
149+ return type switch
150+ {
151+ TypeSignature signature => Resolve ( signature ) ,
152+ ITypeDefOrRef typeDefOrRef => Resolve ( typeDefOrRef ) ,
153+ _ => null ,
154+ } ;
136155 }
137156
138157 public bool TryResolve ( ITypeDescriptor ? type , [ NotNullWhen ( true ) ] out TypeAnalysisContext ? result )
@@ -169,7 +188,7 @@ public bool TryResolve(IEnumerable<TypeSignature?> types, [NotNullWhen(true)] ou
169188
170189 public FieldAnalysisContext ? Resolve ( IFieldDescriptor fieldDescriptor )
171190 {
172- var declaringType = Resolve ( fieldDescriptor . DeclaringType ? . ToTypeSignature ( ) ) ;
191+ var declaringType = Resolve ( fieldDescriptor . DeclaringType ) ;
173192 if ( declaringType is null )
174193 return null ;
175194
@@ -222,7 +241,7 @@ public bool TryResolve(IFieldDescriptor fieldDescriptor, [NotNullWhen(true)] out
222241
223242 Debug . Assert ( nonGenericDeclaringType is not ReferencedTypeAnalysisContext ) ;
224243
225- var targetMethod = new ContextResolver ( nonGenericDeclaringType ) . ResolveInType ( methodDefOrRef ) ;
244+ var targetMethod = new ContextResolver ( nonGenericDeclaringType , runtimeContext ) . ResolveInType ( methodDefOrRef ) ;
226245 if ( targetMethod is null )
227246 return null ;
228247
@@ -245,7 +264,7 @@ public bool TryResolve(IMethodDescriptor methodDescriptor, [NotNullWhen(true)] o
245264 public MethodAnalysisContext ? Resolve ( MethodDefinition methodDefinition )
246265 {
247266 // The declaring type can be resolved with nothing, but resolution for the method itself requires a context.
248- return TryResolve ( methodDefinition . DeclaringType , out var declaringType ) ? new ContextResolver ( declaringType ) . ResolveInType ( methodDefinition ) : null ;
267+ return TryResolve ( methodDefinition . DeclaringType , out var declaringType ) ? new ContextResolver ( declaringType , runtimeContext ) . ResolveInType ( methodDefinition ) : null ;
249268 }
250269
251270 public MethodAnalysisContext ? ResolveInType ( IMethodDefOrRef methodDefOrRef )
@@ -274,7 +293,7 @@ public bool TryResolve(IMethodDescriptor methodDescriptor, [NotNullWhen(true)] o
274293 continue ;
275294
276295 // We need to use a resolver for the method context to resolve potential method generic parameters correctly.
277- var methodResolver = new ContextResolver ( methodContext ) ;
296+ var methodResolver = new ContextResolver ( methodContext , runtimeContext ) ;
278297
279298 if ( ! methodResolver . TryResolve ( methodDefOrRef . Signature . ReturnType , out var returnType ) ||
280299 ! TypeAnalysisContextEqualityComparer . Instance . Equals ( methodContext . ReturnType , returnType ) )
@@ -304,62 +323,4 @@ public bool TryResolve(IMethodDescriptor methodDescriptor, [NotNullWhen(true)] o
304323
305324 return baseMethod . MakeGenericInstanceMethod ( methodTypeArguments ) ;
306325 }
307-
308- public TypeAnalysisContext ResolveOrThrow ( Type ? type , bool allowGenericInstance = true )
309- {
310- return Resolve ( type , allowGenericInstance ) ?? throw new ( $ "Unable to resolve type { type ? . FullName } ") ;
311- }
312-
313- public TypeAnalysisContext ? Resolve ( Type ? type ) => Resolve ( type , true ) ;
314- public TypeAnalysisContext ? Resolve ( Type ? type , bool allowGenericInstance )
315- {
316- if ( type is null )
317- return null ;
318-
319- if ( type . IsSZArray )
320- return Resolve ( type . GetElementType ( ) ) ? . MakeSzArrayType ( ) ;
321-
322- if ( type . IsByRef )
323- return Resolve ( type . GetElementType ( ) ) ? . MakeByReferenceType ( ) ;
324-
325- if ( type . IsPointer )
326- return Resolve ( type . GetElementType ( ) ) ? . MakePointerType ( ) ;
327-
328- if ( type . IsArray )
329- return Resolve ( type . GetElementType ( ) ) ? . MakeArrayType ( type . GetArrayRank ( ) ) ;
330-
331- if ( type . IsGenericParameter )
332- {
333- if ( type . IsGenericTypeParameter )
334- {
335- return TryGetGenericParameter ( referencingType ? . GenericParameters , type . GenericParameterPosition ) ;
336- }
337- else
338- {
339- Debug . Assert ( type . IsGenericMethodParameter ) ;
340- return TryGetGenericParameter ( referencingMethod ? . GenericParameters , type . GenericParameterPosition ) ;
341- }
342- }
343-
344- if ( type . IsGenericType && allowGenericInstance )
345- {
346- var genericArguments = type . GetGenericArguments ( ) . Select ( Resolve ) . ToArray ( ) ;
347- if ( genericArguments . Any ( x => x is null ) )
348- return null ;
349-
350- var genericType = type . GetGenericTypeDefinition ( ) ;
351- return Resolve ( genericType , false ) ? . MakeGenericInstanceType ( genericArguments ! ) ;
352- }
353-
354- if ( type . IsFunctionPointer )
355- throw new NotSupportedException ( $ "Function pointers are not supported: { type . Name } ") ;
356-
357- // Custom modifiers might be possible to support, but probably not necessary
358-
359- var assemblyName = type . Assembly . GetName ( ) . Name ! ;
360- if ( assemblyName == "System.Private.CoreLib" )
361- assemblyName = "mscorlib" ;
362- var assembly = referencedFrom . AppContext . GetAssemblyByName ( assemblyName ) ;
363- return assembly ? . GetTypeByFullName ( type . FullName ! ) ;
364- }
365326}
0 commit comments