Пример #1
0
    void AST_t::tree_iterator(const AST_t& a, const TraverseASTFunctor& functor, 
            ObjectList<AST_t>& result)
    {
        AST tree = a._ast;
        if (tree == NULL)
            return;

        ASTTraversalResult current_node = functor(a);
        if (current_node.matches())
        {
            result.push_back(a);
        }

        if (current_node.recurse())
        {
            for (int i = 0; i < ASTNumChildren(tree); i++)
            {
                if (ASTChild(tree, i) != NULL)
                {
                    AST_t iterate(ASTChild(tree, i));
                    tree_iterator(iterate, functor, result);
                }
            }
        }
    }
Пример #2
0
    void AST_t::remove_in_list()
    {
        AST list = this->_ast;
        // Look for the enclosing list
        while (list != NULL &&
                ASTType(list) != AST_NODE_LIST)
        {
            list = ASTParent(list);
        }

        if (list == NULL)
        {
            std::cerr << "A suitable list has not been found" << std::endl;
            return;
        }

        AST parent = ASTParent(list);
        AST previous = ASTSon0(list);

        if (previous != NULL)
        {
            ast_set_parent(previous, parent);
        }

        int i;
        for (i = 0; i < ASTNumChildren(parent); i++)
        {
            if (ASTChild(parent, i) == list)
            {
                ast_set_child(parent, i, previous);
                break;
            }
        }
    }
Пример #3
0
    void AST_t::replace_with(AST_t ast)
    {
        if (ast._ast == NULL)
        {
            internal_error("Trying to replace a tree with an empty tree.", 0);
        }

        if (this->_ast == NULL)
        {
            internal_error("Trying to replace an empty tree with another tree", 0);
        }

        AST previous_parent = ASTParent(this->_ast);
        ast_replace(this->_ast, ast._ast);
        ast_set_parent(this->_ast, previous_parent);

        // Relink sons
        for (int i = 0; i < ASTNumChildren(this->_ast); i++)
        {
            if (ASTChild(this->_ast, i) != NULL)
            {
                ast_set_parent(ASTChild(this->_ast, i), this->_ast);
            }
        }
    }
Пример #4
0
    void AST_t::relink_parent(AST previous_child, AST new_child)
    {
        AST parent = ASTParent(previous_child);
        int i;
        for (i = 0; i < ASTNumChildren(parent); i++)
        {
            if (ASTChild(parent, i) == previous_child)
                break;
        }
        if (i == ASTNumChildren(parent))
        {
            std::cerr << "Claimed parent does not have the node as a child" << std::endl;
            return;
        }

        AST parent_new_child = ASTParent(new_child);
        if (parent_new_child != NULL)
        {
            int j;
            for (j = 0; j < ASTNumChildren(parent_new_child); j++)
            {
                if (ASTChild(parent_new_child, j) == new_child)
                    break;
            }
            if (j == ASTNumChildren(parent))
            {
                std::cerr << "Claimed parent of new child does not have the node as a child" << std::endl;
                return;
            }

            // Disable for sanity this son now
            ast_set_child(parent, j, NULL);
        }

        // Relink
        ast_set_child(parent, i, new_child);
    }
Пример #5
0
    // XXX - Fixme, implement it using ast_list_concat
    void AST_t::prepend_list(AST orig_list, AST prepended_list)
    {
        if (ASTType(orig_list) != AST_NODE_LIST
                || ASTType(prepended_list) != AST_NODE_LIST)
        {
            std::cerr << "You tried to prepend two lists that are not " 
                << "orig_list=" << ast_print_node_type(ASTType(orig_list)) << " "
                << "prepend_list=" << ast_print_node_type(ASTType(prepended_list)) << std::endl;
            return;
        }

        // Relink the parent, first remove pointer to the prepended_list
        if (ASTParent(prepended_list) != NULL)
        {
            AST parent = ASTParent(prepended_list);
            for (int i = 0; i < ASTNumChildren(parent); i++)
            {
                if (ASTChild(parent, i) == prepended_list)
                {
                    ast_set_child(parent, i, NULL);
                    break;
                }
            }
        }

        // Now make the prepended_list as the son
        AST original_previous = ASTSon0(orig_list);

        ast_set_child(orig_list, 0, prepended_list);

        // Go to the deeper node of prepended_list
        AST iter = prepended_list;
        while (ASTSon0(iter) != NULL)
        {
            iter = ASTSon0(iter);
        }

        ast_set_child(iter, 0, original_previous);
    }
Пример #6
0
    void DepthTraverse::traverse(TL::AST_t node, ScopeLink scope_link)
    {
        bool recurse = true;

        if (!node.is_valid())
            return;

        std::vector<TraverseFunctor*> matching_functors;

        for (std::vector<CondAction>::iterator it = _pred_list.begin();
                it != _pred_list.end();
                it++)
        {
            TraverseASTFunctor& pred = *(it->first);
            ASTTraversalResult result = pred(node);

            bool match = result.matches();

            // Only recurse if no matching functor objected to recursion
            if (match)
            {
                recurse &= result.recurse();
                matching_functors.push_back(it->second);
                break;
            }
        }

        // Create context
        Context* ctx = new Context(scope_link);

        // Run all matching preorders
        for (std::vector<TraverseFunctor*>::iterator it = matching_functors.begin();
                it != matching_functors.end();
                it++)
        {
            (*it)->preorder(*ctx, node);
        }

        if (recurse)
        {
            AST ast = node._ast;
            for (int i = 0; i < ASTNumChildren(ast); i++)
            {
                AST child = ASTChild(ast, i);

                if (child != NULL)
                {
                    AST_t w_child(child);
                    traverse(w_child, scope_link);
                }
            }
        }

        // Run all matching postorders
        for (std::vector<TraverseFunctor*>::iterator it = matching_functors.begin();
                it != matching_functors.end();
                it++)
        {
            (*it)->postorder(*ctx, node);
        }

        // Delete it
        delete ctx;
    }
Пример #7
0
static void ast_dump_graphviz_rec(AST a, FILE* f, int parent_node, int position)
{
    // static char* octagon = "octagon";
    // static char* doubleoctagon = "doubleoctagon";
    static char* ellipse = "ellipse";
    static char* mdiamond = "Mdiamond";
    static char* box = "box";
    char* shape;

    int node_actual = nodes_counter++;
    if (a != NULL)
    {
        // Select shape
        shape = box;
        if (ASTType(a) == AST_AMBIGUITY) shape = ellipse;
        if (ASTType(a) == AST_NODE_LIST) shape = mdiamond;
        // if (a->construct_type == CT_SPECIFICATION) shape = ellipse;
        // else if (a->construct_type == CT_OMP_SPECIFICATION) shape = mdiamond;
        // else if (a->construct_type == CT_EXECUTABLE) shape = octagon;
        // else if (a->construct_type == CT_OMP_EXECUTABLE) shape = doubleoctagon;

        if (ASTText(a))
        {
            fprintf(f, "n%d[shape=%s,label=\"%s\\nNode=%p\\nParent=%p\\n%s\\nText: -%s-\"]\n", 
                    node_actual, shape, ast_print_node_type(ASTType(a)), a, ASTParent(a), ast_location(a), ASTText(a));
        }
        else
        {
            fprintf(f, "n%d[shape=%s,label=\"%s\\nNode=%p\\nParent=%p\\n%s\"]\n", 
                    node_actual, shape, ast_print_node_type(ASTType(a)), a, ASTParent(a), ast_location(a));
        }

        if (parent_node != 0)
        {
            fprintf(f, "n%d -> n%d [label=\"%d\"]\n", parent_node, node_actual, position);
        }


        if (ASTType(a) != AST_AMBIGUITY)
        {
            int i;
            for(i = 0; i < ASTNumChildren(a); i++)
            {
                ast_dump_graphviz_rec(ASTChild(a, i),f,  node_actual, i);
            }
        }
        else if (ASTType(a) == AST_AMBIGUITY)
        {
            int i;
            for(i = 0; i < ast_get_num_ambiguities(a); i++)
            {
                ast_dump_graphviz_rec(ast_get_ambiguity(a, i), f, node_actual, i);
            }
        }
    }
    else
    {
        fprintf(f, "n%d[shape=circle,label=\"\",fixedsize=true,style=filled,fillcolor=black,height=0.1,width=0.1]\n", node_actual);
        if (parent_node != 0)
        {
            fprintf(f, "n%d -> n%d [label=\"%d\"]\n", parent_node, node_actual, position);
        }
    }
}
static void ast_dump_graphviz_rec(AST a, FILE* f, size_t parent_node, int position, char is_extended UNUSED_PARAMETER)
{
    // static char* octagon = "octagon";
    // static char* doubleoctagon = "doubleoctagon";
    static char* ellipse = "ellipse";
    static char* mdiamond = "Mdiamond";
    static char* box = "box";
    char* shape;

    // I know this is not exact, but there is a %z qualifier in printf
    // while there is not such thing for intptr_t
    size_t current_node = (size_t)a;

    if (a != NULL)
    {
        // Select shape
        shape = box;
        if (ASTType(a) == AST_AMBIGUITY) shape = ellipse;
        if (ASTType(a) == AST_NODE_LIST) shape = mdiamond;
        // if (a->construct_type == CT_SPECIFICATION) shape = ellipse;
        // else if (a->construct_type == CT_OMP_SPECIFICATION) shape = mdiamond;
        // else if (a->construct_type == CT_EXECUTABLE) shape = octagon;
        // else if (a->construct_type == CT_OMP_EXECUTABLE) shape = doubleoctagon;

        if (ASTText(a))
        {
            char *quoted = quote_protect(ASTText(a));

            fprintf(f, "n%zd[shape=%s,label=\"%s\\nNode=%p\\nParent=%p\\n%s\\nText: \\\"%s\\\"\"]\n", 
                    current_node, shape, ast_print_node_type(ASTType(a)), a, ASTParent(a), ast_location(a), quoted);

            free(quoted);
        }
        else
        {
            fprintf(f, "n%zd[shape=%s,label=\"%s\\nNode=%p\\nParent=%p\\n%s\"]\n", 
                    current_node, shape, ast_print_node_type(ASTType(a)), a, ASTParent(a), ast_location(a));
        }

        // Print this only for non extended referenced nodes
        if (parent_node != 0)
        {
            fprintf(f, "n%zd -> n%zd [label=\"%d\"]\n", parent_node, current_node, position);
        }

        if (ASTType(a) != AST_AMBIGUITY)
        {
            int i;
            if (!is_extended)
            {
                for(i = 0; i < ASTNumChildren(a); i++)
                {
                    if (ASTChild(a, i) != NULL)
                    {
                        ast_dump_graphviz_rec(ASTChild(a, i), f, current_node, i, /* is_extended */ is_extended);
                    }
                }
            }

            // Now print all extended trees referenced here
            // First get all TL_AST in 'orig' that point to its childrens

            extensible_struct_t* extended_data = ast_get_extensible_struct(a);

            if (extended_data != NULL
                    && !is_extended)
            {
                int num_fields = 0;
                const char** keys = NULL;
                const void** values = NULL;

                extensible_struct_get_all_data(extended_data, &num_fields, &keys, &values);

                for (i = 0; i < num_fields; i++)
                {
                    const char* field_name = keys[i];
                    tl_type_t* tl_data = (tl_type_t*)values[i];
                    if (tl_data->kind == TL_AST)
                    {
                        if (tl_data->data._ast != a)
                        {
                            ast_dump_graphviz_rec(tl_data->data._ast, f, /* parent_node */ 0, /* position */ 0, /* is_extended */ 1);
                        }

                        // Add an edge
                        fprintf(f, "n%zd -> n%zd [label=\"%s\",style=dashed]\n",
                                current_node,
                                (size_t)(tl_data->data._ast),
                                field_name);
                    }
                }
            }
        }
        else if (ASTType(a) == AST_AMBIGUITY)
        {
            int i;
            for(i = 0; i < ast_get_num_ambiguities(a); i++)
            {
                ast_dump_graphviz_rec(ast_get_ambiguity(a, i), f, current_node, i, /* is_extended */ 0);
            }
        }
    }
    else
    {
        fprintf(f, "n%zd[shape=circle,label=\"\",fixedsize=true,style=filled,fillcolor=black,height=0.1,width=0.1]\n", current_node);
        if (parent_node != 0)
        {
            fprintf(f, "n%zd -> n%zd [label=\"%d\"]\n", parent_node, current_node, position);
        }
    }
}