Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DisplayNameHelpers for NativeAOT #70084

Merged
merged 4 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/coreclr/tools/Common/Compiler/DisplayNameHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Reflection.Metadata;
using System.Text;

using Internal.TypeSystem;

using Internal.TypeSystem.Ecma;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we tend to split using Foo = ... from the regular usings with a newline.

using Debug = System.Diagnostics.Debug;

namespace ILCompiler
Expand Down Expand Up @@ -38,6 +39,12 @@ public static string GetDisplayName(this MethodDesc method)
{
sb.Append(method.OwningType.GetDisplayNameWithoutNamespace());
}
else if (method.GetPropertyForAccessor() is PropertyPseudoDesc property)
{
sb.Append(property.Name);
sb.Append('.');
sb.Append(property.GetMethod.Name == method.Name ? "get" : "set");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to match C# syntax, should this also handle init?
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/init

(Not necessary to fix in this PR.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created dotnet/linker#2816 to add this to linker/analyzer/aot (They are going to share tests soon, so we will need to do it in all 3).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be better to just standardize on however reflection displays these. This is the same issue as with interpreting ref/in/out in method signatures. C# tends to come up with new things every release. Do we want to be in a position where we need to play catch up, or in the position of reflection that just doesn't care?

dotnet/linker#2158 (comment) (ref/in/out comment here)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must admit I thought about it - but I was lazy to fix probably 100s of places in the linker tests to change this.
Another way would be to implement some smarts to the test validation system to let it allow a difference here - probably doable - didn't try.

vitek-karas marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
sb.Append(method.Name);
Expand Down Expand Up @@ -68,6 +75,20 @@ public static string GetDisplayName(this MethodDesc method)
return sb.ToString();
}

public static string GetParameterDisplayName(this EcmaMethod method, int parameterIndex)
{
var reader = method.MetadataReader;
var methodDefinition = reader.GetMethodDefinition(method.Handle);
foreach (var parameterHandle in methodDefinition.GetParameters())
{
var parameter = reader.GetParameter(parameterHandle);
if (parameter.SequenceNumber == parameterIndex + 1)
return reader.GetString(parameter.Name);
}

return $"#{parameterIndex}";
}

public static string GetDisplayName(this FieldDesc field)
{
return new StringBuilder(field.OwningType.GetDisplayName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,26 @@ public static bool NotCallableWithoutOwningEEType(this MethodDesc method)
(owningType is not MetadataType mdType || !mdType.IsModuleType) && /* Compiler parks some instance methods on the <Module> type */
!method.IsSharedByGenericInstantiations; /* Current impl limitation; can be lifted */
}

public static PropertyPseudoDesc GetPropertyForAccessor(this MethodDesc accessor)
{
if (accessor.GetTypicalMethodDefinition() is not EcmaMethod ecmaAccessor)
return null;

var type = (EcmaType)ecmaAccessor.OwningType;
var reader = type.MetadataReader;
var module = type.EcmaModule;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module seems to be unused in this method

foreach (var propertyHandle in reader.GetTypeDefinition(type.Handle).GetProperties())
{
var accessors = reader.GetPropertyDefinition(propertyHandle).GetAccessors();
if (ecmaAccessor.Handle == accessors.Getter
|| ecmaAccessor.Handle == accessors.Setter)
{
return new PropertyPseudoDesc(type, propertyHandle);
}
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ internal static Origin GetMethodParameterFromIndex(MethodDesc method, int parame

internal static string GetParameterNameForErrorMessage(ParameterOrigin origin)
{
return $"#{origin.Index}";
return GetParameterNameForErrorMessage(origin.Method, origin.Index);
}

internal static string GetParameterNameForErrorMessage(MethodDesc method, int parameterIndex)
{
if (method is EcmaMethod ecmaMethod)
vitek-karas marked this conversation as resolved.
Show resolved Hide resolved
return ecmaMethod.GetParameterDisplayName(parameterIndex);

return $"#{parameterIndex}";
}

internal static string GetMethodSignatureDisplayName(MethodDesc method)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,26 +513,4 @@ private static DefType[] TryGetExplicitlyImplementedInterfaces(this TypeDesc typ
return Array.Empty<DefType>();
}
}

// Temporary local copy of the enum because the enum we're compiling against
// doesn't define all the values. Can be removed once we update to .NET 6.
public enum DynamicallyAccessedMemberTypes
{
None = 0,
PublicParameterlessConstructor = 0x0001,
PublicConstructors = 0x0002 | PublicParameterlessConstructor,
NonPublicConstructors = 0x0004,
PublicMethods = 0x0008,
NonPublicMethods = 0x0010,
PublicFields = 0x0020,
NonPublicFields = 0x0040,
PublicNestedTypes = 0x0080,
NonPublicNestedTypes = 0x0100,
PublicProperties = 0x0200,
NonPublicProperties = 0x0400,
PublicEvents = 0x0800,
NonPublicEvents = 0x1000,
Interfaces = 0x2000,
All = ~None
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,5 @@ public static PropertyPseudoDesc GetProperty(this MetadataType mdType, string na

return null;
}

public static PropertyPseudoDesc GetPropertyForAccessor(this MethodDesc accessor)
{
if (accessor.GetTypicalMethodDefinition() is not EcmaMethod ecmaAccessor)
return null;
var type = (EcmaType)ecmaAccessor.OwningType;
var reader = type.MetadataReader;
var module = type.EcmaModule;
foreach (var propertyHandle in reader.GetTypeDefinition(type.Handle).GetProperties())
{
var accessors = reader.GetPropertyDefinition(propertyHandle).GetAccessors();
if (ecmaAccessor.Handle == accessors.Getter
|| ecmaAccessor.Handle == accessors.Setter)
{
return new PropertyPseudoDesc(type, propertyHandle);
}
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ protected override TypeAnnotations CreateValueFromKey(TypeDesc key)

if (!IsTypeInterestingForDataflow(signature[parameter.SequenceNumber - 1]))
{
_logger.LogWarning(method, DiagnosticId.DynamicallyAccessedMembersOnMethodParameterCanOnlyApplyToTypesOrStrings, $"#{parameter.SequenceNumber}", method.GetDisplayName());
_logger.LogWarning(method, DiagnosticId.DynamicallyAccessedMembersOnMethodParameterCanOnlyApplyToTypesOrStrings, DiagnosticUtilities.GetParameterNameForErrorMessage(method, parameter.SequenceNumber - 1), method.GetDisplayName());
continue;
}

Expand Down Expand Up @@ -714,8 +714,8 @@ void LogValidationWarning(object provider, object baseProvider, MethodDesc origi
{
case int parameterNumber:
_logger.LogWarning(origin, DiagnosticId.DynamicallyAccessedMembersMismatchOnMethodParameterBetweenOverrides,
$"#{parameterNumber}", DiagnosticUtilities.GetMethodSignatureDisplayName(origin),
$"#{parameterNumber}", DiagnosticUtilities.GetMethodSignatureDisplayName((MethodDesc)baseProvider));
DiagnosticUtilities.GetParameterNameForErrorMessage(origin, parameterNumber), DiagnosticUtilities.GetMethodSignatureDisplayName(origin),
DiagnosticUtilities.GetParameterNameForErrorMessage((MethodDesc)baseProvider, parameterNumber), DiagnosticUtilities.GetMethodSignatureDisplayName((MethodDesc)baseProvider));
break;
case GenericParameterDesc genericParameterOverride:
_logger.LogWarning(origin, DiagnosticId.DynamicallyAccessedMembersMismatchOnGenericParameterBetweenOverrides,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using DependencyList = ILCompiler.DependencyAnalysisFramework.DependencyNodeCore<ILCompiler.DependencyAnalysis.NodeFactory>.DependencyList;
using MethodAttributes = System.Reflection.MethodAttributes;
using DynamicallyAccessedMemberTypes = ILCompiler.Dataflow.DynamicallyAccessedMemberTypes;

namespace ILCompiler.DependencyAnalysis
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@
<Compile Include="..\..\Common\Compiler\InstructionSetSupport.cs" Link="Compiler\InstructionSetSupport.cs" />
<Compile Include="..\..\Common\Compiler\Int128FieldLayoutAlgorithm.cs" Link="Compiler\Int128FieldLayoutAlgorithm.cs" />
<Compile Include="..\..\Common\Compiler\InternalCompilerErrorException.cs" Link="Compiler\InternalCompilerErrorException.cs" />
<Compile Include="..\..\Common\Compiler\MethodExtensions.cs" Link="Compiler\MethodExtensions.cs" />
<Compile Include="..\..\Common\Compiler\NameMangler.cs" Link="Compiler\NameMangler.cs" />
<Compile Include="..\..\Common\Compiler\PropertyPseudoDesc.cs" Link="Compiler\PropertyPseudoDesc.cs" />
<Compile Include="..\..\Common\Compiler\SingleMethodRootProvider.cs" Link="Compiler\SingleMethodRootProvider.cs" />
<Compile Include="..\..\Common\Compiler\TypeExtensions.cs" Link="Compiler\TypeExtensions.cs" />
<Compile Include="..\..\Common\Compiler\VectorFieldLayoutAlgorithm.cs" Link="Compiler\VectorFieldLayoutAlgorithm.cs" />
Expand Down Expand Up @@ -506,15 +508,13 @@
<Compile Include="Compiler\ManagedBinaryEmitter.cs" />
<Compile Include="Compiler\MetadataManager.cs" />
<Compile Include="Compiler\InteropStubManager.cs" />
<Compile Include="Compiler\MethodExtensions.cs" />
<Compile Include="Compiler\MultiFileCompilationModuleGroup.cs" />
<Compile Include="Compiler\NativeLibraryInitializerRootProvider.cs" />
<Compile Include="Compiler\NodeMangler.cs" />
<Compile Include="Compiler\ObjectDumper.cs" />
<Compile Include="Compiler\ExportsFileWriter.cs" />
<Compile Include="Compiler\ProcessLinkerXmlBase.cs" />
<Compile Include="Compiler\ProcessXmlBase.cs" />
<Compile Include="Compiler\PropertyPseudoDesc.cs" />
<Compile Include="Compiler\RootingHelpers.cs" />
<Compile Include="Compiler\RootingServiceProvider.cs" />
<Compile Include="Compiler\RuntimeConfigurationRootProvider.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@
<Compile Include="..\..\Common\Compiler\InstructionSetSupport.cs" Link="Compiler\InstructionSetSupport.cs" />
<Compile Include="..\..\Common\Compiler\Int128FieldLayoutAlgorithm.cs" Link="Compiler\Int128FieldLayoutAlgorithm.cs" />
<Compile Include="..\..\Common\Compiler\InternalCompilerErrorException.cs" Link="Compiler\InternalCompilerErrorException.cs" />
<Compile Include="..\..\Common\Compiler\MethodExtensions.cs" Link="Compiler\MethodExtensions.cs" />
<Compile Include="..\..\Common\Compiler\NameMangler.cs" Link="Compiler\NameMangler.cs" />
<Compile Include="..\..\Common\Compiler\SingleMethodRootProvider.cs" Link="Compiler\SingleMethodRootProvider.cs" />
<Compile Include="..\..\Common\Compiler\PropertyPseudoDesc.cs" Link="Compiler\PropertyPseudoDesc.cs" />
<Compile Include="..\..\Common\Compiler\TypeExtensions.cs" Link="Compiler\TypeExtensions.cs" />
<Compile Include="..\..\Common\Compiler\VectorFieldLayoutAlgorithm.cs" Link="Compiler\VectorFieldLayoutAlgorithm.cs" />
<Compile Include="..\..\Common\JitInterface\CorInfoTypes.VarInfo.cs" Link="JitInterface\CorInfoTypes.VarInfo.cs" />
Expand Down Expand Up @@ -278,6 +280,4 @@
<Link>JitInterface\UnboxingMethodDesc.cs</Link>
</Compile>
</ItemGroup>

<Import Project="..\ILLink.Shared\ILLink.Shared.projitems" Label="Shared" />
</Project>
3 changes: 0 additions & 3 deletions src/coreclr/tools/aot/crossgen2.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ILCompiler.TypeSystem.Tests", "ILCompiler.TypeSystem.Tests\ILCompiler.TypeSystem.Tests.csproj", "{9E65EC58-B500-4C4A-B57D-BF242129A3C6}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
ILLink.Shared\ILLink.Shared.projitems*{83a832de-bf4a-44c4-b361-90f5f88b979b}*SharedItemsImports = 5
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Checked|Any CPU = Checked|Any CPU
Checked|x64 = Checked|x64
Expand Down