diff options
author | David Malcolm <dmalcolm@redhat.com> | 2017-10-11 14:40:46 -0400 |
---|---|---|
committer | David Malcolm <dmalcolm@redhat.com> | 2017-10-11 14:40:46 -0400 |
commit | b9603806c529179349e4393aa771980b0582af8d (patch) | |
tree | 041dddb17407a077824736005332bbba80d1e590 | |
parent | ac95813ff495c76920cecdfa96926613aac8661f (diff) | |
download | gcc-dmalcolm/lsp-v1.3.tar.gz |
Introduce a blt_context classdmalcolm/lsp-v1.3
-rw-r--r-- | gcc/blt.cc | 130 | ||||
-rw-r--r-- | gcc/blt.h | 34 | ||||
-rw-r--r-- | gcc/c/c-decl.c | 4 | ||||
-rw-r--r-- | gcc/c/c-parser.c | 19 | ||||
-rw-r--r-- | gcc/cp/decl.c | 2 | ||||
-rw-r--r-- | gcc/cp/parser.c | 16 | ||||
-rw-r--r-- | gcc/cp/parser.h | 4 | ||||
-rw-r--r-- | gcc/lsp-main.cc | 11 |
8 files changed, 154 insertions, 66 deletions
diff --git a/gcc/blt.cc b/gcc/blt.cc index 1ad7693ec25..62e71a5a5ba 100644 --- a/gcc/blt.cc +++ b/gcc/blt.cc @@ -27,9 +27,6 @@ along with GCC; see the file COPYING3. If not see #include "blt.h" #include "selftest.h" -typedef hash_map <tree, blt_node *> tree_to_blt_map_t; -static tree_to_blt_map_t *tree_to_blt_map; - static const char *blt_kind_to_name (enum blt_kind kind); /* blt_node's ctor. */ @@ -41,6 +38,19 @@ blt_node::blt_node (enum blt_kind kind, location_t start) { } +/* blt_node's dtor. */ + +blt_node::~blt_node () +{ + blt_node *iter = m_first_child; + while (iter) + { + blt_node *next = iter->m_next_sibling; + delete iter; + iter = next; + } +} + /* Add CHILD to this blt_node as its final child. CHILD must be an orphan. */ @@ -132,9 +142,10 @@ blt_node::make_orphan () /* Set the tree associated with this blt_node to be T. */ // FIXME: what if it's called more than once +// FIXME: CTXT void -blt_node::set_tree (tree t) +blt_node::set_tree (tree t, blt_context *ctxt) { m_tree = t; @@ -142,9 +153,10 @@ blt_node::set_tree (tree t) if (!t) return; - if (!tree_to_blt_map) - tree_to_blt_map = new tree_to_blt_map_t (); - tree_to_blt_map->put (t, this); + gcc_assert (ctxt); + gcc_assert (ctxt->m_tree_to_blt_map); + + ctxt->m_tree_to_blt_map->put (t, this); } /* Dump this blt_node and its descendants to FILE. */ @@ -480,16 +492,55 @@ blt_node::assert_invariants () const } } +/* The table of names for enum blt_kind. */ + +static const char * const blt_kind_names[] = { +#define DEF_BLT_NODE(ENUM_NAME, PRETTY_NAME) PRETTY_NAME, +#include "blt.def" +#undef DEF_BLT_NODE +}; + +/* Get a human-readable name for this blt_kind, e.g. "translation-unit". */ + +static const char * +blt_kind_to_name (enum blt_kind kind) +{ + return blt_kind_names[kind]; +} + +/* Dump NODE to stderr. */ + +DEBUG_FUNCTION void +debug (blt_node *node) +{ + node->dump (stderr); +} + +/* class blt_context. */ + +/* blt_context's ctor. */ + +blt_context::blt_context () +: m_tree_to_blt_map (new tree_to_blt_map_t ()), + m_root_node (NULL) +{ +} + +/* blt_context's dtor. */ + +blt_context::~blt_context () +{ + delete m_tree_to_blt_map; +} + /* Given tree node T, get the assocated blt_node, if any, or NULL. */ blt_node * -blt_get_node_for_tree (tree t) +blt_context::get_node_for_tree (tree t) { if (!t) return NULL; - if (!tree_to_blt_map) - return NULL; - blt_node **slot = tree_to_blt_map->get (t); + blt_node **slot = m_tree_to_blt_map->get (t); if (!slot) return NULL; return *slot; @@ -504,7 +555,7 @@ blt_get_node_for_tree (tree t) can lead to multiple FUNCTION_DECL tree nodes from one blt_node). */ void -blt_set_node_for_tree (tree t, blt_node *node) +blt_context::set_node_for_tree (tree t, blt_node *node) { if (!t) return; @@ -512,45 +563,50 @@ blt_set_node_for_tree (tree t, blt_node *node) return; if (node->get_tree () == NULL) - node->set_tree (t); + node->set_tree (t, this); else { /* Don't attempt to change; multiple tree nodes can reference an blt_node *, but an blt_node * references at most one tree node (e.g. template instantiations). */ - gcc_assert (tree_to_blt_map); - tree_to_blt_map->get_or_insert (t) = node; + gcc_assert (m_tree_to_blt_map); + m_tree_to_blt_map->get_or_insert (t) = node; } } -/* The table of names for enum blt_kind. */ +/* FIXME. */ -static const char * const blt_kind_names[] = { -#define DEF_BLT_NODE(ENUM_NAME, PRETTY_NAME) PRETTY_NAME, -#include "blt.def" -#undef DEF_BLT_NODE -}; +blt_context *the_blt_ctxt = NULL; -/* Get a human-readable name for this blt_kind, e.g. "translation-unit". */ +/* FIXME. */ -static const char * -blt_kind_to_name (enum blt_kind kind) +blt_node * +blt_get_root_node () { - return blt_kind_names[kind]; + if (!the_blt_ctxt) + return NULL; + return the_blt_ctxt->get_root_node (); } -/* Dump NODE to stderr. */ +/* FIXME. */ -DEBUG_FUNCTION void -debug (blt_node *node) +blt_node * +blt_get_node_for_tree (tree t) { - node->dump (stderr); + if (!the_blt_ctxt) + return NULL; + return the_blt_ctxt->get_node_for_tree (t); } -/* Global singleton. */ -// FIXME: do we need this? it's been useful when debugging. +/* FIXME. */ -blt_node *the_blt_root_node; +void +blt_set_node_for_tree (tree t, blt_node *n) +{ + if (!the_blt_ctxt) + return; + the_blt_ctxt->set_node_for_tree (t, n); +} #if CHECKING_P @@ -739,6 +795,8 @@ test_basic_ops (const line_table_case &case_) ASSERT_FALSE (s_contents->contains_location_p (tmp.get_filename (), 6, 2)); #undef LOC + + delete tu; } /* Verify that we can wrap cpp tokens. */ @@ -746,11 +804,11 @@ test_basic_ops (const line_table_case &case_) static void test_cpp_tokens () { - blt_node *plus_node = new blt_node (BLT_TOKEN_OP_PLUS, UNKNOWN_LOCATION); - ASSERT_STREQ ("+-token", plus_node->get_name ()); + blt_node plus_node (BLT_TOKEN_OP_PLUS, UNKNOWN_LOCATION); + ASSERT_STREQ ("+-token", plus_node.get_name ()); - blt_node *name_node = new blt_node (BLT_TOKEN_TK_NAME, UNKNOWN_LOCATION); - ASSERT_STREQ ("NAME-token", name_node->get_name ()); + blt_node name_node (BLT_TOKEN_TK_NAME, UNKNOWN_LOCATION); + ASSERT_STREQ ("NAME-token", name_node.get_name ()); } /* Run all of the selftests within this file. */ diff --git a/gcc/blt.h b/gcc/blt.h index 107f169514a..dd7381c96c8 100644 --- a/gcc/blt.h +++ b/gcc/blt.h @@ -57,6 +57,7 @@ enum blt_kind }; class blt_node; +class blt_context; /* Would use a lambda. */ @@ -73,6 +74,7 @@ class blt_node { public: blt_node (enum blt_kind kind, location_t start); + ~blt_node (); /* Structural manipulation. */ void add_child (blt_node *child); @@ -82,7 +84,7 @@ public: /* Modification. */ void set_finish (location_t loc) { m_finish = ::get_finish (loc); } - void set_tree (tree t); + void set_tree (tree t, blt_context *ctxt); void set_kind (enum blt_kind kind) { m_kind = kind; } /* Dumping. */ @@ -136,12 +138,34 @@ private: tree m_tree; }; -extern blt_node *blt_get_node_for_tree (tree); -extern void blt_set_node_for_tree (tree, blt_node *); - extern void DEBUG_FUNCTION debug (blt_node *); /* FIXME. */ -extern blt_node *the_blt_root_node; + +class blt_context +{ + public: + blt_context (); + ~blt_context (); + + blt_node *get_root_node () const { return m_root_node; } + void set_root_node (blt_node *n) { m_root_node = n; } + + blt_node *get_node_for_tree (tree); + void set_node_for_tree (tree, blt_node *); + + private: + friend class blt_node; + + typedef hash_map <tree, blt_node *> tree_to_blt_map_t; + tree_to_blt_map_t *m_tree_to_blt_map; + blt_node *m_root_node; +}; + +extern blt_context *the_blt_ctxt; + +extern blt_node *blt_get_root_node (); +extern blt_node *blt_get_node_for_tree (tree); +extern void blt_set_node_for_tree (tree, blt_node *); #endif /* GCC_BLT_H */ diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index ff27e552445..0b7465da9c6 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -4630,7 +4630,7 @@ start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, if (!decl || decl == error_mark_node) return NULL_TREE; if (declarator->bltnode) - declarator->bltnode->set_tree (decl); + declarator->bltnode->set_tree (decl, the_blt_ctxt); if (expr) add_stmt (fold_convert (void_type_node, expr)); @@ -8532,7 +8532,7 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator, decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL, &attributes, NULL, NULL, DEPRECATED_NORMAL); if (declarator->bltnode) - declarator->bltnode->set_tree (decl1); + declarator->bltnode->set_tree (decl1, the_blt_ctxt); invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1); /* If the declarator is not suitable for a function definition, diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index 119ae237246..abd2b17f70b 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -209,7 +209,6 @@ struct GTY(()) c_parser { vec <c_token, va_gc> *cilk_simd_fn_tokens; location_t last_token_location; - blt_node * GTY((skip)) blt_root_node; blt_node * GTY((skip)) blt_current_node; }; @@ -268,7 +267,7 @@ auto_blt_node::auto_blt_node (c_parser *parser, enum blt_kind kind) if (parent) parent->add_child (node); else - parser->blt_root_node = node; + the_blt_ctxt->set_root_node (node); } /* auto_blt_node's destructor. @@ -313,7 +312,7 @@ auto_blt_node::set_tree (tree tree_node) return; blt_node *node = m_parser->blt_current_node; - node->set_tree (tree_node); + node->set_tree (tree_node, the_blt_ctxt); } /* Return a pointer to the Nth token in PARSERs tokens_buf. */ @@ -3142,7 +3141,7 @@ c_parser_struct_or_union_specifier (c_parser *parser) timevar_pop (TV_PARSE_STRUCT); if (CURRENT_BLT_NODE) - CURRENT_BLT_NODE->set_tree (ret.spec); + CURRENT_BLT_NODE->set_tree (ret.spec, the_blt_ctxt); return ret; } @@ -3159,7 +3158,7 @@ c_parser_struct_or_union_specifier (c_parser *parser) ret = parser_xref_tag (ident_loc, code, ident); if (CURRENT_BLT_NODE) - CURRENT_BLT_NODE->set_tree (ret.spec); + CURRENT_BLT_NODE->set_tree (ret.spec, the_blt_ctxt); return ret; } @@ -18326,12 +18325,16 @@ c_parse_file (void) if (flag_exceptions) using_eh_for_cleanups (); + if (flag_blt) + { + gcc_assert (the_blt_ctxt == NULL); + the_blt_ctxt = new blt_context (); + } + c_parser_translation_unit (the_parser); if (flag_blt && flag_dump_blt) - the_parser->blt_root_node->dump (stderr); - - the_blt_root_node = the_parser->blt_root_node; + the_blt_ctxt->get_root_node ()->dump (stderr); the_parser = NULL; } diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 84a275f26b1..178f462e04b 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -12361,7 +12361,7 @@ grokdeclarator (const cp_declarator *declarator, tree result = grokdeclarator_1 (declarator, declspecs, decl_context, initialized, attrlist); if (declarator && declarator->bltnode) - declarator->bltnode->set_tree (result); + declarator->bltnode->set_tree (result, the_blt_ctxt); return result; } diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 5e0b34e8fb7..15d52f51dec 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -1470,7 +1470,7 @@ auto_blt_node::auto_blt_node (cp_parser *parser, enum blt_kind kind, if (m_parent) m_parent->add_child (node); else - parser->blt_root_node = node; + the_blt_ctxt->set_root_node (node); } /* auto_blt_node's destructor. @@ -1504,7 +1504,7 @@ auto_blt_node::~auto_blt_node () } if (m_expr_ptr) - node->set_tree (m_expr_ptr->get_value ()); + node->set_tree (m_expr_ptr->get_value (), the_blt_ctxt); /* Use stashed parent, rather than the curent node's parent to allow for reparenting within the tree. */ @@ -1520,7 +1520,7 @@ auto_blt_node::set_tree (tree tree_node) return; blt_node *node = m_parser->blt_current_node; - node->set_tree (tree_node); + node->set_tree (tree_node, the_blt_ctxt); } @@ -38977,12 +38977,16 @@ c_parse_file (void) the_parser = cp_parser_new (); push_deferring_access_checks (flag_access_control ? dk_no_deferred : dk_no_check); + if (flag_blt) + { + gcc_assert (the_blt_ctxt == NULL); + the_blt_ctxt = new blt_context (); + } + cp_parser_translation_unit (the_parser); if (flag_blt && flag_dump_blt) - the_parser->blt_root_node->dump (stderr); - - the_blt_root_node = the_parser->blt_root_node; + the_blt_ctxt->get_root_node ()->dump (stderr); the_parser = NULL; } diff --git a/gcc/cp/parser.h b/gcc/cp/parser.h index 49c8eb6527a..76bb105e350 100644 --- a/gcc/cp/parser.h +++ b/gcc/cp/parser.h @@ -214,6 +214,7 @@ struct cp_oacc_routine_data : cp_omp_declare_simd_data { }; class blt_node; +class blt_context; /* The cp_parser structure represents the C++ parser. */ @@ -414,9 +415,6 @@ struct GTY(()) cp_parser { context e.g., because they could never be deduced. */ int prevent_constrained_type_specifiers; - /* The top-level node for the translation-unit. */ - blt_node * GTY((skip)) blt_root_node; - /* The current node being parsed. */ blt_node * GTY((skip)) blt_current_node; }; diff --git a/gcc/lsp-main.cc b/gcc/lsp-main.cc index 3f8cc0f8233..fb4bbb07e0c 100644 --- a/gcc/lsp-main.cc +++ b/gcc/lsp-main.cc @@ -103,9 +103,10 @@ gcc_lsp_server::do_text_document_definition (const TextDocumentPositionParams &p /* Convert from LSP's 0-based lines and columns to GCC's 1-based lines and columns. */ blt_node *blt - = the_blt_root_node->get_descendant_at_location (p.textDocument.uri, - p.position.line + 1, - p.position.character + 1); + = blt_get_root_node ()->get_descendant_at_location + (p.textDocument.uri, + p.position.line + 1, + p.position.character + 1); if (!blt) /* No results. */ return; @@ -123,12 +124,12 @@ gcc_lsp_server::do_text_document_definition (const TextDocumentPositionParams &p return; if (1) - the_blt_root_node->dump (stderr); + the_blt_ctxt->get_root_node ()->dump (stderr); /* Find a struct-contents with tree == record_type. */ is_record_definition pred (record_type); blt_node *blt_struct_contents - = the_blt_root_node->find_descendant_satisfying (pred); + = blt_get_root_node ()->find_descendant_satisfying (pred); if (!blt_struct_contents) return; |