Browse Source

Decompile object initializers more aggressively when the initialized object is stored in a stack slot.

pull/832/head
Daniel Grunwald 8 years ago
parent
commit
a8adc83de4
  1. 4
      ICSharpCode.Decompiler/IL/Transforms/RemoveDeadVariableInit.cs
  2. 7
      ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

4
ICSharpCode.Decompiler/IL/Transforms/RemoveDeadVariableInit.cs

@ -38,8 +38,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -38,8 +38,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms
}
}
if (function.IsIterator) {
// In yield return, the C# compiler tends to store null/default(T) to variables
// when the variable goes out of scope. Remove such useless stores.
foreach (var v in function.Variables) {
if (v.Kind != VariableKind.Parameter && v.StoreCount == 1 && v.LoadCount == 0 && v.AddressCount == 0) {
if (v.Kind == VariableKind.Local && v.StoreCount == 1 && v.LoadCount == 0 && v.AddressCount == 0) {
if (v.StoreInstructions[0] is StLoc stloc && (stloc.Value.MatchLdNull() || stloc.Value is DefaultValue) && stloc.Parent is Block block) {
block.Instructions.Remove(stloc);
}

7
ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

@ -48,8 +48,13 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -48,8 +48,13 @@ namespace ICSharpCode.Decompiler.IL.Transforms
IType instType;
switch (initInst) {
case NewObj newObjInst:
if (newObjInst.ILStackWasEmpty && !context.Function.Method.IsConstructor)
if (newObjInst.ILStackWasEmpty && v.Kind == VariableKind.Local && !context.Function.Method.IsConstructor) {
// on statement level (no other expressions on IL stack),
// prefer to keep local variables (but not stack slots),
// unless we are in a constructor (where inlining object initializers might be critical
// for the base ctor call)
return false;
}
// Do not try to transform display class usages or delegate construction.
// DelegateConstruction transform cannot deal with this.
if (DelegateConstruction.IsSimpleDisplayClass(newObjInst.Method.DeclaringType))

Loading…
Cancel
Save