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

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

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

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

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

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

@ -84,16 +84,10 @@
instance class ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyClass instance class ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyClass
get_Item(int32 index) cil managed get_Item(int32 index) cil managed
{ {
// Code size 7 (0x7) // Code size 2 (0x2)
.maxstack 1 .maxstack 8
.locals init (class ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyClass V_0) IL_0000: ldnull
IL_0000: nop IL_0001: ret
IL_0001: ldnull
IL_0002: stloc.0
IL_0003: br.s IL_0005
IL_0005: ldloc.0
IL_0006: ret
} // end of method MyClass::get_Item } // end of method MyClass::get_Item
.method public hidebysig instance class ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyClass .method public hidebysig instance class ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyClass
@ -180,19 +174,13 @@
instance valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct> instance valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct>
get_Item(int32 index) cil managed get_Item(int32 index) cil managed
{ {
// Code size 15 (0xf) // Code size 10 (0xa)
.maxstack 1 .maxstack 1
.locals init (valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct> V_0, .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: ldloca.s V_0
IL_0000: nop IL_0002: initobj valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct>
IL_0001: ldloca.s V_0 IL_0008: ldloc.0
IL_0003: initobj valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct> IL_0009: ret
IL_0009: ldloc.0
IL_000a: stloc.1
IL_000b: br.s IL_000d
IL_000d: ldloc.1
IL_000e: ret
} // end of method MyStruct::get_Item } // end of method MyStruct::get_Item
.method public hidebysig instance valuetype [mscorlib]System.Nullable`1<valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.NullPropagation/MyStruct> .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
base.VisitPropertyDeclaration(propertyDeclaration); base.VisitPropertyDeclaration(propertyDeclaration);
} }
public override void VisitIndexerDeclaration(IndexerDeclaration indexerDeclaration)
{
if (context.Settings.UseExpressionBodyForCalculatedGetterOnlyProperties) {
SimplifyIndexerDeclaration(indexerDeclaration);
}
base.VisitIndexerDeclaration(indexerDeclaration);
}
static readonly PropertyDeclaration CalculatedGetterOnlyPropertyPattern = new PropertyDeclaration() { static readonly PropertyDeclaration CalculatedGetterOnlyPropertyPattern = new PropertyDeclaration() {
Attributes = { new Repeat(new AnyNode()) }, Attributes = { new Repeat(new AnyNode()) },
Modifiers = Modifiers.Any, Modifiers = Modifiers.Any,
Name = Pattern.AnyString, 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(), ReturnType = new AnyNode(),
Getter = new Accessor() { Body = new BlockStatement() { new ReturnStatement(new AnyNode("expression")) } } Getter = new Accessor() { Body = new BlockStatement() { new ReturnStatement(new AnyNode("expression")) } }
}; };
@ -146,5 +164,14 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
propertyDeclaration.ExpressionBody = m.Get<Expression>("expression").Single().Detach(); propertyDeclaration.ExpressionBody = m.Get<Expression>("expression").Single().Detach();
propertyDeclaration.Getter.Remove(); 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