Browse Source

Fix bugs in TransformCollectionAndObjectInitializers

pull/734/merge
Siegfried Pammer 8 years ago
parent
commit
c3aab802ea
  1. 19
      ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

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

@ -33,6 +33,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms
case NewObj newObjInst: case NewObj newObjInst:
if (DelegateConstruction.IsDelegateConstruction(newObjInst) || DelegateConstruction.IsPotentialClosure(context, newObjInst)) if (DelegateConstruction.IsDelegateConstruction(newObjInst) || DelegateConstruction.IsPotentialClosure(context, newObjInst))
return false; return false;
if (newObjInst.Method.DeclaringType.Kind != TypeKind.Struct && v.Kind != VariableKind.StackSlot)
return false;
break; break;
case DefaultValue defaultVal: case DefaultValue defaultVal:
break; break;
@ -117,8 +119,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms
this.Index = index; this.Index = index;
} }
public IMember Member; public readonly IMember Member;
public ILVariable Index; public readonly ILVariable Index;
public override string ToString() => $"[{Member}, {Index}]"; public override string ToString() => $"[{Member}, {Index}]";
@ -134,7 +136,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
case CallInstruction call: case CallInstruction call:
if (!(call is CallVirt || call is Call)) goto default; if (!(call is CallVirt || call is Call)) goto default;
method = call.Method; method = call.Method;
if (method.IsStatic) goto default; if (!IsMethodApplicable(method)) goto default;
instruction = call.Arguments[0]; instruction = call.Arguments[0];
if (values == null) { if (values == null) {
values = new List<ILInstruction>(call.Arguments.Skip(1)); values = new List<ILInstruction>(call.Arguments.Skip(1));
@ -184,11 +186,20 @@ namespace ICSharpCode.Decompiler.IL.Transforms
break; break;
} }
} }
if (kind != AccessPathKind.Invalid && values.OfType<LdLoc>().Any(ld => ld.Variable == target)) if (kind != AccessPathKind.Invalid && values.SelectMany(v => v.Descendants).OfType<LdLoc>().Any(ld => ld.Variable == target))
kind = AccessPathKind.Invalid; kind = AccessPathKind.Invalid;
return (kind, path, values, target); return (kind, path, values, target);
} }
static bool IsMethodApplicable(IMethod method)
{
if (method.IsStatic)
return false;
if (method.IsAccessor)
return true;
return "Add".Equals(method.Name, StringComparison.Ordinal);
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj is AccessPathElement) if (obj is AccessPathElement)

Loading…
Cancel
Save