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

Add ConstructorBuilder implementation, integrate TypeBuilder.GetXyz() static methods #94732

Merged
merged 13 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ internal RuntimeMethodBuilder(string name, MethodAttributes attributes, CallingC
}
else if ((attributes & MethodAttributes.Virtual) != 0)
{
// On an interface, the rule is slighlty different
if (((attributes & MethodAttributes.Abstract) == 0))
// On an interface, the rule is slightly different
if ((attributes & MethodAttributes.Abstract) == 0)
throw new ArgumentException(SR.Arg_NoStaticVirtual);
}

Expand Down Expand Up @@ -122,7 +122,7 @@ internal RuntimeMethodBuilder(string name, MethodAttributes attributes, CallingC
m_ubBody = null;
m_ilGenerator = null;

// Default is managed IL. Manged IL has bit flag 0x0020 set off
// Default is managed IL. Managed IL has bit flag 0x0020 set off
m_dwMethodImplFlags = MethodImplAttributes.IL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,6 @@

namespace System.Reflection.Emit
{
public abstract partial class TypeBuilder
{
#region Public Static Methods
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern",
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
public static MethodInfo GetMethod(Type type, MethodInfo method)
{
if (type is not TypeBuilder && type is not TypeBuilderInstantiation)
throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type));

// The following checks establishes invariants that more simply put require type to be generic and
// method to be a generic method definition declared on the generic type definition of type.
// To create generic method G<Foo>.M<Bar> these invariants require that G<Foo>.M<S> be created by calling
// this function followed by MakeGenericMethod on the resulting MethodInfo to finally get G<Foo>.M<Bar>.
// We could also allow G<T>.M<Bar> to be created before G<Foo>.M<Bar> (BindGenParm followed by this method)
// if we wanted to but that just complicates things so these checks are designed to prevent that scenario.

if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
throw new ArgumentException(SR.Argument_NeedGenericMethodDefinition, nameof(method));

if (method.DeclaringType == null || !method.DeclaringType.IsGenericTypeDefinition)
throw new ArgumentException(SR.Argument_MethodNeedGenericDeclaringType, nameof(method));

if (type.GetGenericTypeDefinition() != method.DeclaringType)
throw new ArgumentException(SR.Argument_InvalidMethodDeclaringType, nameof(type));

// The following converts from Type or TypeBuilder of G<T> to TypeBuilderInstantiation G<T>. These types
// both logically represent the same thing. The runtime displays a similar convention by having
// G<M>.M() be encoded by a typeSpec whose parent is the typeDef for G<M> and whose instantiation is also G<M>.
if (type.IsGenericTypeDefinition)
type = type.MakeGenericType(type.GetGenericArguments());

if (type is not TypeBuilderInstantiation typeBuilderInstantiation)
throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));

return MethodOnTypeBuilderInstantiation.GetMethod(method, typeBuilderInstantiation);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern",
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
{
if (type is not TypeBuilder && type is not TypeBuilderInstantiation)
throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type));

if (!constructor.DeclaringType!.IsGenericTypeDefinition)
throw new ArgumentException(SR.Argument_ConstructorNeedGenericDeclaringType, nameof(constructor));

if (type.GetGenericTypeDefinition() != constructor.DeclaringType)
throw new ArgumentException(SR.Argument_InvalidConstructorDeclaringType, nameof(type));

// TypeBuilder G<T> ==> TypeBuilderInstantiation G<T>
if (type.IsGenericTypeDefinition)
type = type.MakeGenericType(type.GetGenericArguments());

if (type is not TypeBuilderInstantiation typeBuilderInstantiation)
throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));

return ConstructorOnTypeBuilderInstantiation.GetConstructor(constructor, typeBuilderInstantiation);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern",
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
public static FieldInfo GetField(Type type, FieldInfo field)
{
if (type is not TypeBuilder and not TypeBuilderInstantiation)
throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type));

if (!field.DeclaringType!.IsGenericTypeDefinition)
throw new ArgumentException(SR.Argument_FieldNeedGenericDeclaringType, nameof(field));

if (type.GetGenericTypeDefinition() != field.DeclaringType)
throw new ArgumentException(SR.Argument_InvalidFieldDeclaringType, nameof(type));

// TypeBuilder G<T> ==> TypeBuilderInstantiation G<T>
if (type.IsGenericTypeDefinition)
type = type.MakeGenericType(type.GetGenericArguments());

if (type is not TypeBuilderInstantiation typeBuilderInstantiation)
throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));

return FieldOnTypeBuilderInstantiation.GetField(field, typeBuilderInstantiation);
}
#endregion
}

internal sealed partial class RuntimeTypeBuilder : TypeBuilder
{
public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo)
Expand Down Expand Up @@ -1358,7 +1272,6 @@ private RuntimeConstructorBuilder DefineDefaultConstructorNoLock(MethodAttribute

// Define the constructor Builder
constBuilder = (RuntimeConstructorBuilder)DefineConstructor(attributes, CallingConventions.Standard, null);
m_constructorCount++;

// generate the code to call the parent's default constructor
ILGenerator il = constBuilder.GetILGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,21 +668,21 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\AssemblyBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\AssemblyBuilderAccess.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\ConstructorBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\ConstructorOnTypeBuilderInstantiation.cs" Condition="'$(FeatureNativeAot)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\ConstructorOnTypeBuilderInstantiation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\DynamicMethod.cs" Condition="'$(FeatureNativeAot)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\EmptyCAHolder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\EnumBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\EventBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\FieldBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\FieldOnTypeBuilderInstantiation.cs" Condition="'$(FeatureNativeAot)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\FieldOnTypeBuilderInstantiation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\FlowControl.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\GenericTypeParameterBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\ILGenerator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\Label.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\LocalBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\MethodBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\MethodBuilderInstantiation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\MethodOnTypeBuilderInstantiation.cs" Condition="'$(FeatureNativeAot)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\MethodOnTypeBuilderInstantiation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\ModuleBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\Opcode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\OpCodes.cs" />
Expand Down Expand Up @@ -2737,4 +2737,4 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnaryPlusOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnsignedNumber.cs" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Globalization;

namespace System.Reflection.Emit
{
internal sealed partial class ConstructorOnTypeBuilderInstantiation : ConstructorInfo
{
#region Private Static Members
internal static ConstructorInfo GetConstructor(ConstructorInfo Constructor, TypeBuilderInstantiation type)
internal static ConstructorInfo GetConstructor(ConstructorInfo constructor, TypeBuilderInstantiation type)
{
return new ConstructorOnTypeBuilderInstantiation(Constructor, type);
return new ConstructorOnTypeBuilderInstantiation(constructor, type);
}
#endregion

Expand All @@ -23,8 +22,6 @@ internal static ConstructorInfo GetConstructor(ConstructorInfo Constructor, Type
#region Constructor
internal ConstructorOnTypeBuilderInstantiation(ConstructorInfo constructor, TypeBuilderInstantiation type)
{
Debug.Assert(constructor is ConstructorBuilder || constructor is RuntimeConstructorInfo);

_ctor = constructor;
_type = type;
}
Expand All @@ -45,23 +42,7 @@ internal override Type[] GetParameterTypes()
public override object[] GetCustomAttributes(bool inherit) { return _ctor.GetCustomAttributes(inherit); }
public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return _ctor.GetCustomAttributes(attributeType, inherit); }
public override bool IsDefined(Type attributeType, bool inherit) { return _ctor.IsDefined(attributeType, inherit); }
public override int MetadataToken
{
get
{
ConstructorBuilder? cb = _ctor as ConstructorBuilder;

if (cb != null)
{
return cb.MetadataToken;
}
else
{
Debug.Assert(_ctor is RuntimeConstructorInfo);
return _ctor.MetadataToken;
}
}
}
public override int MetadataToken => _ctor.MetadataToken;
public override Module Module => _ctor.Module;
#endregion

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Globalization;

namespace System.Reflection.Emit
Expand Down Expand Up @@ -35,8 +34,6 @@ internal static FieldInfo GetField(FieldInfo Field, TypeBuilderInstantiation typ
#region Constructor
internal FieldOnTypeBuilderInstantiation(FieldInfo field, TypeBuilderInstantiation type)
{
Debug.Assert(field is FieldBuilder || field is RuntimeFieldInfo);

_field = field;
_type = type;
}
Expand All @@ -52,23 +49,7 @@ internal FieldOnTypeBuilderInstantiation(FieldInfo field, TypeBuilderInstantiati
public override object[] GetCustomAttributes(bool inherit) { return _field.GetCustomAttributes(inherit); }
public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return _field.GetCustomAttributes(attributeType, inherit); }
public override bool IsDefined(Type attributeType, bool inherit) { return _field.IsDefined(attributeType, inherit); }
public override int MetadataToken
{
get
{
FieldBuilder? fb = _field as FieldBuilder;

if (fb != null)
{
return fb.MetadataToken;
}
else
{
Debug.Assert(_field is RuntimeFieldInfo);
return _field.MetadataToken;
}
}
}
public override int MetadataToken => _field.MetadataToken;
public override Module Module => _field.Module;
#endregion

Expand All @@ -84,7 +65,7 @@ public override object GetValueDirect(TypedReference obj)
throw new NotImplementedException();
}
public override RuntimeFieldHandle FieldHandle => throw new NotImplementedException();
public override Type FieldType => throw new NotImplementedException();
public override Type FieldType => _field.FieldType;
public override object GetValue(object? obj) { throw new InvalidOperationException(); }
public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, CultureInfo? culture) { throw new InvalidOperationException(); }
public override FieldAttributes Attributes => _field.Attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ internal MethodBuilderInstantiation(MethodInfo method, Type[] inst)
}
#endregion

#if SYSTEM_PRIVATE_CORELIB
internal override Type[] GetParameterTypes()
{
return _method.GetParameterTypes();
}
#endif

#region MemberBase
public override MemberTypes MemberType => _method.MemberType;
Expand All @@ -49,7 +51,7 @@ internal override Type[] GetParameterTypes()
#endregion

#region MethodBase Members
public override ParameterInfo[] GetParameters() { throw new NotSupportedException(); }
public override ParameterInfo[] GetParameters() => _method.GetParameters();
public override MethodImplAttributes GetMethodImplementationFlags() { return _method.GetMethodImplementationFlags(); }
public override RuntimeMethodHandle MethodHandle => throw new NotSupportedException(SR.NotSupported_DynamicModule);
public override MethodAttributes Attributes => _method.Attributes;
Expand Down Expand Up @@ -82,6 +84,7 @@ public override bool ContainsGenericParameters
}
}

[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override MethodInfo MakeGenericMethod(params Type[] arguments)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

Expand All @@ -24,8 +23,6 @@ internal static MethodInfo GetMethod(MethodInfo method, TypeBuilderInstantiation
#region Constructor
internal MethodOnTypeBuilderInstantiation(MethodInfo method, Type type)
{
Debug.Assert(method is MethodBuilder || method is RuntimeMethodInfo);

_method = method;
_type = type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public override Type MakeArrayType(int rank)
public override int GetArrayRank()
{
if (!IsArray)
throw new NotSupportedException(SR.NotSupported_SubclassOverride);
throw new ArgumentException(SR.Argument_HasToBeArrayClass);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InvalidOperationException instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followed exception type and message for similar cases

if (!arrayClass.IsArray)
{
throw new ArgumentException(SR.Argument_HasToBeArrayClass);
}


return _rank;
}
Expand Down
Loading
Loading