Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make System.Reflection.Emit public types abstract #78544

Merged
merged 45 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
d8d444d
Add abstract AssemblyBuilder
jkotas Feb 2, 2022
97ed454
Create managed RuntimeAssemblyBuilder
jkotas Feb 6, 2022
791b4cb
Add RuntimeAssemblyBuilder to the build
jkotas Feb 6, 2022
eaa8c73
Boilerplate for RuntimeAssemblyBuilder
jkotas Feb 8, 2022
80c310d
Emit Assembly record
jkotas Feb 9, 2022
c82c7d0
Fixups after rebase
jkotas Feb 9, 2022
85b37d6
Add temporary ReflectionEmitLoadContext
jkotas Feb 9, 2022
cde27c2
Factor out RuntimeModuleBuilder
jkotas Feb 9, 2022
6fe4240
Abstract TypeBuilder and EventBuilder
jkotas Mar 23, 2022
79dd71a
Refactor PropertyBuilder
jkotas Mar 25, 2022
7ec068b
Refactor EnumBuilder
jkotas Mar 25, 2022
b36778c
Refactor FieldBuilder
jkotas Mar 25, 2022
6ee8a67
Refactor ConstructorBuilder and MethodBuilder
jkotas Mar 25, 2022
e127952
Fix app compat issues
buyaa-n Nov 10, 2022
e102000
Revert updates that not needed for initial step
buyaa-n Nov 16, 2022
c32f0e5
Make GenericTypeParameterBuilder abstract
buyaa-n Nov 18, 2022
3b4cba9
Merge conflicts
buyaa-n Nov 18, 2022
0bbcaa7
Fix errors found after merge
buyaa-n Nov 18, 2022
9b96074
Fix app compat issue
buyaa-n Nov 18, 2022
8dcc68d
Merge remote-tracking branch 'upstream/main' into reflection-emit
ViktorHofer Nov 22, 2022
9017148
Move suppressions into the common file
ViktorHofer Nov 22, 2022
a8bf9f2
Update mono implementations
buyaa-n Dec 9, 2022
bab5062
Merge conflicts
buyaa-n Dec 9, 2022
c8a2574
Merge conflicts
buyaa-n Dec 9, 2022
0383fc6
Handle API compat warnings
buyaa-n Dec 9, 2022
b656b01
Fix some failures
buyaa-n Dec 12, 2022
fe0fddf
Mono updates
buyaa-n Dec 13, 2022
3433e5a
Mono updates
buyaa-n Dec 20, 2022
a6928a5
Merge branch 'main' of https://github.com/dotnet/runtime into reflect…
buyaa-n Dec 21, 2022
902f550
Suppress unused variable warning
buyaa-n Dec 21, 2022
a6f202e
Mono failure fixes
buyaa-n Dec 28, 2022
ceb7230
Check ModuleBuilder from generic arguments
buyaa-n Dec 29, 2022
9de8783
Update src/mono/System.Private.CoreLib/src/System/Reflection/Emit/Typ…
buyaa-n Dec 29, 2022
9f07478
Keep runtime old behavior for TypeBuilder static methods
buyaa-n Dec 29, 2022
c4287ae
Merge branch 'reflection-emit' of github.com:buyaa-n/runtime into ref…
buyaa-n Dec 29, 2022
6c1e9ed
Remove/revert some unwanted changes
buyaa-n Jan 3, 2023
dd90d0a
Merge branch 'main' of https://github.com/dotnet/runtime into reflect…
buyaa-n Jan 4, 2023
522d3a7
Merge branch 'main' of https://github.com/dotnet/runtime into reflect…
buyaa-n Jan 5, 2023
a348398
Remove unneeded change
buyaa-n Jan 6, 2023
24f5298
Apply latest feedback
buyaa-n Jan 20, 2023
eeab882
Merge conflict
buyaa-n Jan 20, 2023
34c0216
Use protected absract methods and other related changes
buyaa-n Jan 23, 2023
98ca969
Apply feedback and latest API review updates, refert compat suppressions
buyaa-n Jan 25, 2023
039a89a
Add Assert
buyaa-n Jan 26, 2023
a16ce6e
Fix API compat failures
buyaa-n Jan 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.GetMetadataToken(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.GetMetadataToken(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.GetMetadataToken(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.GetMetadataToken(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.GetMetadataToken(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.GetMetadataToken(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()).GetMetadataToken(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