Skip to content

Commit

Permalink
Emit parameter names
Browse files Browse the repository at this point in the history
  • Loading branch information
jasper-d committed Aug 5, 2022
1 parent 08a11e4 commit 6ba83e1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public static string FormatMethodName(MetadataReader metadataReader, TypeDefinit
MethodNameFormatter formatter = new MethodNameFormatter(metadataReader, SigTypeContext.FromMethod(metadataReader, enclosingTypeHandle, methodHandle));

Method method = metadataReader.GetMethod(methodHandle);
MethodSignature methodSignature = metadataReader.GetMethodSignature(method.Signature);
formatter.EmitTypeName(enclosingTypeHandle, namespaceQualified: true);
formatter._outputBuilder.Append('.');
formatter.EmitString(method.Name);
Expand All @@ -64,7 +63,7 @@ public static string FormatMethodName(MetadataReader metadataReader, TypeDefinit
}
else
{
formatter._outputBuilder.Append(", ");
formatter._outputBuilder.Append(',');
}
formatter.EmitTypeName(handle, namespaceQualified: false);
}
Expand All @@ -73,7 +72,7 @@ public static string FormatMethodName(MetadataReader metadataReader, TypeDefinit
formatter._outputBuilder.Append(']');
}

formatter.EmitMethodParameters(methodSignature);
formatter.EmitMethodParameters(methodHandle);

return formatter._outputBuilder.ToString();
}
Expand Down Expand Up @@ -124,28 +123,30 @@ private void EmitMethodReferenceName(MemberReferenceHandle memberRefHandle)
private void EmitMethodInstantiationName(MethodInstantiationHandle methodInstHandle)
{
MethodInstantiation methodInst = _metadataReader.GetMethodInstantiation(methodInstHandle);
MethodSignature methodSignature;

if (methodInst.Method.HandleType == HandleType.MemberReference)
{
MemberReferenceHandle methodRefHandle = methodInst.Method.ToMemberReferenceHandle(_metadataReader);
MemberReference methodRef = methodRefHandle.GetMemberReference(_metadataReader);
EmitContainingTypeAndMethodName(methodRef, out methodSignature);
EmitContainingTypeAndMethodName(methodRef, out MethodSignature methodSignature);
EmitGenericArguments(methodInst.GenericTypeArguments);
EmitMethodParameters(methodSignature);
}
else
{
QualifiedMethodHandle qualifiedMethodHandle = methodInst.Method.ToQualifiedMethodHandle(_metadataReader);
QualifiedMethod qualifiedMethod = _metadataReader.GetQualifiedMethod(qualifiedMethodHandle);
EmitContainingTypeAndMethodName(qualifiedMethod, out methodSignature);
EmitContainingTypeAndMethodName(qualifiedMethod);
EmitGenericArguments(methodInst.GenericTypeArguments);
EmitMethodParameters(qualifiedMethod.Method);
}
EmitGenericArguments(methodInst.GenericTypeArguments);
EmitMethodParameters(methodSignature);
}

private void EmitMethodDefinitionName(QualifiedMethodHandle qualifiedMethodHandle)
{
QualifiedMethod qualifiedMethod = _metadataReader.GetQualifiedMethod(qualifiedMethodHandle);
EmitContainingTypeAndMethodName(qualifiedMethod, out MethodSignature methodSignature);
EmitMethodParameters(methodSignature);
EmitContainingTypeAndMethodName(qualifiedMethod);
EmitMethodParameters(qualifiedMethod.Method);
}

/// <summary>
Expand All @@ -161,10 +162,9 @@ private void EmitContainingTypeAndMethodName(MemberReference methodRef, out Meth
EmitString(methodRef.Name);
}

private void EmitContainingTypeAndMethodName(QualifiedMethod qualifiedMethod, out MethodSignature methodSignature)
private void EmitContainingTypeAndMethodName(QualifiedMethod qualifiedMethod)
{
Method method = _metadataReader.GetMethod(qualifiedMethod.Method);
methodSignature = _metadataReader.GetMethodSignature(method.Signature);
EmitTypeName(qualifiedMethod.EnclosingType, namespaceQualified: true);
_outputBuilder.Append('.');
EmitString(method.Name);
Expand All @@ -181,6 +181,39 @@ private void EmitMethodParameters(MethodSignature methodSignature)
_outputBuilder.Append(')');
}

/// <summary>
/// Emit parenthesized method argument type list with parameter names.
/// </summary>
/// <param name="methodHandle">Method handle to use for parameter formatting</param>
private void EmitMethodParameters(MethodHandle methodHandle)
{
Method method = methodHandle.GetMethod(_metadataReader);
HandleCollection typeVector = method.Signature.GetMethodSignature(_metadataReader).Parameters;
ParameterHandleCollection.Enumerator names = method.Parameters.GetEnumerator();

bool first = true;

_outputBuilder.Append('(');
foreach (Handle handle in typeVector)
{
if (first)
{
first = false;
}
else
{
_outputBuilder.Append(", ");
}

names.MoveNext();

EmitTypeName(handle, namespaceQualified: false);
_outputBuilder.Append(' ');
EmitString(names.Current.GetParameter(_metadataReader).Name);
}
_outputBuilder.Append(')');
}

/// <summary>
/// Emit comma-separated list of type names into the output string builder.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
Expand All @@ -15,8 +16,9 @@ public class EnvironmentStackTrace
static string s_stackTrace;

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/73051", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
[ActiveIssue("https://github.com/mono/mono/issues/15315", TestRuntimes.Mono)]
// Retain parameter names for NativeAOT
[DynamicDependency(DynamicallyAccessedMemberTypes.NonPublicMethods, typeof(EnvironmentStackTrace))]
public void StackTraceTest()
{
//arrange
Expand Down

0 comments on commit 6ba83e1

Please sign in to comment.