Browse Source

Add minimal support for C# 7 discards in out var declarations.

pull/1087/head
Siegfried Pammer 7 years ago
parent
commit
40659a2283
  1. 12
      ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs
  2. 17
      ICSharpCode.Decompiler/DecompilerSettings.cs

12
ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

@ -428,7 +428,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -428,7 +428,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
} else {
type = context.TypeSystemAstBuilder.ConvertType(v.Type);
}
var ovd = new OutVarDeclarationExpression(type, v.Name);
string name;
// Variable is not used and discards are allowed, we can simplify this to 'out var _'.
// TODO : if there are no other (non-out) variables named '_' and type is 'var', we can simplify this to 'out _'.
// However, this needs overload resolution, so we currently cannot do that, as OR does not yet understand out var.
// (for the specific rules see https://github.com/dotnet/roslyn/blob/master/docs/features/discards.md)
if (context.Settings.Discards && v.ILVariable.LoadCount == 0 && v.ILVariable.StoreCount == 0 && v.ILVariable.AddressCount == 1) {
name = "_";
} else {
name = v.Name;
}
var ovd = new OutVarDeclarationExpression(type, name);
ovd.Variable.AddAnnotation(new ILVariableResolveResult(ilVariable));
ovd.CopyAnnotationsFrom(dirExpr);
replacements.Add((dirExpr, ovd));

17
ICSharpCode.Decompiler/DecompilerSettings.cs

@ -74,6 +74,7 @@ namespace ICSharpCode.Decompiler @@ -74,6 +74,7 @@ namespace ICSharpCode.Decompiler
}
if (languageVersion < CSharp.LanguageVersion.CSharp7) {
outVariables = false;
discards = false;
}
if (languageVersion < CSharp.LanguageVersion.CSharp7_2) {
introduceRefAndReadonlyModifiersOnStructs = false;
@ -535,6 +536,22 @@ namespace ICSharpCode.Decompiler @@ -535,6 +536,22 @@ namespace ICSharpCode.Decompiler
}
}
bool discards = true;
/// <summary>
/// Gets/Sets whether discards should be used when possible.
/// Only has an effect if <see cref="OutVariables"/> is enabled.
/// </summary>
public bool Discards {
get { return discards; }
set {
if (discards != value) {
discards = value;
OnPropertyChanged();
}
}
}
bool introduceRefAndReadonlyModifiersOnStructs = true;
/// <summary>

Loading…
Cancel
Save