Browse Source

Fix #952: Issue with discard expressions and variables

pull/925/merge
Siegfried Pammer 8 years ago
parent
commit
4d5d4b3d6b
  1. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs
  2. 1
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  3. 11
      ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs

@ -301,7 +301,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -301,7 +301,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public unsafe string StackAllocStruct(int count)
{
SimpleStruct* ptr = stackalloc SimpleStruct[checked(count * 2)];
SimpleStruct* _ = stackalloc SimpleStruct[10];
SimpleStruct* ptr2 = stackalloc SimpleStruct[10];
return this.UsePointer(&ptr->Y);
}

1
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -765,6 +765,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -765,6 +765,7 @@ namespace ICSharpCode.Decompiler.CSharp
}
entityDecl.AddChild(body, Roles.Body);
entityDecl.AddAnnotation(function);
if (function.IsIterator) {
if (!body.Descendants.Any(d => d is YieldReturnStatement || d is YieldBreakStatement)) {

11
ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

@ -22,6 +22,7 @@ using System.Linq; @@ -22,6 +22,7 @@ using System.Linq;
using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching;
using ICSharpCode.Decompiler.IL;
using ICSharpCode.Decompiler.IL.Transforms;
using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.Util;
@ -152,9 +153,15 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -152,9 +153,15 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
{
foreach (var stmt in rootNode.DescendantsAndSelf.OfType<ExpressionStatement>()) {
if (!IsValidInStatementExpression(stmt.Expression)) {
// fetch ILFunction
var function = stmt.Ancestors.SelectMany(a => a.Annotations.OfType<ILFunction>()).First(f => f.Parent == null);
// assign result to dummy variable
var v = new ILVariable(VariableKind.StackSlot, stmt.Expression.GetResolveResult().Type, 0);
v.Name = "_";
var type = stmt.Expression.GetResolveResult().Type;
var v = function.RegisterVariable(
VariableKind.StackSlot,
type,
AssignVariableNames.GenerateVariableName(function, type)
);
stmt.Expression = new AssignmentExpression(
new IdentifierExpression(v.Name).WithRR(new ILVariableResolveResult(v, v.Type)),
stmt.Expression.Detach());

Loading…
Cancel
Save