From f764eeb8abf1ec50794ddb1f31bc57d025e29a3c Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Fri, 18 Dec 2020 21:07:04 +0800 Subject: Unified representation of macro invocation internal data - will be better for processing --- gcc/rust/ast/rust-ast-full-test.cc | 19 +++++++++++------ gcc/rust/ast/rust-ast.h | 42 ++++++++++++++++++++++++++++++++++---- gcc/rust/ast/rust-macro.h | 24 ++++++++++++---------- 3 files changed, 64 insertions(+), 21 deletions(-) (limited to 'gcc/rust/ast') 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 outer_attrs; +#if 0 SimplePath path; // all delim types except curly must have invocation end with a semicolon DelimType delim_type; std::vector > 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 > token_trees, std::vector 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 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 &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 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 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 -- cgit v1.1