Exemplo n.º 1
0
        void ProcessBlock(BlockStatement node)
        {
            for (int i = 0; i < node.Statements.Count - 1; i++)
            {
                var matcher = new UsingMatcher(node.Statements[i], node.Statements[i + 1]);
                if (!matcher.Match())
                {
                    continue;
                }

                if (matcher.VariableReference != null)
                {
                    context.MethodContext.RemoveVariable(matcher.VariableReference);
                }
                if (matcher.RemoveExpression)
                {
                    node.Statements.RemoveAt(i);                     // declaration
                    node.Statements.RemoveAt(i);                     // try
                    node.AddStatementAt(i, matcher.Using);
                }
                else
                {
                    int index = i + (matcher.HasExpression ? 1 : 0);
                    node.Statements.RemoveAt(index);                     // try
                    node.AddStatementAt(index, matcher.Using);
                }
                ProcessBlock(matcher.Using.Body);
            }
        }
Exemplo n.º 2
0
 private void AddDefaultAssignmentsToNewConditionalVariables(BlockStatement body)
 {
     V_0 = this.labelToVariable.get_Values().GetEnumerator();
     try
     {
         while (V_0.MoveNext())
         {
             V_1 = V_0.get_Current();
             V_2 = new BinaryExpression(26, new VariableReferenceExpression(V_1, null), this.GetLiteralExpression(false), this.typeSystem, null, false);
             body.AddStatementAt(0, new ExpressionStatement(V_2));
         }
     }
     finally
     {
         ((IDisposable)V_0).Dispose();
     }
     if (this.usedBreakVariable)
     {
         V_3 = new BinaryExpression(26, new VariableReferenceExpression(this.breakVariable, null), this.GetLiteralExpression(false), this.typeSystem, null, false);
         body.AddStatementAt(0, new ExpressionStatement(V_3));
     }
     if (this.usedContinueVariable)
     {
         V_4 = new BinaryExpression(26, new VariableReferenceExpression(this.continueVariable, null), this.GetLiteralExpression(false), this.typeSystem, null, false);
         body.AddStatementAt(0, new ExpressionStatement(V_4));
     }
     return;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Moves the statements inside <paramref name="toMove"/> right after the goto statement adn removes the goto statement.
        /// </summary>
        /// <param name="gotoStatement">The goto statement, jumping to the block to be moved.</param>
        /// <param name="toMove">The collection of statements to be moved. This should already contain cloned statements.</param>
        private void MoveStatements(GotoStatement gotoStatement, IEnumerable <Statement> toMove)
        {
            BlockStatement gotoParent = gotoStatement.Parent as BlockStatement;

            if (gotoParent == null)
            {
                throw new DecompilationException("Goto statement not inside a block.");
            }
            int gotoIndex = gotoParent.Statements.IndexOf(gotoStatement);

            gotoParent.Statements.RemoveAt(gotoIndex);
            methodContext.GotoStatements.Remove(gotoStatement);
            int startIndex = gotoIndex;

            foreach (Statement st in toMove)
            {
                gotoParent.AddStatementAt(startIndex, st);
                startIndex++;
            }
            if (!string.IsNullOrEmpty(gotoStatement.Label))
            {
                /// If the goto was target of another goto, update the label to point to the first statement of the coppied block.
                string    theLabel        = gotoStatement.Label;
                Statement newLabelCarrier = gotoParent.Statements[gotoIndex];
                methodContext.GotoLabels[theLabel] = newLabelCarrier;
                newLabelCarrier.Label = gotoStatement.Label;
            }
        }
        public override void VisitBlockStatement(BlockStatement node)
        {
            for (int i = 0; i < node.Statements.Count; i++)
            {
                if (!Match(node.Statements, i))
                {
                    continue;
                }

                // repalce try with lock
                node.Statements.RemoveAt(i);
                node.AddStatementAt(i, Lock);

                //RemoveFlagVariable(i - 1, node, theFlagVariable);

                if (this.lockType == LockType.Simple)
                {
                    node.Statements.RemoveAt(i + 1); //the try
                    node.Statements.RemoveAt(--i);   // the second assign
                    node.Statements.RemoveAt(--i);   // the first assign
                }
                else // LockType.WithFlag
                {
                    Lock.Body.Statements.RemoveAt(0); // the first assign
                    Lock.Body.Statements.RemoveAt(0); // the second assign
                    Lock.Body.Statements.RemoveAt(0); // the method invoke
                    if (i > 0)
                    {
                        node.Statements.RemoveAt(--i);
                    }
                }
            }

            Visit(node.Statements);
        }
Exemplo n.º 5
0
 private void ExtractConditionIntoVariable(VariableReferenceExpression conditionVar, ConditionStatement statement, BlockStatement containingBlock)
 {
     V_0 = new ExpressionStatement(new BinaryExpression(26, conditionVar, statement.get_Condition(), this.typeSystem, null, false));
     containingBlock.AddStatementAt(containingBlock.get_Statements().IndexOf(statement), V_0);
     statement.set_Condition(conditionVar.CloneExpressionOnly());
     return;
 }
Exemplo n.º 6
0
        void ProcessBlock(BlockStatement node)
        {
            for (int i = 0; i < node.Statements.Count - 1; i++)
            {
                ForeachArrayMatcher matcher = new ForeachArrayMatcher(node.Statements[i], node.Statements[i + 1], this.context.MethodContext);
                if (!matcher.Match())
                {
                    continue;
                }

                if (CheckForIndexUsages(matcher))
                {
                    continue;
                }

                context.MethodContext.RemoveVariable(matcher.Incrementor);
                if (matcher.CurrentVariable != null)
                {
                    context.MethodContext.RemoveVariable(matcher.CurrentVariable);
                }

                node.Statements.RemoveAt(i);
                node.Statements.RemoveAt(i);
                node.AddStatementAt(i, matcher.Foreach);
                ProcessBlock(matcher.Foreach.Body);
            }
        }
        private void AttachForeach()
        {
            GenerateForeachStatement();

            if (!isEnumeratorUsedInsideForEach)
            {
                BlockStatement parentBlock = theTry.Parent as BlockStatement;
                parentBlock.Statements.Remove(enumeratorAssignmentStatement);
                int tryIndex = parentBlock.Statements.IndexOf(theTry);

                parentBlock.Statements.RemoveAt(tryIndex);
                parentBlock.AddStatementAt(tryIndex, @foreach);

                YieldStateMachineCodeRemover ysmcr = new YieldStateMachineCodeRemover(@foreach, theEnumerator);
                ysmcr.ProcessForEachStatement();

                CopyLabel();

                CheckVariable();

                ClearState();

                VisitForEachStatement(parentBlock.Statements[tryIndex] as ForEachStatement);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Replaces the goto statement with its target, if the target is goto statement.
        /// </summary>
        /// <param name="labeledStatement"></param>
        /// <param name="gotoStatement"></param>
        /// <returns></returns>
        private bool TryRemoveChainedGoto(Statement labeledStatement, GotoStatement gotoStatement)
        {
            if (!(labeledStatement is GotoStatement))
            {
                return(false);
            }
            BlockStatement parent = gotoStatement.Parent as BlockStatement;
            int            index  = parent.Statements.IndexOf(gotoStatement);
            /// Clone of labeled statement needed here

            Statement clone = labeledStatement.CloneStatementOnly();

            clone.Label = string.Empty;

            parent.Statements.RemoveAt(index);
            parent.AddStatementAt(index, clone);


            methodContext.GotoStatements.Remove(gotoStatement);
            methodContext.GotoStatements.Add(clone as GotoStatement);

            if (!Targeted(labeledStatement.Label))
            {
                UpdateUntargetedStatement(labeledStatement, new Statement[] { labeledStatement });
            }
            return(true);
        }
Exemplo n.º 9
0
 private void AddBreakContinueConditional(int index, BlockStatement containingBlock, Statement statement, VariableReference conditionVariable)
 {
     V_0 = new BlockStatement();
     V_0.AddStatement(statement);
     V_1 = new IfStatement(new VariableReferenceExpression(conditionVariable, null), V_0, null);
     containingBlock.AddStatementAt(index, V_1);
     return;
 }
Exemplo n.º 10
0
        public override void VisitBlockStatement(BlockStatement node)
        {
            for (int i = 0; i < node.Statements.Count; i++)
            {
                if (!Match(node.Statements, i))
                {
                    continue;
                }

                // repalce try with lock
                node.Statements.RemoveAt(i);
                node.AddStatementAt(i, Lock);

                //RemoveFlagVariable(i - 1, node, theFlagVariable);

                if (this.lockType == LockType.Simple)
                {
                    node.Statements.RemoveAt(i + 1); //the try
                    node.Statements.RemoveAt(--i);   // the second assign
                    node.Statements.RemoveAt(--i);   // the first assign
                }
                else // LockType.WithFlag (V1, V2 or V3)
                {
                    int numberOfStatementsToRemove;
                    if (lockType == LockType.WithFlagV1)
                    {
                        numberOfStatementsToRemove = 3;
                    }
                    else if (lockType == LockType.WithFlagV2)
                    {
                        numberOfStatementsToRemove = 1;
                    }
                    else // V3
                    {
                        numberOfStatementsToRemove = 2;
                    }

                    for (int j = 0; j < numberOfStatementsToRemove; j++)
                    {
                        Lock.Body.Statements.RemoveAt(0);
                    }

                    if (i > 0)
                    {
                        node.Statements.RemoveAt(--i);
                        if (lockType == LockType.WithFlagV2)
                        {
                            node.Statements.RemoveAt(--i);
                        }
                    }
                }
            }

            Visit(node.Statements);
        }
Exemplo n.º 11
0
 private BlockStatement FixSwitchingIf(BlockStatement switchBlock)
 {
     if (switchBlock.get_Statements().get_Count() < 1)
     {
         return(switchBlock);
     }
     V_0 = this.FixSwitchingStatement(switchBlock.get_Statements().get_Item(0));
     switchBlock.get_Statements().RemoveAt(0);
     switchBlock.AddStatementAt(0, V_0);
     return(switchBlock);
 }
Exemplo n.º 12
0
 private void RemoveRangeAndInsert(BlockStatement block, int startIndex, int length, Statement newStatement)
 {
     V_0 = block.get_Statements().get_Item(startIndex).get_Label();
     this.RemoveRange(block.get_Statements(), startIndex, length);
     newStatement.set_Label(V_0);
     if (!String.IsNullOrEmpty(V_0))
     {
         this.context.get_MethodContext().get_GotoLabels().set_Item(V_0, newStatement);
     }
     block.AddStatementAt(startIndex, newStatement);
     return;
 }
 private void InsertVariableDeclarationAndAssignment(BlockStatement block, int insertIndex, int variableIndex)
 {
     V_0 = this.methodVariables.get_Item(variableIndex).get_VariableType().GetDefaultValueExpression(this.typeSystem);
     if (V_0 == null)
     {
         this.InsertVariableDeclaration(block, insertIndex, variableIndex);
         return;
     }
     V_1 = new BinaryExpression(26, new VariableDeclarationExpression(this.methodVariables.get_Item(variableIndex), null), V_0, this.typeSystem, null, false);
     block.AddStatementAt(insertIndex, new ExpressionStatement(V_1));
     return;
 }
Exemplo n.º 14
0
        private void RemoveRangeAndInsert(BlockStatement block, int startIndex, int length, Statement newStatement)
        {
            string label = block.Statements[startIndex].Label;

            RemoveRange(block.Statements, startIndex, length);
            newStatement.Label = label;
            if (!string.IsNullOrEmpty(label))
            {
                context.MethodContext.GotoLabels[label] = newStatement;
            }
            block.AddStatementAt(startIndex, newStatement);
        }
Exemplo n.º 15
0
		private void ProcessBlock(BlockStatement node)
		{
			V_0 = 0;
			while (V_0 < node.get_Statements().get_Count() - 1)
			{
				V_1 = new RebuildUsingStatements.UsingMatcher(node.get_Statements().get_Item(V_0), node.get_Statements().get_Item(V_0 + 1));
				if (V_1.Match())
				{
					if (V_1.get_VariableReference() != null)
					{
						this.context.get_MethodContext().RemoveVariable(V_1.get_VariableReference());
					}
					if (!V_1.get_RemoveExpression())
					{
						stackVariable27 = V_0;
						if (V_1.get_HasExpression())
						{
							stackVariable30 = 1;
						}
						else
						{
							stackVariable30 = 0;
						}
						V_2 = stackVariable27 + stackVariable30;
						node.get_Statements().RemoveAt(V_2);
						node.AddStatementAt(V_2, V_1.get_Using());
					}
					else
					{
						node.get_Statements().RemoveAt(V_0);
						node.get_Statements().RemoveAt(V_0);
						node.AddStatementAt(V_0, V_1.get_Using());
					}
					this.ProcessBlock(V_1.get_Using().get_Body());
				}
				V_0 = V_0 + 1;
			}
			return;
		}
        private BlockStatement FixSwitchingIf(BlockStatement switchBlock)
        {
            if (switchBlock.Statements.Count < 1)
            {
                // sanity check.
                return(switchBlock);
            }
            Statement fixedSwitch = FixSwitchingStatement(switchBlock.Statements[0]);

            switchBlock.Statements.RemoveAt(0);
            switchBlock.AddStatementAt(0, fixedSwitch);
            return(switchBlock);
        }
        public void CleanUpUnusedDeclarations()
        {
            foreach (KeyValuePair <VariableReference, ExpressionStatement> referenceToStatementPair in referenceToDeclarationStatementMap)
            {
                VariableReference variableReference = referenceToStatementPair.Key;
                context.MethodContext.RemoveVariable(variableReference);

                ExpressionStatement declarationStatement = referenceToStatementPair.Value;
                BlockStatement      parentBlock          = declarationStatement.Parent as BlockStatement;

                if (IsOptimisableAssignment(declarationStatement))
                {
                    TransferLabel(declarationStatement);
                    parentBlock.Statements.Remove(declarationStatement);
                }
                else
                {
                    /// check if the right side can exist on its own
                    Expression rightSide = (declarationStatement.Expression as BinaryExpression).Right;
                    if (CanExistInStatement(rightSide))
                    {
                        if (rightSide.CodeNodeType == CodeNodeType.ParenthesesExpression)
                        {
                            rightSide = (rightSide as ParenthesesExpression).Expression;
                        }
                        ExpressionStatement newStatement = new ExpressionStatement(rightSide);
                        int index = parentBlock.Statements.IndexOf(declarationStatement);
                        parentBlock.AddStatementAt(index + 1, newStatement);
                        TransferLabel(declarationStatement);
                        parentBlock.Statements.RemoveAt(index);
                    }
                }
            }

            HashSet <VariableDefinition> unusedVariables = new HashSet <VariableDefinition>();

            foreach (VariableDefinition variable in context.MethodContext.Variables)
            {
                if (!bannedVariables.Contains(variable))
                {
                    unusedVariables.Add(variable);
                }
            }

            foreach (VariableDefinition variable in unusedVariables)
            {
                context.MethodContext.RemoveVariable(variable);
            }
        }
        private void InsertTopLevelParameterAssignments(BlockStatement block)
        {
            for (int i = 0; i < this.context.MethodContext.OutParametersToAssign.Count; i++)
            {
                ParameterDefinition parameter            = this.context.MethodContext.OutParametersToAssign[i];
                TypeReference       nonPointerType       = parameter.ParameterType.IsByReference ? parameter.ParameterType.GetElementType() : parameter.ParameterType;
                UnaryExpression     parameterDereference =
                    new UnaryExpression(UnaryOperator.AddressDereference, new ArgumentReferenceExpression(parameter, null), null);

                BinaryExpression assignExpression = new BinaryExpression(BinaryOperator.Assign, parameterDereference,
                                                                         nonPointerType.GetDefaultValueExpression(typeSystem), nonPointerType, typeSystem, null);

                block.AddStatementAt(i, new ExpressionStatement(assignExpression));
            }
        }
        private void InsertVariableDeclarationAndAssignment(BlockStatement block, int insertIndex, int variableIndex)
        {
            Expression defaultValueExpression = methodVariables[variableIndex].VariableType.GetDefaultValueExpression(typeSystem);

            if (defaultValueExpression == null)
            {
                InsertVariableDeclaration(block, insertIndex, variableIndex);
                return;
            }

            BinaryExpression assignExpression =
                new BinaryExpression(BinaryOperator.Assign, new VariableDeclarationExpression(methodVariables[variableIndex], null),
                                     defaultValueExpression, typeSystem, null);

            block.AddStatementAt(insertIndex, new ExpressionStatement(assignExpression));
        }
Exemplo n.º 20
0
        private void ReplaceIfWith(IfStatement theIf, BlockStatement statementBlock)
        {
            if (!CanReplaceIf(statementBlock))
            {
                throw new Exception(InvalidIsEventString);
            }

            DynamicMemberReferenceExpression dynamicMethodInvocation = (statementBlock.Statements[1] as ExpressionStatement).Expression as
                                                                       DynamicMemberReferenceExpression;

            if (dynamicMethodInvocation == null)             // the expression was an assignment
            {
                dynamicMethodInvocation = ((statementBlock.Statements[1] as ExpressionStatement).Expression as BinaryExpression).Right as DynamicMemberReferenceExpression;
            }

            if (dynamicMethodInvocation.MemberName == null || !dynamicMethodInvocation.IsMethodInvocation ||
                dynamicMethodInvocation.IsGenericMethod || dynamicMethodInvocation.InvocationArguments.Count != 1)
            {
                throw new Exception(InvalidIsEventString);
            }

            int charIndex = dynamicMethodInvocation.MemberName.IndexOf('_');

            if (charIndex != 3 && charIndex != 6) //"add_" - 3, "remove_" - 6
            {
                throw new Exception(InvalidIsEventString);
            }

            DynamicMemberReferenceExpression dynamicMemberRef = new DynamicMemberReferenceExpression(dynamicMethodInvocation.Target,
                                                                                                     dynamicMethodInvocation.MemberName.Substring(charIndex + 1), dynamicMethodInvocation.ExpressionType, dynamicMethodInvocation.MappedInstructions);

            BinaryExpression theBinaryExpression = new BinaryExpression(charIndex == 3 ? BinaryOperator.AddAssign : BinaryOperator.SubtractAssign, dynamicMemberRef,
                                                                        dynamicMethodInvocation.InvocationArguments[0], dynamicMemberRef.ExpressionType, typeSystem, null);

            BlockStatement parent  = (BlockStatement)theIf.Parent;
            int            ifIndex = parent.Statements.IndexOf(theIf);

            ExpressionStatement theAssignStatement = new ExpressionStatement(theBinaryExpression);

            theAssignStatement.Parent  = parent;
            parent.Statements[ifIndex] = theAssignStatement;

            if (statementBlock.Statements.Count == 3)
            {
                parent.AddStatementAt(ifIndex + 1, statementBlock.Statements[2].Clone());
            }
        }
Exemplo n.º 21
0
 private void ProcessBlock(BlockStatement node)
 {
     V_0 = 0;
     while (V_0 < node.get_Statements().get_Count() - 1)
     {
         V_1 = new RebuildForeachArrayStatements.ForeachArrayMatcher(node.get_Statements().get_Item(V_0), node.get_Statements().get_Item(V_0 + 1), this.context.get_MethodContext());
         if (V_1.Match() && !this.CheckForIndexUsages(V_1))
         {
             this.context.get_MethodContext().RemoveVariable(V_1.get_Incrementor());
             if (V_1.get_CurrentVariable() != null)
             {
                 this.context.get_MethodContext().RemoveVariable(V_1.get_CurrentVariable());
             }
             node.get_Statements().RemoveAt(V_0);
             node.get_Statements().RemoveAt(V_0);
             node.AddStatementAt(V_0, V_1.get_Foreach());
             this.ProcessBlock(V_1.get_Foreach().get_Body());
         }
         V_0 = V_0 + 1;
     }
     return;
 }
 private void InsertTopLevelParameterAssignments(BlockStatement block)
 {
     V_0 = 0;
     while (V_0 < this.context.get_MethodContext().get_OutParametersToAssign().get_Count())
     {
         V_1 = this.context.get_MethodContext().get_OutParametersToAssign().get_Item(V_0);
         if (V_1.get_ParameterType().get_IsByReference())
         {
             stackVariable18 = V_1.get_ParameterType().GetElementType();
         }
         else
         {
             stackVariable18 = V_1.get_ParameterType();
         }
         V_2 = stackVariable18;
         V_3 = new UnaryExpression(8, new ArgumentReferenceExpression(V_1, null), null);
         V_4 = new BinaryExpression(26, V_3, V_2.GetDefaultValueExpression(this.typeSystem), V_2, this.typeSystem, null, false);
         block.AddStatementAt(V_0, new ExpressionStatement(V_4));
         V_0 = V_0 + 1;
     }
     return;
 }
Exemplo n.º 23
0
        /// <summary>
        /// Removes the goto statement if it targets directly the statement afterwards
        /// </summary>
        /// <param name="labeledStatement">The targeted statement.</param>
        /// <param name="gotoStatement">The goto statement.</param>
        /// <returns></returns>
        private bool TryRemoveGoto(Statement labeledStatement, GotoStatement gotoStatement)
        {
            BlockStatement gotoParent = gotoStatement.Parent as BlockStatement;

            if (gotoParent == null)
            {
                throw new DecompilationException("Goto statement not inside a block.");
            }
            int gotoIndex = gotoParent.Statements.IndexOf(gotoStatement);

            if (labeledStatement.Parent == gotoParent && gotoParent.Statements.IndexOf(labeledStatement) == gotoIndex + 1)
            {
                ///then goto and label are consecutive
                gotoParent.Statements.RemoveAt(gotoIndex);
                methodContext.GotoStatements.Remove(gotoStatement);

                ///Clean up the label from the targeted statement if possible, and remove the targeted statement if it cannot be reached anymore
                if (!Targeted(labeledStatement.Label))
                {
                    UpdateUntargetedStatement(labeledStatement, new List <Statement>());
                }

                return(true);
            }
            if (gotoIndex == gotoParent.Statements.Count - 1)
            {
                ///then the goto is the last statement in the block
                ///it can potentially point to the next statement from the outer block

                Statement parent = gotoParent.Parent;
                if (parent == null)
                {
                    ///the goto is in the outermost block
                    return(false);
                }

                if (parent.CodeNodeType == CodeNodeType.ConditionCase || parent.CodeNodeType == CodeNodeType.DefaultCase)
                {
                    parent = parent.Parent;
                }

                if (parent.CodeNodeType == CodeNodeType.SwitchStatement || parent.CodeNodeType == CodeNodeType.ForEachStatement ||
                    parent.CodeNodeType == CodeNodeType.WhileStatement || parent.CodeNodeType == CodeNodeType.ForStatement ||
                    parent.CodeNodeType == CodeNodeType.DoWhileStatement || parent.CodeNodeType == CodeNodeType.IfStatement ||
                    parent.CodeNodeType == CodeNodeType.IfElseIfStatement)
                {
                    BlockStatement outerBlock = parent.Parent as BlockStatement;
                    if (labeledStatement.Parent != outerBlock)
                    {
                        return(false);
                    }
                    if (outerBlock.Statements.IndexOf(parent) == outerBlock.Statements.IndexOf(labeledStatement) - 1)
                    {
                        ///Then the goto is to the next statement in the natural flow of the program
                        gotoParent.Statements.RemoveAt(gotoIndex);
                        methodContext.GotoStatements.Remove(gotoStatement);
                        if (parent.CodeNodeType != CodeNodeType.IfStatement && parent.CodeNodeType != CodeNodeType.IfElseIfStatement)
                        {
                            ///then the goto must be replaced by break;
                            gotoParent.AddStatementAt(gotoIndex, new BreakStatement(gotoStatement.UnderlyingSameMethodInstructions));
                        }

                        ///Clean up the label from the targeted statement if possible, and remove the targeted statement if it cannot be reached anymore
                        if (!Targeted(labeledStatement.Label))
                        {
                            UpdateUntargetedStatement(labeledStatement, new List <Statement>());
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }
 private void ProcessBlock(BlockStatement node)
 {
     V_0 = 0;
     while (V_0 < node.get_Statements().get_Count() - 1)
     {
         V_1 = null;
         V_2 = new List <VariableReference>();
         V_3 = node.get_Statements().get_Item(V_0);
         V_1 = this.GetFixedStatement(V_3, V_2);
         if (V_1 != null)
         {
             V_7 = V_2.GetEnumerator();
             try
             {
                 while (V_7.MoveNext())
                 {
                     V_8 = V_7.get_Current();
                     this.methodContext.RemoveVariable(V_8);
                 }
             }
             finally
             {
                 ((IDisposable)V_7).Dispose();
             }
             node.get_Statements().RemoveAt(V_0);
             node.AddStatementAt(V_0, V_1);
             V_4             = node.get_Statements().get_Item(V_0 + 1) as ExpressionStatement;
             V_5             = node.get_Statements().get_Count();
             V_6             = null;
             stackVariable47 = new RebuildFixedStatements.VariableReferenceVisitor();
             stackVariable47.Visit(V_1.get_Expression());
             V_6 = stackVariable47.get_Variable();
             V_9 = V_0 + 1;
             while (V_9 < V_5)
             {
                 stackVariable56 = new RebuildFixedStatements.VariableReferenceVisitor();
                 stackVariable56.Visit(node.get_Statements().get_Item(V_9));
                 V_10 = stackVariable56.get_Variable();
                 if (V_10 != null && !this.variables.Contains(V_10))
                 {
                     this.variables.Add(V_10);
                 }
                 if (node.get_Statements().get_Item(V_9).get_CodeNodeType() == 5)
                 {
                     V_4 = node.get_Statements().get_Item(V_9) as ExpressionStatement;
                     if (V_10 != null && (object)V_10 == (object)V_6 && V_4.get_Expression().get_CodeNodeType() == 24 && (V_4.get_Expression() as BinaryExpression).get_IsAssignmentExpression() && (V_4.get_Expression() as BinaryExpression).get_Right().get_CodeNodeType() == 22 && ((V_4.get_Expression() as BinaryExpression).get_Right() as LiteralExpression).get_Value() == null)
                     {
                         node.get_Statements().RemoveAt(V_9);
                         V_9 = V_9 - 1;
                         V_5 = V_5 - 1;
                         break;
                     }
                 }
                 V_1.get_Body().AddStatement(node.get_Statements().get_Item(V_9));
                 node.get_Statements().RemoveAt(V_9);
                 V_9 = V_9 - 1;
                 V_5 = V_5 - 1;
                 V_9 = V_9 + 1;
             }
             this.ProcessBlock(V_1.get_Body());
             return;
         }
         V_0 = V_0 + 1;
     }
     return;
 }
        void ProcessBlock(BlockStatement node)
        {
            for (int i = 0; i < node.Statements.Count - 1; i++)
            {
                FixedStatement           @fixed             = null;
                List <VariableReference> variableReferences = new List <VariableReference>();

                Statement statement = node.Statements[i];

                @fixed = GetFixedStatement(statement, variableReferences);

                if (@fixed == null)
                {
                    continue;
                }

                foreach (VariableReference variable in variableReferences)
                {
                    methodContext.RemoveVariable(variable);
                }

                // remove the first statement.
                node.Statements.RemoveAt(i);
                // append the fixed block
                node.AddStatementAt(i, @fixed);

                ExpressionStatement expressionStmt = node.Statements[i + 1] as ExpressionStatement;

                int len = node.Statements.Count;

                VariableReference fixedVariable = null;

                VariableReferenceVisitor varibleVisitor = new VariableReferenceVisitor();
                varibleVisitor.Visit(@fixed.Expression);

                fixedVariable = varibleVisitor.Variable;

                for (int stmtIndex = i + 1; stmtIndex < len; stmtIndex++)
                {
                    varibleVisitor = new VariableReferenceVisitor();

                    varibleVisitor.Visit(node.Statements[stmtIndex]);

                    VariableReference variable = varibleVisitor.Variable;

                    if (variable != null && !variables.Contains(variable))
                    {
                        variables.Add(variable);
                    }

                    if (node.Statements[stmtIndex].CodeNodeType == CodeNodeType.ExpressionStatement)
                    {
                        expressionStmt = node.Statements[stmtIndex] as ExpressionStatement;

                        if (variable != null &&
                            variable == fixedVariable &&
                            expressionStmt.Expression.CodeNodeType == CodeNodeType.BinaryExpression &&
                            (expressionStmt.Expression as BinaryExpression).IsAssignmentExpression &&
                            (expressionStmt.Expression as BinaryExpression).Right.CodeNodeType == CodeNodeType.LiteralExpression &&
                            ((expressionStmt.Expression as BinaryExpression).Right as LiteralExpression).Value == null)
                        {
                            node.Statements.RemoveAt(stmtIndex);
                            stmtIndex--;
                            len--;
                            break;
                        }
                    }
                    @fixed.Body.AddStatement(node.Statements[stmtIndex]);
                    node.Statements.RemoveAt(stmtIndex);
                    stmtIndex--;
                    len--;
                }

                ProcessBlock(@fixed.Body);

                break;
            }
        }
 private void InsertVariableDeclaration(BlockStatement block, int insertIndex, int variableIndex)
 {
     block.AddStatementAt(insertIndex, new ExpressionStatement(new VariableDeclarationExpression(this.methodVariables.get_Item(variableIndex), null)));
     return;
 }
 private void InsertVariableDeclaration(BlockStatement block, int insertIndex, int variableIndex)
 {
     block.AddStatementAt(insertIndex, new ExpressionStatement(new VariableDeclarationExpression(methodVariables[variableIndex], null)));
 }