Browse Source

Fix #2920: Implement support for DefaultParameterValueAttribute.

pull/2971/head
Siegfried Pammer 2 years ago
parent
commit
5c67844500
  1. 58
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.cs
  2. 2
      ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs
  3. 3
      ICSharpCode.Decompiler/TypeSystem/Implementation/KnownAttributes.cs
  4. 11
      ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataParameter.cs

58
ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.cs

@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
@ -225,5 +226,62 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -225,5 +226,62 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
#endif
public static void Issue2920a(int x)
{
}
public static void Issue2920b([DefaultParameterValue(3)] int x)
{
}
public static void Issue2920c(ref int x)
{
}
public static void Issue2920d([DefaultParameterValue(3)] ref int x)
{
}
public static void Issue2920e(out int x)
{
x = 0;
}
public static void Issue2920f([DefaultParameterValue(3)] out int x)
{
x = 0;
}
#if CS70
public static void Issue2920g(in int x)
{
}
public static void Issue2920h([DefaultParameterValue(3)] in int x)
{
}
#endif
public static void Issue2920i([Optional] int x)
{
}
public static void Issue2920j(int x = 3)
{
}
public static void Issue2920k([Optional] ref int x)
{
}
public static void Issue2920l([Optional][DefaultParameterValue(3)] ref int x)
{
}
public static void Issue2920m([Optional] out int x)
{
x = 0;
}
public static void Issue2920n([Optional][DefaultParameterValue(3)] out int x)
{
x = 0;
}
#if CS70
public static void Issue2920o([Optional] in int x)
{
}
public static void Issue2920p(in int x = 3)
{
}
#endif
}
}

2
ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

@ -1675,7 +1675,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -1675,7 +1675,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
{
decl.Name = parameter.Name;
}
if (parameter.IsOptional && parameter.HasConstantValueInSignature && this.ShowConstantValues)
if (parameter.IsOptional && decl.ParameterModifier is ParameterModifier.None or ParameterModifier.In && parameter.HasConstantValueInSignature && this.ShowConstantValues)
{
try
{

3
ICSharpCode.Decompiler/TypeSystem/Implementation/KnownAttributes.cs

@ -89,6 +89,7 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -89,6 +89,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
In,
Out,
Optional,
DefaultParameterValue,
CallerMemberName,
CallerFilePath,
CallerLineNumber,
@ -165,6 +166,7 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -165,6 +166,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
new TopLevelTypeName("System.Runtime.InteropServices", nameof(InAttribute)),
new TopLevelTypeName("System.Runtime.InteropServices", nameof(OutAttribute)),
new TopLevelTypeName("System.Runtime.InteropServices", nameof(OptionalAttribute)),
new TopLevelTypeName("System.Runtime.InteropServices", nameof(DefaultParameterValueAttribute)),
new TopLevelTypeName("System.Runtime.CompilerServices", nameof(CallerMemberNameAttribute)),
new TopLevelTypeName("System.Runtime.CompilerServices", nameof(CallerFilePathAttribute)),
new TopLevelTypeName("System.Runtime.CompilerServices", nameof(CallerLineNumberAttribute)),
@ -220,6 +222,7 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -220,6 +222,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
case KnownAttribute.MarshalAs:
case KnownAttribute.PermissionSet:
case KnownAttribute.Optional:
case KnownAttribute.DefaultParameterValue:
case KnownAttribute.In:
case KnownAttribute.Out:
case KnownAttribute.IndexerName:

11
ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataParameter.cs

@ -64,8 +64,17 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation @@ -64,8 +64,17 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
var metadata = module.metadata;
var parameter = metadata.GetParameter(handle);
if (IsOptional && !HasConstantValueInSignature)
bool defaultValueAssignmentAllowed = ReferenceKind is ReferenceKind.None or ReferenceKind.In;
if (IsOptional && (!defaultValueAssignmentAllowed || !HasConstantValueInSignature))
{
b.Add(KnownAttribute.Optional);
}
if (!(IsDecimalConstant || !HasConstantValueInSignature) && (!defaultValueAssignmentAllowed || !IsOptional))
{
b.Add(KnownAttribute.DefaultParameterValue, KnownTypeCode.Object, GetConstantValue(throwOnInvalidMetadata: false));
}
if (!IsOut && !IsIn)
{

Loading…
Cancel
Save