public TryFaultLogicalConstruct(BlockLogicalConstruct @try, BlockLogicalConstruct fault) 
		{
			InitiExceptionHandlingLogicalConstruct(@try);

			RedirectChildrenToNewParent(new BlockLogicalConstruct[] { fault });
			Fault = fault;
		}
        public TryFaultLogicalConstruct(BlockLogicalConstruct @try, BlockLogicalConstruct fault)
        {
            InitiExceptionHandlingLogicalConstruct(@try);

            RedirectChildrenToNewParent(new BlockLogicalConstruct[] { fault });
            Fault = fault;
        }
Beispiel #3
0
        public ExceptionHandlingBlockFilter(BlockLogicalConstruct filter, BlockLogicalConstruct handler)
        {
            Filter  = filter;
            Handler = handler;

            RedirectChildrenToNewParent(new BlockLogicalConstruct[] { filter, handler });
        }
Beispiel #4
0
		public TryCatchFilterLogicalConstruct(BlockLogicalConstruct try, IFilteringExceptionHandler handler)
		{
			base();
			this.InitiExceptionHandlingLogicalConstruct(try);
			this.handlers = new List<IFilteringExceptionHandler>();
			this.AddHandler(handler);
			return;
		}
        /// <summary>
        /// Swaps the then and else and negates the condition
        /// </summary>
        public void Negate(TypeSystem typeSystem)
        {
            this.Condition.Negate(typeSystem);
            BlockLogicalConstruct temp = this.Then;

            this.Then = this.Else;
            this.Else = temp;
        }
		protected void InitiExceptionHandlingLogicalConstruct(BlockLogicalConstruct try)
		{
			this.set_Try(try);
			stackVariable4 = new BlockLogicalConstruct[1];
			stackVariable4[0] = try;
			this.RedirectChildrenToNewParent((IEnumerable<ILogicalConstruct>)stackVariable4);
			return;
		}
		public TryFinallyLogicalConstruct(BlockLogicalConstruct try, BlockLogicalConstruct finally)
		{
			base();
			this.InitiExceptionHandlingLogicalConstruct(try);
			this.set_Finally(finally);
			stackVariable7 = new BlockLogicalConstruct[1];
			stackVariable7[0] = this.get_Finally();
			this.RedirectChildrenToNewParent((IEnumerable<ILogicalConstruct>)stackVariable7);
			return;
		}
        private IfLogicalConstruct(ConditionLogicalConstruct condition, BlockLogicalConstruct thenBlock, BlockLogicalConstruct elseBlock)
        {
            condition.LogicalContainer = this;

            this.Condition = condition;
            this.Then      = thenBlock;
            this.Else      = elseBlock;

            RedirectChildrenToNewParent(GetIfBody());
        }
Beispiel #9
0
 private IfLogicalConstruct(ConditionLogicalConstruct condition, BlockLogicalConstruct thenBlock, BlockLogicalConstruct elseBlock)
 {
     base();
     condition.set_LogicalContainer(this);
     this.set_Condition(condition);
     this.set_Then(thenBlock);
     this.set_Else(elseBlock);
     this.RedirectChildrenToNewParent(this.GetIfBody());
     return;
 }
		public TryFaultLogicalConstruct(BlockLogicalConstruct try, BlockLogicalConstruct fault)
		{
			base();
			this.InitiExceptionHandlingLogicalConstruct(try);
			stackVariable5 = new BlockLogicalConstruct[1];
			stackVariable5[0] = fault;
			this.RedirectChildrenToNewParent((IEnumerable<ILogicalConstruct>)stackVariable5);
			this.set_Fault(fault);
			return;
		}
        private IfLogicalConstruct(ConditionLogicalConstruct condition, BlockLogicalConstruct thenBlock, BlockLogicalConstruct elseBlock)
        {
			condition.LogicalContainer = this;

            this.Condition = condition;
            this.Then = thenBlock;
            this.Else = elseBlock;

            RedirectChildrenToNewParent(GetIfBody());
        }
Beispiel #12
0
 public ExceptionHandlingBlockFilter(BlockLogicalConstruct filter, BlockLogicalConstruct handler)
 {
     base();
     this.set_Filter(filter);
     this.set_Handler(handler);
     stackVariable7    = new BlockLogicalConstruct[2];
     stackVariable7[0] = filter;
     stackVariable7[1] = handler;
     this.RedirectChildrenToNewParent((IEnumerable <ILogicalConstruct>)stackVariable7);
     return;
 }
        public void FindExceptionHandlingConstructs()
        {
            //The CLR searchese top to bottom through the EH table at the end of the method declaration looking for a CATCH or FILTER handlers
            //which guarded(try) block encompasses the spot where the exception occurred (FAULT and FINALLY handlers are compeltely ignored at this pass,
            //no matter whether the exception falls in their guarded(try) blocks). Assuming that CATCH/FILTER handler was found at row N of the EH table
            // the CLR marks it (but it does NOT execute it at this point) and does a second pass through the EH table searching ONLY rows 1 to N this time.
            //On hte second pass only FAULT/FINALLY handlers are considered. Any FAULT/FILTER handler found which guarded block encompasses the instruction causing
            //the exception gets executed at this pass. Only then the CATCH/FILTER handler at row N gets executed.
            //That means that the order in which handlers appear in the EH table has precedence over their hierarchical structure in the IL (i.e. which try block is inside another try block) when determineing
            //their order of execution. Hence given a FAULT/FINALLY handler that completely contains a CATCH/FILTER handler in the IL but whose EH table entry occurs before
            // the CATCH/FILTER  handler EH table entry in the LogicalConstruct tree the CATCH/FILTER handler will be parent of the FAULT/FINALLY handler (complete reverse of theri relationship in the IL)
            //since the FAULT/FINALLY handler will be executed before the CATCH/FILTER one.


            foreach (ExceptionHandler handler in context.CFG.RawExceptionHandlers)
            {
                BlockRange tryBlockRange = GetBlockRangeFromInstructions(handler.TryStart, handler.TryEnd);

                ExceptionHandlingLogicalConstruct tryBlockExistingHandler = null;
                if (tryBlocksFound.TryGetValue(tryBlockRange, out tryBlockExistingHandler))
                {
                    AddHandlerToTryBlock(tryBlockExistingHandler, handler);
                }
                else
                {
                    BlockLogicalConstruct             theTryBlock  = CreateExceptionhandlingBlock(tryBlockRange);
                    ExceptionHandlingLogicalConstruct guardedBlock = null;

                    switch (handler.HandlerType)
                    {
                    case ExceptionHandlerType.Catch:
                        guardedBlock = new TryCatchFilterLogicalConstruct(theTryBlock, CreateCatchHandler(handler));
                        break;

                    case ExceptionHandlerType.Filter:
                        guardedBlock = new TryCatchFilterLogicalConstruct(theTryBlock, CreateFilterHandler(handler));
                        break;

                    case ExceptionHandlerType.Fault:
                        BlockRange faultBlockRange = GetBlockRangeFromInstructions(handler.HandlerStart, handler.HandlerEnd);
                        guardedBlock = new TryFaultLogicalConstruct(theTryBlock, CreateExceptionhandlingBlock(faultBlockRange));
                        break;

                    case ExceptionHandlerType.Finally:
                        BlockRange finallyBlockRange = GetBlockRangeFromInstructions(handler.HandlerStart, handler.HandlerEnd);
                        guardedBlock = new TryFinallyLogicalConstruct(theTryBlock, CreateExceptionhandlingBlock(finallyBlockRange));
                        break;
                    }

                    tryBlocksFound.Add(tryBlockRange, guardedBlock);
                }
            }
        }
 private ExceptionHandlingBlockFilter CreateFilterHandler(ExceptionHandler handler)
 {
     V_1 = this.GetBlockRangeFromInstructions(handler.get_FilterStart(), handler.get_FilterEnd());
     V_2 = this.GetLogicalConstructsInRange(V_1, out V_0);
     if (this.FindFollowNodes(V_2) != null)
     {
         throw new Exception("The filter block must not have a follow node");
     }
     stackVariable22 = new BlockLogicalConstruct(this.context.get_CFGBlockToLogicalConstructMap().get_Item(V_1.Start)[0], V_2);
     V_3             = this.GetBlockRangeFromInstructions(handler.get_HandlerStart(), handler.get_HandlerEnd());
     V_4             = this.GetLogicalConstructsInRange(V_3, out V_0);
     V_5             = new BlockLogicalConstruct(this.context.get_CFGBlockToLogicalConstructMap().get_Item(V_3.Start)[0], V_4);
     return(new ExceptionHandlingBlockFilter(stackVariable22, V_5));
 }
Beispiel #15
0
 internal MethodSpecificContext(DecompilationAnalysisResults analysisResults, YieldData yieldData, AsyncData asyncData,
                                bool isMethodBodyChanged, Dictionary <string, Statement> gotoLabels, List <GotoStatement> gotoStatements,
                                StackUsageData stackData, bool isBaseConstructorInvokingConstructor, bool enableEventAnalysis,
                                MethodBody body, Collection <VariableDefinition> variables, ControlFlowGraph controlFlowGraph,
                                ExpressionDecompilerData expressions, BlockLogicalConstruct logicalConstructsTree, LogicalFlowBuilderContext logicalConstructsContext,
                                MethodInvocationExpression ctorInvokeExpression, Dictionary <Statement, ILogicalConstruct> statementToLogicalConstruct,
                                Dictionary <ILogicalConstruct, List <Statement> > logicalConstructToStatements, Dictionary <VariableDefinition, string> variableDefinitionToNameMap,
                                HashSet <string> variableNamesCollection, Dictionary <ParameterDefinition, string> parameterDefinitionToNameMap,
                                HashSet <VariableDefinition> variablesToRename, Dictionary <FieldDefinition, Expression> fieldToExpression,
                                int lambdaVariablesCount, Dictionary <VariableDefinition, AssignmentType> variableAssignmentData, List <ParameterDefinition> outParametersToAssign,
                                bool isDestructor, BlockStatement destructorStatements, HashSet <VariableDefinition> undeclaredLinqVariables,
                                Dictionary <VariableReference, Dictionary <FieldDefinition, Expression> > closureVariableToFieldValue,
                                HashSet <VariableDefinition> variablesToNotDeclare, CompilerOptimizedSwitchByStringData switchByStringData)
 {
     this.AnalysisResults     = analysisResults;
     this.YieldData           = yieldData;
     this.AsyncData           = asyncData;
     this.IsMethodBodyChanged = isMethodBodyChanged;
     this.GotoLabels          = gotoLabels;
     this.GotoStatements      = gotoStatements;
     this.StackData           = stackData;
     this.IsBaseConstructorInvokingConstructor = isBaseConstructorInvokingConstructor;
     this.EnableEventAnalysis = enableEventAnalysis;
     this.Body                         = body;
     this.Variables                    = variables;
     this.ControlFlowGraph             = controlFlowGraph;
     this.Expressions                  = expressions;
     this.LogicalConstructsTree        = logicalConstructsTree;
     this.LogicalConstructsContext     = logicalConstructsContext;
     this.CtorInvokeExpression         = ctorInvokeExpression;
     this.StatementToLogicalConstruct  = statementToLogicalConstruct;
     this.LogicalConstructToStatements = logicalConstructToStatements;
     this.VariableDefinitionToNameMap  = variableDefinitionToNameMap;
     this.VariableNamesCollection      = variableNamesCollection;
     this.ParameterDefinitionToNameMap = parameterDefinitionToNameMap;
     this.VariablesToRename            = variablesToRename;
     this.FieldToExpression            = fieldToExpression;
     this.LambdaVariablesCount         = lambdaVariablesCount;
     this.VariableAssignmentData       = variableAssignmentData;
     this.OutParametersToAssign        = outParametersToAssign;
     this.IsDestructor                 = isDestructor;
     this.DestructorStatements         = destructorStatements;
     this.UndeclaredLinqVariables      = undeclaredLinqVariables;
     this.ClosureVariableToFieldValue  = closureVariableToFieldValue;
     this.VariablesToNotDeclare        = variablesToNotDeclare;
     this.SwitchByStringData           = switchByStringData;
 }
        private ExceptionHandlingBlockFilter CreateFilterHandler(ExceptionHandler handler)
        {
            ILogicalConstruct           theCommonParent;
            BlockRange                  filterBlockRange = GetBlockRangeFromInstructions(handler.FilterStart, handler.FilterEnd);
            HashSet <ILogicalConstruct> filterChildren   = GetLogicalConstructsInRange(filterBlockRange, out theCommonParent);

            if (FindFollowNodes(filterChildren) != null)
            {
                throw new Exception("The filter block must not have a follow node");
            }
            BlockLogicalConstruct filterConstruct = new BlockLogicalConstruct(context.CFGBlockToLogicalConstructMap[filterBlockRange.Start][0], filterChildren);

            BlockRange handlerBlockRange = GetBlockRangeFromInstructions(handler.HandlerStart, handler.HandlerEnd);
            HashSet <ILogicalConstruct> handlerChildren  = GetLogicalConstructsInRange(handlerBlockRange, out theCommonParent);
            BlockLogicalConstruct       handlerConstruct = new BlockLogicalConstruct(context.CFGBlockToLogicalConstructMap[handlerBlockRange.Start][0],
                                                                                     handlerChildren);

            return(new ExceptionHandlingBlockFilter(filterConstruct, handlerConstruct));
        }
Beispiel #17
0
 protected ExceptionHandlingLogicalConstruct(BlockLogicalConstruct @try)
 {
     InitiExceptionHandlingLogicalConstruct(@try);
 }
Beispiel #18
0
        /// <summary>
        /// Tries to build an if construct with condition - the specified condition.
        /// </summary>
        /// <remarks>
        /// The idea is to get the dominated nodes of the true successor to create the then block and the dominated nodes of the false successor
        /// to create the else block.
        /// If both the then and else blocks have successors, then they must have a common successor to create the if construct.
        /// </remarks>
        /// <param name="condition"></param>
        /// <returns>True on success.</returns>
        private bool TryBuildIfConstruct(ConditionLogicalConstruct condition, DominatorTree dominatorTree, DFSTree dfsTree)
        {
            //Store the true and false successors for optimization.
            ILogicalConstruct falseSuccessor = condition.FalseSuccessor;
            ILogicalConstruct trueSuccessor  = condition.TrueSuccessor;

            HashSet <ISingleEntrySubGraph> falseSuccessorFrontier = dominatorTree.GetDominanceFrontier(falseSuccessor);
            HashSet <ISingleEntrySubGraph> trueSuccessorFrontier  = dominatorTree.GetDominanceFrontier(trueSuccessor);

            ILogicalConstruct exitSuccessor = CheckSuccessor(condition, trueSuccessor, falseSuccessorFrontier, dfsTree) ??
                                              CheckSuccessor(condition, falseSuccessor, trueSuccessorFrontier, dfsTree);

            HashSet <ISingleEntrySubGraph> frontierIntersection = new HashSet <ISingleEntrySubGraph>(trueSuccessorFrontier);

            frontierIntersection.IntersectWith(falseSuccessorFrontier);

            if (exitSuccessor == null && falseSuccessorFrontier.Count > 0 && trueSuccessorFrontier.Count > 0 && frontierIntersection.Count == 0)
            {
                //If none of the successors can be a proper exit and the false and true successor frontiers are not empty but have no common node,
                //then we do not make the if since it will not have a common exit.
                return(false);
            }

            HashSet <ILogicalConstruct> thenBody = GetBlockBody(dominatorTree, trueSuccessor, condition);
            HashSet <ILogicalConstruct> elseBody = GetBlockBody(dominatorTree, falseSuccessor, condition);

            if (thenBody == null && elseBody == null)
            {
                return(false);
            }
            else if (thenBody == null)
            {
                condition.Negate(typeSystem);

                ILogicalConstruct swapHelper = trueSuccessor;
                trueSuccessor  = falseSuccessor;
                falseSuccessor = swapHelper;

                thenBody = elseBody;
                elseBody = null;
            }

            //If the else body is null but the false successor is not a successor of the then body then we do not make the if.
            if (elseBody == null && !CheckSuccessors(thenBody, falseSuccessor))
            {
                return(false);
            }

            if (ShouldInvertIfAndRemoveElse(thenBody, trueSuccessor, elseBody, falseSuccessor))
            {
                ///This is performed for cosmetic reasons.
                condition.Negate(typeSystem);

                ILogicalConstruct successorSwapHelper = trueSuccessor;
                trueSuccessor  = falseSuccessor;
                falseSuccessor = successorSwapHelper;

                HashSet <ILogicalConstruct> swapHelper = thenBody;
                thenBody = elseBody;
                elseBody = swapHelper;
                elseBody = null;
            }
            if (elseBody != null && !HasSuccessors(thenBody) &&
                SubtreeEndsInInstructionCode(trueSuccessor.FirstBlock.TheBlock, new Code[] { Code.Ret, Code.Throw }))             // check if all ends are throw and/or return -> allow mixed ends as well
            {
                // we don't need the else
                elseBody = null;
            }

            BlockLogicalConstruct theThenBlock = new BlockLogicalConstruct(trueSuccessor, thenBody);
            BlockLogicalConstruct theElseBlock = elseBody != null ? new BlockLogicalConstruct(falseSuccessor, elseBody) : null;

            IfLogicalConstruct theIfConstruct = IfLogicalConstruct.GroupInIfConstruct(condition, theThenBlock, theElseBlock);

            UpdateDominatorTree(dominatorTree, theIfConstruct);
            return(true);
        }
 public TryFinallyLogicalConstruct(BlockLogicalConstruct @try, BlockLogicalConstruct @finally)
 {
     InitiExceptionHandlingLogicalConstruct(@try);
     Finally = @finally;
     RedirectChildrenToNewParent(new BlockLogicalConstruct[] { Finally });
 }
Beispiel #20
0
 private bool TryBuildIfConstruct(ConditionLogicalConstruct condition, DominatorTree dominatorTree, DFSTree dfsTree)
 {
     V_0             = condition.get_FalseSuccessor();
     V_1             = condition.get_TrueSuccessor();
     V_2             = dominatorTree.GetDominanceFrontier(V_0);
     V_3             = dominatorTree.GetDominanceFrontier(V_1);
     stackVariable15 = this.CheckSuccessor(condition, V_1, V_2, dfsTree);
     if (stackVariable15 == null)
     {
         dummyVar0       = stackVariable15;
         stackVariable15 = this.CheckSuccessor(condition, V_0, V_3, dfsTree);
     }
     V_4 = new HashSet <ISingleEntrySubGraph>(V_3);
     V_4.IntersectWith(V_2);
     if (stackVariable15 == null && V_2.get_Count() > 0 && V_3.get_Count() > 0 && V_4.get_Count() == 0)
     {
         return(false);
     }
     V_5 = this.GetBlockBody(dominatorTree, V_1, condition);
     V_6 = this.GetBlockBody(dominatorTree, V_0, condition);
     if (V_5 == null && V_6 == null)
     {
         return(false);
     }
     if (V_5 == null)
     {
         condition.Negate(this.typeSystem);
         stackVariable86 = V_1;
         V_1             = V_0;
         V_0             = stackVariable86;
         V_5             = V_6;
         V_6             = null;
     }
     if (V_6 == null && !this.CheckSuccessors(V_5, V_0))
     {
         return(false);
     }
     if (this.ShouldInvertIfAndRemoveElse(V_5, V_1, V_6, V_0))
     {
         condition.Negate(this.typeSystem);
         stackVariable73 = V_1;
         V_1             = V_0;
         V_0             = stackVariable73;
         stackVariable75 = V_5;
         V_5             = V_6;
         V_6             = stackVariable75;
         V_6             = null;
     }
     if (V_6 != null && !this.HasSuccessors(V_5))
     {
         stackVariable61    = V_1.get_FirstBlock().get_TheBlock();
         stackVariable63    = new Code[2];
         stackVariable63[0] = 41;
         stackVariable63[1] = 119;
         if (this.SubtreeEndsInInstructionCode(stackVariable61, stackVariable63))
         {
             V_6 = null;
         }
     }
     V_7 = new BlockLogicalConstruct(V_1, V_5);
     if (V_6 != null)
     {
         stackVariable46 = new BlockLogicalConstruct(V_0, V_6);
     }
     else
     {
         stackVariable46 = null;
     }
     this.UpdateDominatorTree(dominatorTree, IfLogicalConstruct.GroupInIfConstruct(condition, V_7, stackVariable46));
     return(true);
 }
 /// <summary>
 /// Creates a new IfLogicalConstruct and adds it to the logical tree.
 /// </summary>
 /// <param name="condition">The condition of the if.</param>
 /// <param name="theThenBlock">The then block of the if.</param>
 /// <param name="theElseBlock">The else block of the if.</param>
 /// <returns>The created if construct.</returns>
 public static IfLogicalConstruct GroupInIfConstruct(ConditionLogicalConstruct condition,
                                                     BlockLogicalConstruct theThenBlock, BlockLogicalConstruct theElseBlock)
 {
     return(new IfLogicalConstruct(condition, theThenBlock, theElseBlock));
 }
		public TryFinallyLogicalConstruct(BlockLogicalConstruct @try, BlockLogicalConstruct @finally)
		{
            InitiExceptionHandlingLogicalConstruct(@try);
			Finally = @finally;
			RedirectChildrenToNewParent(new BlockLogicalConstruct[] { Finally });
		}
Beispiel #23
0
 internal MethodSpecificContext(DecompilationAnalysisResults analysisResults, Telerik.JustDecompiler.Decompiler.YieldData yieldData, Telerik.JustDecompiler.Decompiler.AsyncData asyncData, bool isMethodBodyChanged, Dictionary <string, Statement> gotoLabels, List <GotoStatement> gotoStatements, StackUsageData stackData, bool isBaseConstructorInvokingConstructor, bool enableEventAnalysis, MethodBody body, Collection <VariableDefinition> variables, Telerik.JustDecompiler.Cil.ControlFlowGraph controlFlowGraph, ExpressionDecompilerData expressions, BlockLogicalConstruct logicalConstructsTree, LogicalFlowBuilderContext logicalConstructsContext, MethodInvocationExpression ctorInvokeExpression, Dictionary <Statement, ILogicalConstruct> statementToLogicalConstruct, Dictionary <ILogicalConstruct, List <Statement> > logicalConstructToStatements, Dictionary <VariableDefinition, string> variableDefinitionToNameMap, HashSet <string> variableNamesCollection, Dictionary <ParameterDefinition, string> parameterDefinitionToNameMap, HashSet <VariableDefinition> variablesToRename, Dictionary <FieldDefinition, Expression> fieldToExpression, int lambdaVariablesCount, Dictionary <VariableDefinition, AssignmentType> variableAssignmentData, List <ParameterDefinition> outParametersToAssign, bool isDestructor, BlockStatement destructorStatements, HashSet <VariableDefinition> undeclaredLinqVariables, Dictionary <VariableReference, Dictionary <FieldDefinition, Expression> > closureVariableToFieldValue, HashSet <VariableDefinition> variablesToNotDeclare, CompilerOptimizedSwitchByStringData switchByStringData)
 {
     base();
     this.set_AnalysisResults(analysisResults);
     this.set_YieldData(yieldData);
     this.set_AsyncData(asyncData);
     this.set_IsMethodBodyChanged(isMethodBodyChanged);
     this.set_GotoLabels(gotoLabels);
     this.set_GotoStatements(gotoStatements);
     this.set_StackData(stackData);
     this.set_IsBaseConstructorInvokingConstructor(isBaseConstructorInvokingConstructor);
     this.set_EnableEventAnalysis(enableEventAnalysis);
     this.set_Body(body);
     this.set_Variables(variables);
     this.set_ControlFlowGraph(controlFlowGraph);
     this.set_Expressions(expressions);
     this.set_LogicalConstructsTree(logicalConstructsTree);
     this.set_LogicalConstructsContext(logicalConstructsContext);
     this.set_CtorInvokeExpression(ctorInvokeExpression);
     this.set_StatementToLogicalConstruct(statementToLogicalConstruct);
     this.set_LogicalConstructToStatements(logicalConstructToStatements);
     this.set_VariableDefinitionToNameMap(variableDefinitionToNameMap);
     this.set_VariableNamesCollection(variableNamesCollection);
     this.set_ParameterDefinitionToNameMap(parameterDefinitionToNameMap);
     this.set_VariablesToRename(variablesToRename);
     this.set_FieldToExpression(fieldToExpression);
     this.set_LambdaVariablesCount(lambdaVariablesCount);
     this.set_VariableAssignmentData(variableAssignmentData);
     this.set_OutParametersToAssign(outParametersToAssign);
     this.set_IsDestructor(isDestructor);
     this.set_DestructorStatements(destructorStatements);
     this.set_UndeclaredLinqVariables(undeclaredLinqVariables);
     this.set_ClosureVariableToFieldValue(closureVariableToFieldValue);
     this.set_VariablesToNotDeclare(variablesToNotDeclare);
     this.set_SwitchByStringData(switchByStringData);
     return;
 }
		private ExceptionHandlingBlockFilter CreateFilterHandler(ExceptionHandler handler)
		{
			ILogicalConstruct theCommonParent;
            BlockRange filterBlockRange = GetBlockRangeFromInstructions(handler.FilterStart, handler.FilterEnd);
            HashSet<ILogicalConstruct> filterChildren = GetLogicalConstructsInRange(filterBlockRange, out theCommonParent);
            if(FindFollowNodes(filterChildren) != null)
            {
                throw new Exception("The filter block must not have a follow node");
            }
			BlockLogicalConstruct filterConstruct = new BlockLogicalConstruct(context.CFGBlockToLogicalConstructMap[filterBlockRange.Start][0], filterChildren);

            BlockRange handlerBlockRange = GetBlockRangeFromInstructions(handler.HandlerStart, handler.HandlerEnd);
            HashSet<ILogicalConstruct> handlerChildren = GetLogicalConstructsInRange(handlerBlockRange, out theCommonParent);
			BlockLogicalConstruct handlerConstruct = new BlockLogicalConstruct(context.CFGBlockToLogicalConstructMap[handlerBlockRange.Start][0],
																			   handlerChildren);

			return new ExceptionHandlingBlockFilter(filterConstruct, handlerConstruct);
		}
		public TryCatchFilterLogicalConstruct(BlockLogicalConstruct @try, IFilteringExceptionHandler handler)
		{
            InitiExceptionHandlingLogicalConstruct(@try);
			handlers = new List<IFilteringExceptionHandler>();
			AddHandler(handler);
		}
        /// <summary>
        /// Tries to build an if construct with condition - the specified condition.
        /// </summary>
        /// <remarks>
        /// The idea is to get the dominated nodes of the true successor to create the then block and the dominated nodes of the false successor
        /// to create the else block.
        /// If both the then and else blocks have successors, then they must have a common successor to create the if construct.
        /// </remarks>
        /// <param name="condition"></param>
        /// <returns>True on success.</returns>
		private bool TryBuildIfConstruct(ConditionLogicalConstruct condition, DominatorTree dominatorTree, DFSTree dfsTree)
		{
            //Store the true and false successors for optimization.
            ILogicalConstruct falseSuccessor = condition.FalseSuccessor;
            ILogicalConstruct trueSuccessor = condition.TrueSuccessor;

            HashSet<ISingleEntrySubGraph> falseSuccessorFrontier = dominatorTree.GetDominanceFrontier(falseSuccessor);
            HashSet<ISingleEntrySubGraph> trueSuccessorFrontier = dominatorTree.GetDominanceFrontier(trueSuccessor);

            ILogicalConstruct exitSuccessor = CheckSuccessor(condition, trueSuccessor, falseSuccessorFrontier, dfsTree) ??
                CheckSuccessor(condition, falseSuccessor, trueSuccessorFrontier, dfsTree);

            HashSet<ISingleEntrySubGraph> frontierIntersection = new HashSet<ISingleEntrySubGraph>(trueSuccessorFrontier);
            frontierIntersection.IntersectWith(falseSuccessorFrontier);

            if (exitSuccessor == null && falseSuccessorFrontier.Count > 0 && trueSuccessorFrontier.Count > 0 && frontierIntersection.Count == 0)
            {
                //If none of the successors can be a proper exit and the false and true successor frontiers are not empty but have no common node,
                //then we do not make the if since it will not have a common exit.
                return false;
            }

            HashSet<ILogicalConstruct> thenBody = GetBlockBody(dominatorTree, trueSuccessor, condition);
            HashSet<ILogicalConstruct> elseBody = GetBlockBody(dominatorTree, falseSuccessor, condition);

			if (thenBody == null && elseBody == null)
            {
                return false;
            }
            else if(thenBody == null)
            {
                condition.Negate(typeSystem);

                ILogicalConstruct swapHelper = trueSuccessor;
                trueSuccessor = falseSuccessor;
                falseSuccessor = swapHelper;

                thenBody = elseBody;
                elseBody = null;
            }

            //If the else body is null but the false successor is not a successor of the then body then we do not make the if.
            if(elseBody == null && !CheckSuccessors(thenBody, falseSuccessor))
            {
                return false;
            }

			if (ShouldInvertIfAndRemoveElse(thenBody, trueSuccessor, elseBody, falseSuccessor))
			{
				///This is performed for cosmetic reasons.
				condition.Negate(typeSystem);

				ILogicalConstruct successorSwapHelper = trueSuccessor;
				trueSuccessor = falseSuccessor;
				falseSuccessor = successorSwapHelper;

				HashSet<ILogicalConstruct> swapHelper = thenBody;
				thenBody = elseBody;
				elseBody = swapHelper;
				elseBody = null;
			}
			if (elseBody != null && !HasSuccessors(thenBody) && 
				SubtreeEndsInInstructionCode(trueSuccessor.FirstBlock.TheBlock,new Code[]{Code.Ret, Code.Throw})) // check if all ends are throw and/or return -> allow mixed ends as well
			{
				// we don't need the else
				elseBody = null;
			}

            BlockLogicalConstruct theThenBlock = new BlockLogicalConstruct(trueSuccessor, thenBody);
            BlockLogicalConstruct theElseBlock = elseBody != null ? new BlockLogicalConstruct(falseSuccessor, elseBody) : null;

			IfLogicalConstruct theIfConstruct = IfLogicalConstruct.GroupInIfConstruct(condition, theThenBlock, theElseBlock);
            UpdateDominatorTree(dominatorTree, theIfConstruct);
            return true;
		}
		protected ExceptionHandlingLogicalConstruct(BlockLogicalConstruct @try)
		{
			InitiExceptionHandlingLogicalConstruct(@try);
		}
		protected ExceptionHandlingLogicalConstruct(BlockLogicalConstruct try)
		{
			base();
			this.InitiExceptionHandlingLogicalConstruct(try);
			return;
		}
Beispiel #29
0
 protected void InitiExceptionHandlingLogicalConstruct(BlockLogicalConstruct @try)
 {
     Try = @try;
     RedirectChildrenToNewParent(new BlockLogicalConstruct[] { @try });
 }
		protected void InitiExceptionHandlingLogicalConstruct(BlockLogicalConstruct @try)
		{
			Try = @try;
			RedirectChildrenToNewParent(new BlockLogicalConstruct[] { @try });
		}
 public TryCatchFilterLogicalConstruct(BlockLogicalConstruct @try, IFilteringExceptionHandler handler)
 {
     InitiExceptionHandlingLogicalConstruct(@try);
     handlers = new List <IFilteringExceptionHandler>();
     AddHandler(handler);
 }
        /// <summary>
        /// Creates a new IfLogicalConstruct and adds it to the logical tree.
        /// </summary>
        /// <param name="condition">The condition of the if.</param>
        /// <param name="theThenBlock">The then block of the if.</param>
        /// <param name="theElseBlock">The else block of the if.</param>
        /// <returns>The created if construct.</returns>
		public static IfLogicalConstruct GroupInIfConstruct(ConditionLogicalConstruct condition,
            BlockLogicalConstruct theThenBlock, BlockLogicalConstruct theElseBlock)
		{
			return new IfLogicalConstruct(condition, theThenBlock, theElseBlock);
		}