/// <param name="runtimeTypeHandle">Runtime type handle (MethodTable) for the given type</param>
public sealed override unsafe bool TryGetNamedTypeForMetadata(QTypeDefinition qTypeDefinition, out RuntimeTypeHandle runtimeTypeHandle)
{
- return TypeLoaderEnvironment.Instance.TryGetOrCreateNamedTypeForMetadata(qTypeDefinition, out runtimeTypeHandle);
+ return TypeLoaderEnvironment.Instance.TryGetNamedTypeForMetadata(qTypeDefinition, out runtimeTypeHandle);
}
/// <summary>
public static string LowLevelToString(this RuntimeTypeHandle rtth)
{
TypeReferenceHandle typeRefHandle;
- QTypeDefinition qTypeDefinition;
MetadataReader reader;
- // Try to get the name from metadata
- if (TypeLoaderEnvironment.Instance.TryGetMetadataForNamedType(rtth, out qTypeDefinition))
- {
-#if ECMA_METADATA_SUPPORT
- string result = EcmaMetadataFullName(qTypeDefinition);
- if (result != null)
- return result;
-#endif
-
- reader = qTypeDefinition.NativeFormatReader;
- TypeDefinitionHandle typeDefHandle = qTypeDefinition.NativeFormatHandle;
- return typeDefHandle.GetFullName(reader);
- }
-
// Try to get the name from diagnostic metadata
if (TypeLoaderEnvironment.TryGetTypeReferenceForNamedType(rtth, out reader, out typeRefHandle))
{
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System;
-using Internal.TypeSystem;
-using Internal.NativeFormat;
-
-namespace Internal.Runtime.TypeLoader
-{
- /// <summary>
- /// Represents a field defined in native layout data, but without metadata
- /// </summary>
- internal class NativeLayoutFieldDesc : FieldDesc
- {
- private DefType _owningType;
- private TypeDesc _fieldType;
- private FieldStorage _fieldStorage;
-
- public NativeLayoutFieldDesc(DefType owningType, TypeDesc fieldType, FieldStorage fieldStorage)
- {
- _owningType = owningType;
- _fieldType = fieldType;
- _fieldStorage = fieldStorage;
- }
-
- public override TypeSystemContext Context
- {
- get
- {
- return _owningType.Context;
- }
- }
-
- public override TypeDesc FieldType
- {
- get
- {
- return _fieldType;
- }
- }
-
- public override EmbeddedSignatureData[] GetEmbeddedSignatureData() => null;
-
- public override bool HasRva
- {
- get
- {
- throw NotImplemented.ByDesign;
- }
- }
-
- public override bool IsInitOnly
- {
- get
- {
- throw NotImplemented.ByDesign;
- }
- }
-
- public override bool IsLiteral
- {
- get
- {
- return false;
- }
- }
-
- public override bool IsStatic
- {
- get
- {
- return _fieldStorage != FieldStorage.Instance;
- }
- }
-
- public override bool IsThreadStatic
- {
- get
- {
- return _fieldStorage == FieldStorage.TLSStatic;
- }
- }
-
- internal FieldStorage FieldStorage
- {
- get
- {
- return _fieldStorage;
- }
- }
-
- public override DefType OwningType
- {
- get
- {
- return _owningType;
- }
- }
-
- public override bool HasCustomAttribute(string attributeNamespace, string attributeName)
- {
- throw NotImplemented.ByDesign;
- }
- }
-}
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-
-using System;
-using System.Runtime;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Runtime.InteropServices;
-using System.Threading;
-
-using Internal.Runtime;
-using Internal.Runtime.Augments;
-
-using Internal.NativeFormat;
-
-namespace Internal.Runtime.TypeLoader
-{
- public sealed partial class PermanentAllocatedMemoryBlobs
- {
- // Various functions in the type loader need to create permanent pointers for various purposes.
-
- private static PermanentlyAllocatedMemoryRegions_Uint_In_IntPtr s_uintCellValues = new PermanentlyAllocatedMemoryRegions_Uint_In_IntPtr();
- private static PermanentlyAllocatedMemoryRegions_IntPtr_In_IntPtr s_pointerIndirectionCellValues = new PermanentlyAllocatedMemoryRegions_IntPtr_In_IntPtr();
-
- private class PermanentlyAllocatedMemoryRegions_Uint_In_IntPtr
- {
- private LowLevelDictionary<uint, IntPtr> _allocatedBlocks = new LowLevelDictionary<uint, IntPtr>();
- private Lock _lock = new Lock();
-
- public unsafe IntPtr GetMemoryBlockForValue(uint value)
- {
- using (LockHolder.Hold(_lock))
- {
- IntPtr result;
- if (_allocatedBlocks.TryGetValue(value, out result))
- {
- return result;
- }
- result = MemoryHelpers.AllocateMemory(IntPtr.Size);
- *(uint*)(result.ToPointer()) = value;
- _allocatedBlocks.Add(value, result);
- return result;
- }
- }
- }
-
- private class PermanentlyAllocatedMemoryRegions_IntPtr_In_IntPtr
- {
- private LowLevelDictionary<IntPtr, IntPtr> _allocatedBlocks = new LowLevelDictionary<IntPtr, IntPtr>();
- private Lock _lock = new Lock();
-
- public unsafe IntPtr GetMemoryBlockForValue(IntPtr value)
- {
- using (LockHolder.Hold(_lock))
- {
- IntPtr result;
- if (_allocatedBlocks.TryGetValue(value, out result))
- {
- return result;
- }
- result = MemoryHelpers.AllocateMemory(IntPtr.Size);
- *(IntPtr*)(result.ToPointer()) = value;
- _allocatedBlocks.Add(value, result);
- return result;
- }
- }
- }
-
- public static IntPtr GetPointerToUInt(uint value)
- {
- return s_uintCellValues.GetMemoryBlockForValue(value);
- }
-
- public static IntPtr GetPointerToIntPtr(IntPtr value)
- {
- return s_pointerIndirectionCellValues.GetMemoryBlockForValue(value);
- }
- }
-}
using System;
using System.Diagnostics;
-using System.Collections.Generic;
-using System.Runtime.InteropServices;
-
-using Internal.Runtime;
-using Internal.Runtime.Augments;
using Internal.NativeFormat;
using Internal.TypeSystem;
//
public static TypeDesc TryGetTypeTemplate(TypeDesc concreteType, ref NativeLayoutInfo nativeLayoutInfo)
{
-#if GENERICS_FORCE_USG
- return TryGetUniversalTypeTemplate(concreteType, ref nativeLayoutInfo);
-#else
- // First, see if there is a specific canonical template
- TypeDesc result = TryGetTypeTemplate_Internal(concreteType, CanonicalFormKind.Specific, out nativeLayoutInfo.Module, out nativeLayoutInfo.Offset);
-
- // If not found, see if there's a universal canonical template
- result ??= TryGetUniversalTypeTemplate(concreteType, ref nativeLayoutInfo);
-
- return result;
-#endif
- }
-
- public static TypeDesc TryGetUniversalTypeTemplate(TypeDesc concreteType, ref NativeLayoutInfo nativeLayoutInfo)
- {
- return TryGetTypeTemplate_Internal(concreteType, CanonicalFormKind.Universal, out nativeLayoutInfo.Module, out nativeLayoutInfo.Offset);
- }
-
-#if GENERICS_FORCE_USG
- public TypeDesc TryGetNonUniversalTypeTemplate(TypeDesc concreteType, ref NativeLayoutInfo nativeLayoutInfo)
- {
return TryGetTypeTemplate_Internal(concreteType, CanonicalFormKind.Specific, out nativeLayoutInfo.Module, out nativeLayoutInfo.Offset);
}
-#endif
-
- /// <summary>
- /// Get the NativeLayout for a method from a ReadyToRun image.
- /// </summary>
- public static bool TryGetMetadataNativeLayout(MethodDesc concreteMethod, out NativeFormatModuleInfo nativeLayoutInfoModule, out uint nativeLayoutInfoToken)
- {
- nativeLayoutInfoModule = null;
- nativeLayoutInfoToken = 0;
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
- var nativeMetadataType = concreteMethod.GetTypicalMethodDefinition() as TypeSystem.NativeFormat.NativeFormatMethod;
- if (nativeMetadataType == null)
- return false;
-
- var canonForm = concreteMethod.GetCanonMethodTarget(CanonicalFormKind.Specific);
- var hashCode = canonForm.GetHashCode();
-
-#if SUPPORTS_R2R_LOADING
- foreach (var moduleInfo in ModuleList.EnumerateModules())
- {
- if (moduleInfo.MetadataReader == null)
- continue;
-
- ExternalReferencesTable externalFixupsTable;
- NativeHashtable methodTemplatesHashtable = LoadHashtable(moduleInfo.Handle, ReflectionMapBlob.MetadataBasedGenericMethodsTemplateMap, out externalFixupsTable);
-
- if (methodTemplatesHashtable.IsNull)
- continue;
-
- var enumerator = methodTemplatesHashtable.Lookup(hashCode);
- var nativeMetadataUnit = nativeMetadataType.Context.ResolveMetadataUnit(moduleInfo);
-
- NativeParser entryParser;
- while (!(entryParser = enumerator.GetNext()).IsNull)
- {
- var entryTypeHandle = entryParser.GetUnsigned().AsHandle();
- MethodDesc methodDesc = nativeMetadataUnit.GetMethod(entryTypeHandle, null);
- Debug.Assert(methodDesc != null);
- if (methodDesc == canonForm)
- {
- TypeLoaderLogger.WriteLine("Found metadata template for method " + concreteMethod.ToString() + ": " + methodDesc.ToString());
- nativeLayoutInfoToken = entryParser.GetUnsigned();
- if (nativeLayoutInfoToken == BadTokenFixupValue)
- {
- throw new BadImageFormatException();
- }
-
- nativeLayoutInfoModule = moduleInfo;
- return true;
- }
- }
- }
-#endif
-#endif
- return false;
- }
private static TypeDesc TryGetTypeTemplate_Internal(TypeDesc concreteType, CanonicalFormKind kind, out NativeFormatModuleInfo nativeLayoutInfoModule, out uint nativeLayoutInfoToken)
{
//
public static InstantiatedMethod TryGetGenericMethodTemplate(InstantiatedMethod concreteMethod, out NativeFormatModuleInfo nativeLayoutInfoModule, out uint nativeLayoutInfoToken)
{
- // First, see if there is a specific canonical template
- InstantiatedMethod result = TryGetGenericMethodTemplate_Internal(concreteMethod, CanonicalFormKind.Specific, out nativeLayoutInfoModule, out nativeLayoutInfoToken);
-
- // If not found, see if there's a universal canonical template
- result ??= TryGetGenericMethodTemplate_Internal(concreteMethod, CanonicalFormKind.Universal, out nativeLayoutInfoModule, out nativeLayoutInfoToken);
-
- return result;
+ return TryGetGenericMethodTemplate_Internal(concreteMethod, CanonicalFormKind.Specific, out nativeLayoutInfoModule, out nativeLayoutInfoToken);
}
private static InstantiatedMethod TryGetGenericMethodTemplate_Internal(InstantiatedMethod concreteMethod, CanonicalFormKind kind, out NativeFormatModuleInfo nativeLayoutInfoModule, out uint nativeLayoutInfoToken)
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
-using System.Reflection;
-using System.Runtime;
-using System.Text;
-
-using System.Reflection.Runtime.General;
using Internal.Runtime.Augments;
using Internal.Runtime.CompilerServices;
-using Internal.Metadata.NativeFormat;
using Internal.NativeFormat;
using Internal.TypeSystem;
-using Internal.TypeSystem.NativeFormat;
-using Internal.TypeSystem.NoMetadata;
namespace Internal.Runtime.TypeLoader
{
using DynamicGenericsRegistrationData = TypeLoaderEnvironment.DynamicGenericsRegistrationData;
using GenericTypeEntry = TypeLoaderEnvironment.GenericTypeEntry;
- using TypeEntryToRegister = TypeLoaderEnvironment.TypeEntryToRegister;
using GenericMethodEntry = TypeLoaderEnvironment.GenericMethodEntry;
- using HandleBasedGenericTypeLookup = TypeLoaderEnvironment.HandleBasedGenericTypeLookup;
- using DefTypeBasedGenericTypeLookup = TypeLoaderEnvironment.DefTypeBasedGenericTypeLookup;
using HandleBasedGenericMethodLookup = TypeLoaderEnvironment.HandleBasedGenericMethodLookup;
using MethodDescBasedGenericMethodLookup = TypeLoaderEnvironment.MethodDescBasedGenericMethodLookup;
-#if FEATURE_UNIVERSAL_GENERICS
- using ThunkKind = CallConverterThunk.ThunkKind;
-#endif
internal static class LowLevelListExtensions
{
}
}
- [Flags]
- internal enum FieldLoadState
- {
- None = 0,
- Instance = 1,
- Statics = 2,
- }
-
- public static class TypeBuilderApi
- {
- public static void ResolveMultipleCells(GenericDictionaryCell [] cells, out IntPtr[] fixups)
- {
- TypeBuilder.ResolveMultipleCells(cells, out fixups);
- }
- }
-
-
internal class TypeBuilder
{
public TypeBuilder()
TypeLoaderEnvironment.Instance.VerifyTypeLoaderLockHeld();
}
- private const int MinimumValueTypeSize = 0x1;
-
/// <summary>
/// The StaticClassConstructionContext for a type is encoded in the negative space
/// of the NonGCStatic fields of a type.
private LowLevelList<TypeDesc> _typesThatNeedPreparation;
- private object _epoch = new object();
-
#if DEBUG
private bool _finalTypeBuilding;
#endif
}
}
- private IEnumerable<TypeEntryToRegister> TypesToRegister()
+ private IEnumerable<GenericTypeEntry> TypesToRegister()
{
for (int i = 0; i < _typesThatNeedTypeHandles.Count; i++)
{
if (typeAsDefType == null)
continue;
- if (typeAsDefType.HasInstantiation && !typeAsDefType.IsTypeDefinition)
+ yield return new GenericTypeEntry
{
- yield return new TypeEntryToRegister
- {
- GenericTypeEntry = new GenericTypeEntry
- {
- _genericTypeDefinitionHandle = GetRuntimeTypeHandle(typeAsDefType.GetTypeDefinition()),
- _genericTypeArgumentHandles = GetRuntimeTypeHandles(typeAsDefType.Instantiation),
- _instantiatedTypeHandle = typeAsDefType.GetTypeBuilderState().HalfBakedRuntimeTypeHandle
- }
- };
- }
- else
- {
- yield return new TypeEntryToRegister
- {
- MetadataDefinitionType = (MetadataType)typeAsDefType
- };
- }
+ _genericTypeDefinitionHandle = GetRuntimeTypeHandle(typeAsDefType.GetTypeDefinition()),
+ _genericTypeArgumentHandles = GetRuntimeTypeHandles(typeAsDefType.Instantiation),
+ _instantiatedTypeHandle = typeAsDefType.GetTypeBuilderState().HalfBakedRuntimeTypeHandle
+ };
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
-using System.Reflection;
-using System.Text;
-using Internal.Runtime;
-using Internal.Runtime.Augments;
-using Internal.Runtime.CompilerServices;
-
-using Internal.Metadata.NativeFormat;
using Internal.NativeFormat;
using Internal.TypeSystem;
-using Internal.TypeSystem.NativeFormat;
-using Internal.TypeSystem.NoMetadata;
namespace Internal.Runtime.TypeLoader
{
using System;
-using System.Runtime;
-using System.Collections.Generic;
using System.Diagnostics;
-using System.Runtime.InteropServices;
using System.Threading;
-using Internal.Runtime;
-using Internal.Runtime.Augments;
using Internal.Runtime.CompilerServices;
using Internal.NativeFormat;
Debug.Assert(methodPointer != IntPtr.Zero && dictionaryPointer != IntPtr.Zero);
-#if FEATURE_UNIVERSAL_GENERICS
- if (templateMethod.IsCanonicalMethod(CanonicalFormKind.Universal))
- {
- // Check if we need to wrap the method pointer into a calling convention converter thunk
- if (!TypeLoaderEnvironment.Instance.MethodSignatureHasVarsNeedingCallingConventionConverter(context, nameAndSignature.Signature))
- {
- TypeSystemContextFactory.Recycle(context);
- return true;
- }
-
- RuntimeTypeHandle[] typeArgs = Array.Empty<RuntimeTypeHandle>();
-
- if (RuntimeAugments.IsGenericType(targetTypeHandle))
- {
- RuntimeAugments.GetGenericInstantiation(targetTypeHandle, out typeArgs);
- }
-
- // Create a CallingConventionConverter to call the method correctly
- IntPtr thunkPtr = CallConverterThunk.MakeThunk(
- CallConverterThunk.ThunkKind.StandardToGenericInstantiating,
- methodPointer,
- nameAndSignature.Signature,
- dictionaryPointer,
- typeArgs,
- genericMethodArgumentHandles);
-
- Debug.Assert(thunkPtr != IntPtr.Zero);
-
- methodPointer = thunkPtr;
- // Set dictionaryPointer to null so we don't make a fat function pointer around the whole thing.
- dictionaryPointer = IntPtr.Zero;
-
- // TODO! add a new call converter thunk that will pass the instantiating arg through and use a fat function pointer.
- // should allow us to make fewer thunks.
- }
-#endif
-
TypeSystemContextFactory.Recycle(context);
return true;
}
{
public sealed partial class TypeLoaderEnvironment
{
- internal struct TypeEntryToRegister
- {
- public GenericTypeEntry GenericTypeEntry;
- public MetadataType MetadataDefinitionType;
- }
-
internal class GenericTypeEntry
{
private int? _hashCode;
using System;
-using System.Runtime;
using System.Collections.Generic;
using System.Diagnostics;
-using System.Runtime.InteropServices;
using System.Threading;
-using Internal.Runtime;
-using Internal.Runtime.Augments;
-using Internal.Runtime.CompilerServices;
-
-using Internal.NativeFormat;
using Internal.TypeSystem;
-using System.Reflection.Runtime.General;
namespace Internal.Runtime.TypeLoader
{
internal struct DynamicGenericsRegistrationData
{
public int TypesToRegisterCount;
- public IEnumerable<TypeEntryToRegister> TypesToRegister;
+ public IEnumerable<GenericTypeEntry> TypesToRegister;
public int MethodsToRegisterCount;
public IEnumerable<GenericMethodEntry> MethodsToRegister;
}
{
int registeredTypesCount = 0;
int registeredMethodsCount = 0;
- int nativeFormatTypesRegisteredCount = 0;
- TypeEntryToRegister[] registeredTypes = null;
+ GenericTypeEntry[] registeredTypes = null;
GenericMethodEntry[] registeredMethods = null;
try
{
if (registrationData.TypesToRegister != null)
{
- registeredTypes = new TypeEntryToRegister[registrationData.TypesToRegisterCount];
+ registeredTypes = new GenericTypeEntry[registrationData.TypesToRegisterCount];
- foreach (TypeEntryToRegister typeEntry in registrationData.TypesToRegister)
+ foreach (GenericTypeEntry typeEntry in registrationData.TypesToRegister)
{
// Keep track of registered type handles so that we can rollback the registration on exception.
registeredTypes[registeredTypesCount++] = typeEntry;
// We can save a bit of memory by avoiding the redundancy where possible. For now, we are keeping it simple.
// Register type -> components mapping first so that we can use it during rollback below
- if (typeEntry.GenericTypeEntry != null)
- {
- GenericTypeEntry registeredTypeEntry = _dynamicGenericTypes.AddOrGetExisting(typeEntry.GenericTypeEntry);
- if (registeredTypeEntry != typeEntry.GenericTypeEntry && registeredTypeEntry._isRegisteredSuccessfully)
- throw new ArgumentException(SR.Argument_AddingDuplicate);
-
- registeredTypeEntry._instantiatedTypeHandle = typeEntry.GenericTypeEntry._instantiatedTypeHandle;
- registeredTypeEntry._isRegisteredSuccessfully = true;
- }
- else
- {
- MetadataType metadataType = typeEntry.MetadataDefinitionType;
- IntPtr nonGcStaticFields = IntPtr.Zero;
- IntPtr gcStaticFields = IntPtr.Zero;
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
-#if SUPPORTS_R2R_LOADING
- uint nonGcStaticsRva = 0;
- uint gcStaticsRva = 0;
-
- // For images where statics are directly embedded in the image, store the information about where
- // to find statics info
- if (TypeLoaderEnvironment.TryGetStaticsTableEntry(metadataType, out nonGcStaticsRva, out gcStaticsRva))
- {
- ModuleInfo moduleInfo = TypeLoaderEnvironment.GetModuleInfoForType(metadataType);
-
- if (nonGcStaticsRva == 0)
- nonGcStaticFields = TypeLoaderEnvironment.NoStaticsData;
- else
- nonGcStaticFields = moduleInfo.Handle + checked((int)nonGcStaticsRva);
-
- if (gcStaticsRva == 0)
- gcStaticFields = TypeLoaderEnvironment.NoStaticsData;
- else
- gcStaticFields = moduleInfo.Handle + checked((int)gcStaticsRva);
- }
-#endif
-
- TypeSystem.NativeFormat.NativeFormatType nativeFormatType = metadataType as TypeSystem.NativeFormat.NativeFormatType;
- if (nativeFormatType != null)
- {
- RegisterNewNamedTypeRuntimeTypeHandle(new QTypeDefinition(nativeFormatType.MetadataReader,
- nativeFormatType.Handle),
- nativeFormatType.GetTypeBuilderState().HalfBakedRuntimeTypeHandle,
- nonGcStaticFields,
- gcStaticFields);
- }
-#if ECMA_METADATA_SUPPORT
- TypeSystem.Ecma.EcmaType ecmaFormatType = metadataType as TypeSystem.Ecma.EcmaType;
- if (ecmaFormatType != null)
- {
- RegisterNewNamedTypeRuntimeTypeHandle(new QTypeDefinition(ecmaFormatType.MetadataReader,
- ecmaFormatType.Handle),
- ecmaFormatType.GetTypeBuilderState().HalfBakedRuntimeTypeHandle,
- nonGcStaticFields,
- gcStaticFields);
- }
-#endif
-
- nativeFormatTypesRegisteredCount++;
-#else
- Environment.FailFast("Ready to Run module type?");
-#endif
- }
+ GenericTypeEntry registeredTypeEntry = _dynamicGenericTypes.AddOrGetExisting(typeEntry);
+ if (registeredTypeEntry != typeEntry && registeredTypeEntry._isRegisteredSuccessfully)
+ throw new ArgumentException(SR.Argument_AddingDuplicate);
+
+ registeredTypeEntry._instantiatedTypeHandle = typeEntry._instantiatedTypeHandle;
+ registeredTypeEntry._isRegisteredSuccessfully = true;
}
}
Debug.Assert(registeredTypesCount == registrationData.TypesToRegisterCount);
{
var typeEntry = registeredTypes[i];
// There is no Remove feature in the LockFreeReaderHashtable...
- if (typeEntry.GenericTypeEntry != null)
- {
- GenericTypeEntry failedEntry = _dynamicGenericTypes.GetValueIfExists(typeEntry.GenericTypeEntry);
- if (failedEntry != null)
- failedEntry._isRegisteredSuccessfully = false;
- }
- else
- {
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
- TypeSystem.NativeFormat.NativeFormatType nativeFormatType = typeEntry.MetadataDefinitionType as TypeSystem.NativeFormat.NativeFormatType;
- if (nativeFormatType != null)
- {
- UnregisterNewNamedTypeRuntimeTypeHandle(new QTypeDefinition(nativeFormatType.MetadataReader,
- nativeFormatType.Handle),
- nativeFormatType.GetTypeBuilderState().HalfBakedRuntimeTypeHandle);
- }
-#if ECMA_METADATA_SUPPORT
- TypeSystem.Ecma.EcmaType ecmaFormatType = typeEntry.MetadataDefinitionType as TypeSystem.Ecma.EcmaType;
- if (ecmaFormatType != null)
- {
- UnregisterNewNamedTypeRuntimeTypeHandle(new QTypeDefinition(ecmaFormatType.MetadataReader,
- ecmaFormatType.Handle),
- ecmaFormatType.GetTypeBuilderState().HalfBakedRuntimeTypeHandle);
- }
-#endif
-#else
- Environment.FailFast("Ready to Run module type?");
-#endif
- }
+ GenericTypeEntry failedEntry = _dynamicGenericTypes.GetValueIfExists(typeEntry);
+ if (failedEntry != null)
+ failedEntry._isRegisteredSuccessfully = false;
}
for (int i = 0; i < registeredMethodsCount; i++)
{
throw;
}
-
- if (nativeFormatTypesRegisteredCount > 0)
- FinishAddingNewNamedTypes();
}
}
ref FieldAccessMetadata fieldAccessMetadata)
{
CanonicallyEquivalentEntryLocator canonWrapper = new CanonicallyEquivalentEntryLocator(declaringTypeHandle, canonFormKind);
- string fieldName = null;
- RuntimeTypeHandle declaringTypeHandleDefinition = TypeLoaderEnvironment.GetTypeDefinition(declaringTypeHandle);
foreach (NativeFormatModuleInfo mappingTableModule in ModuleList.EnumerateModules(RuntimeAugments.GetModuleFromTypeHandle(declaringTypeHandle)))
{
}
else
{
- if (fieldName == null)
- {
- QTypeDefinition qTypeDefinition;
-
- bool success = Instance.TryGetMetadataForNamedType(
- declaringTypeHandleDefinition,
- out qTypeDefinition);
- Debug.Assert(success);
-
- MetadataReader nativeFormatMetadataReader = qTypeDefinition.NativeFormatReader;
-
- fieldName = nativeFormatMetadataReader.GetString(fieldHandle.GetField(nativeFormatMetadataReader).Name);
- }
-
- string entryFieldName = entryParser.GetString();
-
- if (fieldName != entryFieldName)
- continue;
+ Debug.Fail("Multifile path");
}
int fieldOffset;
return false;
}
-
- private enum FieldAccessStaticDataKind
- {
- NonGC,
- GC,
- TLS
- }
}
}
using System;
-using System.Runtime;
-using System.Collections.Generic;
using System.Diagnostics;
-using System.Runtime.InteropServices;
using System.Threading;
using System.Reflection.Runtime.General;
-using Internal.Runtime;
using Internal.Runtime.Augments;
using Internal.Runtime.CompilerServices;
using Internal.Metadata.NativeFormat;
using Internal.NativeFormat;
using Internal.TypeSystem;
-using Internal.TypeSystem.NativeFormat;
#if ECMA_METADATA_SUPPORT
using Internal.TypeSystem.Ecma;
#endif
return default(NativeReader);
}
+ /// <summary>
+ /// Return the metadata handle for a TypeDef if the pay-for-policy enabled this type as browsable. This is used to obtain name and other information for types
+ /// obtained via typeof() or Object.GetType(). This can include generic types (not to be confused with generic instances).
+ ///
+ /// Preconditions:
+ /// runtimeTypeHandle is a typedef (not a constructed type such as an array or generic instance.)
+ /// </summary>
+ /// <param name="runtimeTypeHandle">Runtime handle of the type in question</param>
+ /// <param name="qTypeDefinition">TypeDef handle for the type</param>
+ public unsafe bool TryGetMetadataForNamedType(RuntimeTypeHandle runtimeTypeHandle, out QTypeDefinition qTypeDefinition)
+ {
+ int hashCode = runtimeTypeHandle.GetHashCode();
+
+ // Iterate over all modules, starting with the module that defines the MethodTable
+ foreach (NativeFormatModuleInfo module in ModuleList.EnumerateModules(RuntimeAugments.GetModuleFromTypeHandle(runtimeTypeHandle)))
+ {
+ NativeReader typeMapReader;
+ if (TryGetNativeReaderForBlob(module, ReflectionMapBlob.TypeMap, out typeMapReader))
+ {
+ NativeParser typeMapParser = new NativeParser(typeMapReader, 0);
+ NativeHashtable typeHashtable = new NativeHashtable(typeMapParser);
+
+ ExternalReferencesTable externalReferences = default(ExternalReferencesTable);
+ externalReferences.InitializeCommonFixupsTable(module);
+
+ var lookup = typeHashtable.Lookup(hashCode);
+ NativeParser entryParser;
+ while (!(entryParser = lookup.GetNext()).IsNull)
+ {
+ RuntimeTypeHandle foundType = externalReferences.GetRuntimeTypeHandleFromIndex(entryParser.GetUnsigned());
+ if (foundType.Equals(runtimeTypeHandle))
+ {
+ Handle entryMetadataHandle = entryParser.GetUnsigned().AsHandle();
+ if (entryMetadataHandle.HandleType == HandleType.TypeDefinition)
+ {
+ MetadataReader metadataReader = module.MetadataReader;
+ qTypeDefinition = new QTypeDefinition(metadataReader, entryMetadataHandle.ToTypeDefinitionHandle(metadataReader));
+ return true;
+ }
+ }
+ }
+ }
+ }
+
+ qTypeDefinition = default;
+ return false;
+ }
+
+ /// <summary>
+ /// Return the RuntimeTypeHandle for the named type described in metadata. This is used to implement the Create and Invoke
+ /// apis for types.
+ ///
+ /// Preconditions:
+ /// metadataReader + typeDefHandle - a valid metadata reader + typeDefinitionHandle where "metadataReader" is one
+ /// of the metadata readers returned by ExecutionEnvironment.MetadataReaders.
+ ///
+ /// Note: Although this method has a "bool" return value like the other mapping table accessors, the pay-for-play design
+ /// guarantees that any type enabled for metadata also has a RuntimeTypeHandle underneath.
+ /// </summary>
+ /// <param name="qTypeDefinition">TypeDef handle for the type to look up</param>
+ /// <param name="runtimeTypeHandle">Runtime type handle (MethodTable) for the given type</param>
+ public unsafe bool TryGetNamedTypeForMetadata(QTypeDefinition qTypeDefinition, out RuntimeTypeHandle runtimeTypeHandle)
+ {
+ if (qTypeDefinition.IsNativeFormatMetadataBased)
+ {
+ MetadataReader metadataReader = qTypeDefinition.NativeFormatReader;
+ TypeDefinitionHandle typeDefHandle = qTypeDefinition.NativeFormatHandle;
+ int hashCode = typeDefHandle.ComputeHashCode(metadataReader);
+
+ NativeFormatModuleInfo module = ModuleList.Instance.GetModuleInfoForMetadataReader(metadataReader);
+
+ NativeReader typeMapReader;
+ if (TryGetNativeReaderForBlob(module, ReflectionMapBlob.TypeMap, out typeMapReader))
+ {
+ NativeParser typeMapParser = new NativeParser(typeMapReader, 0);
+ NativeHashtable typeHashtable = new NativeHashtable(typeMapParser);
+
+ ExternalReferencesTable externalReferences = default(ExternalReferencesTable);
+ externalReferences.InitializeCommonFixupsTable(module);
+
+ var lookup = typeHashtable.Lookup(hashCode);
+ NativeParser entryParser;
+ while (!(entryParser = lookup.GetNext()).IsNull)
+ {
+ var foundTypeIndex = entryParser.GetUnsigned();
+ if (entryParser.GetUnsigned().AsHandle().Equals(typeDefHandle))
+ {
+ runtimeTypeHandle = externalReferences.GetRuntimeTypeHandleFromIndex(foundTypeIndex);
+ return true;
+ }
+ }
+ }
+ }
+
+ runtimeTypeHandle = default;
+ return false;
+ }
+
/// <summary>
/// Return the metadata handle for a TypeRef if this type was referenced indirectly by other type that pay-for-play has denoted as browsable
/// (for example, as part of a method signature.)
using Internal.Metadata.NativeFormat;
using Internal.NativeFormat;
using Internal.TypeSystem;
-using Internal.TypeSystem.NativeFormat;
namespace Internal.Runtime.TypeLoader
{
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-
-using System;
-using System.Runtime;
-using System.Runtime.CompilerServices;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Runtime.InteropServices;
-using System.Threading;
-
-using System.Reflection.Runtime.General;
-
-using Internal.Runtime;
-using Internal.Runtime.Augments;
-
-using Internal.Metadata.NativeFormat;
-using Internal.NativeFormat;
-using Internal.TypeSystem;
-
-namespace Internal.Runtime.TypeLoader
-{
- public sealed partial class TypeLoaderEnvironment
- {
- private class NamedTypeLookupResult
- {
- public int RuntimeTypeHandleHashcode;
- public RuntimeTypeHandle RuntimeTypeHandle;
- public QTypeDefinition QualifiedTypeDefinition;
- public IntPtr GcStaticFields;
- public IntPtr NonGcStaticFields;
- public volatile int VersionNumber;
- }
-
- private volatile int _namedTypeLookupLiveVersion;
-
- private NamedTypeRuntimeTypeHandleToMetadataHashtable _runtimeTypeHandleToMetadataHashtable = new NamedTypeRuntimeTypeHandleToMetadataHashtable();
-
- public static IntPtr NoStaticsData { get; } = (IntPtr)1;
-
- private class NamedTypeRuntimeTypeHandleToMetadataHashtable : LockFreeReaderHashtable<RuntimeTypeHandle, NamedTypeLookupResult>
- {
- protected override unsafe int GetKeyHashCode(RuntimeTypeHandle key)
- {
- return (int)key.ToEETypePtr()->HashCode;
- }
- protected override bool CompareKeyToValue(RuntimeTypeHandle key, NamedTypeLookupResult value)
- {
- return key.Equals(value.RuntimeTypeHandle);
- }
-
- protected override unsafe int GetValueHashCode(NamedTypeLookupResult value)
- {
- return value.RuntimeTypeHandleHashcode;
- }
-
- protected override bool CompareValueToValue(NamedTypeLookupResult value1, NamedTypeLookupResult value2)
- {
- if (value1.RuntimeTypeHandle.IsNull() || value2.RuntimeTypeHandle.IsNull())
- {
- return value1.QualifiedTypeDefinition.Token.Equals(value2.QualifiedTypeDefinition.Token) &&
- value1.QualifiedTypeDefinition.Reader.Equals(value2.QualifiedTypeDefinition.Reader);
- }
- return value1.RuntimeTypeHandle.Equals(value2.RuntimeTypeHandle);
- }
-
- protected override NamedTypeLookupResult CreateValueFromKey(RuntimeTypeHandle key)
- {
- int hashCode = GetKeyHashCode(key);
-
- // Iterate over all modules, starting with the module that defines the MethodTable
- foreach (NativeFormatModuleInfo module in ModuleList.EnumerateModules(RuntimeAugments.GetModuleFromTypeHandle(key)))
- {
- NativeReader typeMapReader;
- if (TryGetNativeReaderForBlob(module, ReflectionMapBlob.TypeMap, out typeMapReader))
- {
- NativeParser typeMapParser = new NativeParser(typeMapReader, 0);
- NativeHashtable typeHashtable = new NativeHashtable(typeMapParser);
-
- ExternalReferencesTable externalReferences = default(ExternalReferencesTable);
- externalReferences.InitializeCommonFixupsTable(module);
-
- var lookup = typeHashtable.Lookup(hashCode);
- NativeParser entryParser;
- while (!(entryParser = lookup.GetNext()).IsNull)
- {
- RuntimeTypeHandle foundType = externalReferences.GetRuntimeTypeHandleFromIndex(entryParser.GetUnsigned());
- if (foundType.Equals(key))
- {
- Handle entryMetadataHandle = entryParser.GetUnsigned().AsHandle();
- if (entryMetadataHandle.HandleType == HandleType.TypeDefinition)
- {
- MetadataReader metadataReader = module.MetadataReader;
- return new NamedTypeLookupResult()
- {
- QualifiedTypeDefinition = new QTypeDefinition(metadataReader, entryMetadataHandle.ToTypeDefinitionHandle(metadataReader)),
- RuntimeTypeHandle = key,
- RuntimeTypeHandleHashcode = hashCode
- };
- }
- }
- }
- }
- }
-
- return new NamedTypeLookupResult()
- {
- RuntimeTypeHandle = key,
- RuntimeTypeHandleHashcode = hashCode
- };
- }
- }
-
- private QTypeDefinitionToRuntimeTypeHandleHashtable _metadataToRuntimeTypeHandleHashtable = new QTypeDefinitionToRuntimeTypeHandleHashtable();
-
- private class QTypeDefinitionToRuntimeTypeHandleHashtable : LockFreeReaderHashtable<QTypeDefinition, NamedTypeLookupResult>
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static int _rotl(int value, int shift)
- {
- return (int)(((uint)value << shift) | ((uint)value >> (32 - shift)));
- }
-
- protected override unsafe int GetKeyHashCode(QTypeDefinition key)
- {
- return key.Token.GetHashCode() ^ _rotl(key.Reader.GetHashCode(), 8);
- }
- protected override bool CompareKeyToValue(QTypeDefinition key, NamedTypeLookupResult value)
- {
- return key.Token.Equals(value.QualifiedTypeDefinition.Token) &&
- key.Reader.Equals(value.QualifiedTypeDefinition.Reader);
- }
-
- protected override unsafe int GetValueHashCode(NamedTypeLookupResult value)
- {
- return value.QualifiedTypeDefinition.Token.GetHashCode() ^ _rotl(value.QualifiedTypeDefinition.Reader.GetHashCode(), 8);
- }
-
- protected override bool CompareValueToValue(NamedTypeLookupResult value1, NamedTypeLookupResult value2)
- {
- return value1.QualifiedTypeDefinition.Token.Equals(value2.QualifiedTypeDefinition.Token) &&
- value1.QualifiedTypeDefinition.Reader.Equals(value2.QualifiedTypeDefinition.Reader);
- }
-
- protected override NamedTypeLookupResult CreateValueFromKey(QTypeDefinition key)
- {
- RuntimeTypeHandle foundRuntimeTypeHandle = default(RuntimeTypeHandle);
-
- if (key.IsNativeFormatMetadataBased)
- {
- MetadataReader metadataReader = key.NativeFormatReader;
- TypeDefinitionHandle typeDefHandle = key.NativeFormatHandle;
- int hashCode = typeDefHandle.ComputeHashCode(metadataReader);
-
- NativeFormatModuleInfo module = ModuleList.Instance.GetModuleInfoForMetadataReader(metadataReader);
-
- NativeReader typeMapReader;
- if (TryGetNativeReaderForBlob(module, ReflectionMapBlob.TypeMap, out typeMapReader))
- {
- NativeParser typeMapParser = new NativeParser(typeMapReader, 0);
- NativeHashtable typeHashtable = new NativeHashtable(typeMapParser);
-
- ExternalReferencesTable externalReferences = default(ExternalReferencesTable);
- externalReferences.InitializeCommonFixupsTable(module);
-
- var lookup = typeHashtable.Lookup(hashCode);
- NativeParser entryParser;
- while (!(entryParser = lookup.GetNext()).IsNull)
- {
- var foundTypeIndex = entryParser.GetUnsigned();
- if (entryParser.GetUnsigned().AsHandle().Equals(typeDefHandle))
- {
- foundRuntimeTypeHandle = externalReferences.GetRuntimeTypeHandleFromIndex(foundTypeIndex);
- break;
- }
- }
- }
- }
-
- return new NamedTypeLookupResult()
- {
- QualifiedTypeDefinition = key,
- RuntimeTypeHandle = foundRuntimeTypeHandle,
- VersionNumber = TypeLoaderEnvironment.Instance._namedTypeLookupLiveVersion
- };
- }
- }
-
- /// <summary>
- /// Return the metadata handle for a TypeDef if the pay-for-policy enabled this type as browsable. This is used to obtain name and other information for types
- /// obtained via typeof() or Object.GetType(). This can include generic types (not to be confused with generic instances).
- ///
- /// Preconditions:
- /// runtimeTypeHandle is a typedef (not a constructed type such as an array or generic instance.)
- /// </summary>
- /// <param name="runtimeTypeHandle">Runtime handle of the type in question</param>
- /// <param name="qTypeDefinition">TypeDef handle for the type</param>
- public unsafe bool TryGetMetadataForNamedType(RuntimeTypeHandle runtimeTypeHandle, out QTypeDefinition qTypeDefinition)
- {
- NamedTypeLookupResult result = _runtimeTypeHandleToMetadataHashtable.GetOrCreateValue(runtimeTypeHandle);
- qTypeDefinition = result.QualifiedTypeDefinition;
- return qTypeDefinition.Reader != null;
- }
-
- /// <summary>
- /// Get the static addresses of a type if it is in the table
- /// </summary>
- /// <param name="runtimeTypeHandle">Runtime handle of the type in question</param>
- /// <param name="nonGcStaticsData">non-gc static field address</param>
- /// <param name="gcStaticsData">gc static field address</param>
- /// <returns>true if nonGcStaticsData/gcStaticsData are valid, false if not</returns>
- public unsafe bool TryGetStaticsInfoForNamedType(RuntimeTypeHandle runtimeTypeHandle, out IntPtr nonGcStaticsData, out IntPtr gcStaticsData)
- {
- NamedTypeLookupResult result;
-
- if (!_runtimeTypeHandleToMetadataHashtable.TryGetValue(runtimeTypeHandle, out result))
- {
- gcStaticsData = IntPtr.Zero;
- nonGcStaticsData = IntPtr.Zero;
- return false;
- }
-
- gcStaticsData = result.GcStaticFields;
- nonGcStaticsData = result.NonGcStaticFields;
-
- bool noResults = gcStaticsData == IntPtr.Zero || gcStaticsData == IntPtr.Zero;
-
- if (gcStaticsData == (IntPtr)1)
- gcStaticsData = IntPtr.Zero;
-
- if (nonGcStaticsData == (IntPtr)1)
- nonGcStaticsData = IntPtr.Zero;
-
- return result.QualifiedTypeDefinition.Reader != null && !noResults;
- }
-
- /// <summary>
- /// Return the RuntimeTypeHandle for the named type described in metadata. This is used to implement the Create and Invoke
- /// apis for types.
- ///
- /// Preconditions:
- /// metadataReader + typeDefHandle - a valid metadata reader + typeDefinitionHandle where "metadataReader" is one
- /// of the metadata readers returned by ExecutionEnvironment.MetadataReaders.
- ///
- /// Note: Although this method has a "bool" return value like the other mapping table accessors, the pay-for-play design
- /// guarantees that any type enabled for metadata also has a RuntimeTypeHandle underneath.
- /// </summary>
- /// <param name="qTypeDefinition">TypeDef handle for the type to look up</param>
- /// <param name="runtimeTypeHandle">Runtime type handle (MethodTable) for the given type</param>
- public unsafe bool TryGetNamedTypeForMetadata(QTypeDefinition qTypeDefinition, out RuntimeTypeHandle runtimeTypeHandle)
- {
- runtimeTypeHandle = default(RuntimeTypeHandle);
- NamedTypeLookupResult result = _metadataToRuntimeTypeHandleHashtable.GetOrCreateValue(qTypeDefinition);
-
- if (result.VersionNumber <= _namedTypeLookupLiveVersion)
- runtimeTypeHandle = result.RuntimeTypeHandle;
-
- return !runtimeTypeHandle.IsNull();
- }
-
- public void RegisterNewNamedTypeRuntimeTypeHandle(QTypeDefinition qTypeDefinition, RuntimeTypeHandle runtimeTypeHandle, IntPtr nonGcStaticFields, IntPtr gcStaticFields)
- {
- TypeLoaderLogger.WriteLine("Register new type with MethodTable = " + runtimeTypeHandle.ToIntPtr().LowLevelToString() + " nonGcStaticFields " + nonGcStaticFields.LowLevelToString() + " gcStaticFields " + gcStaticFields.LowLevelToString());
- NamedTypeLookupResult result = _metadataToRuntimeTypeHandleHashtable.GetOrCreateValue(qTypeDefinition);
-
- result.VersionNumber = _namedTypeLookupLiveVersion + 1;
- result.RuntimeTypeHandle = runtimeTypeHandle;
- result.GcStaticFields = gcStaticFields;
- result.NonGcStaticFields = nonGcStaticFields;
- unsafe
- {
- result.RuntimeTypeHandleHashcode = (int)runtimeTypeHandle.ToEETypePtr()->HashCode;
- }
-
- NamedTypeLookupResult rthToMetadataResult = _runtimeTypeHandleToMetadataHashtable.AddOrGetExisting(result);
-
- if (!object.ReferenceEquals(rthToMetadataResult, result))
- {
- rthToMetadataResult.QualifiedTypeDefinition = qTypeDefinition;
- rthToMetadataResult.GcStaticFields = gcStaticFields;
- rthToMetadataResult.NonGcStaticFields = nonGcStaticFields;
- }
- }
-
- public void UnregisterNewNamedTypeRuntimeTypeHandle(QTypeDefinition qTypeDefinition, RuntimeTypeHandle runtimeTypeHandle)
- {
- NamedTypeLookupResult metadataLookupResult;
- if (_metadataToRuntimeTypeHandleHashtable.TryGetValue(qTypeDefinition, out metadataLookupResult))
- {
- metadataLookupResult.RuntimeTypeHandle = default(RuntimeTypeHandle);
- metadataLookupResult.VersionNumber = -1;
- }
-
- if (_runtimeTypeHandleToMetadataHashtable.TryGetValue(runtimeTypeHandle, out _))
- {
- metadataLookupResult.GcStaticFields = IntPtr.Zero;
- metadataLookupResult.NonGcStaticFields = IntPtr.Zero;
- metadataLookupResult.RuntimeTypeHandle = default(RuntimeTypeHandle);
- }
- }
-
- public void FinishAddingNewNamedTypes()
- {
- _namedTypeLookupLiveVersion++;
- if (_namedTypeLookupLiveVersion == int.MaxValue)
- Environment.FailFast("Too many types loaded");
- }
- }
-}
/// </summary>
public IntPtr TryGetNonGcStaticFieldData(RuntimeTypeHandle runtimeTypeHandle)
{
- IntPtr nonGcStaticsAddress;
- IntPtr gcStaticsAddress;
- if (TryGetStaticsInfoForNamedType(runtimeTypeHandle, out nonGcStaticsAddress, out gcStaticsAddress))
- {
- return nonGcStaticsAddress;
- }
-
unsafe
{
// Non-generic, non-dynamic static data is found via the FieldAccessMap
/// </summary>
public IntPtr TryGetGcStaticFieldData(RuntimeTypeHandle runtimeTypeHandle)
{
- IntPtr nonGcStaticsAddress;
- IntPtr gcStaticsAddress;
- if (TryGetStaticsInfoForNamedType(runtimeTypeHandle, out nonGcStaticsAddress, out gcStaticsAddress))
- {
- return gcStaticsAddress;
- }
-
unsafe
{
// Non-generic, non-dynamic static data is found via the FieldAccessMap
return new NativeParser();
}
-
- private static unsafe IntPtr TryCreateDictionaryCellWithValue(uint value)
- {
- return PermanentAllocatedMemoryBlobs.GetPointerToUInt(value);
- }
#endregion
}
}
using System;
using System.Threading;
using System.Collections.Generic;
-using System.Runtime;
-using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Reflection.Runtime.General;
-using Internal.Runtime;
using Internal.Runtime.Augments;
using Internal.Runtime.CompilerServices;
using Internal.Metadata.NativeFormat;
using Internal.NativeFormat;
using Internal.TypeSystem;
-using Internal.TypeSystem.NativeFormat;
using Debug = System.Diagnostics.Debug;
}
}
- public unsafe bool TryGetOrCreateNamedTypeForMetadata(
- QTypeDefinition qTypeDefinition,
- out RuntimeTypeHandle runtimeTypeHandle)
- {
- if (TryGetNamedTypeForMetadata(qTypeDefinition, out runtimeTypeHandle))
- {
- return true;
- }
-
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
- using (LockHolder.Hold(_typeLoaderLock))
- {
- IntPtr runtimeTypeHandleAsIntPtr;
- TypeBuilder.ResolveSingleTypeDefinition(qTypeDefinition, out runtimeTypeHandleAsIntPtr);
- runtimeTypeHandle = *(RuntimeTypeHandle*)&runtimeTypeHandleAsIntPtr;
- return true;
- }
-#else
- return false;
-#endif
- }
-
public static IntPtr ConvertUnboxingFunctionPointerToUnderlyingNonUnboxingPointer(IntPtr unboxingFunctionPointer, RuntimeTypeHandle declaringType)
{
if (FunctionPointerOps.IsGenericMethodPointer(unboxingFunctionPointer))
// The .NET Foundation licenses this file to you under the MIT license.
using System;
-using System.Reflection;
using System.Diagnostics;
-using Internal.Metadata.NativeFormat;
-using Internal.Runtime.Augments;
-using Internal.Runtime.CompilerServices;
using Internal.TypeSystem;
-using Internal.TypeSystem.NativeFormat;
-using Internal.TypeSystem.NoMetadata;
-using Internal.Reflection.Core;
-using Internal.Reflection.Execution;
namespace Internal.Runtime.TypeLoader
{
/// </summary>
public partial class TypeLoaderTypeSystemContext : TypeSystemContext
{
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
- private static readonly MetadataFieldLayoutAlgorithm s_metadataFieldLayoutAlgorithm = new MetadataFieldLayoutAlgorithm();
- private static readonly MetadataVirtualMethodAlgorithm s_metadataVirtualMethodAlgorithm = new MetadataVirtualMethodAlgorithm();
- private static readonly MetadataRuntimeInterfacesAlgorithm s_metadataRuntimeInterfacesAlgorithm = new MetadataRuntimeInterfacesAlgorithm();
-#endif
private static readonly NoMetadataFieldLayoutAlgorithm s_noMetadataFieldLayoutAlgorithm = new NoMetadataFieldLayoutAlgorithm();
private static readonly NoMetadataRuntimeInterfacesAlgorithm s_noMetadataRuntimeInterfacesAlgorithm = new NoMetadataRuntimeInterfacesAlgorithm();
private static readonly NativeLayoutFieldAlgorithm s_nativeLayoutFieldAlgorithm = new NativeLayoutFieldAlgorithm();
public TypeLoaderTypeSystemContext(TargetDetails targetDetails) : base(targetDetails)
{
- ModuleDesc systemModule = null;
-
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
- systemModule = ((MetadataType)GetWellKnownType(WellKnownType.Object)).Module;
-#endif
-
- InitializeSystemModule(systemModule);
}
public override FieldLayoutAlgorithm GetLayoutAlgorithmForType(DefType type)
{
- if ((type == UniversalCanonType)
-#if SUPPORT_DYNAMIC_CODE
- || (type.IsRuntimeDeterminedType && (((RuntimeDeterminedType)type).CanonicalType == UniversalCanonType)))
-#else
- )
-#endif
- {
- return UniversalCanonLayoutAlgorithm.Instance;
- }
- else if (type.RetrieveRuntimeTypeHandleIfPossible())
+ if (type.RetrieveRuntimeTypeHandleIfPossible())
{
// If the type is already constructed, use the NoMetadataFieldLayoutAlgorithm.
// its more efficient than loading from native layout or metadata.
{
return s_nativeLayoutFieldAlgorithm;
}
- else if (type is NoMetadataType)
- {
- return s_noMetadataFieldLayoutAlgorithm;
- }
- else
- {
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
- return s_metadataFieldLayoutAlgorithm;
-#else
- Debug.Assert(false);
- return null;
-#endif
- }
+ return s_noMetadataFieldLayoutAlgorithm;
}
protected override RuntimeInterfacesAlgorithm GetRuntimeInterfacesAlgorithmForDefType(DefType type)
{
- if (type.RetrieveRuntimeTypeHandleIfPossible() && !type.IsGenericDefinition)
+ if (type.RetrieveRuntimeTypeHandleIfPossible())
{
// If the type is already constructed, use the NoMetadataRuntimeInterfacesAlgorithm.
// its more efficient than loading from native layout or metadata.
{
return s_nativeLayoutInterfacesAlgorithm;
}
- else if (type is NoMetadataType)
- {
- return s_noMetadataRuntimeInterfacesAlgorithm;
- }
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
- else if (type is MetadataType)
- {
- return s_metadataRuntimeInterfacesAlgorithm;
- }
-#endif
- else
- {
- Debug.Assert(false);
- return null;
- }
+ return s_noMetadataRuntimeInterfacesAlgorithm;
}
protected internal sealed override bool IsIDynamicInterfaceCastableInterface(DefType type)
}
}
- public override ModuleDesc ResolveAssembly(AssemblyName name, bool throwErrorIfNotFound)
- {
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
- AssemblyBindResult bindResult;
- Exception failureException;
- if (!AssemblyBinderImplementation.Instance.Bind(name.ToRuntimeAssemblyName(), cacheMissedLookups: true, out bindResult, out failureException))
- {
- if (throwErrorIfNotFound)
- throw failureException;
- return null;
- }
-
- var moduleList = Internal.Runtime.TypeLoader.ModuleList.Instance;
-
- if (bindResult.Reader != null)
- {
- NativeFormatModuleInfo primaryModule = moduleList.GetModuleInfoForMetadataReader(bindResult.Reader);
- NativeFormatMetadataUnit metadataUnit = ResolveMetadataUnit(primaryModule);
- return metadataUnit.GetModule(bindResult.ScopeDefinitionHandle);
- }
-#if ECMA_METADATA_SUPPORT
- else if (bindResult.EcmaMetadataReader != null)
- {
- EcmaModuleInfo ecmaModule = moduleList.GetModuleInfoForMetadataReader(bindResult.EcmaMetadataReader);
- return ResolveEcmaModule(ecmaModule);
- }
-#endif
- else
- {
- // Should not be possible to reach here
- throw new Exception();
- }
-#else
- return null;
-#endif
- }
-
- public override VirtualMethodAlgorithm GetVirtualMethodAlgorithmForType(TypeDesc type)
- {
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
- Debug.Assert(!type.IsArray, "Wanted to call GetClosestMetadataType?");
-
- return s_metadataVirtualMethodAlgorithm;
-#else
- Debug.Assert(false);
- return null;
-#endif
- }
-
protected internal override Instantiation ConvertInstantiationToCanonForm(Instantiation instantiation, CanonicalFormKind kind, out bool changed)
{
return StandardCanonicalizationAlgorithm.ConvertInstantiationToCanonForm(instantiation, kind, out changed);
return StandardCanonicalizationAlgorithm.ConvertToCanon(typeToConvert, kind);
}
- protected internal override bool ComputeHasGCStaticBase(FieldDesc field)
- {
- Debug.Assert(field.IsStatic);
-
- if (field is NativeLayoutFieldDesc)
- {
- return ((NativeLayoutFieldDesc)field).FieldStorage == Internal.NativeFormat.FieldStorage.GCStatic;
- }
-
- TypeDesc fieldType = field.FieldType;
- if (fieldType.IsValueType)
- {
- FieldDesc typicalField = field.GetTypicalFieldDefinition();
-
- if (field != typicalField)
- {
- if (typicalField.FieldType.IsSignatureVariable)
- return true;
- }
- if (fieldType.IsEnum || fieldType.IsPrimitive)
- return false;
- return true;
- }
- else
- return fieldType.IsGCPointer;
- }
-
protected internal override bool ComputeHasStaticConstructor(TypeDesc type)
{
if (type.RetrieveRuntimeTypeHandleIfPossible())
return type.RuntimeTypeHandle.ToEETypePtr()->HasCctor;
}
}
- else if (type is MetadataType)
- {
- return ((MetadataType)type).GetStaticConstructor() != null;
- }
+ Debug.Assert(type is not DefType);
return false;
}
- public override bool SupportsUniversalCanon => true;
+ public override bool SupportsUniversalCanon => false;
public override bool SupportsCanon => true;
}
}
using System;
-using System.Reflection;
-using System.Collections.Generic;
-using System.Diagnostics;
-using Internal.NativeFormat;
-using Internal.TypeSystem.NativeFormat;
-#if ECMA_METADATA_SUPPORT
-using Internal.TypeSystem.Ecma;
-#endif
using Internal.Runtime.Augments;
-using Internal.Runtime.CompilerServices;
using Internal.TypeSystem;
-using Internal.TypeSystem.NoMetadata;
-using System.Reflection.Runtime.General;
-
-namespace Internal.TypeSystem.NativeFormat
-{
- // When SUPPORTS_NATIVE_METADATA_TYPE_LOADING is not set we may see compile errors from using statements.
- // Add a namespace definition for Internal.TypeSystem.NativeFormat
-}
namespace Internal.Runtime.TypeLoader
{
DefType typeAsDefType = type as DefType;
return typeAsDefType != null && typeAsDefType.HasInstantiation;
}
-
- public static DefType GetClosestDefType(this TypeDesc type)
- {
- if (type is DefType)
- return (DefType)type;
- else
- return type.BaseType;
- }
}
internal static class MethodDescExtensions
return (rtfhValue.ToInt64() & 0x1) == 0x1;
}
}
-
- public static partial class RuntimeSignatureHelper
- {
- public static bool TryCreate(MethodDesc method, out RuntimeSignature methodSignature)
- {
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
- MethodDesc typicalMethod = method.GetTypicalMethodDefinition();
-
- if (typicalMethod is TypeSystem.NativeFormat.NativeFormatMethod)
- {
- TypeSystem.NativeFormat.NativeFormatMethod nativeFormatMethod = (TypeSystem.NativeFormat.NativeFormatMethod)typicalMethod;
- methodSignature = RuntimeSignature.CreateFromMethodHandle(nativeFormatMethod.MetadataUnit.RuntimeModule, nativeFormatMethod.Handle.ToInt());
- return true;
- }
-#if ECMA_METADATA_SUPPORT
- if (typicalMethod is TypeSystem.Ecma.EcmaMethod)
- {
- unsafe
- {
- TypeSystem.Ecma.EcmaMethod ecmaMethod = (TypeSystem.Ecma.EcmaMethod)typicalMethod;
- methodSignature = RuntimeSignature.CreateFromMethodHandle(new IntPtr(ecmaMethod.Module.RuntimeModuleInfo.DynamicModulePtr), System.Reflection.Metadata.Ecma335.MetadataTokens.GetToken(ecmaMethod.Handle));
- }
- return true;
- }
-#endif
-#endif
- methodSignature = default(RuntimeSignature);
- return false;
- }
-
-#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
- public static MethodDesc ToMethodDesc(this RuntimeMethodHandle rmh, TypeSystemContext typeSystemContext)
- {
- RuntimeTypeHandle declaringTypeHandle;
- MethodNameAndSignature nameAndSignature;
- RuntimeTypeHandle[] genericMethodArgs;
-
- if (!TypeLoaderEnvironment.Instance.TryGetRuntimeMethodHandleComponents(rmh, out declaringTypeHandle, out nameAndSignature, out genericMethodArgs))
- {
- return null;
- }
-
- QMethodDefinition methodHandle;
- if (!TypeLoaderEnvironment.Instance.TryGetMetadataForTypeMethodNameAndSignature(declaringTypeHandle, nameAndSignature, out methodHandle))
- {
- return null;
- }
-
- TypeDesc declaringType = typeSystemContext.ResolveRuntimeTypeHandle(declaringTypeHandle);
-
- TypeDesc declaringTypeDefinition = declaringType.GetTypeDefinition();
- MethodDesc typicalMethod = null;
- if (methodHandle.IsNativeFormatMetadataBased)
- {
- var nativeFormatType = (NativeFormatType)declaringTypeDefinition;
- typicalMethod = nativeFormatType.MetadataUnit.GetMethod(methodHandle.NativeFormatHandle, nativeFormatType);
- }
- else if (methodHandle.IsEcmaFormatMetadataBased)
- {
- var ecmaFormatType = (EcmaType)declaringTypeDefinition;
- typicalMethod = ecmaFormatType.EcmaModule.GetMethod(methodHandle.EcmaFormatHandle);
- }
- Debug.Assert(typicalMethod != null);
-
- MethodDesc methodOnInstantiatedType = typicalMethod;
- if (declaringType != declaringTypeDefinition)
- methodOnInstantiatedType = typeSystemContext.GetMethodForInstantiatedType(typicalMethod, (InstantiatedType)declaringType);
-
- MethodDesc instantiatedMethod = methodOnInstantiatedType;
- if (genericMethodArgs != null)
- {
- Debug.Assert(genericMethodArgs.Length > 0);
- Instantiation genericMethodInstantiation = typeSystemContext.ResolveRuntimeTypeHandles(genericMethodArgs);
- typeSystemContext.GetInstantiatedMethod(methodOnInstantiatedType, genericMethodInstantiation);
- }
-
- return instantiatedMethod;
- }
-#endif
- }
}
using Internal.Runtime.Augments;
using Internal.Runtime.CompilerServices;
using Internal.Runtime.TypeLoader;
-using Internal.TypeSystem.NativeFormat;
using Internal.TypeSystem.NoMetadata;
using Internal.Metadata.NativeFormat;
using Internal.NativeFormat;
<Compile Include="$(CompilerCommonPath)\TypeSystem\Common\TypeSystemHelpers.cs">
<Link>Internal\TypeSystem\TypeSystemHelpers.cs</Link>
</Compile>
- <Compile Include="$(CompilerCommonPath)\TypeSystem\Common\UniversalCanonLayoutAlgorithm.cs">
- <Link>UniversalCanonLayoutAlgorithm.cs</Link>
- </Compile>
<Compile Include="$(CompilerCommonPath)\TypeSystem\Common\Utilities\LockFreeReaderHashtableOfPointers.cs">
<Link>LockFreeReaderHashtableOfPointers.cs</Link>
</Compile>
<Compile Include="Internal\Runtime\TypeLoader\MetadataReaderHelpers.cs" />
<Compile Include="Internal\Runtime\TypeLoader\ModuleList.cs" />
<Compile Include="Internal\Runtime\TypeLoader\NativeLayoutFieldAlgorithm.cs" />
- <Compile Include="Internal\Runtime\TypeLoader\NativeLayoutFieldDesc.cs" />
<Compile Include="Internal\Runtime\TypeLoader\NativeLayoutInfoLoadContext.cs" />
<Compile Include="Internal\Runtime\TypeLoader\NativeLayoutInterfacesAlgorithm.cs" />
<Compile Include="Internal\Runtime\TypeLoader\NoMetadataFieldLayoutAlgorithm.cs" />
<Compile Include="Internal\Runtime\TypeLoader\NoMetadataRuntimeInterfacesAlgorithm.cs" />
<Compile Include="Internal\Runtime\TypeLoader\OptionalFields.cs" />
- <Compile Include="Internal\Runtime\TypeLoader\PermanentAllocatedMemoryBlobs.cs" />
<Compile Include="Internal\Runtime\TypeLoader\TemplateLocator.cs" />
<Compile Include="Internal\Runtime\TypeLoader\TypeBuilder.cs" />
<Compile Include="Internal\Runtime\TypeLoader\TypeBuilderState.cs" />
<Compile Include="Internal\Runtime\TypeLoader\TypeLoaderEnvironment.Metadata.cs" />
<Compile Include="Internal\Runtime\TypeLoader\TypeLoaderEnvironment.MetadataSignatureParsing.cs" />
<Compile Include="Internal\Runtime\TypeLoader\TypeLoaderEnvironment.MethodAddress.cs" />
- <Compile Include="Internal\Runtime\TypeLoader\TypeLoaderEnvironment.NamedTypeLookup.cs" />
<Compile Include="Internal\Runtime\TypeLoader\TypeLoaderEnvironment.SignatureParsing.cs" />
<Compile Include="Internal\Runtime\TypeLoader\TypeLoaderEnvironment.StaticsLookup.cs" />
<Compile Include="Internal\Runtime\TypeLoader\TypeLoaderLogger.cs" />