diff options
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-ast-full-test.cc | 19 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast.h | 42 | ||||
-rw-r--r-- | gcc/rust/ast/rust-macro.h | 24 | ||||
-rw-r--r-- | gcc/rust/expand/rust-macro-expand.cc | 6 | ||||
-rw-r--r-- | gcc/rust/expand/rust-macro-expand.h | 4 | ||||
-rw-r--r-- | gcc/rust/parse/rust-parse-impl.h | 57 |
6 files changed, 98 insertions, 54 deletions
diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index 030b0b3..f0a3990 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -1358,13 +1358,9 @@ TypeAlias::as_string () const str += "\n Where clause: "; if (!has_where_clause ()) - { str += "none"; - } else - { str += where_clause.as_string (); - } str += "\n Type: " + existing_type->as_string (); @@ -1383,6 +1379,9 @@ MacroInvocationSemi::as_string () const str += attr.as_string () + "\n"; } + str += invoc_data.as_string (); + +#if 0 str += "\n" + path.as_string () + "!"; std::string tok_trees; @@ -1414,6 +1413,7 @@ MacroInvocationSemi::as_string () const tok_trees += get_string_in_delims (s, delim_type); } +#endif return str; } @@ -1520,8 +1520,15 @@ MacroRulesDefinition::as_string () const std::string MacroInvocation::as_string () const { - return "MacroInvocation: " + path.as_string () + "!" - + token_tree.as_string (); + /*return "MacroInvocation: " + path.as_string () + "!" + + token_tree.as_string ();*/ + return "MacroInvocation: " + invoc_data.as_string (); +} + +std::string +MacroInvocData::as_string () const +{ + return path.as_string () + "!" + token_tree.as_string (); } std::string diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index bdc1f12..4916e36 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -1329,6 +1329,25 @@ protected: virtual ExternalItem *clone_external_item_impl () const = 0; }; +/* Data structure to store the data used in macro invocations and macro + * invocations with semicolons. */ +struct MacroInvocData +{ +private: + SimplePath path; + DelimTokenTree token_tree; + +public: + std::string as_string () const; + + MacroInvocData (SimplePath path, DelimTokenTree token_tree) + : path (std::move (path)), token_tree (std::move (token_tree)) {} + + // Invalid if path is empty, so base stripping on that. + void mark_for_strip () { path = SimplePath::create_empty (); } + bool is_marked_for_strip () const { return path.is_empty (); } +}; + /* A macro invocation item (or statement) AST node (i.e. semi-coloned macro * invocation) */ class MacroInvocationSemi : public MacroItem, @@ -1338,23 +1357,31 @@ class MacroInvocationSemi : public MacroItem, public ExternalItem { std::vector<Attribute> outer_attrs; +#if 0 SimplePath path; // all delim types except curly must have invocation end with a semicolon DelimType delim_type; std::vector<std::unique_ptr<TokenTree> > token_trees; +#endif + MacroInvocData invoc_data; Location locus; public: std::string as_string () const override; - MacroInvocationSemi (SimplePath macro_path, DelimType delim_type, + /*MacroInvocationSemi (SimplePath macro_path, DelimType delim_type, std::vector<std::unique_ptr<TokenTree> > token_trees, std::vector<Attribute> outer_attribs, Location locus) : outer_attrs (std::move (outer_attribs)), path (std::move (macro_path)), delim_type (delim_type), token_trees (std::move (token_trees)), locus (locus) - {} + {}*/ + MacroInvocationSemi (MacroInvocData invoc_data, + std::vector<Attribute> outer_attrs, Location locus) + : outer_attrs (std::move (outer_attrs)), invoc_data (std::move (invoc_data)), + locus (locus) {} + /* // Copy constructor with vector clone MacroInvocationSemi (MacroInvocationSemi const &other) : MacroItem (other), TraitItem (other), InherentImplItem (other), @@ -1364,8 +1391,9 @@ public: token_trees.reserve (other.token_trees.size ()); for (const auto &e : other.token_trees) token_trees.push_back (e->clone_token_tree ()); - } + }*/ + /* // Overloaded assignment operator to vector clone MacroInvocationSemi &operator= (MacroInvocationSemi const &other) { @@ -1383,11 +1411,13 @@ public: token_trees.push_back (e->clone_token_tree ()); return *this; - } + }*/ + /* // Move constructors MacroInvocationSemi (MacroInvocationSemi &&other) = default; MacroInvocationSemi &operator= (MacroInvocationSemi &&other) = default; + */ void accept_vis (ASTVisitor &vis) override; @@ -1398,9 +1428,13 @@ public: clone_macro_invocation_semi_impl ()); } + /* // Invalid if path is empty, so base stripping on that. void mark_for_strip () override { path = SimplePath::create_empty (); } bool is_marked_for_strip () const override { return path.is_empty (); } + */ + void mark_for_strip () override { invoc_data.mark_for_strip (); } + bool is_marked_for_strip () const override { return invoc_data.is_marked_for_strip (); } // TODO: this mutable getter seems really dodgy. Think up better way. const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 2cae0f9..3971572 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -336,30 +336,32 @@ class MacroInvocation : public TypeNoBounds, public Pattern, public ExprWithoutBlock { - SimplePath path; - DelimTokenTree token_tree; + /*SimplePath path; + DelimTokenTree token_tree;*/ + MacroInvocData invoc_data; Location locus; public: std::string as_string () const override; - MacroInvocation (SimplePath path, DelimTokenTree token_tree, + /*MacroInvocation (SimplePath path, DelimTokenTree token_tree, std::vector<Attribute> outer_attrs, Location locus) : ExprWithoutBlock (std::move (outer_attrs)), path (std::move (path)), token_tree (std::move (token_tree)), locus (locus) - {} - + {}*/ + MacroInvocation (MacroInvocData invoc_data, + std::vector<Attribute> outer_attrs, Location locus) + : ExprWithoutBlock (std::move (outer_attrs)), + invoc_data (std::move (invoc_data)), locus (locus) {} + Location get_locus () const { return locus; } - Location get_locus_slow () const override { return get_locus (); } + Location get_locus_slow () const final override { return get_locus (); } void accept_vis (ASTVisitor &vis) override; // Invalid if path is empty, so base stripping on that. - void mark_for_strip () override { path = SimplePath::create_empty (); } - bool is_marked_for_strip () const override { return path.is_empty (); } - - const SimplePath &get_path () const { return path; } - SimplePath &get_path () { return path; } + void mark_for_strip () override { invoc_data.mark_for_strip (); } + bool is_marked_for_strip () const override { return invoc_data.is_marked_for_strip (); } protected: /* Use covariance to implement clone function as returning this object rather diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index ed0069e..53b2851 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -3553,6 +3553,12 @@ public: } }; +AST::Literal +MacroExpander::expand_cfg_macro (AST::MacroInvocData &invoc) +{ + return AST::Literal::create_error (); +} + void MacroExpander::expand_invoc (std::unique_ptr<AST::MacroInvocation> &invoc) { diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index a0c1a076..6752090 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -44,8 +44,8 @@ struct MacroExpander void expand_cfg_attrs (std::vector<AST::Attribute> &attrs); bool fails_cfg (std::vector<AST::Attribute> &attr); - /* TODO: make it extend ASTVisitor so that individual items can be accessed - * properly? */ + // Expand the data of a cfg! macro. + AST::Literal expand_cfg_macro (AST::MacroInvocData &invoc); private: AST::Crate &crate; diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 187f821..671565c 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -1514,6 +1514,7 @@ Parser<ManagedTokenSource>::parse_macro_invocation_semi ( t->get_token_description ()); return nullptr; } + Location tok_tree_locus = t->get_locus (); lexer.skip_token (); // parse actual token trees @@ -1538,6 +1539,9 @@ Parser<ManagedTokenSource>::parse_macro_invocation_semi ( t = lexer.peek_token (); } + + AST::DelimTokenTree delim_tok_tree (delim_type, std::move (token_trees), tok_tree_locus); + AST::MacroInvocData invoc_data (std::move (path), std::move (delim_tok_tree)); // parse end delimiters t = lexer.peek_token (); @@ -1553,8 +1557,7 @@ Parser<ManagedTokenSource>::parse_macro_invocation_semi ( { // as this is the end, allow recovery (probably) - may change return std::unique_ptr<AST::MacroInvocationSemi> ( - new AST::MacroInvocationSemi (std::move (path), delim_type, - std::move (token_trees), + new AST::MacroInvocationSemi (std::move (invoc_data), std::move (outer_attrs), macro_locus)); } @@ -1567,8 +1570,7 @@ Parser<ManagedTokenSource>::parse_macro_invocation_semi ( lexer.peek_token ()->get_token_description ()); return std::unique_ptr<AST::MacroInvocationSemi> ( - new AST::MacroInvocationSemi (std::move (path), delim_type, - std::move (token_trees), + new AST::MacroInvocationSemi (std::move (invoc_data), std::move (outer_attrs), macro_locus)); } else @@ -1616,8 +1618,8 @@ Parser<ManagedTokenSource>::parse_macro_invocation ( Location macro_locus = macro_path.get_locus (); return std::unique_ptr<AST::MacroInvocation> ( - new AST::MacroInvocation (std::move (macro_path), - std::move (delim_tok_tree), + new AST::MacroInvocation (AST::MacroInvocData (std::move (macro_path), + std::move (delim_tok_tree)), std::move (outer_attrs), macro_locus)); } @@ -8826,10 +8828,8 @@ Parser<ManagedTokenSource>::parse_type () AST::DelimTokenTree tok_tree = parse_delim_token_tree (); return std::unique_ptr<AST::MacroInvocation> ( - new AST::MacroInvocation (std::move (macro_path), - std::move (tok_tree), - std::vector<AST::Attribute> (), - locus)); + new AST::MacroInvocation (AST::MacroInvocData (std::move (macro_path), + std::move (tok_tree)), {}, locus)); } case PLUS: { // type param bounds @@ -9629,10 +9629,8 @@ Parser<ManagedTokenSource>::parse_type_no_bounds () AST::DelimTokenTree tok_tree = parse_delim_token_tree (); return std::unique_ptr<AST::MacroInvocation> ( - new AST::MacroInvocation (std::move (macro_path), - std::move (tok_tree), - std::vector<AST::Attribute> (), - locus)); + new AST::MacroInvocation (AST::MacroInvocData (std::move (macro_path), + std::move (tok_tree)), {}, locus)); } case PLUS: { // type param bounds - not allowed, here for error message @@ -11397,25 +11395,24 @@ Parser<ManagedTokenSource>::parse_path_based_stmt_or_expr ( * fixed up via HACKs in semantic analysis (by checking whether it * is the last elem in the vector). */ + AST::DelimTokenTree delim_tok_tree (type, std::move (token_trees), + tok_tree_loc); + AST::MacroInvocData invoc_data (std::move (macro_path), std::move (delim_tok_tree)); + if (lexer.peek_token ()->get_id () == SEMICOLON) { lexer.skip_token (); std::unique_ptr<AST::MacroInvocationSemi> stmt ( - new AST::MacroInvocationSemi (std::move (macro_path), type, - std::move (token_trees), + new AST::MacroInvocationSemi (std::move (invoc_data), std::move (outer_attrs), stmt_or_expr_loc)); return ExprOrStmt (std::move (stmt)); } // otherwise, create macro invocation - AST::DelimTokenTree delim_tok_tree (type, std::move (token_trees), - tok_tree_loc); - std::unique_ptr<AST::MacroInvocation> expr ( - new AST::MacroInvocation (std::move (macro_path), - std::move (delim_tok_tree), + new AST::MacroInvocation (std::move (invoc_data), std::move (outer_attrs), stmt_or_expr_loc)); return ExprOrStmt (std::move (expr)); @@ -11424,8 +11421,7 @@ Parser<ManagedTokenSource>::parse_path_based_stmt_or_expr ( { // tokens don't match opening delimiters, so produce error rust_error_at (t2->get_locus (), - "unexpected token %qs - expecting closing " - "delimiter %qs (for a " + "unexpected token %qs - expecting closing delimiter %qs (for a " "macro invocation)", t2->get_token_description (), (type == AST::PARENS @@ -11697,25 +11693,24 @@ Parser<ManagedTokenSource>::parse_macro_invocation_maybe_semi ( * HACKs in semantic analysis (by checking whether it is the last elem in * the vector). */ + AST::DelimTokenTree delim_tok_tree (type, std::move (token_trees), + tok_tree_loc); + AST::MacroInvocData invoc_data (std::move (macro_path), std::move (delim_tok_tree)); + if (lexer.peek_token ()->get_id () == SEMICOLON) { lexer.skip_token (); std::unique_ptr<AST::MacroInvocationSemi> stmt ( - new AST::MacroInvocationSemi (std::move (macro_path), type, - std::move (token_trees), + new AST::MacroInvocationSemi (std::move (invoc_data), std::move (outer_attrs), macro_locus)); return ExprOrStmt (std::move (stmt)); } // otherwise, create macro invocation - AST::DelimTokenTree delim_tok_tree (type, std::move (token_trees), - tok_tree_loc); - std::unique_ptr<AST::MacroInvocation> expr ( - new AST::MacroInvocation (std::move (macro_path), - std::move (delim_tok_tree), + new AST::MacroInvocation (std::move (invoc_data), std::move (outer_attrs), macro_locus)); return ExprOrStmt (std::move (expr)); } @@ -13926,7 +13921,7 @@ Parser<ManagedTokenSource>::parse_macro_invocation_partial ( Location macro_locus = converted_path.get_locus (); return std::unique_ptr<AST::MacroInvocation> ( - new AST::MacroInvocation (std::move (converted_path), std::move (tok_tree), + new AST::MacroInvocation (AST::MacroInvocData (std::move (converted_path), std::move (tok_tree)), std::move (outer_attrs), macro_locus)); } |