Skip to content

Commit 93b487a

Browse files
buyaa-njkotasViktorHofer
authored
Make System.Reflection.Emit public types abstract (#78544)
* Add abstract AssemblyBuilder * Create managed RuntimeAssemblyBuilder * Add RuntimeAssemblyBuilder to the build * Boilerplate for RuntimeAssemblyBuilder * Emit Assembly record * Fixups after rebase * Add temporary ReflectionEmitLoadContext * Factor out RuntimeModuleBuilder * Abstract TypeBuilder and EventBuilder * Refactor PropertyBuilder * Refactor EnumBuilder * Refactor FieldBuilder * Refactor ConstructorBuilder and MethodBuilder * Move suppressions into the common file --------- Co-authored-by: Jan Kotas <[email protected]> Co-authored-by: Viktor Hofer <[email protected]>
1 parent 71f7f84 commit 93b487a

File tree

66 files changed

+1875
-3718
lines changed

Some content is hidden

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

66 files changed

+1875
-3718
lines changed

src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,26 @@
159159
<Compile Include="$(BclSourcesRoot)\System\Reflection\Associates.cs" />
160160
<Compile Include="$(BclSourcesRoot)\System\Reflection\ConstructorInfo.CoreCLR.cs" />
161161
<Compile Include="$(BclSourcesRoot)\System\Reflection\ConstructorInvoker.CoreCLR.cs" />
162-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\AssemblyBuilder.cs" />
163-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\ConstructorBuilder.cs" />
164162
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\CustomAttributeBuilder.cs" />
165163
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\DynamicILGenerator.cs" />
166164
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\DynamicMethod.CoreCLR.cs" />
167-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\EnumBuilder.cs" />
168-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\EventBuilder.cs" />
169-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\FieldBuilder.cs" />
170-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\GenericTypeParameterBuilder.cs" />
171165
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\ILGenerator.cs" />
172166
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\LocalBuilder.cs" />
173-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\MethodBuilder.cs" />
174167
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\MethodBuilderInstantiation.cs" />
175-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\ModuleBuilder.cs" />
176168
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\ParameterBuilder.cs" />
177-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\PropertyBuilder.cs" />
169+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeAssemblyBuilder.cs" />
170+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeConstructorBuilder.cs" />
171+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeEnumBuilder.cs" />
172+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeEventBuilder.cs" />
173+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeFieldBuilder.cs" />
174+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeGenericTypeParameterBuilder.cs" />
175+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeMethodBuilder.cs" />
176+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeModuleBuilder.cs" />
177+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimePropertyBuilder.cs" />
178+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeTypeBuilder.cs" />
178179
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\SignatureHelper.cs" />
179180
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\SymbolMethod.cs" />
180181
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\SymbolType.cs" />
181-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\TypeBuilder.cs" />
182182
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\TypeBuilderInstantiation.cs" />
183183
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\XXXOnTypeBuilderInstantiation.cs" />
184184
<Compile Include="$(BclSourcesRoot)\System\Reflection\FieldInfo.CoreCLR.cs" />

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr
152152
// Might have failed check because one type is a XXXBuilder
153153
// and the other is not. Deal with these special cases
154154
// separately.
155-
if (!TypeBuilder.IsTypeEqual(property.DeclaringType, con.DeclaringType))
155+
if (!RuntimeTypeBuilder.IsTypeEqual(property.DeclaringType, con.DeclaringType))
156156
{
157157
// IsSubclassOf is overloaded to do the right thing if
158158
// the constructor is a TypeBuilder, but we still need
159159
// to deal with the case where the property's declaring
160160
// type is one.
161161
if (!(property.DeclaringType is TypeBuilder) ||
162-
!con.DeclaringType.IsSubclassOf(((TypeBuilder)property.DeclaringType).BakedRuntimeType))
162+
!con.DeclaringType.IsSubclassOf(((RuntimeTypeBuilder)property.DeclaringType).BakedRuntimeType))
163163
throw new ArgumentException(SR.Argument_BadPropertyForConstructorBuilder);
164164
}
165165
}
@@ -206,14 +206,14 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr
206206
// Might have failed check because one type is a XXXBuilder
207207
// and the other is not. Deal with these special cases
208208
// separately.
209-
if (!TypeBuilder.IsTypeEqual(namedField.DeclaringType, con.DeclaringType))
209+
if (!RuntimeTypeBuilder.IsTypeEqual(namedField.DeclaringType, con.DeclaringType))
210210
{
211211
// IsSubclassOf is overloaded to do the right thing if
212212
// the constructor is a TypeBuilder, but we still need
213213
// to deal with the case where the field's declaring
214214
// type is one.
215215
if (!(namedField.DeclaringType is TypeBuilder) ||
216-
!con.DeclaringType.IsSubclassOf(((TypeBuilder)namedFields[i].DeclaringType!).BakedRuntimeType))
216+
!con.DeclaringType.IsSubclassOf(((RuntimeTypeBuilder)namedFields[i].DeclaringType!).BakedRuntimeType))
217217
throw new ArgumentException(SR.Argument_BadFieldForConstructorBuilder);
218218
}
219219
}
@@ -517,9 +517,9 @@ private static void EmitValue(BinaryWriter writer, Type type, object? value)
517517
}
518518

519519
// return the byte interpretation of the custom attribute
520-
internal void CreateCustomAttribute(ModuleBuilder mod, int tkOwner)
520+
internal void CreateCustomAttribute(RuntimeModuleBuilder mod, int tkOwner)
521521
{
522-
TypeBuilder.DefineCustomAttribute(mod, tkOwner, mod.GetConstructorToken(m_con), m_blob);
522+
RuntimeTypeBuilder.DefineCustomAttribute(mod, tkOwner, mod.GetMethodMetadataToken(m_con), m_blob);
523523
}
524524
}
525525
}

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ private int GetMemberRefToken(MethodInfo methodInfo, Type[]? optionalParameterTy
440440
}
441441

442442
SignatureHelper sig = GetMethodSigHelper(methodInfo.CallingConvention,
443-
MethodBuilder.GetMethodBaseReturnType(methodInfo),
443+
RuntimeMethodBuilder.GetMethodBaseReturnType(methodInfo),
444444
parameterTypes,
445445
requiredCustomModifiers,
446446
optionalCustomModifiers,

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ internal ILGenerator(MethodInfo methodBuilder, int size)
9494
m_methodBuilder = methodBuilder;
9595

9696
// initialize local signature
97-
MethodBuilder? mb = m_methodBuilder as MethodBuilder;
97+
RuntimeMethodBuilder? mb = m_methodBuilder as RuntimeMethodBuilder;
9898
m_localSignature = SignatureHelper.GetLocalVarSigHelper(mb?.GetTypeBuilder().Module);
9999
}
100100

@@ -168,7 +168,7 @@ internal void UpdateStackSize(OpCode opcode, int stackchange)
168168

169169
private int GetMethodToken(MethodBase method, Type[]? optionalParameterTypes, bool useMethodDef)
170170
{
171-
return ((ModuleBuilder)m_methodBuilder.Module).GetMethodTokenInternal(method, optionalParameterTypes, useMethodDef);
171+
return ((RuntimeModuleBuilder)m_methodBuilder.Module).GetMethodTokenInternal(method, optionalParameterTypes, useMethodDef);
172172
}
173173

174174
internal SignatureHelper GetMemberRefSignature(
@@ -188,7 +188,7 @@ internal virtual SignatureHelper GetMemberRefSignature(CallingConventions call,
188188
private SignatureHelper GetMemberRefSignature(CallingConventions call, Type? returnType,
189189
Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers, Type[]? optionalParameterTypes, int cGenericParameters)
190190
{
191-
return ((ModuleBuilder)m_methodBuilder.Module).GetMemberRefSignature(call, returnType, parameterTypes, requiredCustomModifiers, optionalCustomModifiers, optionalParameterTypes, cGenericParameters);
191+
return ((RuntimeModuleBuilder)m_methodBuilder.Module).GetMemberRefSignature(call, returnType, parameterTypes, requiredCustomModifiers, optionalCustomModifiers, optionalParameterTypes, cGenericParameters);
192192
}
193193

194194
internal byte[]? BakeByteArray()
@@ -570,7 +570,7 @@ public virtual void EmitCalli(OpCode opcode, CallingConventions callingConventio
570570
UpdateStackSize(OpCodes.Calli, stackchange);
571571

572572
RecordTokenFixup();
573-
PutInteger4(modBuilder.GetSignatureToken(sig));
573+
PutInteger4(modBuilder.GetSignatureMetadataToken(sig));
574574
}
575575

576576
public virtual void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type? returnType, Type[]? parameterTypes)
@@ -613,7 +613,7 @@ public virtual void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv
613613
EnsureCapacity(7);
614614
Emit(OpCodes.Calli);
615615
RecordTokenFixup();
616-
PutInteger4(modBuilder.GetSignatureToken(sig));
616+
PutInteger4(modBuilder.GetSignatureMetadataToken(sig));
617617
}
618618

619619
public virtual void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? optionalParameterTypes)
@@ -656,7 +656,7 @@ public virtual void Emit(OpCode opcode, SignatureHelper signature)
656656

657657
int stackchange = 0;
658658
ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
659-
int sig = modBuilder.GetSignatureToken(signature);
659+
int sig = modBuilder.GetSignatureMetadataToken(signature);
660660

661661
int tempVal = sig;
662662

@@ -729,19 +729,9 @@ public virtual void Emit(OpCode opcode, Type cls)
729729
// by cls. The location of cls is recorded so that the token can be
730730
// patched if necessary when persisting the module to a PE.
731731

732-
int tempVal;
733-
ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
734-
if (opcode == OpCodes.Ldtoken && cls != null && cls.IsGenericTypeDefinition)
735-
{
736-
// This gets the token for the generic type definition if cls is one.
737-
tempVal = modBuilder.GetTypeToken(cls);
738-
}
739-
else
740-
{
741-
// This gets the token for the generic type instantiated on the formal parameters
742-
// if cls is a generic type definition.
743-
tempVal = modBuilder.GetTypeTokenInternal(cls!);
744-
}
732+
RuntimeModuleBuilder modBuilder = (RuntimeModuleBuilder)m_methodBuilder.Module;
733+
bool getGenericDefinition = (opcode == OpCodes.Ldtoken && cls != null && cls.IsGenericTypeDefinition);
734+
int tempVal = modBuilder.GetTypeTokenInternal(cls!, getGenericDefinition);
745735

746736
EnsureCapacity(7);
747737
InternalEmit(opcode);
@@ -824,7 +814,7 @@ public virtual void Emit(OpCode opcode, Label[] labels)
824814
public virtual void Emit(OpCode opcode, FieldInfo field)
825815
{
826816
ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
827-
int tempVal = modBuilder.GetFieldToken(field);
817+
int tempVal = modBuilder.GetFieldMetadataToken(field);
828818
EnsureCapacity(7);
829819
InternalEmit(opcode);
830820
RecordTokenFixup();
@@ -838,7 +828,7 @@ public virtual void Emit(OpCode opcode, string str)
838828
// fixups if the module is persisted to a PE.
839829

840830
ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
841-
int tempVal = modBuilder.GetStringConstant(str);
831+
int tempVal = modBuilder.GetStringMetadataToken(str);
842832
EnsureCapacity(7);
843833
InternalEmit(opcode);
844834
PutInteger4(tempVal);
@@ -1307,8 +1297,7 @@ public virtual LocalBuilder DeclareLocal(Type localType, bool pinned)
13071297
// Declare a local of type "local". The current active lexical scope
13081298
// will be the scope that local will live.
13091299

1310-
MethodBuilder? methodBuilder = m_methodBuilder as MethodBuilder;
1311-
if (methodBuilder == null)
1300+
if (m_methodBuilder is not RuntimeMethodBuilder methodBuilder)
13121301
throw new NotSupportedException();
13131302

13141303
if (methodBuilder.IsTypeCreated())
@@ -1337,8 +1326,7 @@ public virtual void UsingNamespace(string usingNamespace)
13371326

13381327
ArgumentException.ThrowIfNullOrEmpty(usingNamespace);
13391328

1340-
MethodBuilder? methodBuilder = m_methodBuilder as MethodBuilder;
1341-
if (methodBuilder == null)
1329+
if (m_methodBuilder is not RuntimeMethodBuilder methodBuilder)
13421330
throw new NotSupportedException();
13431331

13441332
int index = methodBuilder.GetILGenerator().m_ScopeTree.GetCurrentActiveScopeIndex();

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class ParameterBuilder
1010
// Set the default value of the parameter
1111
public virtual void SetConstant(object? defaultValue)
1212
{
13-
TypeBuilder.SetConstantValue(
13+
RuntimeTypeBuilder.SetConstantValue(
1414
_methodBuilder.GetModuleBuilder(),
1515
_token,
1616
_position == 0 ? _methodBuilder.ReturnType : _methodBuilder.m_parameterTypes![_position - 1],
@@ -23,10 +23,10 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
2323
ArgumentNullException.ThrowIfNull(con);
2424
ArgumentNullException.ThrowIfNull(binaryAttribute);
2525

26-
TypeBuilder.DefineCustomAttribute(
26+
RuntimeTypeBuilder.DefineCustomAttribute(
2727
_methodBuilder.GetModuleBuilder(),
2828
_token,
29-
((ModuleBuilder)_methodBuilder.GetModule()).GetConstructorToken(con),
29+
((RuntimeModuleBuilder)_methodBuilder.GetModule()).GetMethodMetadataToken(con),
3030
binaryAttribute);
3131
}
3232

@@ -35,11 +35,11 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
3535
{
3636
ArgumentNullException.ThrowIfNull(customBuilder);
3737

38-
customBuilder.CreateCustomAttribute((ModuleBuilder)(_methodBuilder.GetModule()), _token);
38+
customBuilder.CreateCustomAttribute((RuntimeModuleBuilder)(_methodBuilder.GetModule()), _token);
3939
}
4040

4141
internal ParameterBuilder(
42-
MethodBuilder methodBuilder,
42+
RuntimeMethodBuilder methodBuilder,
4343
int sequence,
4444
ParameterAttributes attributes,
4545
string? paramName) // can be NULL string
@@ -48,8 +48,8 @@ internal ParameterBuilder(
4848
_name = paramName;
4949
_methodBuilder = methodBuilder;
5050
_attributes = attributes;
51-
ModuleBuilder module = _methodBuilder.GetModuleBuilder();
52-
_token = TypeBuilder.SetParamInfo(
51+
RuntimeModuleBuilder module = _methodBuilder.GetModuleBuilder();
52+
_token = RuntimeTypeBuilder.SetParamInfo(
5353
new QCallModule(ref module),
5454
_methodBuilder.MetadataToken,
5555
sequence,
@@ -77,7 +77,7 @@ internal int GetToken()
7777
private readonly string? _name;
7878
private readonly int _position;
7979
private readonly ParameterAttributes _attributes;
80-
private MethodBuilder _methodBuilder;
80+
private RuntimeMethodBuilder _methodBuilder;
8181
private int _token;
8282
}
8383
}

0 commit comments

Comments
 (0)