示例#1
0
        private void FlattenStatement()
        {
            LinkedList <_T2081736913> lstStackStatements = new LinkedList <_T2081736913>();

            lstStackStatements.AddLast(new _T2081736913(this, root, new LinkedList <FlattenStatementsHelper.StackEntry
                                                                                    >(), null));
            while (!(lstStackStatements.Count == 0))
            {
                _T2081736913 statEntry = Sharpen.Collections.RemoveFirst(lstStackStatements);
                Statement    stat      = statEntry.statement;
                LinkedList <FlattenStatementsHelper.StackEntry> stackFinally = statEntry.stackFinally;
                int             statementBreakIndex = statEntry.statementIndex;
                DirectNode      node;
                DirectNode      nd;
                List <StatEdge> lstSuccEdges = new List <StatEdge>();
                DirectNode      sourcenode   = null;
                if (statEntry.succEdges == null)
                {
                    switch (stat.type)
                    {
                    case Statement.Type_Basicblock:
                    {
                        node = new DirectNode(DirectNode.Node_Direct, stat, (BasicBlockStatement)stat);
                        if (stat.GetExprents() != null)
                        {
                            node.exprents = stat.GetExprents();
                        }
                        graph.nodes.PutWithKey(node, node.id);
                        Sharpen.Collections.Put(mapDestinationNodes, stat.id, new string[] { node.id, null });
                        Sharpen.Collections.AddAll(lstSuccEdges, stat.GetSuccessorEdges(Statement.Statedge_Direct_All
                                                                                        ));
                        sourcenode = node;
                        List <Exprent> tailExprentList = statEntry.tailExprents;
                        if (tailExprentList != null)
                        {
                            DirectNode tail = new DirectNode(DirectNode.Node_Tail, stat, stat.id + "_tail");
                            tail.exprents = tailExprentList;
                            graph.nodes.PutWithKey(tail, tail.id);
                            Sharpen.Collections.Put(mapDestinationNodes, -stat.id, new string[] { tail.id, null });
                            listEdges.Add(new FlattenStatementsHelper.Edge(node.id, -stat.id, StatEdge.Type_Regular
                                                                           ));
                            sourcenode = tail;
                        }
                        // 'if' statement: record positive branch
                        if (stat.GetLastBasicType() == Statement.Lastbasictype_If)
                        {
                            Sharpen.Collections.Put(mapPosIfBranch, sourcenode.id, lstSuccEdges[0].GetDestination
                                                        ().id);
                        }
                        break;
                    }

                    case Statement.Type_Catchall:
                    case Statement.Type_Trycatch:
                    {
                        DirectNode firstnd = new DirectNode(DirectNode.Node_Try, stat, stat.id + "_try");
                        Sharpen.Collections.Put(mapDestinationNodes, stat.id, new string[] { firstnd.id,
                                                                                             null });
                        graph.nodes.PutWithKey(firstnd, firstnd.id);
                        LinkedList <_T2081736913> lst = new LinkedList <_T2081736913>();
                        foreach (Statement st in stat.GetStats())
                        {
                            listEdges.Add(new FlattenStatementsHelper.Edge(firstnd.id, st.id, StatEdge.Type_Regular
                                                                           ));
                            LinkedList <FlattenStatementsHelper.StackEntry> stack = stackFinally;
                            if (stat.type == Statement.Type_Catchall && ((CatchAllStatement)stat).IsFinally())
                            {
                                stack = new LinkedList <FlattenStatementsHelper.StackEntry>(stackFinally);
                                if (st == stat.GetFirst())
                                {
                                    // catch head
                                    stack.AddLast(new FlattenStatementsHelper.StackEntry((CatchAllStatement)stat, false));
                                }
                                else
                                {
                                    // handler
                                    stack.AddLast(new FlattenStatementsHelper.StackEntry((CatchAllStatement)stat, true, StatEdge
                                                                                         .Type_Break, root.GetDummyExit(), st, st, firstnd, firstnd, true));
                                }
                            }
                            lst.AddLast(new _T2081736913(this, st, stack, null));
                        }
                        lstStackStatements = new LinkedList <_T2081736913>(lst.Concat(lstStackStatements));
                        break;
                    }

                    case Statement.Type_Do:
                    {
                        if (statementBreakIndex == 0)
                        {
                            statEntry.statementIndex = 1;
                            lstStackStatements.AddFirst(statEntry);
                            lstStackStatements.AddFirst(new _T2081736913(this, stat.GetFirst(), stackFinally,
                                                                         null));
                            goto mainloop_continue;
                        }
                        nd = graph.nodes.GetWithKey(mapDestinationNodes.GetOrNull(stat.GetFirst().id)[0]);
                        DoStatement dostat   = (DoStatement)stat;
                        int         looptype = dostat.GetLooptype();
                        if (looptype == DoStatement.Loop_Do)
                        {
                            Sharpen.Collections.Put(mapDestinationNodes, stat.id, new string[] { nd.id, nd.id });
                            break;
                        }
                        lstSuccEdges.Add(stat.GetSuccessorEdges(Statement.Statedge_Direct_All)[0]);
                        switch (looptype)
                        {
                        case DoStatement.Loop_While:
                        case DoStatement.Loop_Dowhile:
                        {
                            // exactly one edge
                            node          = new DirectNode(DirectNode.Node_Condition, stat, stat.id + "_cond");
                            node.exprents = dostat.GetConditionExprentList();
                            graph.nodes.PutWithKey(node, node.id);
                            listEdges.Add(new FlattenStatementsHelper.Edge(node.id, stat.GetFirst().id, StatEdge
                                                                           .Type_Regular));
                            if (looptype == DoStatement.Loop_While)
                            {
                                Sharpen.Collections.Put(mapDestinationNodes, stat.id, new string[] { node.id, node
                                                                                                     .id });
                            }
                            else
                            {
                                Sharpen.Collections.Put(mapDestinationNodes, stat.id, new string[] { nd.id, node.
                                                                                                     id });
                                bool found = false;
                                foreach (FlattenStatementsHelper.Edge edge in listEdges)
                                {
                                    if (edge.statid.Equals(stat.id) && edge.edgetype == StatEdge.Type_Continue)
                                    {
                                        found = true;
                                        break;
                                    }
                                }
                                if (!found)
                                {
                                    listEdges.Add(new FlattenStatementsHelper.Edge(nd.id, stat.id, StatEdge.Type_Continue
                                                                                   ));
                                }
                            }
                            sourcenode = node;
                            break;
                        }

                        case DoStatement.Loop_For:
                        {
                            DirectNode nodeinit = new DirectNode(DirectNode.Node_Init, stat, stat.id + "_init"
                                                                 );
                            if (dostat.GetInitExprent() != null)
                            {
                                nodeinit.exprents = dostat.GetInitExprentList();
                            }
                            graph.nodes.PutWithKey(nodeinit, nodeinit.id);
                            DirectNode nodecond = new DirectNode(DirectNode.Node_Condition, stat, stat.id + "_cond"
                                                                 );
                            nodecond.exprents = dostat.GetConditionExprentList();
                            graph.nodes.PutWithKey(nodecond, nodecond.id);
                            DirectNode nodeinc = new DirectNode(DirectNode.Node_Increment, stat, stat.id + "_inc"
                                                                );
                            nodeinc.exprents = dostat.GetIncExprentList();
                            graph.nodes.PutWithKey(nodeinc, nodeinc.id);
                            Sharpen.Collections.Put(mapDestinationNodes, stat.id, new string[] { nodeinit.id,
                                                                                                 nodeinc.id });
                            Sharpen.Collections.Put(mapDestinationNodes, -stat.id, new string[] { nodecond.id
                                                                                                  , null });
                            listEdges.Add(new FlattenStatementsHelper.Edge(nodecond.id, stat.GetFirst().id, StatEdge
                                                                           .Type_Regular));
                            listEdges.Add(new FlattenStatementsHelper.Edge(nodeinit.id, -stat.id, StatEdge.Type_Regular
                                                                           ));
                            listEdges.Add(new FlattenStatementsHelper.Edge(nodeinc.id, -stat.id, StatEdge.Type_Regular
                                                                           ));
                            bool found_1 = false;
                            foreach (FlattenStatementsHelper.Edge edge in listEdges)
                            {
                                if (edge.statid.Equals(stat.id) && edge.edgetype == StatEdge.Type_Continue)
                                {
                                    found_1 = true;
                                    break;
                                }
                            }
                            if (!found_1)
                            {
                                listEdges.Add(new FlattenStatementsHelper.Edge(nd.id, stat.id, StatEdge.Type_Continue
                                                                               ));
                            }
                            sourcenode = nodecond;
                            break;
                        }
                        }
                        break;
                    }

                    case Statement.Type_Syncronized:
                    case Statement.Type_Switch:
                    case Statement.Type_If:
                    case Statement.Type_Sequence:
                    case Statement.Type_Root:
                    {
                        int statsize = stat.GetStats().Count;
                        if (stat.type == Statement.Type_Syncronized)
                        {
                            statsize = 2;
                        }
                        // exclude the handler if synchronized
                        if (statementBreakIndex <= statsize)
                        {
                            List <Exprent> tailexprlst = null;
                            switch (stat.type)
                            {
                            case Statement.Type_Syncronized:
                            {
                                tailexprlst = ((SynchronizedStatement)stat).GetHeadexprentList();
                                break;
                            }

                            case Statement.Type_Switch:
                            {
                                tailexprlst = ((SwitchStatement)stat).GetHeadexprentList();
                                break;
                            }

                            case Statement.Type_If:
                            {
                                tailexprlst = ((IfStatement)stat).GetHeadexprentList();
                                break;
                            }
                            }
                            for (int i = statementBreakIndex; i < statsize; i++)
                            {
                                statEntry.statementIndex = i + 1;
                                lstStackStatements.AddFirst(statEntry);
                                lstStackStatements.AddFirst(new _T2081736913(this, stat.GetStats()[i], stackFinally
                                                                             , (i == 0 && tailexprlst != null && tailexprlst[0] != null) ? tailexprlst : null
                                                                             ));
                                goto mainloop_continue;
                            }
                            node = graph.nodes.GetWithKey(mapDestinationNodes.GetOrNull(stat.GetFirst().id)[0
                                                          ]);
                            Sharpen.Collections.Put(mapDestinationNodes, stat.id, new string[] { node.id, null });
                            if (stat.type == Statement.Type_If && ((IfStatement)stat).iftype == IfStatement.Iftype_If)
                            {
                                lstSuccEdges.Add(stat.GetSuccessorEdges(Statement.Statedge_Direct_All)[0]);
                                // exactly one edge
                                sourcenode = tailexprlst[0] == null ? node : graph.nodes.GetWithKey(node.id + "_tail"
                                                                                                    );
                            }
                        }
                        break;
                    }
                    }
                }
                // no successor edges
                if (sourcenode != null)
                {
                    if (statEntry.succEdges != null)
                    {
                        lstSuccEdges = statEntry.succEdges;
                    }
                    for (int edgeindex = statEntry.edgeIndex; edgeindex < lstSuccEdges.Count; edgeindex
                         ++)
                    {
                        StatEdge edge = lstSuccEdges[edgeindex];
                        LinkedList <FlattenStatementsHelper.StackEntry> stack = new LinkedList <FlattenStatementsHelper.StackEntry
                                                                                                >(stackFinally);
                        int        edgetype                      = edge.GetType();
                        Statement  destination                   = edge.GetDestination();
                        DirectNode finallyShortRangeSource       = sourcenode;
                        DirectNode finallyLongRangeSource        = sourcenode;
                        Statement  finallyShortRangeEntry        = null;
                        Statement  finallyLongRangeEntry         = null;
                        bool       isFinallyMonitorExceptionPath = false;
                        bool       isFinallyExit                 = false;
                        while (true)
                        {
                            FlattenStatementsHelper.StackEntry entry = null;
                            if (!(stack.Count == 0))
                            {
                                entry = stack.Last();
                            }
                            bool created = true;
                            if (entry == null)
                            {
                                SaveEdge(sourcenode, destination, edgetype, isFinallyExit ? finallyShortRangeSource
                                                                         : null, finallyLongRangeSource, finallyShortRangeEntry, finallyLongRangeEntry,
                                         isFinallyMonitorExceptionPath);
                            }
                            else
                            {
                                CatchAllStatement catchall = entry.catchstatement;
                                if (entry.state)
                                {
                                    // finally handler statement
                                    if (edgetype == StatEdge.Type_Finallyexit)
                                    {
                                        Sharpen.Collections.RemoveLast(stack);
                                        destination                   = entry.destination;
                                        edgetype                      = entry.edgetype;
                                        finallyShortRangeSource       = entry.finallyShortRangeSource;
                                        finallyLongRangeSource        = entry.finallyLongRangeSource;
                                        finallyShortRangeEntry        = entry.finallyShortRangeEntry;
                                        finallyLongRangeEntry         = entry.finallyLongRangeEntry;
                                        isFinallyExit                 = true;
                                        isFinallyMonitorExceptionPath = (catchall.GetMonitor() != null) & entry.isFinallyExceptionPath;
                                        created = false;
                                    }
                                    else if (!catchall.ContainsStatementStrict(destination))
                                    {
                                        Sharpen.Collections.RemoveLast(stack);
                                        created = false;
                                    }
                                    else
                                    {
                                        SaveEdge(sourcenode, destination, edgetype, isFinallyExit ? finallyShortRangeSource
                                                                                         : null, finallyLongRangeSource, finallyShortRangeEntry, finallyLongRangeEntry,
                                                 isFinallyMonitorExceptionPath);
                                    }
                                }
                                else if (!catchall.ContainsStatementStrict(destination))
                                {
                                    // finally protected try statement
                                    SaveEdge(sourcenode, catchall.GetHandler(), StatEdge.Type_Regular, isFinallyExit ?
                                             finallyShortRangeSource : null, finallyLongRangeSource, finallyShortRangeEntry,
                                             finallyLongRangeEntry, isFinallyMonitorExceptionPath);
                                    Sharpen.Collections.RemoveLast(stack);
                                    stack.AddLast(new FlattenStatementsHelper.StackEntry(catchall, true, edgetype, destination
                                                                                         , catchall.GetHandler(), finallyLongRangeEntry == null ? catchall.GetHandler() :
                                                                                         finallyLongRangeEntry, sourcenode, finallyLongRangeSource, false));
                                    statEntry.edgeIndex = edgeindex + 1;
                                    statEntry.succEdges = lstSuccEdges;
                                    lstStackStatements.AddFirst(statEntry);
                                    lstStackStatements.AddFirst(new _T2081736913(this, catchall.GetHandler(), stack,
                                                                                 null));
                                    goto mainloop_continue;
                                }
                                else
                                {
                                    SaveEdge(sourcenode, destination, edgetype, isFinallyExit ? finallyShortRangeSource
                                                                                 : null, finallyLongRangeSource, finallyShortRangeEntry, finallyLongRangeEntry,
                                             isFinallyMonitorExceptionPath);
                                }
                            }
                            if (created)
                            {
                                break;
                            }
                        }
                    }
                }
                mainloop_continue :;
            }
            mainloop_break :;
        }