Browse Source

Fix decompilation of PointerReferenceExpression

pull/728/merge
Daniel Grunwald 9 years ago
parent
commit
f55a93011d
  1. 12
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  2. 2
      ICSharpCode.Decompiler/Tests/CorrectnessTestRunner.cs
  3. 5
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/UnsafeCode.cs
  4. 22
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/ValueTypeCall.cs

12
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -212,8 +212,10 @@ namespace ICSharpCode.Decompiler.CSharp @@ -212,8 +212,10 @@ namespace ICSharpCode.Decompiler.CSharp
IType elementType;
TranslatedExpression countExpression = TranslatePointerArgument(inst.Argument, context, out elementType);
countExpression = countExpression.ConvertTo(compilation.FindType(KnownTypeCode.Int32), this);
if (elementType == null)
elementType = compilation.FindType(KnownTypeCode.Byte);
return new StackAllocExpression {
Type = ConvertType(elementType ?? compilation.FindType(KnownTypeCode.Byte)),
Type = ConvertType(elementType),
CountExpression = countExpression
}.WithILInstruction(inst).WithRR(new ResolveResult(new PointerType(elementType)));
}
@ -997,10 +999,10 @@ namespace ICSharpCode.Decompiler.CSharp @@ -997,10 +999,10 @@ namespace ICSharpCode.Decompiler.CSharp
.WithRR(new ThisResolveResult(member.DeclaringType, nonVirtualInvocation));
} else {
var translatedTarget = Translate(target);
// if (member.DeclaringType.IsReferenceType == false) {
// // when accessing members on value types, ensure we use a reference and not a pointer
// translatedTarget = translatedTarget.ConvertTo(new ByReferenceType(member.DeclaringType), this);
// }
if (member.DeclaringType.IsReferenceType == false && translatedTarget.Type.GetStackType().IsIntegerType()) {
// when accessing members on value types, ensure we use a reference and not a pointer
translatedTarget = translatedTarget.ConvertTo(new ByReferenceType(member.DeclaringType), this);
}
if (translatedTarget.Expression is DirectionExpression) {
translatedTarget = translatedTarget.UnwrapChild(((DirectionExpression)translatedTarget).Expression);
}

2
ICSharpCode.Decompiler/Tests/CorrectnessTestRunner.cs

@ -130,7 +130,7 @@ namespace ICSharpCode.Decompiler.Tests @@ -130,7 +130,7 @@ namespace ICSharpCode.Decompiler.Tests
TestAssembleDecompileCompileOutput("BitNot.il", CompilerOptions.UseDebug | CompilerOptions.Force32Bit, AssemblerOptions.Force32Bit);
}
[Test, Ignore("Pointer reference expression is not supported")]
[Test]
public void UnsafeCode()
{
TestCompileDecompileCompileOutputAll("UnsafeCode.cs");

5
ICSharpCode.Decompiler/Tests/TestCases/Correctness/UnsafeCode.cs

@ -138,6 +138,11 @@ public class UnsafeCode @@ -138,6 +138,11 @@ public class UnsafeCode
return d->ToString();
}
public unsafe string PointerReferenceExpression2(long addr)
{
return ((int*)addr)->ToString();
}
public unsafe void FixMultipleStrings(string text)
{
fixed (char* ptr = text, userName = Environment.UserName, ptr2 = text)

22
ICSharpCode.Decompiler/Tests/TestCases/Correctness/ValueTypeCall.cs

@ -31,6 +31,16 @@ namespace ValueTypeCall @@ -31,6 +31,16 @@ namespace ValueTypeCall
}
}
public struct ValueTypeWithReadOnlyMember
{
public readonly int Member;
public ValueTypeWithReadOnlyMember(int member)
{
this.Member = member;
}
}
public class Program
{
public static void Main()
@ -42,6 +52,7 @@ namespace ValueTypeCall @@ -42,6 +52,7 @@ namespace ValueTypeCall
Box();
var gvt = new GenericValueType<string>("Test");
gvt.Call(ref gvt);
new Program().InstanceFieldTests();
}
static void RefParameter(ref MutValueType m)
@ -78,5 +89,16 @@ namespace ValueTypeCall @@ -78,5 +89,16 @@ namespace ValueTypeCall
((MutValueType)o).Increment();
((MutValueType)o).Increment();
}
MutValueType instanceField;
ValueTypeWithReadOnlyMember mutableInstanceFieldWithReadOnlyMember;
void InstanceFieldTests()
{
this.instanceField.val = 42;
Console.WriteLine(this.instanceField.val);
mutableInstanceFieldWithReadOnlyMember = new ValueTypeWithReadOnlyMember(45);
Console.WriteLine(this.mutableInstanceFieldWithReadOnlyMember.Member);
}
}
}

Loading…
Cancel
Save