Skip to content

Commit f8796dc

Browse files
authored
Use covariant returns in ILCompiler.TypeSystem (#120542)
* Change MetadataType.BaseType to be MetadataType and remove MetadataType.MetadataBaseType and remove EcmaType.EcmaModule * Use covariant return types to narrow down many methods on the Ecma* types. * MetadataType can only be nested in a MetadataType * FieldDesc.OwningType can be MetadataType (and is assumed as such in many locations) * Fix MetadataType usages in NativeAOT type loader * Change return field of OwningType to fix failure
1 parent 404a109 commit f8796dc

File tree

77 files changed

+253
-282
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+253
-282
lines changed

src/coreclr/tools/Common/Compiler/Dataflow/EcmaExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static PropertyPseudoDesc GetProperty(this MetadataType mdType, string na
6161
}
6262
}
6363

64-
mdType = mdType.MetadataBaseType;
64+
mdType = mdType.BaseType;
6565
if (mdType != null)
6666
return GetProperty(mdType, name, signature);
6767

src/coreclr/tools/Common/Compiler/EventPseudoDesc.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,30 @@ public class EventPseudoDesc : TypeSystemEntity
2020

2121
private EventDefinition Definition => _type.MetadataReader.GetEventDefinition(_handle);
2222

23-
public MethodDesc AddMethod
23+
public EcmaMethod AddMethod
2424
{
2525
get
2626
{
2727
MethodDefinitionHandle adder = Definition.GetAccessors().Adder;
28-
return adder.IsNil ? null : _type.EcmaModule.GetMethod(adder);
28+
return adder.IsNil ? null : _type.Module.GetMethod(adder);
2929
}
3030
}
3131

32-
public MethodDesc RemoveMethod
32+
public EcmaMethod RemoveMethod
3333
{
3434
get
3535
{
3636
MethodDefinitionHandle setter = Definition.GetAccessors().Remover;
37-
return setter.IsNil ? null : _type.EcmaModule.GetMethod(setter);
37+
return setter.IsNil ? null : _type.Module.GetMethod(setter);
3838
}
3939
}
4040

41-
public MethodDesc RaiseMethod
41+
public EcmaMethod RaiseMethod
4242
{
4343
get
4444
{
4545
MethodDefinitionHandle raiser = Definition.GetAccessors().Raiser;
46-
return raiser.IsNil ? null : _type.EcmaModule.GetMethod(raiser);
46+
return raiser.IsNil ? null : _type.Module.GetMethod(raiser);
4747
}
4848
}
4949

@@ -55,7 +55,7 @@ public CustomAttributeHandleCollection GetCustomAttributes
5555
}
5656
}
5757

58-
public MetadataType OwningType
58+
public EcmaType OwningType
5959
{
6060
get
6161
{

src/coreclr/tools/Common/Compiler/GenericCycleDetection/GraphBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public GraphBuilder(EcmaModule assembly)
174174
{
175175
try
176176
{
177-
var ecmaType = (EcmaType)assembly.GetObject(typeHandle);
177+
var ecmaType = assembly.GetType(typeHandle);
178178
WalkAncestorTypes(ecmaType);
179179
}
180180
catch (TypeSystemException)
@@ -198,7 +198,7 @@ public GraphBuilder(EcmaModule assembly)
198198
{
199199
try
200200
{
201-
var ecmaMethod = (EcmaMethod)assembly.GetObject(methodHandle);
201+
var ecmaMethod = assembly.GetMethod(methodHandle);
202202
WalkMethod(ecmaMethod);
203203

204204
if (ecmaMethod.IsVirtual)
@@ -218,7 +218,7 @@ public GraphBuilder(EcmaModule assembly)
218218
{
219219
try
220220
{
221-
var ecmaField = (EcmaField)assembly.GetObject(fieldHandle);
221+
var ecmaField = assembly.GetField(fieldHandle);
222222

223223
if (typeContext.IsNull)
224224
{

src/coreclr/tools/Common/Compiler/GenericCycleDetection/ModuleCycleInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public bool IsDeepPossiblyCyclicInstantiation(MethodDesc method)
217217

218218
private bool FormsCycle(TypeSystemEntity entity, out ModuleCycleInfo cycleInfo)
219219
{
220-
EcmaModule ownerModule = (entity as EcmaType)?.EcmaModule ?? (entity as EcmaMethod)?.Module;
220+
EcmaModule ownerModule = (entity as EcmaType)?.Module ?? (entity as EcmaMethod)?.Module;
221221
if (ownerModule != null)
222222
{
223223
cycleInfo = _hashtable.GetOrCreateValue(ownerModule);

src/coreclr/tools/Common/Compiler/InstructionSetSupport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static string GetHardwareIntrinsicId(TargetArchitecture architecture, Typ
6464
if (potentialType.Name.SequenceEqual("X64"u8) || potentialType.Name.SequenceEqual("Arm64"u8))
6565
{
6666
if (architecture is TargetArchitecture.X64 or TargetArchitecture.ARM64)
67-
potentialType = (MetadataType)potentialType.ContainingType;
67+
potentialType = potentialType.ContainingType;
6868
else
6969
return "";
7070
}

src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private string ComputeMangledTypeName(TypeDesc type)
170170
{
171171
if (type is EcmaType ecmaType)
172172
{
173-
string assemblyName = ((EcmaAssembly)ecmaType.EcmaModule).GetName().Name;
173+
string assemblyName = ((EcmaAssembly)ecmaType.Module).GetName().Name;
174174
bool isSystemPrivate = assemblyName.StartsWith("System.Private.");
175175

176176
// Abbreviate System.Private to S.P. This might conflict with user defined assembly names,
@@ -190,7 +190,7 @@ private string ComputeMangledTypeName(TypeDesc type)
190190

191191
if (!_mangledTypeNames.ContainsKey(type))
192192
{
193-
foreach (MetadataType t in ecmaType.EcmaModule.GetAllTypes())
193+
foreach (MetadataType t in ecmaType.Module.GetAllTypes())
194194
{
195195
string name = t.GetFullName();
196196

src/coreclr/tools/Common/Compiler/PropertyPseudoDesc.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ public class PropertyPseudoDesc : TypeSystemEntity
2121
private PropertyDefinition Definition => _type.MetadataReader.GetPropertyDefinition(_handle);
2222

2323
public PropertySignature Signature =>
24-
new EcmaSignatureParser(_type.EcmaModule, _type.MetadataReader.GetBlobReader(Definition.Signature), NotFoundBehavior.Throw)
24+
new EcmaSignatureParser(_type.Module, _type.MetadataReader.GetBlobReader(Definition.Signature), NotFoundBehavior.Throw)
2525
.ParsePropertySignature();
2626

27-
public MethodDesc GetMethod
27+
public EcmaMethod GetMethod
2828
{
2929
get
3030
{
3131
MethodDefinitionHandle getter = Definition.GetAccessors().Getter;
32-
return getter.IsNil ? null : _type.EcmaModule.GetMethod(getter);
32+
return getter.IsNil ? null : _type.Module.GetMethod(getter);
3333
}
3434
}
3535

36-
public MethodDesc SetMethod
36+
public EcmaMethod SetMethod
3737
{
3838
get
3939
{
4040
MethodDefinitionHandle setter = Definition.GetAccessors().Setter;
41-
return setter.IsNil ? null : _type.EcmaModule.GetMethod(setter);
41+
return setter.IsNil ? null : _type.Module.GetMethod(setter);
4242
}
4343
}
4444

@@ -50,7 +50,7 @@ public CustomAttributeHandleCollection GetCustomAttributes
5050
}
5151
}
5252

53-
public MetadataType OwningType
53+
public EcmaType OwningType
5454
{
5555
get
5656
{

src/coreclr/tools/Common/Compiler/PseudoDescExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static PropertyPseudoDesc GetPropertyForAccessor(this MethodDesc accessor
1515
if (!accessor.IsSpecialName || accessor.GetTypicalMethodDefinition() is not EcmaMethod ecmaAccessor)
1616
return null;
1717

18-
var type = (EcmaType)ecmaAccessor.OwningType;
18+
var type = ecmaAccessor.OwningType;
1919
var reader = type.MetadataReader;
2020
foreach (var propertyHandle in reader.GetTypeDefinition(type.Handle).GetProperties())
2121
{
@@ -35,7 +35,7 @@ public static EventPseudoDesc GetEventForAccessor(this MethodDesc accessor)
3535
if (!accessor.IsSpecialName || accessor.GetTypicalMethodDefinition() is not EcmaMethod ecmaAccessor)
3636
return null;
3737

38-
var type = (EcmaType)ecmaAccessor.OwningType;
38+
var type = ecmaAccessor.OwningType;
3939
var reader = type.MetadataReader;
4040
foreach (var eventHandle in reader.GetTypeDefinition(type.Handle).GetEvents())
4141
{

src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,7 +2334,7 @@ private int GatherClassGCLayout(MetadataType type, byte* gcPtrs)
23342334
{
23352335
int result = 0;
23362336

2337-
if (type.MetadataBaseType is { ContainsGCPointers: true } baseType)
2337+
if (type.BaseType is { ContainsGCPointers: true } baseType)
23382338
result += GatherClassGCLayout(baseType, gcPtrs);
23392339

23402340
bool isInlineArray = type.IsInlineArray;
@@ -3443,7 +3443,7 @@ private static string getMethodNameFromMetadataImpl(MethodDesc method, out strin
34433443

34443444
if (method.GetTypicalMethodDefinition() is EcmaMethod ecmaMethod)
34453445
{
3446-
EcmaType owningType = (EcmaType)ecmaMethod.OwningType;
3446+
EcmaType owningType = ecmaMethod.OwningType;
34473447
var reader = owningType.MetadataReader;
34483448

34493449
if (className != null)
@@ -3456,7 +3456,7 @@ private static string getMethodNameFromMetadataImpl(MethodDesc method, out strin
34563456
EcmaType containingType = owningType;
34573457
for (nuint i = 0; i < maxEnclosingClassNames; i++)
34583458
{
3459-
containingType = containingType.ContainingType as EcmaType;
3459+
containingType = containingType.ContainingType;
34603460
if (containingType == null)
34613461
break;
34623462

src/coreclr/tools/Common/JitInterface/CorInfoInstructionSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ public static InstructionSet LookupPlatformIntrinsicInstructionSet(TargetArchite
13111311
string nestedTypeName = null;
13121312
while (metadataType.ContainingType != null)
13131313
{
1314-
var enclosingType = (MetadataType)metadataType.ContainingType;
1314+
var enclosingType = metadataType.ContainingType;
13151315
namespaceName = enclosingType.GetNamespace();
13161316
nestedTypeName = nestedTypeName is null ? metadataType.GetName() : $"{metadataType.GetName()}_{nestedTypeName}";
13171317
typeName = enclosingType.GetName();

0 commit comments

Comments
 (0)