From 765798c49d5d908e512af0931b18fed782b4cf00 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Fri, 13 Oct 2023 22:18:12 -0700 Subject: [PATCH] [NativeAOT] Miscellaneous cleanup (#93446) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [NativeAOT] Miscellaneous cleanup - Delete unnecessary abstractions - Simplify ifdefs - Use more derived types * Apply suggestions from code review Co-authored-by: Michal Strehovský --- .../src/System/Runtime/MethodTable.Runtime.cs | 17 +------ .../Core/Execution/ExecutionDomain.cs | 14 ++--- .../Core/Execution/ExecutionEnvironment.cs | 4 -- .../Core/NonPortable/RuntimeTypeUnifier.cs | 13 ++--- .../Runtime/Augments/RuntimeAugments.cs | 6 +-- .../Runtime/CompilerHelpers/LdTokenHelpers.cs | 2 +- .../SynchronizedMethodHelpers.cs | 2 +- .../IDynamicInterfaceCastableSupport.cs | 4 +- .../Internal/Runtime/MethodTable.Runtime.cs | 34 +++++++++++-- .../src/System/EETypePtr.cs | 11 +--- .../InteropServices/ComWrappers.NativeAot.cs | 2 +- .../Runtime/InteropServices/UnsafeGCHandle.cs | 2 +- .../src/System/Runtime/RuntimeImports.cs | 4 +- .../src/System/RuntimeTypeHandle.cs | 11 ++-- .../src/System/Type.NativeAot.cs | 16 +++--- ...EnvironmentImplementation.MappingTables.cs | 51 ------------------- .../Runtime/TypeLoader/MethodTable.Runtime.cs | 2 +- .../Internal/Runtime/MethodTable.Runtime.cs | 11 ++-- .../src/System/RuntimeType.cs | 7 +-- 19 files changed, 80 insertions(+), 133 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/MethodTable.Runtime.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/MethodTable.Runtime.cs index deb071aa30024..1c786946c82bf 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/MethodTable.Runtime.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/MethodTable.Runtime.cs @@ -11,42 +11,29 @@ namespace Internal.Runtime // Extensions to MethodTable that are specific to the use in Runtime.Base. internal unsafe partial struct MethodTable { -#pragma warning disable CA1822 +#if !INPLACE_RUNTIME internal MethodTable* GetArrayEEType() { -#if INPLACE_RUNTIME - return MethodTable.Of(); -#else MethodTable* pThis = (MethodTable*)Unsafe.Pointer(ref this); void* pGetArrayEEType = InternalCalls.RhpGetClasslibFunctionFromEEType(pThis, ClassLibFunctionId.GetSystemArrayEEType); return ((delegate* )pGetArrayEEType)(); -#endif } internal Exception GetClasslibException(ExceptionIDs id) { -#if INPLACE_RUNTIME - return RuntimeExceptionHelpers.GetRuntimeException(id); -#else if (IsParameterizedType) { return RelatedParameterType->GetClasslibException(id); } return EH.GetClasslibExceptionFromEEType(id, (MethodTable*)Unsafe.AsPointer(ref this)); -#endif } -#pragma warning restore CA1822 +#endif internal IntPtr GetClasslibFunction(ClassLibFunctionId id) { return (IntPtr)InternalCalls.RhpGetClasslibFunctionFromEEType((MethodTable*)Unsafe.AsPointer(ref this), id); } - - internal static bool AreSameType(MethodTable* mt1, MethodTable* mt2) - { - return mt1 == mt2; - } } internal static class WellKnownEETypes diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionDomain.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionDomain.cs index 171db2bf5b5c6..4300ab6737b4d 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionDomain.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionDomain.cs @@ -117,27 +117,27 @@ public Type GetNamedTypeForHandle(RuntimeTypeHandle typeHandle) public Type GetArrayTypeForHandle(RuntimeTypeHandle typeHandle) { - RuntimeTypeHandle elementTypeHandle = ExecutionEnvironment.GetArrayTypeElementType(typeHandle); + RuntimeTypeHandle elementTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(typeHandle); return elementTypeHandle.GetTypeForRuntimeTypeHandle().GetArrayType(typeHandle); } public Type GetMdArrayTypeForHandle(RuntimeTypeHandle typeHandle, int rank) { - RuntimeTypeHandle elementTypeHandle = ExecutionEnvironment.GetArrayTypeElementType(typeHandle); + RuntimeTypeHandle elementTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(typeHandle); return elementTypeHandle.GetTypeForRuntimeTypeHandle().GetMultiDimArrayType(rank, typeHandle); } public Type GetPointerTypeForHandle(RuntimeTypeHandle typeHandle) { - RuntimeTypeHandle targetTypeHandle = ExecutionEnvironment.GetPointerTypeTargetType(typeHandle); + RuntimeTypeHandle targetTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(typeHandle); return targetTypeHandle.GetTypeForRuntimeTypeHandle().GetPointerType(typeHandle); } public Type GetFunctionPointerTypeForHandle(RuntimeTypeHandle typeHandle) { - ExecutionEnvironment.GetFunctionPointerTypeComponents(typeHandle, out RuntimeTypeHandle returnTypeHandle, - out RuntimeTypeHandle[] parameterHandles, - out bool isUnmanaged); + RuntimeTypeHandle returnTypeHandle = RuntimeAugments.GetFunctionPointerReturnType(typeHandle); + RuntimeTypeHandle[] parameterHandles = RuntimeAugments.GetFunctionPointerParameterTypes(typeHandle); + bool isUnmanaged = RuntimeAugments.IsUnmanagedFunctionPointerType(typeHandle); RuntimeTypeInfo returnType = returnTypeHandle.GetTypeForRuntimeTypeHandle(); int count = parameterHandles.Length; @@ -152,7 +152,7 @@ public Type GetFunctionPointerTypeForHandle(RuntimeTypeHandle typeHandle) public Type GetByRefTypeForHandle(RuntimeTypeHandle typeHandle) { - RuntimeTypeHandle targetTypeHandle = ExecutionEnvironment.GetByRefTypeTargetType(typeHandle); + RuntimeTypeHandle targetTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(typeHandle); return targetTypeHandle.GetTypeForRuntimeTypeHandle().GetByRefType(typeHandle); } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionEnvironment.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionEnvironment.cs index fee9f2509cead..67fd7bdfaabfa 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionEnvironment.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionEnvironment.cs @@ -53,18 +53,14 @@ public abstract class ExecutionEnvironment public abstract bool TryGetNamedTypeForMetadata(QTypeDefinition qTypeDefinition, out RuntimeTypeHandle runtimeTypeHandle); public abstract bool TryGetArrayTypeForElementType(RuntimeTypeHandle elementTypeHandle, out RuntimeTypeHandle arrayTypeHandle); - public abstract RuntimeTypeHandle GetArrayTypeElementType(RuntimeTypeHandle arrayTypeHandle); public abstract bool TryGetMultiDimArrayTypeForElementType(RuntimeTypeHandle elementTypeHandle, int rank, out RuntimeTypeHandle arrayTypeHandle); public abstract bool TryGetFunctionPointerTypeForComponents(RuntimeTypeHandle returnTypeHandle, RuntimeTypeHandle[] parameterHandles, bool isUnmanaged, out RuntimeTypeHandle functionPointerTypeHandle); - public abstract void GetFunctionPointerTypeComponents(RuntimeTypeHandle functionPointerHandle, out RuntimeTypeHandle returnTypeHandle, out RuntimeTypeHandle[] parameterHandles, out bool isUnmanaged); public abstract bool TryGetPointerTypeForTargetType(RuntimeTypeHandle targetTypeHandle, out RuntimeTypeHandle pointerTypeHandle); - public abstract RuntimeTypeHandle GetPointerTypeTargetType(RuntimeTypeHandle pointerTypeHandle); public abstract bool TryGetByRefTypeForTargetType(RuntimeTypeHandle targetTypeHandle, out RuntimeTypeHandle byRefTypeHandle); - public abstract RuntimeTypeHandle GetByRefTypeTargetType(RuntimeTypeHandle byRefTypeHandle); public abstract bool TryGetConstructedGenericTypeForComponents(RuntimeTypeHandle genericTypeDefinitionHandle, RuntimeTypeHandle[] genericTypeArgumentHandles, out RuntimeTypeHandle runtimeTypeHandle); public abstract bool TryGetConstructedGenericTypeForComponentsNoConstraintCheck(RuntimeTypeHandle genericTypeDefinitionHandle, RuntimeTypeHandle[] genericTypeArgumentHandles, out RuntimeTypeHandle runtimeTypeHandle); diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/NonPortable/RuntimeTypeUnifier.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/NonPortable/RuntimeTypeUnifier.cs index 2f46272cd3b75..4fa1ad786cfee 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/NonPortable/RuntimeTypeUnifier.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/NonPortable/RuntimeTypeUnifier.cs @@ -7,6 +7,7 @@ using System.Collections.Concurrent; using System.Runtime.CompilerServices; +using Internal.Runtime; using Internal.Runtime.Augments; namespace Internal.Reflection.Core.NonPortable @@ -42,12 +43,12 @@ internal static partial class RuntimeTypeUnifier // // Retrieves the unified Type object for given RuntimeTypeHandle (this is basically the Type.GetTypeFromHandle() api without the input validation.) // - internal static Type GetRuntimeTypeForEEType(EETypePtr eeType) + internal static unsafe RuntimeType GetRuntimeTypeForMethodTable(MethodTable* eeType) { // If writable data is supported, we shouldn't be using the hashtable - the runtime type - // is accessible through a couple indirections from the EETypePtr which is much faster. + // is accessible through a couple indirections from the MethodTable which is much faster. Debug.Assert(!Internal.Runtime.MethodTable.SupportsWritableData); - return RuntimeTypeHandleToTypeCache.Table.GetOrAdd(eeType.RawValue); + return RuntimeTypeHandleToTypeCache.Table.GetOrAdd((IntPtr)eeType); } // @@ -59,14 +60,14 @@ internal static Type GetRuntimeTypeForEEType(EETypePtr eeType) // does a second lookup in the true unifying tables rather than creating the Type itself. // Thus, the one-to-one relationship between Type reference identity and Type semantic identity is preserved. // - private sealed class RuntimeTypeHandleToTypeCache : ConcurrentUnifierW + private sealed class RuntimeTypeHandleToTypeCache : ConcurrentUnifierW { private RuntimeTypeHandleToTypeCache() { } - protected sealed override Type Factory(IntPtr rawRuntimeTypeHandleKey) + protected sealed override RuntimeType Factory(IntPtr rawRuntimeTypeHandleKey) { EETypePtr eeType = new EETypePtr(rawRuntimeTypeHandleKey); - return GetRuntimeTypeBypassCache(eeType); + return (RuntimeType)GetRuntimeTypeBypassCache(eeType); } public static readonly RuntimeTypeHandleToTypeCache Table = new RuntimeTypeHandleToTypeCache(); diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs index ce194bb2fcad5..c94c1b4370595 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs @@ -602,7 +602,7 @@ public static bool IsFunctionPointerType(RuntimeTypeHandle typeHandle) public static unsafe RuntimeTypeHandle GetFunctionPointerReturnType(RuntimeTypeHandle typeHandle) { - return new RuntimeTypeHandle(new EETypePtr(typeHandle.ToMethodTable()->FunctionPointerReturnType)); + return new RuntimeTypeHandle(typeHandle.ToMethodTable()->FunctionPointerReturnType); } public static unsafe int GetFunctionPointerParameterCount(RuntimeTypeHandle typeHandle) @@ -613,7 +613,7 @@ public static unsafe int GetFunctionPointerParameterCount(RuntimeTypeHandle type public static unsafe RuntimeTypeHandle GetFunctionPointerParameterType(RuntimeTypeHandle typeHandle, int argumentIndex) { Debug.Assert(argumentIndex < GetFunctionPointerParameterCount(typeHandle)); - return new RuntimeTypeHandle(new EETypePtr(typeHandle.ToMethodTable()->FunctionPointerParameters[argumentIndex])); + return new RuntimeTypeHandle(typeHandle.ToMethodTable()->FunctionPointerParameters[argumentIndex]); } public static unsafe RuntimeTypeHandle[] GetFunctionPointerParameterTypes(RuntimeTypeHandle typeHandle) @@ -626,7 +626,7 @@ public static unsafe RuntimeTypeHandle[] GetFunctionPointerParameterTypes(Runtim MethodTableList parameters = typeHandle.ToMethodTable()->FunctionPointerParameters; for (int i = 0; i < result.Length; i++) { - result[i] = new RuntimeTypeHandle(new EETypePtr(parameters[i])); + result[i] = new RuntimeTypeHandle(parameters[i]); } return result; diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/LdTokenHelpers.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/LdTokenHelpers.cs index 81b96f861b6eb..88f98794c5db5 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/LdTokenHelpers.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/LdTokenHelpers.cs @@ -31,7 +31,7 @@ private static unsafe RuntimeFieldHandle GetRuntimeFieldHandle(IntPtr pHandleSig return returnValue; } - private static unsafe Type GetRuntimeType(MethodTable* pMT) + private static unsafe RuntimeType GetRuntimeType(MethodTable* pMT) { return Type.GetTypeFromMethodTable(pMT); } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/SynchronizedMethodHelpers.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/SynchronizedMethodHelpers.cs index 2330843b6602d..4f4db6916b511 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/SynchronizedMethodHelpers.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/SynchronizedMethodHelpers.cs @@ -67,7 +67,7 @@ private static unsafe void MonitorExitStatic(MethodTable* pMT, ref bool lockTake lockTaken = false; } - private static unsafe Type GetStaticLockObject(MethodTable* pMT) + private static unsafe RuntimeType GetStaticLockObject(MethodTable* pMT) { return Type.GetTypeFromMethodTable(pMT); } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/IDynamicInterfaceCastableSupport.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/IDynamicInterfaceCastableSupport.cs index 38526fbb59bf3..a17efbb21375b 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/IDynamicInterfaceCastableSupport.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/IDynamicInterfaceCastableSupport.cs @@ -12,13 +12,13 @@ static unsafe class IDynamicCastableSupport [RuntimeExport("IDynamicCastableIsInterfaceImplemented")] internal static bool IDynamicCastableIsInterfaceImplemented(IDynamicInterfaceCastable instance, MethodTable* interfaceType, bool throwIfNotImplemented) { - return instance.IsInterfaceImplemented(new RuntimeTypeHandle(new EETypePtr(interfaceType)), throwIfNotImplemented); + return instance.IsInterfaceImplemented(new RuntimeTypeHandle(interfaceType), throwIfNotImplemented); } [RuntimeExport("IDynamicCastableGetInterfaceImplementation")] internal static IntPtr IDynamicCastableGetInterfaceImplementation(IDynamicInterfaceCastable instance, MethodTable* interfaceType, ushort slot) { - RuntimeTypeHandle handle = instance.GetInterfaceImplementation(new RuntimeTypeHandle(new EETypePtr(interfaceType))); + RuntimeTypeHandle handle = instance.GetInterfaceImplementation(new RuntimeTypeHandle(interfaceType)); MethodTable* implType = handle.ToMethodTable(); if (implType == null) { diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/MethodTable.Runtime.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/MethodTable.Runtime.cs index a4ee0179f159b..963709e0cf15a 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/MethodTable.Runtime.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/MethodTable.Runtime.cs @@ -11,17 +11,43 @@ namespace Internal.Runtime // Extensions to MethodTable that are specific to the use in the CoreLib. internal unsafe partial struct MethodTable { -#if !INPLACE_RUNTIME - internal static MethodTable* GetArrayEEType() +#pragma warning disable CA1822 + internal MethodTable* GetArrayEEType() { - return MethodTable.Of(); } + internal Exception GetClasslibException(ExceptionIDs id) + { + return RuntimeExceptionHelpers.GetRuntimeException(id); + } +#pragma warning restore CA1822 + internal static bool AreSameType(MethodTable* mt1, MethodTable* mt2) { return mt1 == mt2; } -#endif + + internal bool IsEnum + { + get + { + // Q: When is an enum type a constructed generic type? + // A: When it's nested inside a generic type. + + // Generic type definitions that return true for IsPrimitive are type definitions of generic enums. + // Otherwise check the base type. + return IsPrimitive && (IsGenericTypeDefinition || NonArrayBaseType == MethodTable.Of()); + } + } + + // Returns true for actual primitives only, returns false for enums + internal bool IsActualPrimitive + { + get + { + return IsPrimitive && NonArrayBaseType == MethodTable.Of(); + } + } } } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/EETypePtr.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/EETypePtr.cs index ef62b320751d5..471a0964b6b3f 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/EETypePtr.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/EETypePtr.cs @@ -156,20 +156,11 @@ internal bool IsPrimitive } } - // WARNING: Never call unless the MethodTable came from an instanced object. Nested enums can be open generics (typeof(Outer<>).NestedEnum) - // and this helper has undefined behavior when passed such as a enum. internal bool IsEnum { get { - // Q: When is an enum type a constructed generic type? - // A: When it's nested inside a generic type. - if (!IsDefType) - return false; - - // Generic type definitions that return true for IsPrimitive are type definitions of generic enums. - // Otherwise check the base type. - return (IsGenericTypeDefinition && IsPrimitive) || this.BaseType == EETypePtr.EETypePtrOf(); + return _value->IsEnum; } } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs index 5ad028deda118..e776860562ca4 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs @@ -417,7 +417,7 @@ internal sealed unsafe class ManagedObjectWrapperHolder static ManagedObjectWrapperHolder() { delegate* unmanaged callback = &IsRootedCallback; - if (!RuntimeImports.RhRegisterRefCountedHandleCallback((nint)callback, typeof(ManagedObjectWrapperHolder).GetEEType())) + if (!RuntimeImports.RhRegisterRefCountedHandleCallback((nint)callback, EETypePtr.EETypePtrOf())) { throw new OutOfMemoryException(); } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/UnsafeGCHandle.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/UnsafeGCHandle.cs index bea66b1dfe1c0..ba0e9c5c83902 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/UnsafeGCHandle.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/UnsafeGCHandle.cs @@ -41,7 +41,7 @@ private UnsafeGCHandle(object value, GCHandleType type) _handle = RuntimeImports.RhHandleAlloc(value, type); } - public static UnsafeGCHandle Alloc(object value, GCHandleType type) + public static UnsafeGCHandle Alloc(object value, GCHandleType type = GCHandleType.Normal) { return new UnsafeGCHandle(value, type); } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs index a2cba6759a0ac..46e91a79aa636 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs @@ -391,10 +391,10 @@ internal static unsafe bool AreTypesAssignable(EETypePtr pSourceType, EETypePtr [MethodImpl(MethodImplOptions.InternalCall)] [RuntimeImport(RuntimeLibrary, "RhTypeCast_IsInstanceOfAny")] - private static extern unsafe object IsInstanceOfAny(MethodTable* pTargetType, object obj); + internal static extern unsafe object IsInstanceOf(MethodTable* pTargetType, object obj); internal static unsafe object IsInstanceOf(EETypePtr pTargetType, object obj) - => IsInstanceOfAny(pTargetType.ToPointer(), obj); + => IsInstanceOf(pTargetType.ToPointer(), obj); // // calls to runtime for allocation diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs index e9c15296ccd88..f259e57d575de 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs @@ -18,15 +18,14 @@ public unsafe struct RuntimeTypeHandle : IEquatable, ISeriali { private IntPtr _value; + internal unsafe RuntimeTypeHandle(MethodTable* pEEType) + => _value = (IntPtr)pEEType; + internal RuntimeTypeHandle(EETypePtr pEEType) - : this(pEEType.RawValue) - { - } + => _value = pEEType.RawValue; private RuntimeTypeHandle(IntPtr value) - { - _value = value; - } + => _value = value; public override bool Equals(object? obj) { diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Type.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Type.NativeAot.cs index 326aaae47faf6..73871328157d0 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Type.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Type.NativeAot.cs @@ -23,17 +23,17 @@ public abstract partial class Type : MemberInfo, IReflect public static unsafe Type? GetTypeFromHandle(RuntimeTypeHandle handle) => handle.IsNull ? null : GetTypeFromMethodTable(handle.ToMethodTable()); [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static unsafe Type GetTypeFromMethodTable(MethodTable* pMT) + internal static unsafe RuntimeType GetTypeFromMethodTable(MethodTable* pMT) { // If we support the writable data section on MethodTables, the runtime type associated with the MethodTable // is cached there. If writable data is not supported, we need to do a lookup in the runtime type // unifier's hash table. if (MethodTable.SupportsWritableData) { - ref GCHandle handle = ref Unsafe.AsRef(pMT->WritableData); + ref UnsafeGCHandle handle = ref Unsafe.AsRef(pMT->WritableData); if (handle.IsAllocated) { - return Unsafe.As(handle.Target); + return Unsafe.As(handle.Target); } else { @@ -42,26 +42,26 @@ internal static unsafe Type GetTypeFromMethodTable(MethodTable* pMT) } else { - return RuntimeTypeUnifier.GetRuntimeTypeForEEType(new EETypePtr(pMT)); + return RuntimeTypeUnifier.GetRuntimeTypeForMethodTable(pMT); } } [MethodImpl(MethodImplOptions.NoInlining)] - private static unsafe Type GetTypeFromMethodTableSlow(MethodTable* pMT, ref GCHandle handle) + private static unsafe RuntimeType GetTypeFromMethodTableSlow(MethodTable* pMT, ref UnsafeGCHandle handle) { // Note: this is bypassing the "fast" unifier cache (based on a simple IntPtr // identity of MethodTable pointers). There is another unifier behind that cache // that ensures this code is race-free. Type result = RuntimeTypeUnifier.GetRuntimeTypeBypassCache(new EETypePtr(pMT)); - GCHandle tempHandle = GCHandle.Alloc(result); + UnsafeGCHandle tempHandle = UnsafeGCHandle.Alloc(result); // We don't want to leak a handle if there's a race - if (Interlocked.CompareExchange(ref Unsafe.As(ref handle), (IntPtr)tempHandle, default) != default) + if (Interlocked.CompareExchange(ref Unsafe.As(ref handle), Unsafe.As(ref tempHandle), default) != default) { tempHandle.Free(); } - return result; + return Unsafe.As(handle.Target); } internal EETypePtr GetEEType() diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.MappingTables.cs b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.MappingTables.cs index 8611b4b9f845a..2d7c9b8fc7661 100644 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.MappingTables.cs +++ b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.MappingTables.cs @@ -150,20 +150,6 @@ public sealed override unsafe bool TryGetArrayTypeForElementType(RuntimeTypeHand return TypeLoaderEnvironment.Instance.TryGetArrayTypeForElementType(elementTypeHandle, false, -1, out arrayTypeHandle); } - // - // Given a RuntimeTypeHandle for any array type E[], return a RuntimeTypeHandle for type E, if the pay for play policy denoted E[] as browsable. - // - // Preconditions: - // arrayTypeHandle is a valid RuntimeTypeHandle of type array. - // - // This is not equivalent to calling TryGetMultiDimTypeElementType() with a rank of 1! - // - public sealed override RuntimeTypeHandle GetArrayTypeElementType(RuntimeTypeHandle arrayTypeHandle) - { - return RuntimeAugments.GetRelatedParameterTypeHandle(arrayTypeHandle); - } - - // // Given a RuntimeTypeHandle for any type E, return a RuntimeTypeHandle for type E[,,], if the pay for policy denotes E[,,] as browsable. This is used to // implement Type.MakeArrayType(Type, int). @@ -201,36 +187,11 @@ public sealed override unsafe bool TryGetPointerTypeForTargetType(RuntimeTypeHan return TypeLoaderEnvironment.Instance.TryGetPointerTypeForTargetType(targetTypeHandle, out pointerTypeHandle); } - // - // Given a RuntimeTypeHandle for any pointer type E*, return a RuntimeTypeHandle for type E, if the pay-for-play policy denotes E* as browsable. - // This is used to implement Type.GetElementType() for pointers. - // - // Preconditions: - // pointerTypeHandle is a valid RuntimeTypeHandle of type pointer. - // - public sealed override RuntimeTypeHandle GetPointerTypeTargetType(RuntimeTypeHandle pointerTypeHandle) - { - return RuntimeAugments.GetRelatedParameterTypeHandle(pointerTypeHandle); - } - public override bool TryGetFunctionPointerTypeForComponents(RuntimeTypeHandle returnTypeHandle, RuntimeTypeHandle[] parameterHandles, bool isUnmanaged, out RuntimeTypeHandle functionPointerTypeHandle) { return TypeLoaderEnvironment.Instance.TryGetFunctionPointerTypeForComponents(returnTypeHandle, parameterHandles, isUnmanaged, out functionPointerTypeHandle); } - // - // Given a RuntimeTypeHandle for any function pointer pointer type, return the composition of it. - // - // Preconditions: - // pointerTypeHandle is a valid RuntimeTypeHandle of type function pointer. - // - public override void GetFunctionPointerTypeComponents(RuntimeTypeHandle functionPointerHandle, out RuntimeTypeHandle returnTypeHandle, out RuntimeTypeHandle[] parameterHandles, out bool isUnmanaged) - { - returnTypeHandle = RuntimeAugments.GetFunctionPointerReturnType(functionPointerHandle); - parameterHandles = RuntimeAugments.GetFunctionPointerParameterTypes(functionPointerHandle); - isUnmanaged = RuntimeAugments.IsUnmanagedFunctionPointerType(functionPointerHandle); - } - // // Given a RuntimeTypeHandle for any type E, return a RuntimeTypeHandle for type E&, if the pay-for-play policy denotes E& as browsable. This is used to // ensure that "typeof(E&)" and "typeof(E).MakeByRefType()" returns the same Type object. @@ -243,18 +204,6 @@ public sealed override unsafe bool TryGetByRefTypeForTargetType(RuntimeTypeHandl return TypeLoaderEnvironment.Instance.TryGetByRefTypeForTargetType(targetTypeHandle, out byRefTypeHandle); } - // - // Given a RuntimeTypeHandle for any byref type E&, return a RuntimeTypeHandle for type E, if the pay-for-play policy denotes E& as browsable. - // This is used to implement Type.GetElementType() for byrefs. - // - // Preconditions: - // byRefTypeHandle is a valid RuntimeTypeHandle of a byref. - // - public sealed override unsafe RuntimeTypeHandle GetByRefTypeTargetType(RuntimeTypeHandle byRefTypeHandle) - { - return RuntimeAugments.GetRelatedParameterTypeHandle(byRefTypeHandle); - } - // // Given a RuntimeTypeHandle for a generic type G and a set of RuntimeTypeHandles T1, T2.., return the RuntimeTypeHandle for the generic // instance G if the pay-for-play policy denotes G as browsable. This is used to implement Type.MakeGenericType(). diff --git a/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/MethodTable.Runtime.cs b/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/MethodTable.Runtime.cs index b0d08b06e521b..49b75a220bf00 100644 --- a/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/MethodTable.Runtime.cs +++ b/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/MethodTable.Runtime.cs @@ -15,7 +15,7 @@ internal partial struct MethodTable { private static unsafe MethodTable* GetArrayEEType() { - return typeof(Array).TypeHandle.ToEETypePtr(); + return MethodTable.Of(); } internal unsafe RuntimeTypeHandle ToRuntimeTypeHandle() diff --git a/src/coreclr/nativeaot/Test.CoreLib/src/Internal/Runtime/MethodTable.Runtime.cs b/src/coreclr/nativeaot/Test.CoreLib/src/Internal/Runtime/MethodTable.Runtime.cs index c2805338037e9..1bb7dcd62abd1 100644 --- a/src/coreclr/nativeaot/Test.CoreLib/src/Internal/Runtime/MethodTable.Runtime.cs +++ b/src/coreclr/nativeaot/Test.CoreLib/src/Internal/Runtime/MethodTable.Runtime.cs @@ -2,18 +2,21 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Runtime; namespace Internal.Runtime { // Extensions to MethodTable that are specific to the use in the CoreLib. internal unsafe partial struct MethodTable { -#if !INPLACE_RUNTIME - internal static MethodTable* GetArrayEEType() + internal MethodTable* GetArrayEEType() { + return MethodTable.Of(); + } - return EETypePtr.EETypePtrOf().ToPointer(); + internal Exception GetClasslibException(ExceptionIDs id) + { + return RuntimeExceptionHelpers.GetRuntimeException(id); } -#endif } } diff --git a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs index 83572e7b59d03..bd6a6f89bb135 100644 --- a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs @@ -259,12 +259,7 @@ public override bool IsEnumDefined(object value) public override bool IsInstanceOfType([NotNullWhen(true)] object? o) => RuntimeTypeHandle.IsInstanceOfType(this, o); public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo) - { - if (typeInfo == null) - return false; - - return IsAssignableFrom(typeInfo.AsType()); - } + => typeInfo != null && IsAssignableFrom(typeInfo.AsType()); public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) {