diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index a0b956c3d8fc9d..376019c4adc5f0 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -1915,12 +1915,19 @@ private CorInfoType asCorInfoType(CORINFO_CLASS_STRUCT_* cls) private byte* getClassNameFromMetadata(CORINFO_CLASS_STRUCT_* cls, byte** namespaceName) { - var type = HandleToObject(cls) as MetadataType; - if (type != null) + TypeDesc type = HandleToObject(cls); + if (type.GetTypeDefinition() is EcmaType ecmaType) { + var reader = ecmaType.MetadataReader; if (namespaceName != null) - *namespaceName = (byte*)GetPin(StringToUTF8(type.Namespace)); - return (byte*)GetPin(StringToUTF8(type.Name)); + *namespaceName = reader.GetTypeNamespacePointer(ecmaType.Handle); + return reader.GetTypeNamePointer(ecmaType.Handle); + } + else if (type is MetadataType mdType) + { + if (namespaceName != null) + *namespaceName = (byte*)GetPin(StringToUTF8(mdType.Namespace)); + return (byte*)GetPin(StringToUTF8(mdType.Name)); } if (namespaceName != null) @@ -3172,21 +3179,47 @@ private static string getMethodNameFromMetadataImpl(MethodDesc method, out strin { MethodDesc method = HandleToObject(ftn); - string result; - string classResult; - string namespaceResult; - string enclosingResult; + if (method.GetTypicalMethodDefinition() is EcmaMethod ecmaMethod) + { + EcmaType owningType = (EcmaType)ecmaMethod.OwningType; + var reader = owningType.MetadataReader; - result = getMethodNameFromMetadataImpl(method, out classResult, out namespaceResult, out enclosingResult); + if (className != null) + *className = reader.GetTypeNamePointer(owningType.Handle); + if (namespaceName != null) + *namespaceName = reader.GetTypeNamespacePointer(owningType.Handle); - if (className != null) - *className = classResult != null ? (byte*)GetPin(StringToUTF8(classResult)) : null; - if (namespaceName != null) - *namespaceName = namespaceResult != null ? (byte*)GetPin(StringToUTF8(namespaceResult)) : null; - if (enclosingClassName != null) - *enclosingClassName = enclosingResult != null ? (byte*)GetPin(StringToUTF8(enclosingResult)) : null; + // Query enclosingClassName when the method is in a nested class + // and get the namespace of enclosing classes (nested class's namespace is empty) + var containingType = owningType.ContainingType as EcmaType; + if (containingType != null) + { + if (enclosingClassName != null) + *enclosingClassName = reader.GetTypeNamePointer(containingType.Handle); + if (namespaceName != null) + *namespaceName = reader.GetTypeNamespacePointer(containingType.Handle); + } + + return reader.GetMethodNamePointer(ecmaMethod.Handle); + } + else + { + string result; + string classResult; + string namespaceResult; + string enclosingResult; + + result = getMethodNameFromMetadataImpl(method, out classResult, out namespaceResult, out enclosingResult); - return result != null ? (byte*)GetPin(StringToUTF8(result)) : null; + if (className != null) + *className = classResult != null ? (byte*)GetPin(StringToUTF8(classResult)) : null; + if (namespaceName != null) + *namespaceName = namespaceResult != null ? (byte*)GetPin(StringToUTF8(namespaceResult)) : null; + if (enclosingClassName != null) + *enclosingClassName = enclosingResult != null ? (byte*)GetPin(StringToUTF8(enclosingResult)) : null; + + return result != null ? (byte*)GetPin(StringToUTF8(result)) : null; + } } private uint getMethodHash(CORINFO_METHOD_STRUCT_* ftn) diff --git a/src/coreclr/tools/Common/TypeSystem/Ecma/MetadataExtensions.cs b/src/coreclr/tools/Common/TypeSystem/Ecma/MetadataExtensions.cs index 640a70efa7ce67..f3196658504420 100644 --- a/src/coreclr/tools/Common/TypeSystem/Ecma/MetadataExtensions.cs +++ b/src/coreclr/tools/Common/TypeSystem/Ecma/MetadataExtensions.cs @@ -295,5 +295,20 @@ public static bool IsPublic(this MethodAttributes flags) { return (flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Public; } + + public static unsafe byte* GetTypeNamePointer(this MetadataReader reader, TypeDefinitionHandle handle) + { + return reader.GetBlobReader(reader.GetTypeDefinition(handle).Name).CurrentPointer; + } + + public static unsafe byte* GetTypeNamespacePointer(this MetadataReader reader, TypeDefinitionHandle handle) + { + return reader.GetBlobReader(reader.GetTypeDefinition(handle).Namespace).CurrentPointer; + } + + public static unsafe byte* GetMethodNamePointer(this MetadataReader reader, MethodDefinitionHandle handle) + { + return reader.GetBlobReader(reader.GetMethodDefinition(handle).Name).CurrentPointer; + } } }