Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -4,13 +4,20 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
#if NATIVEAOT
using System.Reflection.Runtime.General;
using System.Reflection.Runtime.PropertyInfos;
using Internal.Metadata.NativeFormat;
#else
using MdToken = System.Reflection.MetadataToken;
#endif

namespace System.Reflection
{
internal sealed class RuntimeParameterInfo : ParameterInfo
internal sealed partial class RuntimeParameterInfo : ParameterInfo
{
#region Static Members
#if !NATIVEAOT
internal static ParameterInfo[] GetParameters(IRuntimeMethodInfo method, MemberInfo member, Signature sig)
{
Debug.Assert(method is RuntimeMethodInfo || method is RuntimeConstructorInfo);
Expand Down Expand Up @@ -109,12 +116,15 @@ private static ParameterInfo[] GetParameters(

return args;
}
#endif
#endregion

#region Private Data Members
#if !NATIVEAOT
private readonly int m_tkParamDef;
private readonly MetadataImport m_scope;
private readonly Signature? m_signature;
#endif
private volatile bool m_nameIsCached;
private readonly bool m_noMetadata;
private bool m_noDefaultValue;
Expand Down Expand Up @@ -150,7 +160,11 @@ internal void SetAttributes(ParameterAttributes attributes)
internal RuntimeParameterInfo(RuntimeParameterInfo accessor, RuntimePropertyInfo property)
: this(accessor, (MemberInfo)property)
{
#if NATIVEAOT
m_signature = property.GetParameterTypeHandle(PositionImpl);
#else
m_signature = property.Signature;
#endif
}

private RuntimeParameterInfo(RuntimeParameterInfo accessor, MemberInfo member)
Expand All @@ -172,10 +186,16 @@ private RuntimeParameterInfo(RuntimeParameterInfo accessor, MemberInfo member)

// Strictly speaking, properties don't contain parameter tokens
// However we need this to make ca's work... oh well...
#if NATIVEAOT
m_tkParamDef = accessor.m_tkParamDef;
m_typeContext = accessor.m_typeContext;
#else
m_tkParamDef = MdToken.IsNullToken(accessor.MetadataToken) ? (int)MetadataTokenType.ParamDef : accessor.MetadataToken;
#endif
m_scope = accessor.m_scope;
}

#if !NATIVEAOT
private RuntimeParameterInfo(
Signature signature, MetadataImport scope, int tkParamDef,
int position, ParameterAttributes attributes, MemberInfo member)
Expand All @@ -194,6 +214,7 @@ private RuntimeParameterInfo(
ClassImpl = null;
NameImpl = null;
}
#endif

// ctor for no metadata MethodInfo in the DynamicMethod and RuntimeMethodInfo cases
internal RuntimeParameterInfo(MethodInfo owner, string? name, Type parameterType, int position)
Expand All @@ -205,7 +226,9 @@ internal RuntimeParameterInfo(MethodInfo owner, string? name, Type parameterType
ClassImpl = parameterType;
PositionImpl = position;
AttrsImpl = ParameterAttributes.None;
#if !NATIVEAOT
m_tkParamDef = (int)MetadataTokenType.ParamDef;
#endif
m_scope = default;
}
#endregion
Expand All @@ -218,6 +241,9 @@ public override Type ParameterType
// only instance of ParameterInfo has ClassImpl, all its subclasses don't
if (ClassImpl == null)
{
#if NATIVEAOT
ClassImpl = m_signature.Resolve(m_typeContext).ToType();
#else
Debug.Assert(m_signature != null);

RuntimeType parameterType;
Expand All @@ -229,6 +255,7 @@ public override Type ParameterType
Debug.Assert(parameterType != null);
// different thread could only write ClassImpl to the same value, so a race condition is not a problem here
ClassImpl = parameterType;
#endif
}

return ClassImpl;
Expand Down Expand Up @@ -344,8 +371,12 @@ private bool TryGetDefaultValueInternal(bool raw, out object? defaultValue)
// Prioritize metadata constant over custom attribute constant
#region Look for a default value in metadata
// This will return DBNull.Value if no constant value is defined on m_tkParamDef in the metadata.
#if NATIVEAOT
defaultValue = GetDefaultValueFromMetadata(raw);
#else
defaultValue = MdConstant.GetValue(m_scope, m_tkParamDef, ParameterType.TypeHandle, raw);
GC.KeepAlive(this);
#endif

// If default value is not specified in metadata, look for it in custom attributes
if (defaultValue == DBNull.Value)
Expand Down Expand Up @@ -414,6 +445,7 @@ private static DateTime GetRawDateTimeConstant(CustomAttributeData attr)
return DBNull.Value;
}

#if !NATIVEAOT
internal RuntimeModule? GetRuntimeModule()
{
RuntimeMethodInfo? method = Member as RuntimeMethodInfo;
Expand Down Expand Up @@ -444,6 +476,7 @@ public override Type[] GetOptionalCustomModifiers()

public override Type GetModifiedParameterType() =>
ModifiedType.Create(unmodifiedType: ParameterType, m_signature, parameterIndex: PositionImpl + 1);
#endif

#endregion

Expand Down Expand Up @@ -491,4 +524,17 @@ public override IList<CustomAttributeData> GetCustomAttributesData()
}
#endregion
}
#if NATIVEAOT

file static class MdToken
{
public static bool IsNullToken(ParameterHandle token) => token.IsNil;
}

file static class MetadataImportExtensions
{
public static string GetName(this MetadataReader? scope, ParameterHandle token) =>
scope!.GetParameter(token).Name.GetStringOrNull(scope!) ?? string.Empty;
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="$(CoreClrProjectRoot)System.Private.CoreLib\src\System\Reflection\RuntimeParameterInfo.cs" />
<!-- TODO: (async) once we know which helpers can actually be shared, move those to libraries partition -->
<Compile Include="$(CoreClrProjectRoot)System.Private.CoreLib\src\System\Runtime\CompilerServices\AsyncHelpers.CoreCLR.cs" />
<Compile Include="$(CoreClrProjectRoot)System.Private.CoreLib\src\System\Runtime\CompilerServices\AsyncHelpers.ValueTaskSourceContinuation.cs" />
Expand Down Expand Up @@ -164,6 +165,7 @@
<Compile Include="System\Reflection\ModifiedType.NativeAot.cs" />
<Compile Include="System\Reflection\RuntimeCustomAttributeData.NativeAot.cs" />
<Compile Include="System\Reflection\RuntimeMethodInfo.cs" />
<Compile Include="System\Reflection\RuntimeParameterInfo.NativeAot.cs" />
<Compile Include="System\Reflection\Runtime\TypeInfos\RuntimeFunctionPointerTypeInfo.cs" />
<Compile Include="System\Reflection\Runtime\TypeInfos\RuntimeFunctionPointerTypeInfo.UnificationKey.cs" />
<Compile Include="System\CrashInfo.cs" />
Expand Down Expand Up @@ -434,16 +436,8 @@
<Compile Include="System\Reflection\Runtime\MethodInfos\RuntimeNamedMethodInfo.cs" />
<Compile Include="System\Reflection\Runtime\MethodInfos\RuntimeSyntheticMethodInfo.cs" />
<Compile Include="System\Reflection\Runtime\MethodInfos\SyntheticMethodId.cs" />
<Compile Include="System\Reflection\Runtime\MethodInfos\VirtualRuntimeParameterInfoArray.cs" />
<Compile Include="System\Reflection\Runtime\Modules\RuntimeModule.cs" />
<Compile Include="System\Reflection\Runtime\Modules\NativeFormat\NativeFormatRuntimeModule.cs" />
<Compile Include="System\Reflection\Runtime\ParameterInfos\NativeFormat\NativeFormatMethodParameterInfo.cs" />
<Compile Include="System\Reflection\Runtime\ParameterInfos\RuntimeFatMethodParameterInfo.cs" />
<Compile Include="System\Reflection\Runtime\ParameterInfos\RuntimeMethodParameterInfo.cs" />
<Compile Include="System\Reflection\Runtime\ParameterInfos\RuntimeParameterInfo.cs" />
<Compile Include="System\Reflection\Runtime\ParameterInfos\RuntimePropertyIndexParameterInfo.cs" />
<Compile Include="System\Reflection\Runtime\ParameterInfos\RuntimeThinMethodParameterInfo.cs" />
<Compile Include="System\Reflection\Runtime\ParameterInfos\RuntimeSyntheticParameterInfo.cs" />
<Compile Include="System\Reflection\Runtime\PropertyInfos\NativeFormat\NativeFormatRuntimePropertyInfo.cs" />
<Compile Include="System\Reflection\Runtime\PropertyInfos\RuntimePropertyInfo.cs" />
<Compile Include="System\Reflection\Runtime\TypeInfos\NativeFormat\NativeFormatRuntimeNamedTypeInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public TypeSignature(MetadataReader reader, Handle handle)
internal Type GetTypeParameter(Type unmodifiedType, int index)
{
MetadataReader reader = _typeSignature.Reader;
if (reader is null)
return Create(unmodifiedType);

Handle handle = _typeSignature.Handle;

while (handle.HandleType == HandleType.ModifiedType)
Expand Down Expand Up @@ -77,6 +80,9 @@ internal Type GetTypeParameter(Type unmodifiedType, int index)
internal SignatureCallingConvention GetCallingConventionFromFunctionPointer()
{
MetadataReader reader = _typeSignature.Reader;
if (reader is null)
return default;

Handle fnPtrTypeSigHandle = reader.GetTypeSpecification(
_typeSignature.Handle.ToTypeSpecificationHandle(reader)).Signature;
MethodSignatureHandle methodSigHandle = reader.GetFunctionPointerSignature(
Expand All @@ -93,6 +99,9 @@ private Type[] GetCustomModifiers(bool required)
ArrayBuilder<Type> builder = default;

MetadataReader reader = _typeSignature.Reader;
if (reader is null)
return [];

Handle handle = _typeSignature.Handle;

while (handle.HandleType == HandleType.ModifiedType)
Expand All @@ -117,6 +126,9 @@ private Type[] GetCustomModifiers(bool required)
return result;
}

internal static Type Create(Type unmodifiedType)
=> Create(unmodifiedType, default(TypeSignature));

public static Type Create(Type unmodifiedType, MetadataReader reader, Handle typeSignature)
=> ModifiedType.Create(unmodifiedType, new TypeSignature(reader, typeSignature));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Reflection.Runtime.General;
using System.Reflection.Runtime.MethodInfos;
using System.Reflection.Runtime.MethodInfos.NativeFormat;
using System.Reflection.Runtime.ParameterInfos;
using System.Reflection.Runtime.TypeInfos;
using System.Reflection.Runtime.TypeInfos.NativeFormat;
using System.Runtime.CompilerServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Runtime.General;
using System.Reflection.Runtime.ParameterInfos;
using System.Reflection.Runtime.TypeInfos;
using System.Runtime.CompilerServices;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,3 @@ internal static RuntimeModule GetRuntimeModule(NativeFormatRuntimeAssembly assem
}
}
}

namespace System.Reflection.Runtime.ParameterInfos.NativeFormat
{
//-----------------------------------------------------------------------------------------------------------
// ParameterInfos for MethodBase objects with Parameter metadata.
//-----------------------------------------------------------------------------------------------------------
internal sealed partial class NativeFormatMethodParameterInfo
{
internal static NativeFormatMethodParameterInfo GetNativeFormatMethodParameterInfo(MethodBase member, int position, ParameterHandle parameterHandle, QSignatureTypeHandle qualifiedParameterType, TypeContext typeContext)
{
return new NativeFormatMethodParameterInfo(member, position, parameterHandle, qualifiedParameterType, typeContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,39 +158,3 @@ internal static RuntimeMethodInfo GetRuntimeSyntheticMethodInfo(SyntheticMethodI
}
}
}

namespace System.Reflection.Runtime.ParameterInfos
{
//-----------------------------------------------------------------------------------------------------------
// ParameterInfos for MethodBase objects with no Parameter metadata.
//-----------------------------------------------------------------------------------------------------------
internal sealed partial class RuntimeThinMethodParameterInfo : RuntimeMethodParameterInfo
{
internal static RuntimeThinMethodParameterInfo GetRuntimeThinMethodParameterInfo(MethodBase member, int position, QSignatureTypeHandle qualifiedParameterType, TypeContext typeContext)
{
return new RuntimeThinMethodParameterInfo(member, position, qualifiedParameterType, typeContext);
}
}

//-----------------------------------------------------------------------------------------------------------
// ParameterInfos returned by PropertyInfo.GetIndexParameters()
//-----------------------------------------------------------------------------------------------------------
internal sealed partial class RuntimePropertyIndexParameterInfo : RuntimeParameterInfo
{
internal static RuntimePropertyIndexParameterInfo GetRuntimePropertyIndexParameterInfo(RuntimePropertyInfo member, RuntimeParameterInfo backingParameter)
{
return new RuntimePropertyIndexParameterInfo(member, backingParameter);
}
}

//-----------------------------------------------------------------------------------------------------------
// ParameterInfos returned by Get/Set methods on array types.
//-----------------------------------------------------------------------------------------------------------
internal sealed partial class RuntimeSyntheticParameterInfo : RuntimeParameterInfo
{
internal static RuntimeSyntheticParameterInfo GetRuntimeSyntheticParameterInfo(MemberInfo member, int position, RuntimeTypeInfo parameterType)
{
return new RuntimeSyntheticParameterInfo(member, position, parameterType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ internal Type[] GetCustomModifiers(TypeContext typeContext, bool optional)
#endif
}

internal Type GetModifiedType(TypeContext typeContext)
internal Type GetModifiedType(Type unmodifiedType)
{
return ModifiedType.Create(Resolve(typeContext).ToType(), (global::Internal.Metadata.NativeFormat.MetadataReader)Reader, _handle);
return Reader is null
? ModifiedType.Create(unmodifiedType)
: ModifiedType.Create(unmodifiedType, (global::Internal.Metadata.NativeFormat.MetadataReader)Reader, _handle);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Runtime.General;
using System.Reflection.Runtime.ParameterInfos;
using System.Reflection.Runtime.TypeInfos;
using System.Text;

Expand Down Expand Up @@ -38,12 +37,7 @@ internal interface IRuntimeMethodCommon<TRuntimeMethodCommon> where TRuntimeMeth
MetadataReader GetMetadataReader();
CustomAttributeHandleCollection GetCustomAttributeHandles();

/// <summary>
/// Parse the metadata that describes parameters, and for each parameter for which there is specific metadata
/// construct a RuntimeParameterInfo and fill in the VirtualRuntimeParameterInfoArray. Do remember to use contextMethod
/// instead of using the one internal to the RuntimeMethodCommon, as the runtime may pass in a subtly different context.
/// </summary>
void FillInMetadataDescribedParameters(ref VirtualRuntimeParameterInfoArray result, QSignatureTypeHandle[] parameterTypes, MethodBase contextMethod, TypeContext typeContext);
ParameterHandleCollection ParameterHandles { get; }

string Name { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection.Runtime.General;
using System.Reflection.Runtime.ParameterInfos;
using System.Reflection.Runtime.ParameterInfos.NativeFormat;
using System.Reflection.Runtime.TypeInfos;
using System.Reflection.Runtime.TypeInfos.NativeFormat;

Expand Down Expand Up @@ -55,21 +53,7 @@ public NativeFormatMethodCommon RuntimeMethodCommonOfUninstantiatedMethod
}
}

public void FillInMetadataDescribedParameters(ref VirtualRuntimeParameterInfoArray result, QSignatureTypeHandle[] typeSignatures, MethodBase contextMethod, TypeContext typeContext)
{
foreach (ParameterHandle parameterHandle in _method.Parameters)
{
Parameter parameterRecord = parameterHandle.GetParameter(_reader);
int index = parameterRecord.Sequence;
result[index] =
NativeFormatMethodParameterInfo.GetNativeFormatMethodParameterInfo(
contextMethod,
index - 1,
parameterHandle,
typeSignatures[index],
typeContext);
}
}
public ParameterHandleCollection ParameterHandles => _method.Parameters;

public int GenericParameterCount => MethodHandle.GetMethod(Reader).GenericParameters.Count;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Runtime.ParameterInfos;
using System.Reflection.Runtime.TypeInfos;

using Internal.Reflection.Core.Execution;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Reflection.Runtime.General;
using System.Reflection.Runtime.ParameterInfos;
using System.Reflection.Runtime.TypeInfos;

using Internal.Metadata.NativeFormat;
Expand Down Expand Up @@ -205,9 +204,14 @@ internal sealed override string RuntimeName
}
}

internal sealed override RuntimeParameterInfo[] GetRuntimeParameters(RuntimeMethodInfo contextMethod, out RuntimeParameterInfo returnParameter)
internal sealed override RuntimeParameterInfo[] GetRuntimeParameters(RuntimeMethodInfo contextMethod)
{
return _genericMethodDefinition.GetRuntimeParameters(this, out returnParameter);
return _genericMethodDefinition.GetRuntimeParameters(this);
}

internal sealed override RuntimeParameterInfo GetRuntimeReturnParameter(RuntimeMethodInfo contextMethod)
{
return _genericMethodDefinition.GetRuntimeReturnParameter(this);
}

internal sealed override RuntimeMethodInfo WithReflectedTypeSetToDeclaringType
Expand Down
Loading
Loading