Skip to content

Commit

Permalink
Make System.Reflection.Emit public types abstract (#78544)
Browse files Browse the repository at this point in the history
* 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 <jkotas@microsoft.com>
Co-authored-by: Viktor Hofer <viktor.hofer@microsoft.com>
  • Loading branch information
3 people committed Jan 27, 2023
1 parent 71f7f84 commit 93b487a
Show file tree
Hide file tree
Showing 66 changed files with 1,875 additions and 3,718 deletions.
20 changes: 10 additions & 10 deletions src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,26 @@
<Compile Include="$(BclSourcesRoot)\System\Reflection\Associates.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\ConstructorInfo.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\ConstructorInvoker.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\AssemblyBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\ConstructorBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\CustomAttributeBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\DynamicILGenerator.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\DynamicMethod.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\EnumBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\EventBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\FieldBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\GenericTypeParameterBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\ILGenerator.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\LocalBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\MethodBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\MethodBuilderInstantiation.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\ModuleBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\ParameterBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\PropertyBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeAssemblyBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeConstructorBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeEnumBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeEventBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeFieldBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeGenericTypeParameterBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeMethodBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeModuleBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimePropertyBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeTypeBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\SignatureHelper.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\SymbolMethod.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\SymbolType.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\TypeBuilder.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\TypeBuilderInstantiation.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\XXXOnTypeBuilderInstantiation.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\FieldInfo.CoreCLR.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr
// Might have failed check because one type is a XXXBuilder
// and the other is not. Deal with these special cases
// separately.
if (!TypeBuilder.IsTypeEqual(property.DeclaringType, con.DeclaringType))
if (!RuntimeTypeBuilder.IsTypeEqual(property.DeclaringType, con.DeclaringType))
{
// IsSubclassOf is overloaded to do the right thing if
// the constructor is a TypeBuilder, but we still need
// to deal with the case where the property's declaring
// type is one.
if (!(property.DeclaringType is TypeBuilder) ||
!con.DeclaringType.IsSubclassOf(((TypeBuilder)property.DeclaringType).BakedRuntimeType))
!con.DeclaringType.IsSubclassOf(((RuntimeTypeBuilder)property.DeclaringType).BakedRuntimeType))
throw new ArgumentException(SR.Argument_BadPropertyForConstructorBuilder);
}
}
Expand Down Expand Up @@ -206,14 +206,14 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr
// Might have failed check because one type is a XXXBuilder
// and the other is not. Deal with these special cases
// separately.
if (!TypeBuilder.IsTypeEqual(namedField.DeclaringType, con.DeclaringType))
if (!RuntimeTypeBuilder.IsTypeEqual(namedField.DeclaringType, con.DeclaringType))
{
// IsSubclassOf is overloaded to do the right thing if
// the constructor is a TypeBuilder, but we still need
// to deal with the case where the field's declaring
// type is one.
if (!(namedField.DeclaringType is TypeBuilder) ||
!con.DeclaringType.IsSubclassOf(((TypeBuilder)namedFields[i].DeclaringType!).BakedRuntimeType))
!con.DeclaringType.IsSubclassOf(((RuntimeTypeBuilder)namedFields[i].DeclaringType!).BakedRuntimeType))
throw new ArgumentException(SR.Argument_BadFieldForConstructorBuilder);
}
}
Expand Down Expand Up @@ -517,9 +517,9 @@ private static void EmitValue(BinaryWriter writer, Type type, object? value)
}

// return the byte interpretation of the custom attribute
internal void CreateCustomAttribute(ModuleBuilder mod, int tkOwner)
internal void CreateCustomAttribute(RuntimeModuleBuilder mod, int tkOwner)
{
TypeBuilder.DefineCustomAttribute(mod, tkOwner, mod.GetConstructorToken(m_con), m_blob);
RuntimeTypeBuilder.DefineCustomAttribute(mod, tkOwner, mod.GetMethodMetadataToken(m_con), m_blob);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ private int GetMemberRefToken(MethodInfo methodInfo, Type[]? optionalParameterTy
}

SignatureHelper sig = GetMethodSigHelper(methodInfo.CallingConvention,
MethodBuilder.GetMethodBaseReturnType(methodInfo),
RuntimeMethodBuilder.GetMethodBaseReturnType(methodInfo),
parameterTypes,
requiredCustomModifiers,
optionalCustomModifiers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internal ILGenerator(MethodInfo methodBuilder, int size)
m_methodBuilder = methodBuilder;

// initialize local signature
MethodBuilder? mb = m_methodBuilder as MethodBuilder;
RuntimeMethodBuilder? mb = m_methodBuilder as RuntimeMethodBuilder;
m_localSignature = SignatureHelper.GetLocalVarSigHelper(mb?.GetTypeBuilder().Module);
}

Expand Down Expand Up @@ -168,7 +168,7 @@ internal void UpdateStackSize(OpCode opcode, int stackchange)

private int GetMethodToken(MethodBase method, Type[]? optionalParameterTypes, bool useMethodDef)
{
return ((ModuleBuilder)m_methodBuilder.Module).GetMethodTokenInternal(method, optionalParameterTypes, useMethodDef);
return ((RuntimeModuleBuilder)m_methodBuilder.Module).GetMethodTokenInternal(method, optionalParameterTypes, useMethodDef);
}

internal SignatureHelper GetMemberRefSignature(
Expand All @@ -188,7 +188,7 @@ private int GetMethodToken(MethodBase method, Type[]? optionalParameterTypes, bo
private SignatureHelper GetMemberRefSignature(CallingConventions call, Type? returnType,
Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers, Type[]? optionalParameterTypes, int cGenericParameters)
{
return ((ModuleBuilder)m_methodBuilder.Module).GetMemberRefSignature(call, returnType, parameterTypes, requiredCustomModifiers, optionalCustomModifiers, optionalParameterTypes, cGenericParameters);
return ((RuntimeModuleBuilder)m_methodBuilder.Module).GetMemberRefSignature(call, returnType, parameterTypes, requiredCustomModifiers, optionalCustomModifiers, optionalParameterTypes, cGenericParameters);
}

internal byte[]? BakeByteArray()
Expand Down Expand Up @@ -570,7 +570,7 @@ public virtual void Emit(OpCode opcode, MethodInfo meth)
UpdateStackSize(OpCodes.Calli, stackchange);

RecordTokenFixup();
PutInteger4(modBuilder.GetSignatureToken(sig));
PutInteger4(modBuilder.GetSignatureMetadataToken(sig));
}

public virtual void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type? returnType, Type[]? parameterTypes)
Expand Down Expand Up @@ -613,7 +613,7 @@ public virtual void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv
EnsureCapacity(7);
Emit(OpCodes.Calli);
RecordTokenFixup();
PutInteger4(modBuilder.GetSignatureToken(sig));
PutInteger4(modBuilder.GetSignatureMetadataToken(sig));
}

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

int stackchange = 0;
ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
int sig = modBuilder.GetSignatureToken(signature);
int sig = modBuilder.GetSignatureMetadataToken(signature);

int tempVal = sig;

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

int tempVal;
ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
if (opcode == OpCodes.Ldtoken && cls != null && cls.IsGenericTypeDefinition)
{
// This gets the token for the generic type definition if cls is one.
tempVal = modBuilder.GetTypeToken(cls);
}
else
{
// This gets the token for the generic type instantiated on the formal parameters
// if cls is a generic type definition.
tempVal = modBuilder.GetTypeTokenInternal(cls!);
}
RuntimeModuleBuilder modBuilder = (RuntimeModuleBuilder)m_methodBuilder.Module;
bool getGenericDefinition = (opcode == OpCodes.Ldtoken && cls != null && cls.IsGenericTypeDefinition);
int tempVal = modBuilder.GetTypeTokenInternal(cls!, getGenericDefinition);

EnsureCapacity(7);
InternalEmit(opcode);
Expand Down Expand Up @@ -824,7 +814,7 @@ public virtual void Emit(OpCode opcode, Label[] labels)
public virtual void Emit(OpCode opcode, FieldInfo field)
{
ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
int tempVal = modBuilder.GetFieldToken(field);
int tempVal = modBuilder.GetFieldMetadataToken(field);
EnsureCapacity(7);
InternalEmit(opcode);
RecordTokenFixup();
Expand All @@ -838,7 +828,7 @@ public virtual void Emit(OpCode opcode, string str)
// fixups if the module is persisted to a PE.

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

MethodBuilder? methodBuilder = m_methodBuilder as MethodBuilder;
if (methodBuilder == null)
if (m_methodBuilder is not RuntimeMethodBuilder methodBuilder)
throw new NotSupportedException();

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

ArgumentException.ThrowIfNullOrEmpty(usingNamespace);

MethodBuilder? methodBuilder = m_methodBuilder as MethodBuilder;
if (methodBuilder == null)
if (m_methodBuilder is not RuntimeMethodBuilder methodBuilder)
throw new NotSupportedException();

int index = methodBuilder.GetILGenerator().m_ScopeTree.GetCurrentActiveScopeIndex();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ParameterBuilder
// Set the default value of the parameter
public virtual void SetConstant(object? defaultValue)
{
TypeBuilder.SetConstantValue(
RuntimeTypeBuilder.SetConstantValue(
_methodBuilder.GetModuleBuilder(),
_token,
_position == 0 ? _methodBuilder.ReturnType : _methodBuilder.m_parameterTypes![_position - 1],
Expand All @@ -23,10 +23,10 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
ArgumentNullException.ThrowIfNull(con);
ArgumentNullException.ThrowIfNull(binaryAttribute);

TypeBuilder.DefineCustomAttribute(
RuntimeTypeBuilder.DefineCustomAttribute(
_methodBuilder.GetModuleBuilder(),
_token,
((ModuleBuilder)_methodBuilder.GetModule()).GetConstructorToken(con),
((RuntimeModuleBuilder)_methodBuilder.GetModule()).GetMethodMetadataToken(con),
binaryAttribute);
}

Expand All @@ -35,11 +35,11 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
{
ArgumentNullException.ThrowIfNull(customBuilder);

customBuilder.CreateCustomAttribute((ModuleBuilder)(_methodBuilder.GetModule()), _token);
customBuilder.CreateCustomAttribute((RuntimeModuleBuilder)(_methodBuilder.GetModule()), _token);
}

internal ParameterBuilder(
MethodBuilder methodBuilder,
RuntimeMethodBuilder methodBuilder,
int sequence,
ParameterAttributes attributes,
string? paramName) // can be NULL string
Expand All @@ -48,8 +48,8 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
_name = paramName;
_methodBuilder = methodBuilder;
_attributes = attributes;
ModuleBuilder module = _methodBuilder.GetModuleBuilder();
_token = TypeBuilder.SetParamInfo(
RuntimeModuleBuilder module = _methodBuilder.GetModuleBuilder();
_token = RuntimeTypeBuilder.SetParamInfo(
new QCallModule(ref module),
_methodBuilder.MetadataToken,
sequence,
Expand Down Expand Up @@ -77,7 +77,7 @@ internal int GetToken()
private readonly string? _name;
private readonly int _position;
private readonly ParameterAttributes _attributes;
private MethodBuilder _methodBuilder;
private RuntimeMethodBuilder _methodBuilder;
private int _token;
}
}
Loading

0 comments on commit 93b487a

Please sign in to comment.