-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proper type name parser for native AOT compiler (#83657)
* Proper type name parser for native AOT compiler * Track failure of CA search rules in the type name parser * Fix tests * Suppress new native AOT warning for libraries tests Co-authored-by: vitek-karas <[email protected]> Fixes #72833
- Loading branch information
Showing
13 changed files
with
206 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 0 additions & 99 deletions
99
.../tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ReflectionMethodBodyScanner.cs
This file was deleted.
Oops, something went wrong.
151 changes: 151 additions & 0 deletions
151
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/TypeNameParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
using Internal.TypeSystem; | ||
|
||
namespace System.Reflection | ||
{ | ||
internal unsafe ref partial struct TypeNameParser | ||
{ | ||
private TypeSystemContext _context; | ||
private ModuleDesc _callingModule; | ||
private List<ModuleDesc> _referencedModules; | ||
private bool _typeWasNotFoundInAssemblyNorBaseLibrary; | ||
|
||
public static TypeDesc ResolveType(string name, ModuleDesc callingModule, | ||
TypeSystemContext context, List<ModuleDesc> referencedModules, out bool typeWasNotFoundInAssemblyNorBaseLibrary) | ||
{ | ||
var parser = new System.Reflection.TypeNameParser(name) | ||
{ | ||
_context = context, | ||
_callingModule = callingModule, | ||
_referencedModules = referencedModules | ||
}; | ||
|
||
TypeDesc result = parser.Parse()?.Value; | ||
|
||
typeWasNotFoundInAssemblyNorBaseLibrary = parser._typeWasNotFoundInAssemblyNorBaseLibrary; | ||
return result; | ||
} | ||
|
||
private sealed class Type | ||
{ | ||
public Type(TypeDesc type) => Value = type; | ||
public TypeDesc Value { get; } | ||
|
||
public Type MakeArrayType() => new Type(Value.MakeArrayType()); | ||
public Type MakeArrayType(int rank) => new Type(Value.MakeArrayType(rank)); | ||
public Type MakePointerType() => new Type(Value.MakePointerType()); | ||
public Type MakeByRefType() => new Type(Value.MakeByRefType()); | ||
|
||
public Type MakeGenericType(Type[] typeArguments) | ||
{ | ||
TypeDesc[] instantiation = new TypeDesc[typeArguments.Length]; | ||
for (int i = 0; i < typeArguments.Length; i++) | ||
instantiation[i] = typeArguments[i].Value; | ||
return new Type(((MetadataType)Value).MakeInstantiatedType(instantiation)); | ||
} | ||
} | ||
|
||
private static bool CheckTopLevelAssemblyQualifiedName() => true; | ||
|
||
private Type GetType(string typeName, ReadOnlySpan<string> nestedTypeNames, string assemblyNameIfAny) | ||
{ | ||
ModuleDesc module; | ||
|
||
if (assemblyNameIfAny != null) | ||
{ | ||
module = (TryParseAssemblyName(assemblyNameIfAny) is AssemblyName an) ? | ||
_context.ResolveAssembly(an, throwIfNotFound: false) : null; | ||
} | ||
else | ||
{ | ||
module = _callingModule; | ||
} | ||
|
||
Type type; | ||
|
||
if (module != null) | ||
{ | ||
type = GetTypeCore(module, typeName, nestedTypeNames); | ||
if (type != null) | ||
{ | ||
_referencedModules?.Add(module); | ||
return type; | ||
} | ||
} | ||
|
||
// If it didn't resolve and wasn't assembly-qualified, we also try core library | ||
if (assemblyNameIfAny == null) | ||
{ | ||
type = GetTypeCore(_context.SystemModule, typeName, nestedTypeNames); | ||
if (type != null) | ||
{ | ||
_referencedModules?.Add(_context.SystemModule); | ||
return type; | ||
} | ||
|
||
_typeWasNotFoundInAssemblyNorBaseLibrary = true; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static AssemblyName TryParseAssemblyName(string assemblyName) | ||
{ | ||
try | ||
{ | ||
return new AssemblyName(assemblyName); | ||
} | ||
catch (FileLoadException) | ||
{ | ||
return null; | ||
} | ||
catch (ArgumentException) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
private static Type GetTypeCore(ModuleDesc module, string typeName, ReadOnlySpan<string> nestedTypeNames) | ||
{ | ||
string typeNamespace, name; | ||
|
||
int separator = typeName.LastIndexOf('.'); | ||
if (separator <= 0) | ||
{ | ||
typeNamespace = ""; | ||
name = typeName; | ||
} | ||
else | ||
{ | ||
if (typeName[separator - 1] == '.') | ||
separator--; | ||
typeNamespace = typeName.Substring(0, separator); | ||
name = typeName.Substring(separator + 1); | ||
} | ||
|
||
MetadataType type = module.GetType(typeNamespace, name, throwIfNotFound: false); | ||
if (type == null) | ||
return null; | ||
|
||
for (int i = 0; i < nestedTypeNames.Length; i++) | ||
{ | ||
type = type.GetNestedType(nestedTypeNames[i]); | ||
if (type == null) | ||
return null; | ||
} | ||
|
||
return new Type(type); | ||
} | ||
|
||
private static void ParseError() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.