Browse Source

Apply expression-body transform to indexers as well.

pull/1440/head
Siegfried Pammer 6 years ago
parent
commit
d9b7df637b
  1. 9
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CustomAttributeSamples.cs
  2. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/MemberTests.cs
  3. 12
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullPropagation.cs
  4. 32
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullPropagation.roslyn.il
  5. 27
      ICSharpCode.Decompiler/CSharp/Transforms/NormalizeBlockStatements.cs

9
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CustomAttributeSamples.cs

@ -101,12 +101,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.CustomAttributeSamples @@ -101,12 +101,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.CustomAttributeSamples
}
[Obsolete("reason")]
#if ROSLYN
public int this[int i] => 0;
#else
public int this[int i] {
get {
return 0;
}
}
#endif
[MyAttribute]
public event EventHandler MyEvent;
@ -254,11 +257,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.CustomAttributeSamples @@ -254,11 +257,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.CustomAttributeSamples
public class MyClass11
{
#if ROSLYN
public int this[[MyAttribute] string s] => 3;
#else
public int this[[MyAttribute] string s] {
get {
return 3;
}
}
#endif
}
public class MyClass12

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/MemberTests.cs

@ -27,11 +27,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -27,11 +27,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public class IndexerNonDefaultName
{
[IndexerName("Foo")]
#if ROSLYN
public int this[int index] => 0;
#else
public int this[int index] {
get {
return 0;
}
}
#endif
}
[DefaultMember("Bar")]

12
ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullPropagation.cs

@ -31,11 +31,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -31,11 +31,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
get;
set;
}
public MyClass this[int index] {
get {
return null;
}
}
public MyClass this[int index] => null;
public MyClass Method(int arg)
{
return null;
@ -52,11 +48,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -52,11 +48,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public MyClass Field;
public MyStruct? Property1 => null;
public MyStruct Property2 => default(MyStruct);
public MyStruct? this[int index] {
get {
return null;
}
}
public MyStruct? this[int index] => null;
public MyStruct? Method1(int arg)
{
return null;

32
ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullPropagation.roslyn.il

@ -84,16 +84,10 @@ @@ -84,16 +84,10 @@
instance class ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyClass
get_Item(int32 index) cil managed
{
// Code size 7 (0x7)
.maxstack 1
.locals init (class ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyClass V_0)
IL_0000: nop
IL_0001: ldnull
IL_0002: stloc.0
IL_0003: br.s IL_0005
IL_0005: ldloc.0
IL_0006: ret
// Code size 2 (0x2)
.maxstack 8
IL_0000: ldnull
IL_0001: ret
} // end of method MyClass::get_Item
.method public hidebysig instance class ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyClass
@ -180,19 +174,13 @@ @@ -180,19 +174,13 @@
instance valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct>
get_Item(int32 index) cil managed
{
// Code size 15 (0xf)
// Code size 10 (0xa)
.maxstack 1
.locals init (valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct> V_0,
valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct> V_1)
IL_0000: nop
IL_0001: ldloca.s V_0
IL_0003: initobj valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct>
IL_0009: ldloc.0
IL_000a: stloc.1
IL_000b: br.s IL_000d
IL_000d: ldloc.1
IL_000e: ret
.locals init (valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct> V_0)
IL_0000: ldloca.s V_0
IL_0002: initobj valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct>
IL_0008: ldloc.0
IL_0009: ret
} // end of method MyStruct::get_Item
.method public hidebysig instance valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct>

27
ICSharpCode.Decompiler/CSharp/Transforms/NormalizeBlockStatements.cs

@ -130,10 +130,28 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -130,10 +130,28 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
base.VisitPropertyDeclaration(propertyDeclaration);
}
public override void VisitIndexerDeclaration(IndexerDeclaration indexerDeclaration)
{
if (context.Settings.UseExpressionBodyForCalculatedGetterOnlyProperties) {
SimplifyIndexerDeclaration(indexerDeclaration);
}
base.VisitIndexerDeclaration(indexerDeclaration);
}
static readonly PropertyDeclaration CalculatedGetterOnlyPropertyPattern = new PropertyDeclaration() {
Attributes = { new Repeat(new AnyNode()) },
Modifiers = Modifiers.Any,
Name = Pattern.AnyString,
PrivateImplementationType = new AnyNodeOrNull(),
ReturnType = new AnyNode(),
Getter = new Accessor() { Body = new BlockStatement() { new ReturnStatement(new AnyNode("expression")) } }
};
static readonly IndexerDeclaration CalculatedGetterOnlyIndexerPattern = new IndexerDeclaration() {
Attributes = { new Repeat(new AnyNode()) },
Modifiers = Modifiers.Any,
PrivateImplementationType = new AnyNodeOrNull(),
Parameters = { new Repeat(new AnyNode()) },
ReturnType = new AnyNode(),
Getter = new Accessor() { Body = new BlockStatement() { new ReturnStatement(new AnyNode("expression")) } }
};
@ -146,5 +164,14 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -146,5 +164,14 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
propertyDeclaration.ExpressionBody = m.Get<Expression>("expression").Single().Detach();
propertyDeclaration.Getter.Remove();
}
void SimplifyIndexerDeclaration(IndexerDeclaration indexerDeclaration)
{
var m = CalculatedGetterOnlyIndexerPattern.Match(indexerDeclaration);
if (!m.Success)
return;
indexerDeclaration.ExpressionBody = m.Get<Expression>("expression").Single().Detach();
indexerDeclaration.Getter.Remove();
}
}
}

Loading…
Cancel
Save