-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
- Loading branch information
Showing
8 changed files
with
178 additions
and
141 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
99 changes: 0 additions & 99 deletions
99
.../tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ReflectionMethodBodyScanner.cs
This file was deleted.
Oops, something went wrong.
141 changes: 141 additions & 0 deletions
141
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,141 @@ | ||
// 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; | ||
|
||
public static TypeDesc ResolveType(string name, ModuleDesc callingModule, TypeSystemContext context, List<ModuleDesc> referencedModules) | ||
{ | ||
return new System.Reflection.TypeNameParser(name) | ||
{ | ||
_context = context, | ||
_callingModule = callingModule, | ||
_referencedModules = referencedModules | ||
}.Parse()?.Value; | ||
} | ||
|
||
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 = null; | ||
|
||
if (assemblyNameIfAny != null) | ||
{ | ||
AssemblyName an = TryParseAssemblyName(assemblyNameIfAny); | ||
if (an != null) | ||
module = _context.ResolveAssembly(an, throwIfNotFound: false); | ||
} | ||
else | ||
{ | ||
module = _callingModule; | ||
} | ||
|
||
if (module == null) | ||
return null; | ||
|
||
Type 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; | ||
} | ||
} | ||
|
||
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
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.