private void TryRemoveConditionVariable(BlockStatement node, Statement statement, int index)
		{
			if (!(statement is ExpressionStatement))
				return;

			var expressionStatement = (ExpressionStatement) statement;
			if (!(expressionStatement.Expression.CodeNodeType == CodeNodeType.BinaryExpression &&
                (expressionStatement.Expression as BinaryExpression).IsAssignmentExpression))
				return;

			var assingExpression = (BinaryExpression) expressionStatement.Expression;

			if (!(assingExpression.Left is VariableReferenceExpression))
				return;

            if (assingExpression.Right is MethodInvocationExpression)
            {
                var variable = assingExpression.Left as VariableReferenceExpression;

                if (variable.Variable.VariableType.Name != TypeCode.Boolean.ToString())
                    return;
            }

			var variableReferenceExpression = (VariableReferenceExpression) assingExpression.Left;
			if (ContainsKey(variableReferenceExpression.Variable))
			{
				methodContext.RemoveVariable(variableReferenceExpression.Variable);
				node.Statements.RemoveAt(index);
			}
		}
		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);
			}
		}
 public FilterMethodToBeDecompiled(MethodDefinition method, CatchClause catchClause, DecompilationContext context, BlockStatement block)
 {
     this.Method = method;
     this.CatchClause = catchClause;
     this.Context = context;
     this.Block = block;
 }
 public ForStatement(Expression initializer, Expression condition, Expression increment, BlockStatement body)
     : base(condition)
 {
     this.Initializer = initializer;
     this.Increment = increment;
     this.Body = body;
 }
 /// <summary>
 /// The entry point for the step.
 /// </summary>
 /// <param name="context">The decompilation context.</param>
 /// <param name="body">The body of the method.</param>
 /// <returns>Returns the updated body of the method.</returns>
 public BlockStatement Process(DecompilationContext context, BlockStatement body)
 {
     MethodSpecificContext methodContext = context.MethodContext;
     foreach (int key in methodContext.Expressions.BlockExpressions.Keys)
     {
         IList<Expression> expressionList = methodContext.Expressions.BlockExpressions[key];
         Code lastInstructionCode = methodContext.ControlFlowGraph.InstructionToBlockMapping[key].Last.OpCode.Code;
         bool endsWithConditionalJump = lastInstructionCode == Code.Brtrue || lastInstructionCode == Code.Brtrue_S ||
                                        lastInstructionCode == Code.Brfalse || lastInstructionCode == Code.Brfalse_S;
         for (int i = 0; i < expressionList.Count; i++)
         {
             expressionList[i] = (Expression)Visit(expressionList[i]);
         }
         if (endsWithConditionalJump)
         {
             expressionList[expressionList.Count - 1] = (Expression)
                 FixBranchingExpression(expressionList[expressionList.Count - 1], methodContext.ControlFlowGraph.InstructionToBlockMapping[key].Last);
         }
         //if (lastInstructionCode == Code.Switch)
         //{
         //    //the type of this expression is needed if the switch instruction causes IrregularSwitchLC
         //    //so that correct expressions can be produced for case's conditions
         //    //Expression lastExpression = expressionList[expressionList.Count - 1];
         //}
     }
     return body;
 }
        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);
        }
		public BlockStatement Process(DecompilationContext context, BlockStatement block)
		{
            this.typeSystem = context.MethodContext.Method.Module.TypeSystem;
			this.context = context;
            BlockStatement newBlock = (BlockStatement)VisitBlockStatement(block);
            return newBlock;
		}
        protected BlockStatement RunInternal(MethodBody body, BlockStatement block, ILanguage language)
        {
            try
            {
                if (body.Instructions.Count != 0 || body.Method.IsJustDecompileGenerated)
                {
                    foreach (IDecompilationStep step in steps)
                    {
                        if (language != null && language.IsStopped)
                        {
                            break;
                        }

                        block = step.Process(Context, block);
                    }
                }
            }
            finally
            {
                if (Context.MethodContext.IsMethodBodyChanged)
                {
                    body.Method.RefreshBody();
                }
            }

            return block;
        }
		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 bool CheckDictionaryIfBody(BlockStatement then)
		{
			/// Check for the pattern
			/// someVariable = new Dictionary<string,int>();
			/// someVariable.Add("SomeString",0);
			/// <moreAdds>
			/// conditionField = someField;
			if (then.Statements.Count < 1)
			{ 
				return false;
			}
			VariableReferenceExpression localDictionaryVariable;
			if (!CheckDictionaryCreation(then.Statements[0], out localDictionaryVariable ))
			{
				return false;
			}
			if (localDictionaryVariable == null)
			{
				// sanity check.
				return false;
			}

			for (int i = 1; i < then.Statements.Count - 1; i++)
			{
				// check push expressions
				if (!CheckDictionaryAdd(then.Statements[i], localDictionaryVariable))
				{
					return false;
				}
			}

			return CheckDictionaryFieldAssignExpression(then.Statements[then.Statements.Count - 1], localDictionaryVariable);
		}
		public BlockStatement Process(DecompilationContext context, BlockStatement body)
		{
			MethodDefinition method = context.MethodContext.Method;
			if (method.Name == "Finalize" && method.IsVirtual)
			{
				if (body.Statements.Count == 1 && body.Statements[0] is TryStatement)
				{
					TryStatement tryFinally = body.Statements[0] as TryStatement;

					if (tryFinally.Finally != null && tryFinally.Finally.Body.Statements.Count == 1 && tryFinally.Finally.Body.Statements[0] is ExpressionStatement)
					{
						ExpressionStatement finallyStatementExpressionStatement = tryFinally.Finally.Body.Statements[0] as ExpressionStatement;

						if (finallyStatementExpressionStatement.Expression is MethodInvocationExpression)
						{
							MethodDefinition baseDestructor = (finallyStatementExpressionStatement.Expression as MethodInvocationExpression).MethodExpression.MethodDefinition;

							if (baseDestructor != null)
							{
								if (baseDestructor.Name == "Finalize" && baseDestructor.DeclaringType.FullName == method.DeclaringType.BaseType.FullName)
								{
									context.MethodContext.IsDestructor = true;
									context.MethodContext.DestructorStatements = new BlockStatement() { Statements = tryFinally.Try.Statements };
								}
							}
						}
					}

				}
			}

			return body;
		}
		public override void VisitBlockStatement (BlockStatement node)
		{
			ProcessBlock (node);

			foreach (var statement in node.Statements)
				Visit (statement);
		}
 public BlockStatement Process(DecompilationContext context, BlockStatement body)
 {
     this.context = context;
     this.typeSystem = context.MethodContext.Method.Module.TypeSystem;
     InsertTopLevelParameterAssignments(body);
     return body;
 }
		public BlockStatement Process(DecompilationContext context, BlockStatement body)
		{
			this.typeSystem = context.MethodContext.Method.Module.TypeSystem;
			this.decompiledMethodReturnType = context.MethodContext.Method.ReturnType;
			Visit(body);
			return body;
		}
Esempio n. 15
0
        public CatchClause(BlockStatement body, TypeReference type, VariableDeclarationExpression variable, Statement filter = null)
        {
            this.Body = body;
            this.Type = type;
            this.Variable = variable;
            this.Filter = filter;
		}
        private BlockStatement body;///This is left to make debugging easier

        public BlockStatement Process(DecompilationContext context, BlockStatement body)
        {
            this.methodContext = context.MethodContext;
            this.body = body;
            RemoveGotoStatements();
            return body;
        }
 public BlockStatement Process(DecompilationContext context, BlockStatement body)
 {
     theRebuilder = new AnonymousDelegateRebuilder(context, body);
     VisitBlockStatement(body);
     theRebuilder.CleanUpVariableCopyAssignments();
     return body;
 }
		public BlockStatement Process(DecompilationContext context, BlockStatement body)
		{		
			MethodDefinition method = context.MethodContext.Method;
			if (method.IsGetter || method.IsSetter)
			{
				PropertyDefinition property;
				if (!context.TypeContext.MethodToPropertyMap.TryGetValue(method, out property))
				{
					throw new Exception("PropertyDefinition not found in method to property map.");
				}

				if (property.ShouldStaySplit())
				{
					string methodDefinitionNewName = Constants.JustDecompileGenerated + "_" + method.Name;
					context.TypeContext.MethodDefinitionToNameMap.Add(method, methodDefinitionNewName);

					FieldDefinition backingField = Utilities.GetCompileGeneratedBackingField(property);
					if (backingField != null)
					{
						string backingFieldNewName = backingField.Name.Replace("<" + property.Name + ">", Constants.JustDecompileGenerated + "_" + property.Name + "_");
						if (!context.TypeContext.BackingFieldToNameMap.ContainsKey(backingField))
						{
							context.TypeContext.BackingFieldToNameMap.Add(backingField, backingFieldNewName);
						}
					}
				}
			}

			return body;
		}
		public BlockStatement Process(DecompilationContext context, BlockStatement block)
		{
			this.context = context;
			fixer = new SwitchByStringFixer(context.MethodContext.Method.Module.TypeSystem);
			Visit(block);
			return block;
		}
		private void InsertTopLevelDeclarations(BlockStatement block)
		{
			int insertIndex = 0;
			if (context.MethodContext.Method.IsConstructor)
			{
				insertIndex = GetIndexOfCtorCall(block) + 1;
			}
			for (int i = 0; i < methodVariables.Count; i++, insertIndex++)
			{
                AssignmentType assignmentType;
                bool hasAssignmentData = this.context.MethodContext.VariableAssignmentData.TryGetValue(methodVariables[i], out assignmentType);
                if (hasAssignmentData && assignmentType == AssignmentType.NotUsed)
                {
                    --insertIndex;
                    continue;
                }

				bool isFirstUsageAssignment;
				if (variableReferences.TryGetValue(methodVariables[i], out isFirstUsageAssignment))
				{
					if (!isFirstUsageAssignment || hasAssignmentData && assignmentType == AssignmentType.NotAssigned)
					{
						InsertVariableDeclarationAndAssignment(block, insertIndex, i);
						continue;
					}
				}
				InsertVariableDeclaration(block, insertIndex, i);
			}
		}
		public BlockStatement Process(DecompilationContext context, BlockStatement body)
		{
			this.context = context;
            this.typeSystem = context.MethodContext.Method.Module.TypeSystem;
            this.patternsContext = new CodePatternsContext(body);
			body = (BlockStatement)Visit(body);
			return body;
		}
		public BlockStatement Process(DecompilationContext context, BlockStatement body)
        {
            this.methodContext = context.MethodContext;

            Visit(body);

            return body;
        }
 public BlockStatement Process(DecompilationContext context, BlockStatement body)
 {
     this.methodContext = context.MethodContext;
     ClearState();
     this.foreachBody = new BlockStatement();
     Visit(body);
     return body;
 }
		public BlockStatement Process(DecompilationContext context, BlockStatement body)
		{
			this.context = context;
			this.theBody = body;
			Visit((ICodeNode)body);
			CleanUpUnusedDeclarations();
			return body;
		}
        public BlockStatement Process(DecompilationContext context, BlockStatement body)
        {
            this.typeSystem = context.MethodContext.Method.Module.TypeSystem;

            Visit(body);
            CleanupRedundantAssignments();
            CleanupEmptyIfs(body);
            return body;
        }
 public BlockStatement Process(DecompilationContext context, BlockStatement body)
 {
     methodContext = context.MethodContext;
     Visit(body);
     body = CallSiteInvocationReplacer.ReplaceInvocations(body, fieldToCallSiteInfoMap, variableToCallSiteInfoMap, statementsToRemove,
         methodContext.Method.Module.TypeSystem);
     RemoveStatements();
     return body;
 }
        public override void VisitBlockStatement(BlockStatement node)
        {
            for (int i = 0; i < node.Statements.Count - 1; i++)
            {
                theRebuilder.Match(node, i);
            }

            base.VisitBlockStatement(node);
        }
            public void Match(BlockStatement theBlock, int index)
            {
                delegatesFound = new List<BlockStatement>();
                statementsToKeep = new HashSet<Statement>();
                delegateCopies = new HashSet<VariableReference>();
                fieldDefToAssignedValueMap = new Dictionary<FieldDefinition, Expression>();

                state = State.DelegateCreate;
                if (!CheckAssignExpression(theBlock.Statements[index] as ExpressionStatement))
                {
                    return;
                }

                delegateCopies.Add(delegateVariableReference);
                variableCopyFinder.FindCopiesOfDelegate(delegateCopies, assignmentsToRemove);

                startIndex = index;
                state = State.FieldAssign;
                for (; ++index < theBlock.Statements.Count;)
                {
                    if (!CheckAssignExpression(theBlock.Statements[index] as ExpressionStatement))
                    {
                        break;
                    }
                }

                if (index == theBlock.Statements.Count)
                {
                    return;
                }

                MapTheRestOfTheFieldsToVariables();

                RemoveFieldAssignments(theBlock.Statements, index);
                CleanUpVariableCopyAssignments();

                state = State.ReplaceFields;
                VisitBlockStatement(methodBodyBlock);

                state = State.ReplaceDelegate;
                for (int i = startIndex; i < theBlock.Statements.Count; i++)
                {
                    theBlock.Statements[i] = (Statement)Visit(theBlock.Statements[i]);

                    state = State.ReplaceFields;
                    foreach (BlockStatement delegateBody in delegatesFound)
                    {
                        VisitBlockStatement(delegateBody);
                    }
                    state = State.ReplaceDelegate;

                    delegatesFound = new List<BlockStatement>();
                }

                SaveClosureToArchive();
            }
 public BlockStatement Process(DecompilationContext context, BlockStatement body)
 {
     this.context = context;
     this.fieldsToRemove = new Dictionary<FieldDefinition, Expression>();
     this.variablesToRemove = new Dictionary<VariableReference, Expression>();
     this.initializationsToRemove = new Dictionary<VariableReference, Statement>();
     BlockStatement result = (BlockStatement)Visit(body);
     RemoveInitializations();
     return result;
 }
		public BlockStatement Process(DecompilationContext context, BlockStatement body)
		{
			this.context = context;
			Visit(body);
            if(this.context.MethodContext.CtorInvokeExpression != null)
            {
                Visit(this.context.MethodContext.CtorInvokeExpression);
            }
			return body;
		}
 public TryStatement(BlockStatement @try, BlockStatement fault, FinallyClause @finally)
 {
     this.Try     = @try;
     this.Fault   = fault;
     this.Finally = @finally;
 }
Esempio n. 32
0
 public ConditionCase(Expression condition, BlockStatement body)
 {
     base(body);
     this.set_Condition(condition);
     return;
 }
 public DoWhileStatement(Expression condition, BlockStatement body)
 {
     base(condition);
     this.set_Body(body);
     return;
 }
Esempio n. 34
0
 public CatchClause(BlockStatement body, TypeReference type, VariableDeclarationExpression variable)
 {
     this.Body     = body;
     this.Type     = type;
     this.Variable = variable;
 }
 public IfElseIfStatement(List <KeyValuePair <Expression, BlockStatement> > conditionBlocks, BlockStatement @else)
 {
     this.ConditionBlocks = conditionBlocks;
     this.Else            = @else;
 }
Esempio n. 36
0
 public DefaultCase(BlockStatement body)
     : base(body)
 {
 }
Esempio n. 37
0
 public DefaultCase(BlockStatement body)
 {
     base(body);
     return;
 }
Esempio n. 38
0
 public SwitchCase(BlockStatement body)
 {
     this.Body = body;
 }
 public FixedStatement(Expression expression, BlockStatement body)
 {
     this.Expression = expression;
     this.Body       = body;
 }
Esempio n. 40
0
 public CatchClause(BlockStatement filter, BlockStatement body)
 {
     this.Filter = filter;
     this.Body   = body;
 }