From f0cc1df59137c0d29408c18ccd88d8077979efb3 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Tue, 15 Dec 2020 14:05:17 +0800 Subject: Changed some for index loops to for iterator loops that can deal with representation changes --- gcc/rust/expand/rust-macro-expand.cc | 195 ++++++++++++++++++++++++++++++++++- 1 file changed, 191 insertions(+), 4 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 79e74ac..64dd710 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -16,6 +16,30 @@ public: void expand_struct_fields (std::vector &fields) { + for (auto it = fields.begin (); it != fields.end ();) + { + auto &field = *it; + + auto &field_attrs = field.get_outer_attrs (); + expander.expand_cfg_attrs (field_attrs); + if (expander.fails_cfg (field_attrs)) + { + it = fields.erase (it); + continue; + } + + // expand sub-types of type, but can't strip type itself + auto &type = field.get_field_type (); + type->accept_vis (*this); + if (type->is_marked_for_strip ()) + rust_error_at (type->get_locus_slow (), + "cannot strip type in this position"); + + // if nothing else happens, increment + ++it; + } + + #if 0 for (int i = 0; i < fields.size ();) { auto &field = fields[i]; @@ -38,10 +62,35 @@ public: // if nothing else happens, increment i++; } + #endif } void expand_tuple_fields (std::vector &fields) { + for (auto it = fields.begin (); it != fields.end ();) + { + auto &field = *it; + + auto &field_attrs = field.get_outer_attrs (); + expander.expand_cfg_attrs (field_attrs); + if (expander.fails_cfg (field_attrs)) + { + it = fields.erase (it); + continue; + } + + // expand sub-types of type, but can't strip type itself + auto &type = field.get_field_type (); + type->accept_vis (*this); + if (type->is_marked_for_strip ()) + rust_error_at (type->get_locus_slow (), + "cannot strip type in this position"); + + // if nothing else happens, increment + ++it; + } + + #if 0 for (int i = 0; i < fields.size ();) { auto &field = fields[i]; @@ -64,10 +113,41 @@ public: // if nothing else happens, increment i++; } + #endif } void expand_function_params (std::vector ¶ms) { + for (auto it = params.begin (); it != params.end ();) + { + auto ¶m = *it; + + auto ¶m_attrs = param.get_outer_attrs (); + expander.expand_cfg_attrs (param_attrs); + if (expander.fails_cfg (param_attrs)) + { + it = params.erase (it); + continue; + } + + // TODO: should an unwanted strip lead to break out of loop? + auto &pattern = param.get_pattern (); + pattern->accept_vis (*this); + if (pattern->is_marked_for_strip ()) + rust_error_at (pattern->get_locus_slow (), + "cannot strip pattern in this position"); + + auto &type = param.get_type (); + type->accept_vis (*this); + if (type->is_marked_for_strip ()) + rust_error_at (type->get_locus_slow (), + "cannot strip type in this position"); + + // increment + ++it; + } + + #if 0 for (int i = 0; i < params.size ();) { auto ¶m = params[i]; @@ -96,6 +176,7 @@ public: // increment i++; } + #endif } void expand_generic_args (AST::GenericArgs &args) @@ -142,6 +223,38 @@ public: void expand_closure_params (std::vector ¶ms) { + for (auto it = params.begin (); it != params.end ();) + { + auto ¶m = *it; + + auto ¶m_attrs = param.get_outer_attrs (); + expander.expand_cfg_attrs (param_attrs); + if (expander.fails_cfg (param_attrs)) + { + it = params.erase (it); + continue; + } + + auto &pattern = param.get_pattern (); + pattern->accept_vis (*this); + if (pattern->is_marked_for_strip ()) + rust_error_at (pattern->get_locus_slow (), + "cannot strip pattern in this position"); + + if (param.has_type_given ()) + { + auto &type = param.get_type (); + type->accept_vis (*this); + if (type->is_marked_for_strip ()) + rust_error_at (type->get_locus_slow (), + "cannot strip type in this position"); + } + + // increment if found nothing else so far + ++it; + } + + #if 0 for (int i = 0; i < params.size ();) { auto ¶m = params[i]; @@ -172,6 +285,7 @@ public: // increment if found nothing else so far i++; } + #endif } void expand_self_param (AST::SelfParam &self_param) @@ -184,8 +298,8 @@ public: rust_error_at (type->get_locus_slow (), "cannot strip type in this position"); } - // TODO: maybe check for invariants being violated - e.g. both type and - // lifetime? + /* TODO: maybe check for invariants being violated - e.g. both type and + * lifetime? */ } void expand_where_clause (AST::WhereClause &where_clause) @@ -245,6 +359,23 @@ public: if (decl.has_where_clause ()) expand_where_clause (decl.get_where_clause ()); } + + template + void expand_pointer_allow_strip (T &values) + { + for (auto it = values.begin (); it != values.end ();) + { + auto &value = *it; + + // mark for stripping if required + value->accept_vis (*this); + + if (value->is_marked_for_strip ()) + it = values.erase (it); + else + ++it; + } + } void visit (AST::Token &) override { @@ -649,6 +780,9 @@ public: { /* apparently outer attributes are allowed in "elements of array * expressions" according to spec */ + expand_pointer_allow_strip (elems.get_values ()); + +#if 0 auto &values = elems.get_values (); for (int i = 0; i < values.size ();) { @@ -662,6 +796,7 @@ public: else i++; } +#endif } void visit (AST::ArrayElemsCopied &elems) override { @@ -763,6 +898,9 @@ public: /* apparently outer attributes are allowed in "elements of tuple * expressions" according to spec */ + expand_pointer_allow_strip (expr.get_tuple_elems ()); + +#if 0 auto &values = expr.get_tuple_elems (); for (int i = 0; i < values.size ();) { @@ -776,6 +914,7 @@ public: else i++; } +#endif } void visit (AST::TupleIndexExpr &expr) override { @@ -961,6 +1100,9 @@ public: /* spec says outer attributes are specifically allowed for elements * of tuple-style struct expressions, so full stripping possible */ + expand_pointer_allow_strip (expr.get_elems ()); + +#if 0 auto &values = expr.get_elems (); for (int i = 0; i < values.size ();) { @@ -974,6 +1116,7 @@ public: else i++; } +#endif } void visit (AST::StructExprUnit &expr) override { @@ -1068,6 +1211,9 @@ public: /* spec says outer attributes are specifically allowed for elements * of tuple-style enum expressions, so full stripping possible */ + expand_pointer_allow_strip (expr.get_elems ()); + +#if 0 auto &values = expr.get_elems (); for (int i = 0; i < values.size ();) { @@ -1081,6 +1227,7 @@ public: else i++; } +#endif } void visit (AST::EnumExprFieldless &expr) override { @@ -1115,6 +1262,9 @@ public: /* spec says outer attributes are specifically allowed for elements * of call expressions, so full stripping possible */ + expand_pointer_allow_strip (expr.get_params ()); + +#if 0 auto ¶ms = expr.get_params (); for (int i = 0; i < params.size ();) { @@ -1128,6 +1278,7 @@ public: else i++; } +#endif } void visit (AST::MethodCallExpr &expr) override { @@ -1155,6 +1306,9 @@ public: /* spec says outer attributes are specifically allowed for elements * of method call expressions, so full stripping possible */ + expand_pointer_allow_strip (expr.get_params ()); + +#if 0 auto ¶ms = expr.get_params (); for (int i = 0; i < params.size ();) { @@ -1168,6 +1322,7 @@ public: else i++; } +#endif } void visit (AST::FieldAccessExpr &expr) override { @@ -1231,6 +1386,9 @@ public: } // strip all statements + expand_pointer_allow_strip (expr.get_statements ()); + +#if 0 auto &stmts = expr.get_statements (); for (int i = 0; i < stmts.size ();) { @@ -1244,6 +1402,7 @@ public: else i++; } +#endif // strip tail expression if exists - can actually fully remove it if (expr.has_tail_expr ()) @@ -2083,6 +2242,9 @@ public: } // strip items if required + expand_pointer_allow_strip (module.get_items ()); + +#if 0 auto &items = module.get_items (); for (int i = 0; i < items.size ();) { @@ -2096,6 +2258,7 @@ public: else i++; } +#endif } void visit (AST::ModuleNoBody &module) override { @@ -2321,6 +2484,9 @@ public: /* strip enum fields if required - this is presumably * allowed by spec */ + expand_pointer_allow_strip (enum_item.get_variants ()); + +#if 0 auto &variants = enum_item.get_variants (); for (int i = 0; i < variants.size ();) { @@ -2334,6 +2500,7 @@ public: else i++; } +#endif } void visit (AST::Union &union_item) override { @@ -2540,6 +2707,9 @@ public: expand_where_clause (trait.get_where_clause ()); // strip trait items if required + expand_pointer_allow_strip (trait.get_trait_items ()); + +#if 0 auto &trait_items = trait.get_trait_items (); for (int i = 0; i < trait_items.size ();) { @@ -2553,6 +2723,7 @@ public: else i++; } +#endif } void visit (AST::InherentImpl &impl) override { @@ -2586,6 +2757,9 @@ public: expand_where_clause (impl.get_where_clause ()); // strip inherent impl items if required + expand_pointer_allow_strip (impl.get_impl_items ()); + +#if 0 auto &impl_items = impl.get_impl_items (); for (int i = 0; i < impl_items.size ();) { @@ -2599,6 +2773,7 @@ public: else i++; } +#endif } void visit (AST::TraitImpl &impl) override { @@ -2638,6 +2813,9 @@ public: expand_where_clause (impl.get_where_clause ()); // strip trait impl items if required + expand_pointer_allow_strip (impl.get_impl_items ()); + +#if 0 auto &impl_items = impl.get_impl_items (); for (int i = 0; i < impl_items.size ();) { @@ -2651,6 +2829,7 @@ public: else i++; } +#endif } void visit (AST::ExternalStaticItem &item) override { @@ -2744,6 +2923,9 @@ public: } // strip external items if required + expand_pointer_allow_strip (block.get_extern_items ()); + +#if 0 auto &extern_items = block.get_extern_items (); for (int i = 0; i < extern_items.size ();) { @@ -2757,6 +2939,7 @@ public: else i++; } +#endif } // I don't think it would be possible to strip macros without expansion @@ -2902,14 +3085,17 @@ public: if (path.is_marked_for_strip ()) rust_error_at (path.get_locus (), "cannot strip path in this position"); - // TODO: apparently struct pattern fields can have outer attrs. so can they - // be stripped? + /* TODO: apparently struct pattern fields can have outer attrs. so can they + * be stripped? */ if (!pattern.has_struct_pattern_elems ()) return; auto &elems = pattern.get_struct_pattern_elems (); // assuming you can strip struct pattern fields + expand_pointer_allow_strip (elems.get_struct_pattern_fields ()); + +#if 0 auto &fields = elems.get_struct_pattern_fields (); for (int i = 0; i < fields.size ();) { @@ -2922,6 +3108,7 @@ public: else i++; } +#endif // assuming you can strip the ".." part if (elems.has_etc ()) -- cgit v1.1 From 3e459226165206f305fd98e030c3cc4a234d8838 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Tue, 15 Dec 2020 14:33:30 +0800 Subject: Changed more index for loops to iterator for loops --- gcc/rust/expand/rust-macro-expand.cc | 111 +++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) (limited to 'gcc') diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 64dd710..ed0069e 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -2054,6 +2054,55 @@ public: // strip match cases auto &match_cases = expr.get_match_cases (); + for (auto it = match_cases.begin (); it != match_cases.end ();) + { + auto &match_case = *it; + + // strip match case based on outer attributes in match arm + auto &match_arm = match_case.get_arm (); + expander.expand_cfg_attrs (match_arm.get_outer_attrs ()); + if (expander.fails_cfg (match_arm.get_outer_attrs ())) + { + // strip match case + it = match_cases.erase (it); + continue; + } + + for (auto &pattern : match_arm.get_patterns ()) + { + pattern->accept_vis (*this); + if (pattern->is_marked_for_strip ()) + rust_error_at (pattern->get_locus_slow (), + "cannot strip pattern in this position"); + } + + /* assuming that guard expression cannot be stripped as + * strictly speaking you would have to strip the whole guard to + * make syntactical sense, which you can't do. as such, only + * strip sub-expressions */ + if (match_arm.has_match_arm_guard ()) + { + auto &guard_expr = match_arm.get_guard_expr (); + guard_expr->accept_vis (*this); + if (guard_expr->is_marked_for_strip ()) + rust_error_at (guard_expr->get_locus_slow (), + "cannot strip expression in this position - outer " + "attributes not allowed"); + } + + // strip sub-expressions from match cases + auto &case_expr = match_case.get_expr (); + case_expr->accept_vis (*this); + if (case_expr->is_marked_for_strip ()) + rust_error_at (case_expr->get_locus_slow (), + "cannot strip expression in this position - outer " + "attributes not allowed"); + + // increment to next case if haven't continued + ++it; + } + +#if 0 for (int i = 0; i < match_cases.size ();) { auto &match_case = match_cases[i]; @@ -2101,6 +2150,7 @@ public: // increment to next case if haven't continued i++; } +#endif } void visit (AST::AwaitExpr &expr) override { @@ -2864,6 +2914,28 @@ public: /* strip function parameters if required - this is specifically * allowed by spec */ auto ¶ms = item.get_function_params (); + for (auto it = params.begin (); it != params.end ();) + { + auto ¶m = *it; + + auto ¶m_attrs = param.get_outer_attrs (); + expander.expand_cfg_attrs (param_attrs); + if (expander.fails_cfg (param_attrs)) + { + it = params.erase (it); + continue; + } + + auto &type = param.get_type (); + type->accept_vis (*this); + if (type->is_marked_for_strip ()) + rust_error_at (type->get_locus_slow (), + "cannot strip type in this position"); + + // increment if nothing else happens + ++it; + } +#if 0 for (int i = 0; i < params.size ();) { auto ¶m = params[i]; @@ -2885,6 +2957,7 @@ public: // increment if nothing else happens i++; } +#endif /* NOTE: these are extern function params, which may have different * rules and restrictions to "normal" function params. So expansion * handled separately. */ @@ -3419,6 +3492,28 @@ public: // presumably function params can be stripped auto ¶ms = type.get_function_params (); + for (auto it = params.begin (); it != params.end ();) + { + auto ¶m = *it; + + auto ¶m_attrs = param.get_outer_attrs (); + expander.expand_cfg_attrs (param_attrs); + if (expander.fails_cfg (param_attrs)) + { + it = params.erase (it); + continue; + } + + auto &type = param.get_type (); + type->accept_vis (*this); + if (type->is_marked_for_strip ()) + rust_error_at (type->get_locus_slow (), + "cannot strip type in this position"); + + // increment if nothing else happens + ++it; + } +#if 0 for (int i = 0; i < params.size ();) { auto ¶m = params[i]; @@ -3440,6 +3535,7 @@ public: // increment if nothing else happens i++; } +#endif /* TODO: assuming that variadic nature cannot be stripped. If this * is not true, then have code here to do so. */ @@ -3574,6 +3670,20 @@ MacroExpander::expand_crate () // expand attributes recursively and strip items if required AttrVisitor attr_visitor (*this); auto &items = crate.items; + for (auto it = items.begin (); it != items.end ();) + { + auto &item = *it; + + // mark for stripping if required + item->accept_vis (attr_visitor); + + if (item->is_marked_for_strip ()) + it = items.erase (it); + else + ++it; + } + +#if 0 for (int i = 0; i < items.size ();) { auto &item = items[i]; @@ -3586,6 +3696,7 @@ MacroExpander::expand_crate () else i++; } +#endif // TODO: should recursive attribute and macro expansion be done in the same // transversal? Or in separate ones like currently? -- cgit v1.1 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 ++++++++------- gcc/rust/expand/rust-macro-expand.cc | 6 ++++ gcc/rust/expand/rust-macro-expand.h | 4 +-- gcc/rust/parse/rust-parse-impl.h | 57 ++++++++++++++++-------------------- 6 files changed, 98 insertions(+), 54 deletions(-) (limited to 'gcc') 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 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 &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 &attrs); bool fails_cfg (std::vector &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::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::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::parse_macro_invocation_semi ( { // as this is the end, allow recovery (probably) - may change return std::unique_ptr ( - 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::parse_macro_invocation_semi ( lexer.peek_token ()->get_token_description ()); return std::unique_ptr ( - 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::parse_macro_invocation ( Location macro_locus = macro_path.get_locus (); return std::unique_ptr ( - 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::parse_type () AST::DelimTokenTree tok_tree = parse_delim_token_tree (); return std::unique_ptr ( - new AST::MacroInvocation (std::move (macro_path), - std::move (tok_tree), - std::vector (), - 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::parse_type_no_bounds () AST::DelimTokenTree tok_tree = parse_delim_token_tree (); return std::unique_ptr ( - new AST::MacroInvocation (std::move (macro_path), - std::move (tok_tree), - std::vector (), - 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::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 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 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::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::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 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 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::parse_macro_invocation_partial ( Location macro_locus = converted_path.get_locus (); return std::unique_ptr ( - 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)); } -- cgit v1.1 From 627121b6996146d7b14f2ad848f416682b1ce0fe Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Tue, 22 Dec 2020 12:17:34 +0800 Subject: Added code to expand cfg! macros Moved function body to prevent compile issues Moved around some definitions in rust-ast.h in attempt to prevent compile error --- gcc/rust/ast/rust-ast-full-test.cc | 30 ++- gcc/rust/ast/rust-ast.h | 296 ++++++++++++++---------- gcc/rust/ast/rust-expr.h | 2 + gcc/rust/expand/rust-macro-expand.cc | 309 +++++++++++++++----------- gcc/rust/expand/rust-macro-expand.h | 5 +- gcc/testsuite/rust.test/compilable/arrays2.rs | 66 ++++++ 6 files changed, 446 insertions(+), 262 deletions(-) create mode 100644 gcc/testsuite/rust.test/compilable/arrays2.rs (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index f0a3990..e6bcdb3 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -4628,8 +4628,8 @@ ModuleBodied::add_crate_name (std::vector &names) const void Attribute::parse_attr_to_meta_item () { - // only parse if has attribute input - if (!has_attr_input ()) + // only parse if has attribute input and not already parsed + if (!has_attr_input () || is_parsed_to_meta_item ()) return; std::unique_ptr converted_input ( @@ -4639,7 +4639,7 @@ Attribute::parse_attr_to_meta_item () attr_input = std::move (converted_input); } -AttrInput * +AttrInputMetaItemContainer * DelimTokenTree::parse_to_meta_item () const { // must have token trees @@ -5391,34 +5391,40 @@ AttrInputMetaItemContainer::separate_cfg_attrs () const } bool -Attribute::check_cfg_predicate (const Session &session) +Attribute::check_cfg_predicate (const Session &session) const { /* assume that cfg predicate actually can exist, i.e. attribute has cfg or * cfg_attr path */ if (!has_attr_input () || (path.as_string () != "cfg" && path.as_string () != "cfg_attr")) return false; - - // TODO: maybe replace with storing a "has been parsed" variable? - parse_attr_to_meta_item (); - // can't be const because of this anyway + + // assume that it has already been parsed + if (!is_parsed_to_meta_item ()) + return false; return attr_input->check_cfg_predicate (session); } std::vector -Attribute::separate_cfg_attrs () +Attribute::separate_cfg_attrs () const { if (!has_attr_input () || path.as_string () != "cfg_attr") return {}; - // TODO: maybe replace with storing a "has been parsed" variable? - parse_attr_to_meta_item (); - // can't be const because of this anyway + // assume that it has already been parsed + if (!is_parsed_to_meta_item ()) + return {}; return attr_input->separate_cfg_attrs (); } +bool +Attribute::is_parsed_to_meta_item () const +{ + return has_attr_input () && attr_input->is_meta_item (); +} + /* Visitor implementations - these are short but inlining can't happen anyway * due to virtual functions and I didn't want to make the ast header includes * any longer than they already are. */ diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 58bc372..8ca89fa 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -362,6 +362,11 @@ public: } }; +// path-to-string inverse comparison operator +inline bool operator!= (const SimplePath& lhs, const std::string &rhs) { + return !(lhs == rhs); +} + // forward decl for Attribute class AttrInput; @@ -392,27 +397,9 @@ public: // default destructor ~Attribute () = default; - // Copy constructor must deep copy attr_input as unique pointer - /*Attribute (Attribute const &other) : path (other.path), locus (other.locus) - { - // guard to protect from null pointer dereference - if (other.attr_input != nullptr) - attr_input = other.attr_input->clone_attr_input (); - }*/ // no point in being defined inline as requires virtual call anyway Attribute (const Attribute &other); - // overload assignment operator to use custom clone method - /*Attribute &operator= (Attribute const &other) - { - path = other.path; - locus = other.locus; - // guard to protect from null pointer dereference - if (other.attr_input != nullptr) - attr_input = other.attr_input->clone_attr_input (); - - return *this; - }*/ // no point in being defined inline as requires virtual call anyway Attribute &operator= (const Attribute &other); @@ -489,7 +476,7 @@ public: std::string as_string () const; - // TODO: does this require visitor pattern as not polymorphic? + // no visitor pattern as not currently polymorphic const SimplePath &get_path () const { return path; } SimplePath &get_path () { return path; } @@ -498,10 +485,15 @@ public: void parse_attr_to_meta_item (); /* Determines whether cfg predicate is true and item with attribute should not - * be stripped. */ - bool check_cfg_predicate (const Session &session); + * be stripped. Attribute body must already be parsed to meta item. */ + bool check_cfg_predicate (const Session &session) const; + + // Returns whether body has been parsed to meta item form or not. + bool is_parsed_to_meta_item () const; - std::vector separate_cfg_attrs (); + /* Returns any attributes generated from cfg_attr attributes. Attribute body + * must already be parsed to meta item. */ + std::vector separate_cfg_attrs () const; protected: // not virtual as currently no subclasses of Attribute, but could be in future @@ -534,99 +526,14 @@ public: virtual std::vector separate_cfg_attrs () const { return {}; } + // Returns whether attr input has been parsed to meta item syntax. + virtual bool is_meta_item () const = 0; + protected: // pure virtual clone implementation virtual AttrInput *clone_attr_input_impl () const = 0; }; -// A token tree with delimiters -class DelimTokenTree : public TokenTree, public AttrInput -{ - DelimType delim_type; - std::vector > token_trees; - Location locus; - -protected: - DelimTokenTree *clone_delim_tok_tree_impl () const - { - return new DelimTokenTree (*this); - } - - /* Use covariance to implement clone function as returning a DelimTokenTree - * object */ - DelimTokenTree *clone_attr_input_impl () const override - { - return clone_delim_tok_tree_impl (); - } - - /* Use covariance to implement clone function as returning a DelimTokenTree - * object */ - DelimTokenTree *clone_token_tree_impl () const override - { - return clone_delim_tok_tree_impl (); - } - -public: - DelimTokenTree (DelimType delim_type, - std::vector > token_trees - = std::vector > (), - Location locus = Location ()) - : delim_type (delim_type), token_trees (std::move (token_trees)), - locus (locus) - {} - - // Copy constructor with vector clone - DelimTokenTree (DelimTokenTree const &other) - : delim_type (other.delim_type), locus (other.locus) - { - 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 with vector clone - DelimTokenTree &operator= (DelimTokenTree const &other) - { - delim_type = other.delim_type; - locus = other.locus; - - token_trees.reserve (other.token_trees.size ()); - for (const auto &e : other.token_trees) - token_trees.push_back (e->clone_token_tree ()); - - return *this; - } - - // move constructors - DelimTokenTree (DelimTokenTree &&other) = default; - DelimTokenTree &operator= (DelimTokenTree &&other) = default; - - static DelimTokenTree create_empty () { return DelimTokenTree (PARENS); } - - std::string as_string () const override; - - void accept_vis (ASTVisitor &vis) override; - - bool check_cfg_predicate (const Session &) const override - { - // this should never be called - should be converted first - return false; - } - - AttrInput *parse_to_meta_item () const override; - - std::vector > to_token_stream () const override; - - std::unique_ptr clone_delim_token_tree () const - { - return std::unique_ptr (clone_delim_tok_tree_impl ()); - } -}; - -/* Forward decl - definition moved to rust-expr.h as it requires LiteralExpr to - * be defined */ -class AttrInputLiteral; - // Forward decl - defined in rust-macro.h class MetaNameValueStr; @@ -679,7 +586,26 @@ public: : items (std::move (items)) {} - // no destructor definition required + // copy constructor with vector clone + AttrInputMetaItemContainer (const AttrInputMetaItemContainer &other) + { + items.reserve (other.items.size ()); + for (const auto &e : other.items) + items.push_back (e->clone_meta_item_inner ()); + } + + // copy assignment operator with vector clone + AttrInputMetaItemContainer & + operator= (const AttrInputMetaItemContainer &other) + { + AttrInput::operator= (other); + + items.reserve (other.items.size ()); + for (const auto &e : other.items) + items.push_back (e->clone_meta_item_inner ()); + + return *this; + } // default move constructors AttrInputMetaItemContainer (AttrInputMetaItemContainer &&other) = default; @@ -702,6 +628,12 @@ public: std::vector separate_cfg_attrs () const override; + bool is_meta_item () const override { return true; } + + // TODO: this mutable getter seems dodgy + std::vector > &get_items () { return items; } + const std::vector > &get_items () const { return items; } + protected: // Use covariance to implement clone function as returning this type AttrInputMetaItemContainer *clone_attr_input_impl () const override @@ -713,29 +645,98 @@ protected: { return new AttrInputMetaItemContainer (*this); } +}; - // copy constructor with vector clone - AttrInputMetaItemContainer (const AttrInputMetaItemContainer &other) +// A token tree with delimiters +class DelimTokenTree : public TokenTree, public AttrInput +{ + DelimType delim_type; + std::vector > token_trees; + Location locus; + +protected: + DelimTokenTree *clone_delim_tok_tree_impl () const { - items.reserve (other.items.size ()); - for (const auto &e : other.items) - items.push_back (e->clone_meta_item_inner ()); + return new DelimTokenTree (*this); } - // copy assignment operator with vector clone - AttrInputMetaItemContainer & - operator= (const AttrInputMetaItemContainer &other) + /* Use covariance to implement clone function as returning a DelimTokenTree + * object */ + DelimTokenTree *clone_attr_input_impl () const override { - AttrInput::operator= (other); + return clone_delim_tok_tree_impl (); + } - items.reserve (other.items.size ()); - for (const auto &e : other.items) - items.push_back (e->clone_meta_item_inner ()); + /* Use covariance to implement clone function as returning a DelimTokenTree + * object */ + DelimTokenTree *clone_token_tree_impl () const override + { + return clone_delim_tok_tree_impl (); + } + +public: + DelimTokenTree (DelimType delim_type, + std::vector > token_trees + = std::vector > (), + Location locus = Location ()) + : delim_type (delim_type), token_trees (std::move (token_trees)), + locus (locus) + {} + + // Copy constructor with vector clone + DelimTokenTree (DelimTokenTree const &other) + : delim_type (other.delim_type), locus (other.locus) + { + 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 with vector clone + DelimTokenTree &operator= (DelimTokenTree const &other) + { + delim_type = other.delim_type; + locus = other.locus; + + token_trees.reserve (other.token_trees.size ()); + for (const auto &e : other.token_trees) + token_trees.push_back (e->clone_token_tree ()); return *this; } + + // move constructors + DelimTokenTree (DelimTokenTree &&other) = default; + DelimTokenTree &operator= (DelimTokenTree &&other) = default; + + static DelimTokenTree create_empty () { return DelimTokenTree (PARENS); } + + std::string as_string () const override; + + void accept_vis (ASTVisitor &vis) override; + + bool check_cfg_predicate (const Session &) const override + { + // this should never be called - should be converted first + return false; + } + + AttrInputMetaItemContainer *parse_to_meta_item () const override; + + std::vector > to_token_stream () const override; + + std::unique_ptr clone_delim_token_tree () const + { + return std::unique_ptr (clone_delim_tok_tree_impl ()); + } + + bool is_meta_item () const override { return false; } }; +/* Forward decl - definition moved to rust-expr.h as it requires LiteralExpr to + * be defined */ +class AttrInputLiteral; + // abstract base meta item class class MetaItem : public MetaItemInner { @@ -1378,15 +1379,64 @@ private: SimplePath path; DelimTokenTree token_tree; + // One way of parsing the macro. Probably not applicable for all macros. + std::vector > parsed_items; + bool parsed_to_meta_item = false; + public: std::string as_string () const; MacroInvocData (SimplePath path, DelimTokenTree token_tree) : path (std::move (path)), token_tree (std::move (token_tree)) {} + // Copy constructor with vector clone + MacroInvocData (const MacroInvocData &other) : path (other.path), token_tree (other.token_tree), parsed_to_meta_item (other.parsed_to_meta_item) { + parsed_items.reserve (other.parsed_items.size ()); + for (const auto &e : other.parsed_items) + parsed_items.push_back (e->clone_meta_item_inner ()); + } + + // Copy assignment operator with vector clone + MacroInvocData &operator= (const MacroInvocData &other) + { + path = other.path; + token_tree = other.token_tree; + parsed_to_meta_item = other.parsed_to_meta_item; + + parsed_items.reserve (other.parsed_items.size ()); + for (const auto &e : other.parsed_items) + parsed_items.push_back (e->clone_meta_item_inner ()); + + return *this; + } + + // Move constructors + MacroInvocData (MacroInvocData &&other) = default; + MacroInvocData &operator= (MacroInvocData &&other) = default; + // 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 (); } + + // Returns whether the macro has been parsed already. + bool is_parsed () const { return parsed_to_meta_item; } + // TODO: update on other ways of parsing it + + // TODO: this mutable getter seems kinda dodgy + DelimTokenTree &get_delim_tok_tree () { return token_tree; } + const DelimTokenTree &get_delim_tok_tree () const { return token_tree; } + + // TODO: this mutable getter seems kinda dodgy + SimplePath &get_path () { return path; } + const SimplePath &get_path () const { return path; } + + void set_meta_item_output (std::vector > new_items) + { + parsed_items = std::move (new_items); + } + // TODO: mutable getter seems kinda dodgy + std::vector > &get_meta_items () { return parsed_items; } + const std::vector > &get_meta_items () const { return parsed_items; } }; /* A macro invocation item (or statement) AST node (i.e. semi-coloned macro diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index de011c1..89718bc 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -121,6 +121,8 @@ public: * cfg */ bool check_cfg_predicate (const Session &) const override { return false; } + bool is_meta_item () const override { return false; } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 53b2851..97e455e 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -22,7 +22,7 @@ public: auto &field_attrs = field.get_outer_attrs (); expander.expand_cfg_attrs (field_attrs); - if (expander.fails_cfg (field_attrs)) + if (expander.fails_cfg_with_expand (field_attrs)) { it = fields.erase (it); continue; @@ -39,14 +39,14 @@ public: ++it; } - #if 0 +#if 0 for (int i = 0; i < fields.size ();) { auto &field = fields[i]; auto &field_attrs = field.get_outer_attrs (); expander.expand_cfg_attrs (field_attrs); - if (expander.fails_cfg (field_attrs)) + if (expander.fails_cfg_with_expand (field_attrs)) { fields.erase (fields.begin () + i); continue; @@ -62,7 +62,7 @@ public: // if nothing else happens, increment i++; } - #endif +#endif } void expand_tuple_fields (std::vector &fields) @@ -73,7 +73,7 @@ public: auto &field_attrs = field.get_outer_attrs (); expander.expand_cfg_attrs (field_attrs); - if (expander.fails_cfg (field_attrs)) + if (expander.fails_cfg_with_expand (field_attrs)) { it = fields.erase (it); continue; @@ -90,14 +90,14 @@ public: ++it; } - #if 0 +#if 0 for (int i = 0; i < fields.size ();) { auto &field = fields[i]; auto &field_attrs = field.get_outer_attrs (); expander.expand_cfg_attrs (field_attrs); - if (expander.fails_cfg (field_attrs)) + if (expander.fails_cfg_with_expand (field_attrs)) { fields.erase (fields.begin () + i); continue; @@ -113,7 +113,7 @@ public: // if nothing else happens, increment i++; } - #endif +#endif } void expand_function_params (std::vector ¶ms) @@ -124,7 +124,7 @@ public: auto ¶m_attrs = param.get_outer_attrs (); expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg (param_attrs)) + if (expander.fails_cfg_with_expand (param_attrs)) { it = params.erase (it); continue; @@ -147,14 +147,14 @@ public: ++it; } - #if 0 +#if 0 for (int i = 0; i < params.size ();) { auto ¶m = params[i]; auto ¶m_attrs = param.get_outer_attrs (); expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg (param_attrs)) + if (expander.fails_cfg_with_expand (param_attrs)) { params.erase (params.begin () + i); continue; @@ -176,7 +176,7 @@ public: // increment i++; } - #endif +#endif } void expand_generic_args (AST::GenericArgs &args) @@ -229,7 +229,7 @@ public: auto ¶m_attrs = param.get_outer_attrs (); expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg (param_attrs)) + if (expander.fails_cfg_with_expand (param_attrs)) { it = params.erase (it); continue; @@ -254,14 +254,14 @@ public: ++it; } - #if 0 +#if 0 for (int i = 0; i < params.size ();) { auto ¶m = params[i]; auto ¶m_attrs = param.get_outer_attrs (); expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg (param_attrs)) + if (expander.fails_cfg_with_expand (param_attrs)) { params.erase (params.begin () + i); continue; @@ -285,7 +285,7 @@ public: // increment if found nothing else so far i++; } - #endif +#endif } void expand_self_param (AST::SelfParam &self_param) @@ -359,9 +359,8 @@ public: if (decl.has_where_clause ()) expand_where_clause (decl.get_where_clause ()); } - - template - void expand_pointer_allow_strip (T &values) + + template void expand_pointer_allow_strip (T &values) { for (auto it = values.begin (); it != values.end ();) { @@ -393,7 +392,7 @@ public: { // strip test based on outer attrs expander.expand_cfg_attrs (ident_expr.get_outer_attrs ()); - if (expander.fails_cfg (ident_expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (ident_expr.get_outer_attrs ())) { ident_expr.mark_for_strip (); return; @@ -411,7 +410,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (macro_invoc.get_outer_attrs ()); - if (expander.fails_cfg (macro_invoc.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (macro_invoc.get_outer_attrs ())) { macro_invoc.mark_for_strip (); return; @@ -428,7 +427,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (path.get_outer_attrs ()); - if (expander.fails_cfg (path.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (path.get_outer_attrs ())) { path.mark_for_strip (); return; @@ -484,7 +483,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (path.get_outer_attrs ()); - if (expander.fails_cfg (path.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (path.get_outer_attrs ())) { path.mark_for_strip (); return; @@ -511,7 +510,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -533,7 +532,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -553,7 +552,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -573,7 +572,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -593,7 +592,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -751,7 +750,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -760,7 +759,7 @@ public: /* strip test based on inner attrs - spec says these are inner * attributes, not outer attributes of inner expr */ expander.expand_cfg_attrs (expr.get_inner_attrs ()); - if (expander.fails_cfg (expr.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_inner_attrs ())) { expr.mark_for_strip (); return; @@ -824,7 +823,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -833,7 +832,7 @@ public: /* strip test based on inner attrs - spec says there are separate * inner attributes, not just outer attributes of inner exprs */ expander.expand_cfg_attrs (expr.get_inner_attrs ()); - if (expander.fails_cfg (expr.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_inner_attrs ())) { expr.mark_for_strip (); return; @@ -851,7 +850,7 @@ public: * having expansion code anyway. TODO */ // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -881,7 +880,7 @@ public: // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -890,7 +889,7 @@ public: /* strip test based on inner attrs - spec says these are inner * attributes, not outer attributes of inner expr */ expander.expand_cfg_attrs (expr.get_inner_attrs ()); - if (expander.fails_cfg (expr.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_inner_attrs ())) { expr.mark_for_strip (); return; @@ -920,7 +919,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -940,7 +939,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -949,7 +948,7 @@ public: /* strip test based on inner attrs - spec says these are inner * attributes, not outer attributes of inner expr */ expander.expand_cfg_attrs (expr.get_inner_attrs ()); - if (expander.fails_cfg (expr.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_inner_attrs ())) { expr.mark_for_strip (); return; @@ -992,7 +991,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1001,7 +1000,7 @@ public: /* strip test based on inner attrs - spec says these are inner * attributes, not outer attributes of inner expr */ expander.expand_cfg_attrs (expr.get_inner_attrs ()); - if (expander.fails_cfg (expr.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_inner_attrs ())) { expr.mark_for_strip (); return; @@ -1040,7 +1039,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1049,7 +1048,7 @@ public: /* strip test based on inner attrs - spec says these are inner * attributes, not outer attributes of inner expr */ expander.expand_cfg_attrs (expr.get_inner_attrs ()); - if (expander.fails_cfg (expr.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_inner_attrs ())) { expr.mark_for_strip (); return; @@ -1076,7 +1075,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1085,7 +1084,7 @@ public: /* strip test based on inner attrs - spec says these are inner * attributes, not outer attributes of inner expr */ expander.expand_cfg_attrs (expr.get_inner_attrs ()); - if (expander.fails_cfg (expr.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_inner_attrs ())) { expr.mark_for_strip (); return; @@ -1122,7 +1121,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1165,7 +1164,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1194,7 +1193,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1244,7 +1243,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1284,7 +1283,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1328,7 +1327,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1348,7 +1347,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1370,7 +1369,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1379,7 +1378,7 @@ public: /* strip test based on inner attrs - spec says there are inner * attributes, not just outer attributes of inner stmts */ expander.expand_cfg_attrs (expr.get_inner_attrs ()); - if (expander.fails_cfg (expr.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_inner_attrs ())) { expr.mark_for_strip (); return; @@ -1419,7 +1418,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1448,7 +1447,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1458,7 +1457,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1581,7 +1580,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1611,7 +1610,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1629,7 +1628,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1647,7 +1646,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1673,7 +1672,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1707,7 +1706,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1740,7 +1739,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1766,7 +1765,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1800,7 +1799,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1834,7 +1833,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1869,7 +1868,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1903,7 +1902,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1945,7 +1944,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -1987,7 +1986,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -2030,7 +2029,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -2038,7 +2037,7 @@ public: // inner attr strip test expander.expand_cfg_attrs (expr.get_inner_attrs ()); - if (expander.fails_cfg (expr.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_inner_attrs ())) { expr.mark_for_strip (); return; @@ -2061,7 +2060,7 @@ public: // strip match case based on outer attributes in match arm auto &match_arm = match_case.get_arm (); expander.expand_cfg_attrs (match_arm.get_outer_attrs ()); - if (expander.fails_cfg (match_arm.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (match_arm.get_outer_attrs ())) { // strip match case it = match_cases.erase (it); @@ -2110,7 +2109,7 @@ public: // strip match case based on outer attributes in match arm auto &match_arm = match_case.get_arm (); expander.expand_cfg_attrs (match_arm.get_outer_attrs ()); - if (expander.fails_cfg (match_arm.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (match_arm.get_outer_attrs ())) { // strip match case match_cases.erase (match_cases.begin () + i); @@ -2156,7 +2155,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -2175,7 +2174,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg (expr.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); return; @@ -2232,7 +2231,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (method.get_outer_attrs ()); - if (expander.fails_cfg (method.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (method.get_outer_attrs ())) { method.mark_for_strip (); return; @@ -2277,7 +2276,7 @@ public: { // strip test based on outer attrs expander.expand_cfg_attrs (module.get_outer_attrs ()); - if (expander.fails_cfg (module.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (module.get_outer_attrs ())) { module.mark_for_strip (); return; @@ -2285,7 +2284,7 @@ public: // strip test based on inner attrs expander.expand_cfg_attrs (module.get_inner_attrs ()); - if (expander.fails_cfg (module.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (module.get_inner_attrs ())) { module.mark_for_strip (); return; @@ -2314,7 +2313,7 @@ public: { // strip test based on outer attrs expander.expand_cfg_attrs (module.get_outer_attrs ()); - if (expander.fails_cfg (module.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (module.get_outer_attrs ())) { module.mark_for_strip (); return; @@ -2324,7 +2323,7 @@ public: { // strip test based on outer attrs expander.expand_cfg_attrs (crate.get_outer_attrs ()); - if (expander.fails_cfg (crate.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (crate.get_outer_attrs ())) { crate.mark_for_strip (); return; @@ -2346,7 +2345,7 @@ public: { // strip test based on outer attrs expander.expand_cfg_attrs (use_decl.get_outer_attrs ()); - if (expander.fails_cfg (use_decl.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (use_decl.get_outer_attrs ())) { use_decl.mark_for_strip (); return; @@ -2356,7 +2355,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (function.get_outer_attrs ()); - if (expander.fails_cfg (function.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (function.get_outer_attrs ())) { function.mark_for_strip (); return; @@ -2396,7 +2395,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (type_alias.get_outer_attrs ()); - if (expander.fails_cfg (type_alias.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (type_alias.get_outer_attrs ())) { type_alias.mark_for_strip (); return; @@ -2419,7 +2418,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (struct_item.get_outer_attrs ()); - if (expander.fails_cfg (struct_item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (struct_item.get_outer_attrs ())) { struct_item.mark_for_strip (); return; @@ -2440,7 +2439,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (tuple_struct.get_outer_attrs ()); - if (expander.fails_cfg (tuple_struct.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (tuple_struct.get_outer_attrs ())) { tuple_struct.mark_for_strip (); return; @@ -2461,7 +2460,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (item.get_outer_attrs ()); - if (expander.fails_cfg (item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (item.get_outer_attrs ())) { item.mark_for_strip (); return; @@ -2471,7 +2470,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (item.get_outer_attrs ()); - if (expander.fails_cfg (item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (item.get_outer_attrs ())) { item.mark_for_strip (); return; @@ -2485,7 +2484,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (item.get_outer_attrs ()); - if (expander.fails_cfg (item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (item.get_outer_attrs ())) { item.mark_for_strip (); return; @@ -2499,7 +2498,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (item.get_outer_attrs ()); - if (expander.fails_cfg (item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (item.get_outer_attrs ())) { item.mark_for_strip (); return; @@ -2519,7 +2518,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (enum_item.get_outer_attrs ()); - if (expander.fails_cfg (enum_item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (enum_item.get_outer_attrs ())) { enum_item.mark_for_strip (); return; @@ -2556,7 +2555,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (union_item.get_outer_attrs ()); - if (expander.fails_cfg (union_item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (union_item.get_outer_attrs ())) { union_item.mark_for_strip (); return; @@ -2577,7 +2576,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (const_item.get_outer_attrs ()); - if (expander.fails_cfg (const_item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (const_item.get_outer_attrs ())) { const_item.mark_for_strip (); return; @@ -2604,7 +2603,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (static_item.get_outer_attrs ()); - if (expander.fails_cfg (static_item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (static_item.get_outer_attrs ())) { static_item.mark_for_strip (); return; @@ -2631,7 +2630,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (item.get_outer_attrs ()); - if (expander.fails_cfg (item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (item.get_outer_attrs ())) { item.mark_for_strip (); return; @@ -2656,7 +2655,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (item.get_outer_attrs ()); - if (expander.fails_cfg (item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (item.get_outer_attrs ())) { item.mark_for_strip (); return; @@ -2681,7 +2680,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (item.get_outer_attrs ()); - if (expander.fails_cfg (item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (item.get_outer_attrs ())) { item.mark_for_strip (); return; @@ -2711,7 +2710,7 @@ public: { // initial test based on outer attrs expander.expand_cfg_attrs (item.get_outer_attrs ()); - if (expander.fails_cfg (item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (item.get_outer_attrs ())) { item.mark_for_strip (); return; @@ -2728,7 +2727,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (trait.get_outer_attrs ()); - if (expander.fails_cfg (trait.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (trait.get_outer_attrs ())) { trait.mark_for_strip (); return; @@ -2736,7 +2735,7 @@ public: // strip test based on inner attrs expander.expand_cfg_attrs (trait.get_inner_attrs ()); - if (expander.fails_cfg (trait.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (trait.get_inner_attrs ())) { trait.mark_for_strip (); return; @@ -2779,7 +2778,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (impl.get_outer_attrs ()); - if (expander.fails_cfg (impl.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (impl.get_outer_attrs ())) { impl.mark_for_strip (); return; @@ -2787,7 +2786,7 @@ public: // strip test based on inner attrs expander.expand_cfg_attrs (impl.get_inner_attrs ()); - if (expander.fails_cfg (impl.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (impl.get_inner_attrs ())) { impl.mark_for_strip (); return; @@ -2829,7 +2828,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (impl.get_outer_attrs ()); - if (expander.fails_cfg (impl.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (impl.get_outer_attrs ())) { impl.mark_for_strip (); return; @@ -2837,7 +2836,7 @@ public: // strip test based on inner attrs expander.expand_cfg_attrs (impl.get_inner_attrs ()); - if (expander.fails_cfg (impl.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (impl.get_inner_attrs ())) { impl.mark_for_strip (); return; @@ -2885,7 +2884,7 @@ public: { // strip test based on outer attrs expander.expand_cfg_attrs (item.get_outer_attrs ()); - if (expander.fails_cfg (item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (item.get_outer_attrs ())) { item.mark_for_strip (); return; @@ -2901,7 +2900,7 @@ public: { // strip test based on outer attrs expander.expand_cfg_attrs (item.get_outer_attrs ()); - if (expander.fails_cfg (item.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (item.get_outer_attrs ())) { item.mark_for_strip (); return; @@ -2920,7 +2919,7 @@ public: auto ¶m_attrs = param.get_outer_attrs (); expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg (param_attrs)) + if (expander.fails_cfg_with_expand (param_attrs)) { it = params.erase (it); continue; @@ -2942,7 +2941,7 @@ public: auto ¶m_attrs = param.get_outer_attrs (); expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg (param_attrs)) + if (expander.fails_cfg_with_expand (param_attrs)) { params.erase (params.begin () + i); continue; @@ -2981,7 +2980,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (block.get_outer_attrs ()); - if (expander.fails_cfg (block.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (block.get_outer_attrs ())) { block.mark_for_strip (); return; @@ -2989,7 +2988,7 @@ public: // strip test based on inner attrs expander.expand_cfg_attrs (block.get_inner_attrs ()); - if (expander.fails_cfg (block.get_inner_attrs ())) + if (expander.fails_cfg_with_expand (block.get_inner_attrs ())) { block.mark_for_strip (); return; @@ -3023,7 +3022,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (rules_def.get_outer_attrs ()); - if (expander.fails_cfg (rules_def.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (rules_def.get_outer_attrs ())) { rules_def.mark_for_strip (); return; @@ -3035,7 +3034,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (macro_invoc.get_outer_attrs ()); - if (expander.fails_cfg (macro_invoc.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (macro_invoc.get_outer_attrs ())) { macro_invoc.mark_for_strip (); return; @@ -3110,7 +3109,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (field.get_outer_attrs ()); - if (expander.fails_cfg (field.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (field.get_outer_attrs ())) { field.mark_for_strip (); return; @@ -3127,7 +3126,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (field.get_outer_attrs ()); - if (expander.fails_cfg (field.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (field.get_outer_attrs ())) { field.mark_for_strip (); return; @@ -3144,7 +3143,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (field.get_outer_attrs ()); - if (expander.fails_cfg (field.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (field.get_outer_attrs ())) { field.mark_for_strip (); return; @@ -3187,7 +3186,7 @@ public: if (elems.has_etc ()) { expander.expand_cfg_attrs (elems.get_etc_outer_attrs ()); - if (expander.fails_cfg (elems.get_etc_outer_attrs ())) + if (expander.fails_cfg_with_expand (elems.get_etc_outer_attrs ())) elems.strip_etc (); } } @@ -3310,7 +3309,7 @@ public: { // initial strip test based on outer attrs expander.expand_cfg_attrs (stmt.get_outer_attrs ()); - if (expander.fails_cfg (stmt.get_outer_attrs ())) + if (expander.fails_cfg_with_expand (stmt.get_outer_attrs ())) { stmt.mark_for_strip (); return; @@ -3498,7 +3497,7 @@ public: auto ¶m_attrs = param.get_outer_attrs (); expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg (param_attrs)) + if (expander.fails_cfg_with_expand (param_attrs)) { it = params.erase (it); continue; @@ -3520,7 +3519,7 @@ public: auto ¶m_attrs = param.get_outer_attrs (); expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg (param_attrs)) + if (expander.fails_cfg_with_expand (param_attrs)) { params.erase (params.begin () + i); continue; @@ -3553,10 +3552,46 @@ public: } }; -AST::Literal +void +MacroExpander::parse_macro_to_meta_item (AST::MacroInvocData &invoc) +{ + // only parse if not already parsed + if (invoc.is_parsed ()) + return; + + std::unique_ptr converted_input ( + invoc.get_delim_tok_tree ().parse_to_meta_item ()); + + if (converted_input == nullptr) + { + fprintf (stderr, "DEBUG: failed to parse macro to meta item\n"); + // TODO: do something now? is this an actual error? + } + else + { + std::vector > meta_items (std::move (converted_input->get_items ())); + invoc.set_meta_item_output (std::move (meta_items)); + } +} + +AST::Literal MacroExpander::expand_cfg_macro (AST::MacroInvocData &invoc) { - return AST::Literal::create_error (); + // only allow on cfg macros + if (invoc.get_path () != "cfg") + return AST::Literal::create_error (); + + parse_macro_to_meta_item (invoc); + + // TODO: assuming that cfg! macros can only have one meta item inner, like cfg attributes + if (invoc.get_meta_items ().size () != 1) + return AST::Literal::create_error (); + + bool result = invoc.get_meta_items ()[0]->check_cfg_predicate (session); + if (result) + return AST::Literal ("true", AST::Literal::BOOL); + else + return AST::Literal ("false", AST::Literal::BOOL); } void @@ -3580,7 +3615,7 @@ MacroExpander::expand_invoc (std::unique_ptr &invoc) - derive or legacy derive - "token-based" vs "AST-based" - else is unreachable - derive container macro - unreachable*/ - + #if 0 // macro_rules macro test code auto rule_def = find_rules_def(invoc->get_path()); @@ -3602,11 +3637,11 @@ MacroExpander::expand_invoc (std::unique_ptr &invoc) } /* Determines whether any cfg predicate is false and hence item with attributes - * should be stripped. */ + * should be stripped. Note that attributes must be expanded before calling. */ bool -MacroExpander::fails_cfg (std::vector &attrs) +MacroExpander::fails_cfg (const std::vector &attrs) const { - for (auto &attr : attrs) + for (const auto &attr : attrs) { if (attr.get_path () == "cfg" && !attr.check_cfg_predicate (session)) return true; @@ -3614,6 +3649,25 @@ MacroExpander::fails_cfg (std::vector &attrs) return false; } +/* Determines whether any cfg predicate is false and hence item with attributes + * should be stripped. Will expand attributes as well. */ +bool +MacroExpander::fails_cfg_with_expand (std::vector &attrs) const +{ + for (auto &attr : attrs) + { + if (attr.get_path () == "cfg") + { + if (!attr.is_parsed_to_meta_item ()) + attr.parse_attr_to_meta_item (); + + if (!attr.check_cfg_predicate (session)) + return true; + } + } + return false; +} + // Expands cfg_attr attributes. void MacroExpander::expand_cfg_attrs (std::vector &attrs) @@ -3623,6 +3677,9 @@ MacroExpander::expand_cfg_attrs (std::vector &attrs) auto &attr = attrs[i]; if (attr.get_path () == "cfg_attr") { + if (!attr.is_parsed_to_meta_item ()) + attr.parse_attr_to_meta_item (); + if (attr.check_cfg_predicate (session)) { // split off cfg_attr @@ -3665,7 +3722,7 @@ MacroExpander::expand_crate () // expand crate cfg_attr attributes expand_cfg_attrs (crate.inner_attrs); - if (fails_cfg (crate.inner_attrs)) + if (fails_cfg_with_expand (crate.inner_attrs)) { // basically, delete whole crate crate.strip_crate (); diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index 6752090..0218600 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -42,9 +42,12 @@ struct MacroExpander void expand_invoc (std::unique_ptr &invoc); void expand_cfg_attrs (std::vector &attrs); - bool fails_cfg (std::vector &attr); + bool fails_cfg (const std::vector &attr) const; + bool fails_cfg_with_expand (std::vector &attrs) const; // Expand the data of a cfg! macro. + void parse_macro_to_meta_item (AST::MacroInvocData &invoc); + // Get the literal representation of a cfg! macro. AST::Literal expand_cfg_macro (AST::MacroInvocData &invoc); private: diff --git a/gcc/testsuite/rust.test/compilable/arrays2.rs b/gcc/testsuite/rust.test/compilable/arrays2.rs new file mode 100644 index 0000000..974e346 --- /dev/null +++ b/gcc/testsuite/rust.test/compilable/arrays2.rs @@ -0,0 +1,66 @@ +fn main() { + // const, inferred array + let const_inferred_array = [0, 1, 2, 3]; + // const, inferred, copied array + let const_inferred_copied_array = [0; 4]; + + // const index of const, inferred array + let const_inferred_index = const_inferred_array[1]; + // variable index of const, inferred array + let some_var = 3; + let const_inferred_index_var = const_inferred_array[some_var]; + + // const index of const, inferred, copied array + let const_inferred_copied_index = const_inferred_copied_array[1]; + // variable index of const, inferred array + let const_inferred_copied_index_var = const_inferred_copied_array[some_var]; + + // mut, inferred array + let mut mut_inferred_array = [0, 1, 2, 3]; + // mut, inferred, copied array + let mut mut_inferred_copied_array = [0; 6]; + + // const index of mut, inferred array + let mut_inferred_index = mut_inferred_array[1]; + // variable index of mut, inferred array + let mut_inferred_index_var = mut_inferred_array[some_var]; + + // const index of mut, inferred, copied array + let mut_inferred_copied_index = mut_inferred_copied_array[1]; + // variable index of mut, inferred array + let mut_inferred_copied_index_var = mut_inferred_copied_array[some_var]; + + // const, typed array + let const_typed_array: [i32; 5] = [0, 1, 2, 3, 4]; + // const, typed, copied array + let const_typed_copied_array: [i32; 4] = [2; 4]; + + // const index of const, typed array + let const_typed_index = const_typed_array[1]; + // variable index of const, typed array + let const_typed_index_var = const_typed_array[some_var]; + + // const index of const, typed, copied array + let const_typed_copied_index = const_typed_copied_array[1]; + // variable index of const, typed array + let const_typed_copied_index_var = const_typed_copied_array[some_var]; + + // mut, typed array + let mut mut_typed_array: [i32; 4] = [0, 1, 2, 3]; + // mut, typed, copied array + let mut mut_typed_copied_array: [i32; 4] = [0; 4]; + + // const index of mut, typed array + let mut_typed_index = mut_typed_array[1]; + // variable index of mut, typed array + let mut_typed_index_var = mut_typed_array[some_var]; + + // const index of mut, typed, copied array + let mut_typed_copied_index = mut_typed_copied_array[1]; + // variable index of mut, typed array + let mut_typed_copied_index_var = mut_typed_copied_array[some_var]; + + + // index + 1 expression + let some_thing = mut_inferred_copied_array[some_var + 1]; +} \ No newline at end of file -- cgit v1.1 From 7299d6d84ac4a1e8118915b00d361e7251e32b65 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Fri, 25 Dec 2020 16:25:40 +0800 Subject: Test a new ASTFragment union representation --- gcc/rust/ast/rust-ast-full-test.cc | 14 +-- gcc/rust/ast/rust-ast.h | 56 ++++++---- gcc/rust/ast/rust-macro.h | 143 ++++++++++++++++++++++++-- gcc/rust/expand/rust-macro-expand.cc | 35 ++++--- gcc/rust/expand/rust-macro-expand.h | 9 +- gcc/testsuite/rust.test/compilable/arrays2.rs | 66 ------------ gcc/testsuite/rust.test/compilable/arrays3.rs | 66 ++++++++++++ 7 files changed, 275 insertions(+), 114 deletions(-) delete mode 100644 gcc/testsuite/rust.test/compilable/arrays2.rs create mode 100644 gcc/testsuite/rust.test/compilable/arrays3.rs (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index e6bcdb3..1329978 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -1358,9 +1358,9 @@ TypeAlias::as_string () const str += "\n Where clause: "; if (!has_where_clause ()) - str += "none"; + str += "none"; else - str += where_clause.as_string (); + str += where_clause.as_string (); str += "\n Type: " + existing_type->as_string (); @@ -5398,7 +5398,7 @@ Attribute::check_cfg_predicate (const Session &session) const if (!has_attr_input () || (path.as_string () != "cfg" && path.as_string () != "cfg_attr")) return false; - + // assume that it has already been parsed if (!is_parsed_to_meta_item ()) return false; @@ -5419,10 +5419,10 @@ Attribute::separate_cfg_attrs () const return attr_input->separate_cfg_attrs (); } -bool -Attribute::is_parsed_to_meta_item () const -{ - return has_attr_input () && attr_input->is_meta_item (); +bool +Attribute::is_parsed_to_meta_item () const +{ + return has_attr_input () && attr_input->is_meta_item (); } /* Visitor implementations - these are short but inlining can't happen anyway diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 8ca89fa..46dde8c 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -363,7 +363,9 @@ public: }; // path-to-string inverse comparison operator -inline bool operator!= (const SimplePath& lhs, const std::string &rhs) { +inline bool +operator!= (const SimplePath &lhs, const std::string &rhs) +{ return !(lhs == rhs); } @@ -632,7 +634,10 @@ public: // TODO: this mutable getter seems dodgy std::vector > &get_items () { return items; } - const std::vector > &get_items () const { return items; } + const std::vector > &get_items () const + { + return items; + } protected: // Use covariance to implement clone function as returning this type @@ -1371,7 +1376,7 @@ protected: virtual ExternalItem *clone_external_item_impl () const = 0; }; -/* Data structure to store the data used in macro invocations and macro +/* Data structure to store the data used in macro invocations and macro * invocations with semicolons. */ struct MacroInvocData { @@ -1386,11 +1391,15 @@ private: public: std::string as_string () const; - MacroInvocData (SimplePath path, DelimTokenTree token_tree) - : path (std::move (path)), token_tree (std::move (token_tree)) {} - + MacroInvocData (SimplePath path, DelimTokenTree token_tree) + : path (std::move (path)), token_tree (std::move (token_tree)) + {} + // Copy constructor with vector clone - MacroInvocData (const MacroInvocData &other) : path (other.path), token_tree (other.token_tree), parsed_to_meta_item (other.parsed_to_meta_item) { + MacroInvocData (const MacroInvocData &other) + : path (other.path), token_tree (other.token_tree), + parsed_to_meta_item (other.parsed_to_meta_item) + { parsed_items.reserve (other.parsed_items.size ()); for (const auto &e : other.parsed_items) parsed_items.push_back (e->clone_meta_item_inner ()); @@ -1413,7 +1422,7 @@ public: // Move constructors MacroInvocData (MacroInvocData &&other) = default; MacroInvocData &operator= (MacroInvocData &&other) = default; - + // 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 (); } @@ -1430,13 +1439,20 @@ public: SimplePath &get_path () { return path; } const SimplePath &get_path () const { return path; } - void set_meta_item_output (std::vector > new_items) - { - parsed_items = std::move (new_items); + void + set_meta_item_output (std::vector > new_items) + { + parsed_items = std::move (new_items); } // TODO: mutable getter seems kinda dodgy - std::vector > &get_meta_items () { return parsed_items; } - const std::vector > &get_meta_items () const { return parsed_items; } + std::vector > &get_meta_items () + { + return parsed_items; + } + const std::vector > &get_meta_items () const + { + return parsed_items; + } }; /* A macro invocation item (or statement) AST node (i.e. semi-coloned macro @@ -1467,10 +1483,11 @@ public: 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) {} + 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 @@ -1525,7 +1542,10 @@ public: 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 (); } + 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 3971572..eeb8ecf 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -349,11 +349,12 @@ public: : 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) {} - + 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 final override { return get_locus (); } @@ -361,7 +362,10 @@ public: // Invalid if path is empty, so base stripping on that. void mark_for_strip () override { invoc_data.mark_for_strip (); } - bool is_marked_for_strip () const override { return invoc_data.is_marked_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 @@ -586,6 +590,133 @@ protected: } }; +/* A "tagged union" describing a single AST node. Due to technical difficulties + * with unions, had to use raw pointers. */ +struct SingleASTNode +{ + public: + union { + std::unique_ptr expr; + std::unique_ptr stmt; + std::unique_ptr item; + std::unique_ptr type; + std::unique_ptr pattern; + std::unique_ptr trait_item; + std::unique_ptr inherent_impl_item; + std::unique_ptr trait_impl_item; + std::unique_ptr external_item; + }; + + enum tag_types { + EXPR, + STMT, + ITEM, + TYPE, + PATTERN, + TRAIT_ITEM, + INHERENT_IMPL_ITEM, + TRAIT_IMPL_ITEM, + EXTERNAL_ITEM + // TODO: other types like inside macro_rules? + }; + + SingleASTNode (std::unique_ptr expr) : tag (EXPR), expr (std::move (expr)) { + + } + + SingleASTNode (std::unique_ptr stmt) : tag (STMT), stmt (std::move (stmt)) { + + } + + SingleASTNode (std::unique_ptr item) : tag (ITEM), item (std::move (item)) { + + } + + SingleASTNode (std::unique_ptr type) : tag (TYPE), type (std::move (type)) { + + } + + SingleASTNode (std::unique_ptr pattern) : tag (PATTERN), pattern (std::move (pattern)) { + + } + + SingleASTNode (std::unique_ptr trait_item) : tag (TRAIT_ITEM), trait_item (std::move (trait_item)) { + + } + + SingleASTNode (std::unique_ptr inherent_impl_item) : tag (INHERENT_IMPL_ITEM), inherent_impl_item (std::move (inherent_impl_item)) { + + } + + SingleASTNode (std::unique_ptr trait_impl_item) : tag (TRAIT_IMPL_ITEM), trait_impl_item (std::move (trait_impl_item)) { + + } + + SingleASTNode (std::unique_ptr external_item) : tag (EXTERNAL_ITEM), external_item (std::move (external_item)) { + + } + + ~SingleASTNode () { + switch (tag) { + case EXPR: + expr.~unique_ptr (); + break; + case STMT: + stmt.~unique_ptr (); + break; + case ITEM: + item.~unique_ptr (); + break; + case TYPE: + type.~unique_ptr (); + break; + case PATTERN: + pattern.~unique_ptr (); + break; + case TRAIT_ITEM: + trait_item.~unique_ptr (); + break; + case INHERENT_IMPL_ITEM: + inherent_impl_item.~unique_ptr (); + break; + case TRAIT_IMPL_ITEM: + trait_impl_item.~unique_ptr (); + break; + case EXTERNAL_ITEM: + external_item.~unique_ptr (); + break; + default: + // should not happen + gcc_unreachable (); + break; + } + } + + private: + tag_types tag; + +}; + +/* Basically, a "fragment" that can be incorporated into the AST, created as + * a result of macro expansion. Really annoying to work with due to the fact + * that macros can really expand to anything. As such, horrible representation + * at the moment. */ +struct ASTFragment +{ +private: + /* basic idea: essentially, a vector of tagged unions of different AST node + * types. Now, this could actually be stored without a tagged union if the + * different AST node types had a unified parent, but that would create + * issues with the diamond problem or significant performance penalties. So + * a tagged union had to be used instead. A vector is used to represent the + * ability for a macro to expand to two statements, for instance. */ + + std::vector nodes; + +public: + ASTFragment (std::vector nodes) : nodes (std::move (nodes)) {} +}; + // Object that parses macros from a token stream. /* TODO: would "AttributeParser" be a better name? MetaItems are only for * attributes, I believe */ diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 97e455e..02dcf87 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -3561,17 +3561,18 @@ MacroExpander::parse_macro_to_meta_item (AST::MacroInvocData &invoc) std::unique_ptr converted_input ( invoc.get_delim_tok_tree ().parse_to_meta_item ()); - - if (converted_input == nullptr) - { - fprintf (stderr, "DEBUG: failed to parse macro to meta item\n"); - // TODO: do something now? is this an actual error? - } + + if (converted_input == nullptr) + { + fprintf (stderr, "DEBUG: failed to parse macro to meta item\n"); + // TODO: do something now? is this an actual error? + } else - { - std::vector > meta_items (std::move (converted_input->get_items ())); - invoc.set_meta_item_output (std::move (meta_items)); - } + { + std::vector > meta_items ( + std::move (converted_input->get_items ())); + invoc.set_meta_item_output (std::move (meta_items)); + } } AST::Literal @@ -3583,17 +3584,23 @@ MacroExpander::expand_cfg_macro (AST::MacroInvocData &invoc) parse_macro_to_meta_item (invoc); - // TODO: assuming that cfg! macros can only have one meta item inner, like cfg attributes - if (invoc.get_meta_items ().size () != 1) + /* TODO: assuming that cfg! macros can only have one meta item inner, like cfg + * attributes */ + if (invoc.get_meta_items ().size () != 1) return AST::Literal::create_error (); - + bool result = invoc.get_meta_items ()[0]->check_cfg_predicate (session); if (result) return AST::Literal ("true", AST::Literal::BOOL); - else + else return AST::Literal ("false", AST::Literal::BOOL); } +AST::ASTFragment +MacroExpander::expand_decl_macro (AST::MacroInvocData &invoc, AST::MacroRulesDefinition &rules_def) { + +} + void MacroExpander::expand_invoc (std::unique_ptr &invoc) { diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index 0218600..66e506d 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -16,9 +16,9 @@ struct ExpansionCfg { // features? unsigned int recursion_limit = 50; // TODO: determine default recursion limit - // trace macros? - // should test? - // more default stuff? + // trace macros? + // should test? + // more default stuff? }; // Object used to store shared data (between functions) for macro expansion. @@ -41,6 +41,9 @@ struct MacroExpander // should this be public or private? void expand_invoc (std::unique_ptr &invoc); + // Expands a single declarative macro. + AST::ASTFragment expand_decl_macro (AST::MacroInvocData &invoc, AST::MacroRulesDefinition &rules_def); + void expand_cfg_attrs (std::vector &attrs); bool fails_cfg (const std::vector &attr) const; bool fails_cfg_with_expand (std::vector &attrs) const; diff --git a/gcc/testsuite/rust.test/compilable/arrays2.rs b/gcc/testsuite/rust.test/compilable/arrays2.rs deleted file mode 100644 index 974e346..0000000 --- a/gcc/testsuite/rust.test/compilable/arrays2.rs +++ /dev/null @@ -1,66 +0,0 @@ -fn main() { - // const, inferred array - let const_inferred_array = [0, 1, 2, 3]; - // const, inferred, copied array - let const_inferred_copied_array = [0; 4]; - - // const index of const, inferred array - let const_inferred_index = const_inferred_array[1]; - // variable index of const, inferred array - let some_var = 3; - let const_inferred_index_var = const_inferred_array[some_var]; - - // const index of const, inferred, copied array - let const_inferred_copied_index = const_inferred_copied_array[1]; - // variable index of const, inferred array - let const_inferred_copied_index_var = const_inferred_copied_array[some_var]; - - // mut, inferred array - let mut mut_inferred_array = [0, 1, 2, 3]; - // mut, inferred, copied array - let mut mut_inferred_copied_array = [0; 6]; - - // const index of mut, inferred array - let mut_inferred_index = mut_inferred_array[1]; - // variable index of mut, inferred array - let mut_inferred_index_var = mut_inferred_array[some_var]; - - // const index of mut, inferred, copied array - let mut_inferred_copied_index = mut_inferred_copied_array[1]; - // variable index of mut, inferred array - let mut_inferred_copied_index_var = mut_inferred_copied_array[some_var]; - - // const, typed array - let const_typed_array: [i32; 5] = [0, 1, 2, 3, 4]; - // const, typed, copied array - let const_typed_copied_array: [i32; 4] = [2; 4]; - - // const index of const, typed array - let const_typed_index = const_typed_array[1]; - // variable index of const, typed array - let const_typed_index_var = const_typed_array[some_var]; - - // const index of const, typed, copied array - let const_typed_copied_index = const_typed_copied_array[1]; - // variable index of const, typed array - let const_typed_copied_index_var = const_typed_copied_array[some_var]; - - // mut, typed array - let mut mut_typed_array: [i32; 4] = [0, 1, 2, 3]; - // mut, typed, copied array - let mut mut_typed_copied_array: [i32; 4] = [0; 4]; - - // const index of mut, typed array - let mut_typed_index = mut_typed_array[1]; - // variable index of mut, typed array - let mut_typed_index_var = mut_typed_array[some_var]; - - // const index of mut, typed, copied array - let mut_typed_copied_index = mut_typed_copied_array[1]; - // variable index of mut, typed array - let mut_typed_copied_index_var = mut_typed_copied_array[some_var]; - - - // index + 1 expression - let some_thing = mut_inferred_copied_array[some_var + 1]; -} \ No newline at end of file diff --git a/gcc/testsuite/rust.test/compilable/arrays3.rs b/gcc/testsuite/rust.test/compilable/arrays3.rs new file mode 100644 index 0000000..974e346 --- /dev/null +++ b/gcc/testsuite/rust.test/compilable/arrays3.rs @@ -0,0 +1,66 @@ +fn main() { + // const, inferred array + let const_inferred_array = [0, 1, 2, 3]; + // const, inferred, copied array + let const_inferred_copied_array = [0; 4]; + + // const index of const, inferred array + let const_inferred_index = const_inferred_array[1]; + // variable index of const, inferred array + let some_var = 3; + let const_inferred_index_var = const_inferred_array[some_var]; + + // const index of const, inferred, copied array + let const_inferred_copied_index = const_inferred_copied_array[1]; + // variable index of const, inferred array + let const_inferred_copied_index_var = const_inferred_copied_array[some_var]; + + // mut, inferred array + let mut mut_inferred_array = [0, 1, 2, 3]; + // mut, inferred, copied array + let mut mut_inferred_copied_array = [0; 6]; + + // const index of mut, inferred array + let mut_inferred_index = mut_inferred_array[1]; + // variable index of mut, inferred array + let mut_inferred_index_var = mut_inferred_array[some_var]; + + // const index of mut, inferred, copied array + let mut_inferred_copied_index = mut_inferred_copied_array[1]; + // variable index of mut, inferred array + let mut_inferred_copied_index_var = mut_inferred_copied_array[some_var]; + + // const, typed array + let const_typed_array: [i32; 5] = [0, 1, 2, 3, 4]; + // const, typed, copied array + let const_typed_copied_array: [i32; 4] = [2; 4]; + + // const index of const, typed array + let const_typed_index = const_typed_array[1]; + // variable index of const, typed array + let const_typed_index_var = const_typed_array[some_var]; + + // const index of const, typed, copied array + let const_typed_copied_index = const_typed_copied_array[1]; + // variable index of const, typed array + let const_typed_copied_index_var = const_typed_copied_array[some_var]; + + // mut, typed array + let mut mut_typed_array: [i32; 4] = [0, 1, 2, 3]; + // mut, typed, copied array + let mut mut_typed_copied_array: [i32; 4] = [0; 4]; + + // const index of mut, typed array + let mut_typed_index = mut_typed_array[1]; + // variable index of mut, typed array + let mut_typed_index_var = mut_typed_array[some_var]; + + // const index of mut, typed, copied array + let mut_typed_copied_index = mut_typed_copied_array[1]; + // variable index of mut, typed array + let mut_typed_copied_index_var = mut_typed_copied_array[some_var]; + + + // index + 1 expression + let some_thing = mut_inferred_copied_array[some_var + 1]; +} \ No newline at end of file -- cgit v1.1 From 859720937474816d4d386664d56d80a9e840f06f Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Fri, 25 Dec 2020 17:16:25 +0800 Subject: Rewrote SingleASTNode representation --- gcc/rust/ast/rust-macro.h | 49 +++++++++++++++++++++++++++---------- gcc/rust/expand/rust-macro-expand.h | 1 + 2 files changed, 37 insertions(+), 13 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index eeb8ecf..5f05985 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -591,22 +591,11 @@ protected: }; /* A "tagged union" describing a single AST node. Due to technical difficulties - * with unions, had to use raw pointers. */ + * with unions, this is actually a struct and so wastes space. FIXME */ +/* struct SingleASTNode { public: - union { - std::unique_ptr expr; - std::unique_ptr stmt; - std::unique_ptr item; - std::unique_ptr type; - std::unique_ptr pattern; - std::unique_ptr trait_item; - std::unique_ptr inherent_impl_item; - std::unique_ptr trait_impl_item; - std::unique_ptr external_item; - }; - enum tag_types { EXPR, STMT, @@ -656,6 +645,7 @@ struct SingleASTNode } + // destructor definition is required ~SingleASTNode () { switch (tag) { case EXPR: @@ -695,6 +685,39 @@ struct SingleASTNode private: tag_types tag; + union { + std::unique_ptr expr; + std::unique_ptr stmt; + std::unique_ptr item; + std::unique_ptr type; + std::unique_ptr pattern; + std::unique_ptr trait_item; + std::unique_ptr inherent_impl_item; + std::unique_ptr trait_impl_item; + std::unique_ptr external_item; + }; +}; +*/ +struct SingleASTNode { + std::unique_ptr expr; + std::unique_ptr stmt; + std::unique_ptr item; + std::unique_ptr type; + std::unique_ptr pattern; + std::unique_ptr trait_item; + std::unique_ptr inherent_impl_item; + std::unique_ptr trait_impl_item; + std::unique_ptr external_item; + + SingleASTNode (std::unique_ptr expr) : expr (std::move (expr)) {} + SingleASTNode (std::unique_ptr stmt) : stmt (std::move (stmt)) {} + SingleASTNode (std::unique_ptr item) : item (std::move (item)) {} + SingleASTNode (std::unique_ptr type) : type (std::move (type)) {} + SingleASTNode (std::unique_ptr pattern) : pattern (std::move (pattern)) {} + SingleASTNode (std::unique_ptr trait_item) : trait_item (std::move (trait_item)) {} + SingleASTNode (std::unique_ptr inherent_impl_item) : inherent_impl_item (std::move (inherent_impl_item)) {} + SingleASTNode (std::unique_ptr trait_impl_item) : trait_impl_item (std::move (trait_impl_item)) {} + SingleASTNode (std::unique_ptr external_item) : external_item (std::move (external_item)) {} }; /* Basically, a "fragment" that can be incorporated into the AST, created as diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index 66e506d..17adf0e 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -2,6 +2,7 @@ #define RUST_MACRO_EXPAND_H #include "rust-ast.h" +#include "rust-macro.h" // Provides objects and method prototypes for macro expansion -- cgit v1.1 From b5f86dca7e6b53ca3701ef01ae36070a760dff78 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Fri, 8 Jan 2021 15:08:14 +0800 Subject: Added final to some overriden methods for optimisation, planned macro parsing --- gcc/rust/ast/rust-ast.h | 26 +++++++-------- gcc/rust/ast/rust-expr.h | 61 ++++++++++++++++++------------------ gcc/rust/ast/rust-item.h | 12 ++++--- gcc/rust/ast/rust-macro.h | 21 ++++++++++--- gcc/rust/ast/rust-path.h | 26 ++++++++++----- gcc/rust/expand/rust-macro-expand.cc | 31 ++++++++++++++++-- 6 files changed, 113 insertions(+), 64 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 46dde8c..7700bf3 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -218,11 +218,11 @@ protected: /* Use covariance to implement clone function as returning this object rather * than base */ - Token *clone_token_tree_impl () const override { return clone_token_impl (); } + Token *clone_token_tree_impl () const final override { return clone_token_impl (); } /* Use covariance to implement clone function as returning this object rather * than base */ - Token *clone_macro_match_impl () const override + Token *clone_macro_match_impl () const final override { return clone_token_impl (); } @@ -641,7 +641,7 @@ public: protected: // Use covariance to implement clone function as returning this type - AttrInputMetaItemContainer *clone_attr_input_impl () const override + AttrInputMetaItemContainer *clone_attr_input_impl () const final override { return clone_attr_input_meta_item_container_impl (); } @@ -667,14 +667,14 @@ protected: /* Use covariance to implement clone function as returning a DelimTokenTree * object */ - DelimTokenTree *clone_attr_input_impl () const override + DelimTokenTree *clone_attr_input_impl () const final override { return clone_delim_tok_tree_impl (); } /* Use covariance to implement clone function as returning a DelimTokenTree * object */ - DelimTokenTree *clone_token_tree_impl () const override + DelimTokenTree *clone_token_tree_impl () const final override { return clone_delim_tok_tree_impl (); } @@ -930,7 +930,7 @@ public: /* downcasting hack from expr to use pratt parsing with * parse_expr_without_block */ - ExprWithoutBlock *as_expr_without_block () const override + ExprWithoutBlock *as_expr_without_block () const final override { return clone_expr_without_block_impl (); } @@ -956,7 +956,7 @@ public: std::string as_string () const override { return ident; } 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 (); } Identifier get_ident () const { return ident; } @@ -974,7 +974,7 @@ public: protected: // Clone method implementation - IdentifierExpr *clone_expr_without_block_impl () const override + IdentifierExpr *clone_expr_without_block_impl () const final override { return clone_identifier_expr_impl (); } @@ -1559,35 +1559,35 @@ protected: /* Use covariance to implement clone function as returning this object rather * than base */ - MacroInvocationSemi *clone_item_impl () const override + MacroInvocationSemi *clone_item_impl () const final override { return clone_macro_invocation_semi_impl (); } /* Use covariance to implement clone function as returning this object rather * than base */ - MacroInvocationSemi *clone_inherent_impl_item_impl () const override + MacroInvocationSemi *clone_inherent_impl_item_impl () const final override { return clone_macro_invocation_semi_impl (); } /* Use covariance to implement clone function as returning this object rather * than base */ - MacroInvocationSemi *clone_trait_impl_item_impl () const override + MacroInvocationSemi *clone_trait_impl_item_impl () const final override { return clone_macro_invocation_semi_impl (); } /* Use covariance to implement clone function as returning this object rather * than base */ - MacroInvocationSemi *clone_trait_item_impl () const override + MacroInvocationSemi *clone_trait_item_impl () const final override { return clone_macro_invocation_semi_impl (); } /* Use covariance to implement clone function as returning this object rather * than base */ - MacroInvocationSemi *clone_external_item_impl () const override + MacroInvocationSemi *clone_external_item_impl () const final override { return clone_macro_invocation_semi_impl (); } diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index 4fb4785..d531342 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -69,7 +69,7 @@ public: } 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 (); } Literal get_literal () const { return literal; } @@ -82,7 +82,7 @@ public: protected: /* Use covariance to implement clone function as returning this object rather * than base */ - LiteralExpr *clone_expr_without_block_impl () const override + LiteralExpr *clone_expr_without_block_impl () const final override { return clone_literal_expr_impl (); } @@ -238,7 +238,7 @@ protected: public: 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 (); } // Invalid if expr is null, so base stripping on that. void mark_for_strip () override { main_or_left_expr = nullptr; } @@ -861,7 +861,6 @@ class GroupedExpr : public ExprWithoutBlock { std::vector inner_attrs; std::unique_ptr expr_in_parens; - Location locus; public: @@ -910,7 +909,7 @@ public: GroupedExpr &operator= (GroupedExpr &&other) = default; 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; @@ -1148,7 +1147,7 @@ public: ArrayExpr &operator= (ArrayExpr &&other) = default; 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; @@ -1231,7 +1230,7 @@ public: ArrayIndexExpr &operator= (ArrayIndexExpr &&other) = default; 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; @@ -1329,7 +1328,7 @@ public: * comma, i.e. (0,) rather than (0) */ 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; @@ -1411,7 +1410,7 @@ public: TupleIndexExpr &operator= (TupleIndexExpr &&other) = default; 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; @@ -1484,7 +1483,7 @@ public: {} 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; @@ -1866,7 +1865,7 @@ public: StructExprTuple &operator= (StructExprTuple &&other) = default; 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; @@ -1903,7 +1902,7 @@ public: {} 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; @@ -2143,7 +2142,7 @@ public: EnumExprStruct &operator= (EnumExprStruct &&other) = default; 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; @@ -2208,7 +2207,7 @@ public: EnumExprTuple &operator= (EnumExprTuple &&other) = default; 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; @@ -2247,7 +2246,7 @@ public: {} 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; @@ -2326,7 +2325,7 @@ public: bool has_params () const { return !params.empty (); } 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; @@ -2429,7 +2428,7 @@ public: MethodCallExpr &operator= (MethodCallExpr &&other) = default; 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; @@ -2514,7 +2513,7 @@ public: FieldAccessExpr &operator= (FieldAccessExpr &&other) = default; 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; @@ -2647,7 +2646,7 @@ public: std::string as_string () const override; 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 (); } // TODO: this mutable getter seems really dodgy. Think up better way. const std::vector &get_params () const { return params; } @@ -2846,7 +2845,7 @@ public: protected: /* Use covariance to implement clone function as returning this object rather * than base */ - BlockExpr *clone_expr_with_block_impl () const override + BlockExpr *clone_expr_with_block_impl () const final override { return clone_block_expr_impl (); } @@ -2974,7 +2973,7 @@ public: {} 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; @@ -3058,7 +3057,7 @@ public: BreakExpr &operator= (BreakExpr &&other) = default; 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; @@ -3095,7 +3094,7 @@ protected: public: 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 (); } }; // Range from (inclusive) and to (exclusive) expression AST node object @@ -3536,7 +3535,7 @@ public: ReturnExpr &operator= (ReturnExpr &&other) = default; 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; @@ -3609,7 +3608,7 @@ public: UnsafeBlockExpr &operator= (UnsafeBlockExpr &&other) = default; 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; @@ -3714,7 +3713,7 @@ public: bool has_loop_label () const { return !loop_label.is_error (); } 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 (); } // Invalid if loop block is null, so base stripping on that. void mark_for_strip () override { loop_block = nullptr; } @@ -4029,7 +4028,7 @@ public: * better approach? or does it not parse correctly and have downsides? */ 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; @@ -4258,7 +4257,7 @@ public: } 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; @@ -4877,7 +4876,7 @@ public: MatchExpr &operator= (MatchExpr &&other) = default; 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; @@ -4953,7 +4952,7 @@ public: std::string as_string () const override; 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; @@ -5024,7 +5023,7 @@ public: std::string as_string () const override; 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; diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 4b3a2d5..fc34670 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -149,7 +149,7 @@ public: } protected: - // Clone function implementation as (not pure) virtual method + // Clone function implementation as virtual method TypeParam *clone_generic_param_impl () const override { return new TypeParam (*this); @@ -863,17 +863,19 @@ public: protected: /* Use covariance to implement clone function as returning this object * rather than base */ - Method *clone_inherent_impl_item_impl () const override + Method *clone_inherent_impl_item_impl () const final override { - return new Method (*this); + return clone_method_impl (); } /* Use covariance to implement clone function as returning this object * rather than base */ - Method *clone_trait_impl_item_impl () const override + Method *clone_trait_impl_item_impl () const final override { - return new Method (*this); + return clone_method_impl (); } + + /*virtual*/ Method *clone_method_impl () const { return new Method (*this); } }; // Item that supports visibility - abstract base class diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index b0f4956..b5fb858 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -316,6 +316,12 @@ class MacroRulesDefinition : public MacroItem Location locus; + /* NOTE: in rustc, macro definitions are considered (and parsed as) a type + * of macro, whereas here they are considered part of the language itself. + * I am not aware of the implications of this decision. The rustc spec does + * mention that using the same parser for macro definitions and invocations + * is "extremely self-referential and non-intuitive". */ + public: std::string as_string () const override; @@ -388,21 +394,26 @@ public: protected: /* Use covariance to implement clone function as returning this object rather * than base */ - MacroInvocation *clone_pattern_impl () const override + MacroInvocation *clone_pattern_impl () const final override { - return new MacroInvocation (*this); + return clone_macro_invocation_impl (); } /* Use covariance to implement clone function as returning this object rather * than base */ - MacroInvocation *clone_expr_without_block_impl () const override + MacroInvocation *clone_expr_without_block_impl () const final override { - return new MacroInvocation (*this); + return clone_macro_invocation_impl (); } /* Use covariance to implement clone function as returning this object rather * than base */ - MacroInvocation *clone_type_no_bounds_impl () const override + MacroInvocation *clone_type_no_bounds_impl () const final override + { + return clone_macro_invocation_impl (); + } + + /*virtual*/ MacroInvocation *clone_macro_invocation_impl () const { return new MacroInvocation (*this); } diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h index d1be6f3..2b7c973 100644 --- a/gcc/rust/ast/rust-path.h +++ b/gcc/rust/ast/rust-path.h @@ -353,7 +353,7 @@ public: } 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; @@ -367,14 +367,19 @@ public: protected: /* Use covariance to implement clone function as returning this object rather * than base */ - PathInExpression *clone_pattern_impl () const override + PathInExpression *clone_pattern_impl () const final override { - return new PathInExpression (*this); + return clone_path_in_expression_impl (); } /* Use covariance to implement clone function as returning this object rather * than base */ - PathInExpression *clone_expr_without_block_impl () const override + PathInExpression *clone_expr_without_block_impl () const final override + { + return clone_path_in_expression_impl (); + } + + /*virtual*/ PathInExpression *clone_path_in_expression_impl () const { return new PathInExpression (*this); } @@ -860,7 +865,7 @@ public: } 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; @@ -881,14 +886,19 @@ public: protected: /* Use covariance to implement clone function as returning this object rather * than base */ - QualifiedPathInExpression *clone_pattern_impl () const override + QualifiedPathInExpression *clone_pattern_impl () const final override { - return new QualifiedPathInExpression (*this); + return clone_qual_path_in_expression_impl (); } /* Use covariance to implement clone function as returning this object rather * than base */ - QualifiedPathInExpression *clone_expr_without_block_impl () const override + QualifiedPathInExpression *clone_expr_without_block_impl () const final override + { + return clone_qual_path_in_expression_impl (); + } + + /*virtual*/ QualifiedPathInExpression *clone_qual_path_in_expression_impl () const { return new QualifiedPathInExpression (*this); } diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 956f89f..de24504 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -3616,7 +3616,34 @@ MacroExpander::expand_cfg_macro (AST::MacroInvocData &invoc) AST::ASTFragment MacroExpander::expand_decl_macro (AST::MacroInvocData &invoc, AST::MacroRulesDefinition &rules_def) { - + // ensure that both invocation and rules are in a valid state + rust_assert (!invoc.is_marked_for_strip ()); + rust_assert (!rules_def.is_marked_for_strip ()); + rust_assert (rules_def.get_macro_rules ().size () > 0); + + /* probably something here about parsing invoc and rules def token trees to token stream. if not, + * how would parser handle the captures of exprs and stuff? on the other hand, token trees may be + * kind of useful in rules def as creating a point where recursion can occur (like having + * "compare_macro_match" and then it calling itself when it finds delimiters) */ + + /* find matching rule to invoc token tree, based on macro rule's matcher. if none exist, error. + * - specifically, check each matcher in order. if one fails to match, move onto next. */ + /* TODO: does doing this require parsing expressions and whatever in the invoc? if so, might as + * well save the results if referenced using $ or whatever. If not, do another pass saving them. + * Except this is probably useless as different rules could have different starting points for exprs + * or whatever. Decision trees could avoid this, but they have their own issues. */ + /* TODO: will need to modify the parser so that it can essentially "catch" errors - maybe + * "try_parse_expr" or whatever methods. */ + // this technically creates a back-tracking parser - this will be the implementation style + + /* then, after results are saved, generate the macro output from the transcriber token tree. if i + * understand this correctly, the macro invocation gets replaced by the transcriber tokens, except + * with substitutions made (e.g. for $i variables) */ + + /* TODO: it is probably better to modify AST::Token to store a pointer to a Lexer::Token (rather + * than being converted) - i.e. not so much have AST::Token as a Token but rather a TokenContainer + * (as it is another type of TokenTree). This will prevent re-conversion of Tokens between each type + * all the time, while still allowing the heterogenous storage of token trees. */ } void @@ -3697,7 +3724,7 @@ MacroExpander::fails_cfg_with_expand (std::vector &attrs) const void MacroExpander::expand_cfg_attrs (std::vector &attrs) { - for (int i = 0; i < attrs.size ();) + for (std::size_t i = 0; i < attrs.size ();) { auto &attr = attrs[i]; if (attr.get_path () == "cfg_attr") -- cgit v1.1 From ee85db852a5a819e559ab00e7a382a34e925447a Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Fri, 8 Jan 2021 22:05:03 +0800 Subject: Added type hint to literal creation for cfg! macro --- gcc/rust/expand/rust-macro-expand.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index de24504..591d96c 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -3609,9 +3609,9 @@ MacroExpander::expand_cfg_macro (AST::MacroInvocData &invoc) bool result = invoc.get_meta_items ()[0]->check_cfg_predicate (session); if (result) - return AST::Literal ("true", AST::Literal::BOOL); + return AST::Literal ("true", AST::Literal::BOOL, CORETYPE_BOOL); else - return AST::Literal ("false", AST::Literal::BOOL); + return AST::Literal ("false", AST::Literal::BOOL, CORETYPE_BOOL); } AST::ASTFragment -- cgit v1.1 From aa487ef2717e0f319f2f8d3496d0003939ec2b09 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Sun, 10 Jan 2021 14:40:56 +0800 Subject: Cleanup - remove old for loop implementations in AttrVisitor --- gcc/rust/expand/rust-macro-expand.cc | 447 ----------------------------------- 1 file changed, 447 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 591d96c..39bba4f 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -56,31 +56,6 @@ public: // if nothing else happens, increment ++it; } - -#if 0 - for (int i = 0; i < fields.size ();) - { - auto &field = fields[i]; - - auto &field_attrs = field.get_outer_attrs (); - expander.expand_cfg_attrs (field_attrs); - if (expander.fails_cfg_with_expand (field_attrs)) - { - fields.erase (fields.begin () + i); - continue; - } - - // expand sub-types of type, but can't strip type itself - auto &type = field.get_field_type (); - type->accept_vis (*this); - if (type->is_marked_for_strip ()) - rust_error_at (type->get_locus_slow (), - "cannot strip type in this position"); - - // if nothing else happens, increment - i++; - } -#endif } void expand_tuple_fields (std::vector &fields) @@ -107,31 +82,6 @@ public: // if nothing else happens, increment ++it; } - -#if 0 - for (int i = 0; i < fields.size ();) - { - auto &field = fields[i]; - - auto &field_attrs = field.get_outer_attrs (); - expander.expand_cfg_attrs (field_attrs); - if (expander.fails_cfg_with_expand (field_attrs)) - { - fields.erase (fields.begin () + i); - continue; - } - - // expand sub-types of type, but can't strip type itself - auto &type = field.get_field_type (); - type->accept_vis (*this); - if (type->is_marked_for_strip ()) - rust_error_at (type->get_locus_slow (), - "cannot strip type in this position"); - - // if nothing else happens, increment - i++; - } -#endif } void expand_function_params (std::vector ¶ms) @@ -164,37 +114,6 @@ public: // increment ++it; } - -#if 0 - for (int i = 0; i < params.size ();) - { - auto ¶m = params[i]; - - auto ¶m_attrs = param.get_outer_attrs (); - expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg_with_expand (param_attrs)) - { - params.erase (params.begin () + i); - continue; - } - - // TODO: should an unwanted strip lead to break out of loop? - auto &pattern = param.get_pattern (); - pattern->accept_vis (*this); - if (pattern->is_marked_for_strip ()) - rust_error_at (pattern->get_locus_slow (), - "cannot strip pattern in this position"); - - auto &type = param.get_type (); - type->accept_vis (*this); - if (type->is_marked_for_strip ()) - rust_error_at (type->get_locus_slow (), - "cannot strip type in this position"); - - // increment - i++; - } -#endif } void expand_generic_args (AST::GenericArgs &args) @@ -271,39 +190,6 @@ public: // increment if found nothing else so far ++it; } - -#if 0 - for (int i = 0; i < params.size ();) - { - auto ¶m = params[i]; - - auto ¶m_attrs = param.get_outer_attrs (); - expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg_with_expand (param_attrs)) - { - params.erase (params.begin () + i); - continue; - } - - auto &pattern = param.get_pattern (); - pattern->accept_vis (*this); - if (pattern->is_marked_for_strip ()) - rust_error_at (pattern->get_locus_slow (), - "cannot strip pattern in this position"); - - if (param.has_type_given ()) - { - auto &type = param.get_type (); - type->accept_vis (*this); - if (type->is_marked_for_strip ()) - rust_error_at (type->get_locus_slow (), - "cannot strip type in this position"); - } - - // increment if found nothing else so far - i++; - } -#endif } void expand_self_param (AST::SelfParam &self_param) @@ -798,22 +684,6 @@ public: /* apparently outer attributes are allowed in "elements of array * expressions" according to spec */ expand_pointer_allow_strip (elems.get_values ()); - -#if 0 - auto &values = elems.get_values (); - for (int i = 0; i < values.size ();) - { - auto &value = values[i]; - - // mark for stripping if required - value->accept_vis (*this); - - if (value->is_marked_for_strip ()) - values.erase (values.begin () + i); - else - i++; - } -#endif } void visit (AST::ArrayElemsCopied &elems) override { @@ -916,22 +786,6 @@ public: /* apparently outer attributes are allowed in "elements of tuple * expressions" according to spec */ expand_pointer_allow_strip (expr.get_tuple_elems ()); - -#if 0 - auto &values = expr.get_tuple_elems (); - for (int i = 0; i < values.size ();) - { - auto &value = values[i]; - - // mark for stripping if required - value->accept_vis (*this); - - if (value->is_marked_for_strip ()) - values.erase (values.begin () + i); - else - i++; - } -#endif } void visit (AST::TupleIndexExpr &expr) override { @@ -1118,22 +972,6 @@ public: /* spec says outer attributes are specifically allowed for elements * of tuple-style struct expressions, so full stripping possible */ expand_pointer_allow_strip (expr.get_elems ()); - -#if 0 - auto &values = expr.get_elems (); - for (int i = 0; i < values.size ();) - { - auto &value = values[i]; - - // mark for stripping if required - value->accept_vis (*this); - - if (value->is_marked_for_strip ()) - values.erase (values.begin () + i); - else - i++; - } -#endif } void visit (AST::StructExprUnit &expr) override { @@ -1229,22 +1067,6 @@ public: /* spec says outer attributes are specifically allowed for elements * of tuple-style enum expressions, so full stripping possible */ expand_pointer_allow_strip (expr.get_elems ()); - -#if 0 - auto &values = expr.get_elems (); - for (int i = 0; i < values.size ();) - { - auto &value = values[i]; - - // mark for stripping if required - value->accept_vis (*this); - - if (value->is_marked_for_strip ()) - values.erase (values.begin () + i); - else - i++; - } -#endif } void visit (AST::EnumExprFieldless &expr) override { @@ -1280,22 +1102,6 @@ public: /* spec says outer attributes are specifically allowed for elements * of call expressions, so full stripping possible */ expand_pointer_allow_strip (expr.get_params ()); - -#if 0 - auto ¶ms = expr.get_params (); - for (int i = 0; i < params.size ();) - { - auto ¶m = params[i]; - - // mark for stripping if required - param->accept_vis (*this); - - if (param->is_marked_for_strip ()) - params.erase (params.begin () + i); - else - i++; - } -#endif } void visit (AST::MethodCallExpr &expr) override { @@ -1324,22 +1130,6 @@ public: /* spec says outer attributes are specifically allowed for elements * of method call expressions, so full stripping possible */ expand_pointer_allow_strip (expr.get_params ()); - -#if 0 - auto ¶ms = expr.get_params (); - for (int i = 0; i < params.size ();) - { - auto ¶m = params[i]; - - // mark for stripping if required - param->accept_vis (*this); - - if (param->is_marked_for_strip ()) - params.erase (params.begin () + i); - else - i++; - } -#endif } void visit (AST::FieldAccessExpr &expr) override { @@ -1405,22 +1195,6 @@ public: // strip all statements expand_pointer_allow_strip (expr.get_statements ()); -#if 0 - auto &stmts = expr.get_statements (); - for (int i = 0; i < stmts.size ();) - { - auto &stmt = stmts[i]; - - // mark for stripping if required - stmt->accept_vis (*this); - - if (stmt->is_marked_for_strip ()) - stmts.erase (stmts.begin () + i); - else - i++; - } -#endif - // strip tail expression if exists - can actually fully remove it if (expr.has_tail_expr ()) { @@ -2118,56 +1892,6 @@ public: // increment to next case if haven't continued ++it; } - -#if 0 - for (int i = 0; i < match_cases.size ();) - { - auto &match_case = match_cases[i]; - - // strip match case based on outer attributes in match arm - auto &match_arm = match_case.get_arm (); - expander.expand_cfg_attrs (match_arm.get_outer_attrs ()); - if (expander.fails_cfg_with_expand (match_arm.get_outer_attrs ())) - { - // strip match case - match_cases.erase (match_cases.begin () + i); - continue; - } - - for (auto &pattern : match_arm.get_patterns ()) - { - pattern->accept_vis (*this); - if (pattern->is_marked_for_strip ()) - rust_error_at (pattern->get_locus_slow (), - "cannot strip pattern in this position"); - } - - /* assuming that guard expression cannot be stripped as - * strictly speaking you would have to strip the whole guard to - * make syntactical sense, which you can't do. as such, only - * strip sub-expressions */ - if (match_arm.has_match_arm_guard ()) - { - auto &guard_expr = match_arm.get_guard_expr (); - guard_expr->accept_vis (*this); - if (guard_expr->is_marked_for_strip ()) - rust_error_at (guard_expr->get_locus_slow (), - "cannot strip expression in this position - outer " - "attributes not allowed"); - } - - // strip sub-expressions from match cases - auto &case_expr = match_case.get_expr (); - case_expr->accept_vis (*this); - if (case_expr->is_marked_for_strip ()) - rust_error_at (case_expr->get_locus_slow (), - "cannot strip expression in this position - outer " - "attributes not allowed"); - - // increment to next case if haven't continued - i++; - } -#endif } void visit (AST::AwaitExpr &expr) override { @@ -2310,22 +2034,6 @@ public: // strip items if required expand_pointer_allow_strip (module.get_items ()); - -#if 0 - auto &items = module.get_items (); - for (int i = 0; i < items.size ();) - { - auto &item = items[i]; - - // mark for stripping if required - item->accept_vis (*this); - - if (item->is_marked_for_strip ()) - items.erase (items.begin () + i); - else - i++; - } -#endif } void visit (AST::ModuleNoBody &module) override { @@ -2552,22 +2260,6 @@ public: /* strip enum fields if required - this is presumably * allowed by spec */ expand_pointer_allow_strip (enum_item.get_variants ()); - -#if 0 - auto &variants = enum_item.get_variants (); - for (int i = 0; i < variants.size ();) - { - auto &variant = variants[i]; - - // mark for stripping if required - variant->accept_vis (*this); - - if (variant->is_marked_for_strip ()) - variants.erase (variants.begin () + i); - else - i++; - } -#endif } void visit (AST::Union &union_item) override { @@ -2775,22 +2467,6 @@ public: // strip trait items if required expand_pointer_allow_strip (trait.get_trait_items ()); - -#if 0 - auto &trait_items = trait.get_trait_items (); - for (int i = 0; i < trait_items.size ();) - { - auto &item = trait_items[i]; - - // mark for stripping if required - item->accept_vis (*this); - - if (item->is_marked_for_strip ()) - trait_items.erase (trait_items.begin () + i); - else - i++; - } -#endif } void visit (AST::InherentImpl &impl) override { @@ -2825,22 +2501,6 @@ public: // strip inherent impl items if required expand_pointer_allow_strip (impl.get_impl_items ()); - -#if 0 - auto &impl_items = impl.get_impl_items (); - for (int i = 0; i < impl_items.size ();) - { - auto &item = impl_items[i]; - - // mark for stripping if required - item->accept_vis (*this); - - if (item->is_marked_for_strip ()) - impl_items.erase (impl_items.begin () + i); - else - i++; - } -#endif } void visit (AST::TraitImpl &impl) override { @@ -2881,22 +2541,6 @@ public: // strip trait impl items if required expand_pointer_allow_strip (impl.get_impl_items ()); - -#if 0 - auto &impl_items = impl.get_impl_items (); - for (int i = 0; i < impl_items.size ();) - { - auto &item = impl_items[i]; - - // mark for stripping if required - item->accept_vis (*this); - - if (item->is_marked_for_strip ()) - impl_items.erase (impl_items.begin () + i); - else - i++; - } -#endif } void visit (AST::ExternalStaticItem &item) override { @@ -2952,29 +2596,6 @@ public: // increment if nothing else happens ++it; } -#if 0 - for (int i = 0; i < params.size ();) - { - auto ¶m = params[i]; - - auto ¶m_attrs = param.get_outer_attrs (); - expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg_with_expand (param_attrs)) - { - params.erase (params.begin () + i); - continue; - } - - auto &type = param.get_type (); - type->accept_vis (*this); - if (type->is_marked_for_strip ()) - rust_error_at (type->get_locus_slow (), - "cannot strip type in this position"); - - // increment if nothing else happens - i++; - } -#endif /* NOTE: these are extern function params, which may have different * rules and restrictions to "normal" function params. So expansion * handled separately. */ @@ -3014,22 +2635,6 @@ public: // strip external items if required expand_pointer_allow_strip (block.get_extern_items ()); - -#if 0 - auto &extern_items = block.get_extern_items (); - for (int i = 0; i < extern_items.size ();) - { - auto &item = extern_items[i]; - - // mark for stripping if required - item->accept_vis (*this); - - if (item->is_marked_for_strip ()) - extern_items.erase (extern_items.begin () + i); - else - i++; - } -#endif } // I don't think it would be possible to strip macros without expansion @@ -3185,21 +2790,6 @@ public: // assuming you can strip struct pattern fields expand_pointer_allow_strip (elems.get_struct_pattern_fields ()); -#if 0 - auto &fields = elems.get_struct_pattern_fields (); - for (int i = 0; i < fields.size ();) - { - auto &field = fields[i]; - - field->accept_vis (*this); - - if (field->is_marked_for_strip ()) - fields.erase (fields.begin () + i); - else - i++; - } -#endif - // assuming you can strip the ".." part if (elems.has_etc ()) { @@ -3530,29 +3120,6 @@ public: // increment if nothing else happens ++it; } -#if 0 - for (int i = 0; i < params.size ();) - { - auto ¶m = params[i]; - - auto ¶m_attrs = param.get_outer_attrs (); - expander.expand_cfg_attrs (param_attrs); - if (expander.fails_cfg_with_expand (param_attrs)) - { - params.erase (params.begin () + i); - continue; - } - - auto &type = param.get_type (); - type->accept_vis (*this); - if (type->is_marked_for_strip ()) - rust_error_at (type->get_locus_slow (), - "cannot strip type in this position"); - - // increment if nothing else happens - i++; - } -#endif /* TODO: assuming that variadic nature cannot be stripped. If this * is not true, then have code here to do so. */ @@ -3798,20 +3365,6 @@ MacroExpander::expand_crate () ++it; } -#if 0 - for (int i = 0; i < items.size ();) - { - auto &item = items[i]; - - // mark for stripping if required - item->accept_vis (attr_visitor); - - if (item->is_marked_for_strip ()) - items.erase (items.begin () + i); - else - i++; - } -#endif // TODO: should recursive attribute and macro expansion be done in the same // transversal? Or in separate ones like currently? -- cgit v1.1 From 3a0c8ca2156038b726e2689e9b46be4d8c40c55f Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Tue, 2 Feb 2021 13:19:26 +0800 Subject: Cleanup of AST - moved outer_attrs to child classes Fixed compile errors Fixed more compile errors --- gcc/rust/ast/rust-ast-full-test.cc | 566 ++++++++++++++++------------------- gcc/rust/ast/rust-ast.h | 55 +--- gcc/rust/ast/rust-expr.h | 339 +++++++++++++-------- gcc/rust/ast/rust-macro.h | 8 +- gcc/rust/ast/rust-path.h | 35 ++- gcc/rust/expand/rust-macro-expand.cc | 69 +---- gcc/rust/parse/rust-parse-impl.h | 79 +++-- 7 files changed, 570 insertions(+), 581 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index 092ab00..53007e7 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -109,6 +109,47 @@ frag_spec_to_str (MacroFragSpec frag_spec) } } +enum AttrMode +{ + OUTER, + INNER +}; + +std::string get_mode_dump_desc (AttrMode mode) { + switch (mode) { + case OUTER: + return "outer attributes"; + case INNER: + return "inner attributes"; + default: + gcc_unreachable (); + return ""; + } +} + +// Adds lines below adding attributes +std::string append_attributes (std::vector attrs, AttrMode mode) { + indent_spaces (enter); + + std::string str = "\n" + indent_spaces (stay) + get_mode_dump_desc (mode) + ": "; + //str += "\n" + indent_spaces (stay) + "inner attributes: "; + if (attrs.empty ()) + { + str += "none"; + } + else + { + /* note that this does not print them with outer or "inner attribute" + * syntax - just prints the body */ + for (const auto &attr : attrs) + str += "\n" + indent_spaces (stay) + attr.as_string (); + } + + indent_spaces (out); + + return str; +} + std::string Crate::as_string () const { @@ -156,7 +197,7 @@ Crate::as_string () const { fprintf (stderr, "something really terrible has gone wrong - " "null pointer item in crate."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + item->as_string (); @@ -236,7 +277,7 @@ DelimTokenTree::as_string () const stderr, "something really terrible has gone wrong - null pointer " "token tree in delim token tree."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += tree->as_string (); @@ -405,7 +446,7 @@ ModuleBodied::as_string () const { fprintf (stderr, "something really terrible has gone wrong - " "null pointer item in crate."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + item->as_string (); @@ -444,7 +485,7 @@ StaticItem::as_string () const { fprintf (stderr, "something really terrible has gone wrong - null " "pointer type in static item."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n" + indent_spaces (stay) + "Type: " + type->as_string (); @@ -453,7 +494,7 @@ StaticItem::as_string () const { fprintf (stderr, "something really terrible has gone wrong - null " "pointer expr in static item."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n" + indent_spaces (stay) + "Expression: " + expr->as_string (); @@ -499,7 +540,7 @@ TupleStruct::as_string () const stderr, "something really terrible has gone wrong - null pointer " "generic param in enum."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + param->as_string (); @@ -545,7 +586,7 @@ ConstantItem::as_string () const { fprintf (stderr, "something really terrible has gone wrong - null " "pointer type in const item."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n Type: " + type->as_string (); @@ -554,7 +595,7 @@ ConstantItem::as_string () const { fprintf (stderr, "something really terrible has gone wrong - null " "pointer expr in const item."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n Expression: " + const_expr->as_string (); @@ -585,7 +626,7 @@ InherentImpl::as_string () const stderr, "something really terrible has gone wrong - null pointer " "generic param in inherent impl."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + param->as_string (); @@ -663,7 +704,7 @@ Method::as_string () const stderr, "something really terrible has gone wrong - null pointer " "generic param in method."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + param->as_string (); @@ -725,7 +766,7 @@ StructStruct::as_string () const stderr, "something really terrible has gone wrong - null pointer " "generic param in enum."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + param->as_string (); @@ -775,7 +816,7 @@ UseDeclaration::as_string () const stderr, "something really terrible has gone wrong - null pointer use tree in " "use declaration."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "use " + use_tree->as_string (); @@ -835,7 +876,7 @@ UseTreeList::as_string () const fprintf (stderr, "something really terrible has gone wrong - null pointer " "tree in use tree list."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } for (; i != e; i++) @@ -900,7 +941,7 @@ Enum::as_string () const stderr, "something really terrible has gone wrong - null pointer " "generic param in enum."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + param->as_string (); @@ -934,7 +975,7 @@ Enum::as_string () const stderr, "something really terrible has gone wrong - null pointer " "enum item in enum."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + item->as_string (); @@ -950,9 +991,7 @@ Trait::as_string () const std::string str = VisItem::as_string (); if (has_unsafe) - { str += "unsafe "; - } str += "trait " + name; @@ -973,7 +1012,7 @@ Trait::as_string () const stderr, "something really terrible has gone wrong - null pointer " "generic param in trait."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + param->as_string (); @@ -996,7 +1035,7 @@ Trait::as_string () const stderr, "something really terrible has gone wrong - null pointer " "type param bound in trait."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + bound->as_string (); @@ -1005,13 +1044,9 @@ Trait::as_string () const str += "\n Where clause: "; if (!has_where_clause ()) - { str += "none"; - } else - { str += where_clause.as_string (); - } str += "\n Trait items: "; if (!has_trait_items ()) @@ -1029,7 +1064,7 @@ Trait::as_string () const stderr, "something really terrible has gone wrong - null pointer " "trait item in trait."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + item->as_string (); @@ -1063,7 +1098,7 @@ Union::as_string () const stderr, "something really terrible has gone wrong - null pointer " "generic param in union."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + param->as_string (); @@ -1072,13 +1107,9 @@ Union::as_string () const str += "\n Where clause: "; if (has_where_clause ()) - { str += where_clause.as_string (); - } else - { str += "none"; - } // struct fields str += "\n Struct fields (variants): "; @@ -1089,9 +1120,7 @@ Union::as_string () const else { for (const auto &field : variants) - { str += "\n " + field.as_string (); - } } return str; @@ -1114,7 +1143,7 @@ Function::as_string () const stderr, "something really terrible has gone wrong - null pointer return " "type in function."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += return_type->as_string () + " "; @@ -1139,7 +1168,7 @@ Function::as_string () const fprintf (stderr, "something really terrible has gone wrong - null pointer " "generic param in function item."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } for (; i != e; i++) @@ -1181,7 +1210,7 @@ Function::as_string () const stderr, "something really terrible has gone wrong - null pointer function " "body in function."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += function_body->as_string () + "\n"; @@ -1212,8 +1241,20 @@ BlockExpr::as_string () const { std::string istr = indent_spaces (enter); std::string str = istr + "BlockExpr:\n" + istr; + // get outer attributes - str += "{\n" + indent_spaces (stay) + Expr::as_string (); + str += "{\n" + indent_spaces (stay) + "outer attributes: "; + if (outer_attrs.empty ()) + { + str += "none"; + } + else + { + /* note that this does not print them with "outer attribute" syntax - + * just the body */ + for (const auto &attr : outer_attrs) + str += "\n" + indent_spaces (stay) + attr.as_string (); + } // inner attributes str += "\n" + indent_spaces (stay) + "inner attributes: "; @@ -1246,7 +1287,7 @@ BlockExpr::as_string () const stderr, "something really terrible has gone wrong - null pointer " "stmt in block expr."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n" + indent_spaces (stay) + stmt->as_string (); @@ -1400,7 +1441,7 @@ MacroInvocationSemi::as_string () const fprintf (stderr, "something really terrible has gone wrong - null pointer " "token tree in macro invocation semi."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } std::string s; @@ -1425,9 +1466,7 @@ ExternBlock::as_string () const str += "extern "; if (has_abi ()) - { str += "\"" + abi + "\" "; - } // inner attributes str += "\n inner attributes: "; @@ -1576,7 +1615,27 @@ ClosureParam::as_string () const std::string ClosureExpr::as_string () const { - std::string str ("ClosureExpr:\n Has move: "); + std::string str = "ClosureExpr:"; + + str += append_attributes (outer_attrs, OUTER); + +#if 0 + // outer attributes + str += "\n outer attributes: "; + if (outer_attrs.empty ()) + { + str += "none"; + } + else + { + /* note that this does not print them with "outer attribute" syntax - + * just the body */ + for (const auto &attr : outer_attrs) + str += "\n " + attr.as_string (); + } +#endif + + str += "\n Has move: "; if (has_move) str += "true"; else @@ -1643,6 +1702,9 @@ QualifiedPathInExpression::as_string () const std::string BorrowExpr::as_string () const { + /* TODO: find way to incorporate outer attrs - may have to represent in + * different style (i.e. something more like BorrowExpr: \n outer attrs) */ + std::string str ("&"); if (double_borrow) @@ -1659,6 +1721,9 @@ BorrowExpr::as_string () const std::string ReturnExpr::as_string () const { + /* TODO: find way to incorporate outer attrs - may have to represent in + * different style (i.e. something more like BorrowExpr: \n outer attrs) */ + std::string str ("return "); if (has_returned_expr ()) @@ -1672,7 +1737,11 @@ GroupedExpr::as_string () const { std::string str ("Grouped expr:"); + // outer attrs + str += append_attributes (outer_attrs, OUTER); + // inner attributes +#if 0 str += "\n inner attributes: "; if (inner_attrs.empty ()) { @@ -1685,6 +1754,8 @@ GroupedExpr::as_string () const for (const auto &attr : inner_attrs) str += "\n " + attr.as_string (); } +#endif + str += append_attributes (inner_attrs, INNER); str += "\n Expr in parens: " + expr_in_parens->as_string (); @@ -1700,6 +1771,7 @@ RangeToExpr::as_string () const std::string ContinueExpr::as_string () const { + // TODO: rewrite format to allow outer attributes std::string str ("continue "); if (has_label ()) @@ -1711,6 +1783,7 @@ ContinueExpr::as_string () const std::string NegationExpr::as_string () const { + // TODO: rewrite formula to allow outer attributes std::string str; switch (negation_type) @@ -1745,6 +1818,7 @@ RangeFullExpr::as_string () const std::string ArrayIndexExpr::as_string () const { + // TODO: rewrite formula to allow outer attributes return array_expr->as_string () + "[" + index_expr->as_string () + "]"; } @@ -1775,7 +1849,8 @@ AsyncBlockExpr::as_string () const std::string str = "AsyncBlockExpr: "; // get outer attributes - str += "\n " + Expr::as_string (); + //str += "\n " + Expr::as_string (); + str += append_attributes (outer_attrs, OUTER); str += "\n Has move: "; str += has_move ? "true" : "false"; @@ -1786,6 +1861,7 @@ AsyncBlockExpr::as_string () const std::string ComparisonExpr::as_string () const { + // TODO: rewrite to better reflect non-literal expressions std::string str (main_or_left_expr->as_string ()); switch (expr_type) @@ -1820,12 +1896,14 @@ ComparisonExpr::as_string () const std::string MethodCallExpr::as_string () const { - std::string str ("MethodCallExpr: \n Object (receiver) expr: "); + std::string str = "MethodCallExpr: "; + str += append_attributes (outer_attrs, OUTER); + + str += "\n Object (receiver) expr: \n"; str += receiver->as_string (); str += "\n Method path segment: \n"; - str += method_name.as_string (); str += "\n Call params:"; @@ -1850,24 +1928,28 @@ MethodCallExpr::as_string () const std::string TupleIndexExpr::as_string () const { + // TODO: rewrite dump to better reflect non-literal exprs return tuple_expr->as_string () + "." + std::to_string (tuple_index); } std::string DereferenceExpr::as_string () const { + // TODO: rewrite dump to better reflect non-literal exprs return "*" + main_or_left_expr->as_string (); } std::string FieldAccessExpr::as_string () const { + // TODO: rewrite dump to better reflect non-literal exprs return receiver->as_string () + "." + field; } std::string LazyBooleanExpr::as_string () const { + // TODO: rewrite dump to better reflect non-literal exprs std::string str (main_or_left_expr->as_string ()); switch (expr_type) @@ -1890,12 +1972,14 @@ LazyBooleanExpr::as_string () const std::string RangeFromToExpr::as_string () const { + // TODO: rewrite dump to better reflect non-literal exprs return from->as_string () + ".." + to->as_string (); } std::string RangeToInclExpr::as_string () const { + // TODO: rewrite dump to better reflect non-literal exprs return "..=" + to->as_string (); } @@ -1907,7 +1991,8 @@ UnsafeBlockExpr::as_string () const str += istr + "{"; // get outer attributes - str += "\n" + indent_spaces (stay) + Expr::as_string (); + //str += "\n" + indent_spaces (stay) + Expr::as_string (); + str += append_attributes (outer_attrs, OUTER); return str + "\n" + indent_spaces (out) + "}\n" + expr->as_string (); } @@ -1925,6 +2010,8 @@ ClosureExprInner::as_string () const std::string IfExpr::as_string () const { + /* TODO: can if exprs have outer attributes? i thought no, but can't find it + * in docs */ std::string str ("IfExpr: "); str += "\n Condition expr: " + condition->as_string (); @@ -1967,6 +2054,8 @@ IfExprConseqIfLet::as_string () const std::string IfLetExpr::as_string () const { + /* TODO: can if let exprs have outer attributes? i thought no, but can't find + * it in docs */ std::string str ("IfLetExpr: "); str += "\n Condition match arm patterns: "; @@ -2020,12 +2109,14 @@ IfLetExprConseqIfLet::as_string () const std::string RangeFromToInclExpr::as_string () const { + // TODO: rewrite to allow dumps with non-literal exprs return from->as_string () + "..=" + to->as_string (); } std::string ErrorPropagationExpr::as_string () const { + // TODO: rewrite to allow dumps with non-literal exprs return main_or_left_expr->as_string () + "?"; } @@ -2152,8 +2243,11 @@ ArithmeticOrLogicalExpr::as_string () const std::string CallExpr::as_string () const { - std::string str ("CallExpr: \n Function expr: "); + std::string str = "CallExpr: "; + str += append_attributes (outer_attrs, OUTER); + + str += "\n Function expr: "; str += function->as_string (); str += "\n Call params:"; @@ -2178,7 +2272,9 @@ CallExpr::as_string () const std::string WhileLoopExpr::as_string () const { - std::string str ("WhileLoopExpr: "); + std::string str = "WhileLoopExpr: "; + + str += append_attributes (outer_attrs, OUTER); str += "\n Label: "; if (!has_loop_label ()) @@ -2196,7 +2292,9 @@ WhileLoopExpr::as_string () const std::string WhileLetLoopExpr::as_string () const { - std::string str ("WhileLetLoopExpr: "); + std::string str = "WhileLetLoopExpr: "; + + str += append_attributes (outer_attrs, OUTER); str += "\n Label: "; if (!has_loop_label ()) @@ -2225,7 +2323,9 @@ WhileLetLoopExpr::as_string () const std::string LoopExpr::as_string () const { - std::string str ("LoopExpr: (infinite loop)"); + std::string str = "LoopExpr: (infinite loop)"; + + str += append_attributes (outer_attrs, OUTER); str += "\n Label: "; if (!has_loop_label ()) @@ -2241,9 +2341,13 @@ LoopExpr::as_string () const std::string ArrayExpr::as_string () const { - std::string str ("ArrayExpr:"); + std::string str = "ArrayExpr:"; + + str += append_attributes (outer_attrs, OUTER); // inner attributes + str += append_attributes (inner_attrs, INNER); + #if 0 str += "\n inner attributes: "; if (inner_attrs.empty ()) { @@ -2256,6 +2360,7 @@ ArrayExpr::as_string () const for (const auto &attr : inner_attrs) str += "\n " + attr.as_string (); } + #endif str += "\n Array elems: "; if (!has_array_elems ()) @@ -2269,12 +2374,14 @@ ArrayExpr::as_string () const std::string AwaitExpr::as_string () const { + // TODO: rewrite dump to allow non-literal exprs return awaited_expr->as_string () + ".await"; } std::string BreakExpr::as_string () const { + // TODO: rewrite dump to allow outer attrs, non-literal exprs std::string str ("break "); if (has_label ()) @@ -2296,6 +2403,8 @@ std::string MatchArm::as_string () const { // outer attributes + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str = "Outer attributes: "; if (outer_attrs.empty ()) { @@ -2308,6 +2417,7 @@ MatchArm::as_string () const for (const auto &attr : outer_attrs) str += "\n " + attr.as_string (); } +#endif str += "\nPatterns: "; if (match_arm_patterns.empty ()) @@ -2365,9 +2475,13 @@ MatchExpr::as_string () const { std::string str ("MatchExpr:"); + str += append_attributes (outer_attrs, OUTER); + str += "\n Scrutinee expr: " + branch_value->as_string (); // inner attributes + str += append_attributes (inner_attrs, INNER); +#if 0 str += "\n inner attributes: "; if (inner_attrs.empty ()) { @@ -2378,10 +2492,9 @@ MatchExpr::as_string () const /* note that this does not print them with "inner attribute" syntax - * just the body */ for (const auto &attr : inner_attrs) - { str += "\n " + attr.as_string (); - } } +#endif // match arms str += "\n Match arms: "; @@ -2403,7 +2516,11 @@ TupleExpr::as_string () const { std::string str ("TupleExpr:"); + str += append_attributes (outer_attrs, OUTER); + // inner attributes + str += append_attributes (inner_attrs, INNER); +#if 0 str += "\n inner attributes: "; if (inner_attrs.empty ()) { @@ -2414,10 +2531,9 @@ TupleExpr::as_string () const /* note that this does not print them with "inner attribute" syntax - * just the body */ for (const auto &attr : inner_attrs) - { str += "\n " + attr.as_string (); - } } +#endif str += "\n Tuple elements: "; if (tuple_elems.empty ()) @@ -2427,9 +2543,7 @@ TupleExpr::as_string () const else { for (const auto &elem : tuple_elems) - { str += "\n " + elem->as_string (); - } } return str; @@ -2443,13 +2557,9 @@ ExprStmtWithoutBlock::as_string () const str += indent_spaces (stay); if (expr == nullptr) - { str += "none (this shouldn't happen and is probably an error)"; - } else - { str += expr->as_string (); - } indent_spaces (out); return str; @@ -2458,6 +2568,7 @@ ExprStmtWithoutBlock::as_string () const std::string FunctionParam::as_string () const { + // TODO: rewrite dump to allow non-literal types return param_name->as_string () + " : " + type->as_string (); } @@ -2482,17 +2593,13 @@ FunctionQualifiers::as_string () const } if (has_unsafe) - { str += "unsafe "; - } if (has_extern) { str += "extern"; if (extern_abi != "") - { - str += " \"" + extern_abi + "\""; - } + str += " \"" + extern_abi + "\""; } return str; @@ -2505,13 +2612,9 @@ TraitBound::as_string () const str += "\n Has opening question mark: "; if (opening_question_mark) - { str += "true"; - } else - { str += "false"; - } str += "\n For lifetimes: "; if (!has_for_lifetimes ()) @@ -2521,9 +2624,7 @@ TraitBound::as_string () const else { for (const auto &lifetime : for_lifetimes) - { str += "\n " + lifetime.as_string (); - } } str += "\n Type path: " + type_path.as_string (); @@ -2562,9 +2663,7 @@ MacroMatcher::as_string () const else { for (const auto &match : matches) - { - str += "\n " + match->as_string (); - } + str += "\n " + match->as_string (); } return str; @@ -2577,13 +2676,9 @@ LifetimeParam::as_string () const str += "\n Outer attribute: "; if (!has_outer_attribute ()) - { str += "none"; - } else - { str += outer_attr.as_string (); - } str += "\n Lifetime: " + lifetime.as_string (); @@ -2595,9 +2690,7 @@ LifetimeParam::as_string () const else { for (const auto &bound : lifetime_bounds) - { - str += "\n " + bound.as_string (); - } + str += "\n " + bound.as_string (); } return str; @@ -2612,12 +2705,12 @@ MacroMatchFragment::as_string () const std::string QualifiedPathInType::as_string () const { + /* TODO: this may need adjusting if segments (e.g. with functions) can't be + * literalised */ std::string str = path_type.as_string (); for (const auto &segment : segments) - { str += "::" + segment->as_string (); - } return str; } @@ -2635,20 +2728,14 @@ MacroMatchRepetition::as_string () const else { for (const auto &match : matches) - { str += "\n " + match->as_string (); - } } str += "\n Sep: "; if (!has_sep ()) - { str += "none"; - } else - { str += sep->as_string (); - } str += "\n Op: "; switch (op) @@ -2676,9 +2763,7 @@ std::string Lifetime::as_string () const { if (is_error ()) - { return "error lifetime"; - } switch (lifetime_type) { @@ -2696,17 +2781,15 @@ Lifetime::as_string () const std::string TypePath::as_string () const { + /* TODO: this may need to be rewritten if a segment (e.g. function) can't be + * literalised */ std::string str; if (has_opening_scope_resolution) - { str = "::"; - } for (const auto &segment : segments) - { str += segment->as_string () + "::"; - } // kinda hack - remove last 2 '::' characters str.erase (str.length () - 2); @@ -2721,13 +2804,9 @@ TypeParam::as_string () const str += "\n Outer attribute: "; if (!has_outer_attribute ()) - { str += "none"; - } else - { str += outer_attr.as_string (); - } str += "\n Identifier: " + type_representation; @@ -2739,20 +2818,14 @@ TypeParam::as_string () const else { for (const auto &bound : type_param_bounds) - { str += "\n " + bound->as_string (); - } } str += "\n Type: "; if (!has_type ()) - { str += "none"; - } else - { str += type->as_string (); - } return str; } @@ -2761,9 +2834,7 @@ SimplePath PathPattern::convert_to_simple_path (bool with_opening_scope_resolution) const { if (!has_segments ()) - { return SimplePath::create_empty (); - } // create vector of reserved size (to minimise reallocations) std::vector simple_segments; @@ -2774,9 +2845,7 @@ PathPattern::convert_to_simple_path (bool with_opening_scope_resolution) const // return empty path if doesn't meet simple path segment requirements if (segment.is_error () || segment.has_generic_args () || segment.as_string () == "Self") - { return SimplePath::create_empty (); - } // create segment and add to vector std::string segment_str = segment.as_string (); @@ -2787,13 +2856,10 @@ PathPattern::convert_to_simple_path (bool with_opening_scope_resolution) const // kind of a HACK to get locus depending on opening scope resolution Location locus = Linemap::unknown_location (); if (with_opening_scope_resolution) - { locus = simple_segments[0].get_locus () - 2; // minus 2 chars for :: - } else - { locus = simple_segments[0].get_locus (); - } + // FIXME: this hack probably doesn't actually work return SimplePath (std::move (simple_segments), with_opening_scope_resolution, locus); @@ -2803,9 +2869,7 @@ SimplePath TypePath::as_simple_path () const { if (segments.empty ()) - { return SimplePath::create_empty (); - } // create vector of reserved size (to minimise reallocations) std::vector simple_segments; @@ -2816,9 +2880,7 @@ TypePath::as_simple_path () const // return empty path if doesn't meet simple path segment requirements if (segment == nullptr || segment->is_error () || !segment->is_ident_only () || segment->as_string () == "Self") - { return SimplePath::create_empty (); - } // create segment and add to vector std::string segment_str = segment->as_string (); @@ -2833,11 +2895,10 @@ TypePath::as_simple_path () const std::string PathExprSegment::as_string () const { + // TODO: rewrite dump to work with non-literalisable types std::string ident_str = segment_name.as_string (); if (has_generic_args ()) - { ident_str += "::<" + generic_args.as_string () + ">"; - } return ident_str; } @@ -2895,23 +2956,22 @@ GenericArgs::as_string () const std::string GenericArgsBinding::as_string () const { + // TODO: rewrite to work with non-literalisable types return identifier + " = " + type->as_string (); } std::string ForLoopExpr::as_string () const { - std::string str ("ForLoopExpr: "); + std::string str = "ForLoopExpr: "; + + str += append_attributes (outer_attrs, OUTER); str += "\n Label: "; if (!has_loop_label ()) - { str += "none"; - } else - { str += loop_label.as_string (); - } str += "\n Pattern: " + pattern->as_string (); @@ -2925,14 +2985,11 @@ ForLoopExpr::as_string () const std::string RangePattern::as_string () const { + // TODO: maybe rewrite to work with non-linearisable bounds if (has_ellipsis_syntax) - { return lower->as_string () + "..." + upper->as_string (); - } else - { return lower->as_string () + "..=" + upper->as_string (); - } } std::string @@ -2941,9 +2998,7 @@ RangePatternBoundLiteral::as_string () const std::string str; if (has_minus) - { str += "-"; - } str += literal.as_string (); @@ -2956,9 +3011,7 @@ SlicePattern::as_string () const std::string str ("SlicePattern: "); for (const auto &pattern : items) - { str += "\n " + pattern->as_string (); - } return str; } @@ -2969,9 +3022,7 @@ TuplePatternItemsMultiple::as_string () const std::string str; for (const auto &pattern : patterns) - { str += "\n " + pattern->as_string (); - } return str; } @@ -2989,9 +3040,7 @@ TuplePatternItemsRanged::as_string () const else { for (const auto &lower : lower_patterns) - { str += "\n " + lower->as_string (); - } } str += "\n Upper patterns: "; @@ -3002,9 +3051,7 @@ TuplePatternItemsRanged::as_string () const else { for (const auto &upper : upper_patterns) - { str += "\n " + upper->as_string (); - } } return str; @@ -3020,6 +3067,8 @@ std::string StructPatternField::as_string () const { // outer attributes + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str ("Outer attributes: "); if (outer_attrs.empty ()) { @@ -3030,10 +3079,9 @@ StructPatternField::as_string () const /* note that this does not print them with "outer attribute" syntax - * just the body */ for (const auto &attr : outer_attrs) - { str += "\n " + attr.as_string (); - } } +#endif return str; } @@ -3046,14 +3094,10 @@ StructPatternFieldIdent::as_string () const str += "\n"; if (has_ref) - { str += "ref "; - } if (has_mut) - { str += "mut "; - } str += ident; @@ -3063,6 +3107,7 @@ StructPatternFieldIdent::as_string () const std::string StructPatternFieldTuplePat::as_string () const { + // TODO: maybe rewrite to work with non-linearisable patterns std::string str = StructPatternField::as_string (); str += "\n"; @@ -3075,6 +3120,7 @@ StructPatternFieldTuplePat::as_string () const std::string StructPatternFieldIdentPat::as_string () const { + // TODO: maybe rewrite to work with non-linearisable patterns std::string str = StructPatternField::as_string (); str += "\n"; @@ -3096,20 +3142,14 @@ StructPatternElements::as_string () const else { for (const auto &field : fields) - { str += "\n " + field->as_string (); - } } str += "\n Etc: "; if (has_struct_pattern_etc) - { str += "true"; - } else - { str += "false"; - } return str; } @@ -3123,13 +3163,9 @@ StructPattern::as_string () const str += "\n Struct pattern elems: "; if (!has_struct_pattern_elems ()) - { str += "none"; - } else - { str += elems.as_string (); - } return str; } @@ -3140,9 +3176,7 @@ LiteralPattern::as_string () const std::string str; if (has_minus) - { str += "-"; - } return str + lit.as_string (); } @@ -3150,17 +3184,14 @@ LiteralPattern::as_string () const std::string ReferencePattern::as_string () const { + // TODO: maybe rewrite to work with non-linearisable patterns std::string str ("&"); if (has_two_amps) - { str += "&"; - } if (is_mut) - { str += "mut "; - } str += pattern->as_string (); @@ -3170,24 +3201,19 @@ ReferencePattern::as_string () const std::string IdentifierPattern::as_string () const { + // TODO: maybe rewrite to work with non-linearisable patterns std::string str; if (is_ref) - { str += "ref "; - } if (is_mut) - { str += "mut "; - } str += variable_ident; if (has_pattern_to_bind ()) - { str += " @ " + to_bind->as_string (); - } return str; } @@ -3198,9 +3224,7 @@ TupleStructItemsNoRange::as_string () const std::string str; for (const auto &pattern : patterns) - { str += "\n " + pattern->as_string (); - } return str; } @@ -3217,9 +3241,7 @@ TupleStructItemsRange::as_string () const else { for (const auto &lower : lower_patterns) - { str += "\n " + lower->as_string (); - } } str += "\n Upper patterns: "; @@ -3230,9 +3252,7 @@ TupleStructItemsRange::as_string () const else { for (const auto &upper : upper_patterns) - { str += "\n " + upper->as_string (); - } } return str; @@ -3253,39 +3273,21 @@ TupleStructPattern::as_string () const std::string LetStmt::as_string () const { - // outer attributes - std::string str = "Outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - indent_spaces (enter); - for (const auto &attr : outer_attrs) - { - str += "\n" + indent_spaces (stay) + attr.as_string (); - } - indent_spaces (out); - } + // TODO: rewrite to work with non-linearisable types and exprs + std::string str = append_attributes (outer_attrs, OUTER); str += "\n" + indent_spaces (stay) + "let " + variables_pattern->as_string (); if (has_type ()) - { str += " : " + type->as_string (); - } if (has_init_expr ()) - { str += " = " + init_expr->as_string (); - } return str; } +#if 0 // Used to get outer attributes for expressions. std::string Expr::as_string () const @@ -3301,22 +3303,18 @@ Expr::as_string () const /* note that this does not print them with "outer attribute" syntax - * just the body */ for (const auto &attr : outer_attrs) - { str += "\n " + attr.as_string (); - } } return str; } +#endif // hopefully definition here will prevent circular dependency issue TraitBound * TypePath::to_trait_bound (bool in_parens) const { - // create clone FIXME is this required? or is copy constructor automatically - // called? - TypePath copy (*this); - return new TraitBound (std::move (copy), copy.get_locus (), in_parens); + return new TraitBound (TypePath (*this), get_locus (), in_parens); } std::string @@ -3328,6 +3326,7 @@ InferredType::as_string () const std::string TypeCastExpr::as_string () const { + // TODO: rewrite to work with non-linearisable exprs and types return main_or_left_expr->as_string () + " as " + type_to_convert_to->as_string (); } @@ -3344,9 +3343,7 @@ ImplTraitType::as_string () const else { for (const auto &bound : type_param_bounds) - { str += "\n " + bound->as_string (); - } } return str; @@ -3355,17 +3352,14 @@ ImplTraitType::as_string () const std::string ReferenceType::as_string () const { + // TODO: rewrite to work with non-linearisable types std::string str ("&"); if (has_lifetime ()) - { str += lifetime.as_string () + " "; - } if (has_mut) - { str += "mut "; - } str += type->as_string (); @@ -3375,6 +3369,7 @@ ReferenceType::as_string () const std::string RawPointerType::as_string () const { + // TODO: rewrite to work with non-linearisable types std::string str ("*"); switch (pointer_type) @@ -3400,13 +3395,9 @@ TraitObjectType::as_string () const std::string str ("TraitObjectType: \n Has dyn dispatch: "); if (has_dyn) - { str += "true"; - } else - { str += "false"; - } str += "\n TypeParamBounds: "; if (type_param_bounds.empty ()) @@ -3416,9 +3407,7 @@ TraitObjectType::as_string () const else { for (const auto &bound : type_param_bounds) - { str += "\n " + bound->as_string (); - } } return str; @@ -3436,9 +3425,7 @@ BareFunctionType::as_string () const else { for (const auto &for_lifetime : for_lifetimes) - { str += "\n " + for_lifetime.as_string (); - } } str += "\n Qualifiers: " + function_qualifiers.as_string (); @@ -3451,30 +3438,20 @@ BareFunctionType::as_string () const else { for (const auto ¶m : params) - { str += "\n " + param.as_string (); - } } str += "\n Is variadic: "; if (is_variadic) - { str += "true"; - } else - { str += "false"; - } str += "\n Return type: "; if (!has_return_type ()) - { str += "none (void)"; - } else - { str += return_type->as_string (); - } return str; } @@ -3490,6 +3467,7 @@ ImplTraitTypeOneBound::as_string () const std::string TypePathSegmentGeneric::as_string () const { + // TODO: rewrite to work with non-linearisable types return TypePathSegment::as_string () + "<" + generic_args.as_string () + ">"; } @@ -3499,13 +3477,9 @@ TraitObjectTypeOneBound::as_string () const std::string str ("TraitObjectTypeOneBound: \n Has dyn dispatch: "); if (has_dyn) - { str += "true"; - } else - { str += "false"; - } str += "\n TraitBound: " + trait_bound.as_string (); @@ -3515,6 +3489,7 @@ TraitObjectTypeOneBound::as_string () const std::string TypePathFunction::as_string () const { + // TODO: rewrite to work with non-linearisable types std::string str ("("); if (has_inputs ()) @@ -3533,9 +3508,7 @@ TypePathFunction::as_string () const str += ")"; if (has_return_type ()) - { str += " -> " + return_type->as_string (); - } return str; } @@ -3543,24 +3516,28 @@ TypePathFunction::as_string () const std::string TypePathSegmentFunction::as_string () const { + // TODO: rewrite to work with non-linearisable types return TypePathSegment::as_string () + function_path.as_string (); } std::string ArrayType::as_string () const { + // TODO: rewrite to work with non-linearisable types and exprs return "[" + elem_type->as_string () + "; " + size->as_string () + "]"; } std::string SliceType::as_string () const { + // TODO: rewrite to work with non-linearisable types return "[" + elem_type->as_string () + "]"; } std::string TupleType::as_string () const { + // TODO: rewrite to work with non-linearisable types std::string str ("("); if (!is_unit_type ()) @@ -3584,7 +3561,7 @@ TupleType::as_string () const std::string StructExpr::as_string () const { - std::string str = ExprWithoutBlock::as_string (); + std::string str = append_attributes (outer_attrs, OUTER); indent_spaces (enter); str += "\n" + indent_spaces (stay) + "StructExpr:"; indent_spaces (enter); @@ -3611,9 +3588,7 @@ StructExprTuple::as_string () const // debug - null pointer check if (*i == nullptr) - { return "ERROR_MARK_STRING - nullptr struct expr tuple field"; - } str += '('; for (; i != e; i++) @@ -3649,11 +3624,14 @@ StructExprTuple::as_string () const std::string StructExprStruct::as_string () const { + // TODO: doesn't this require data from StructExpr? std::string str ("StructExprStruct (or subclass): "); str += "\n Path: " + get_struct_name ().as_string (); // inner attributes + str += append_attributes (inner_attrs, INNER); +#if 0 str += "\n inner attributes: "; if (inner_attrs.empty ()) { @@ -3666,6 +3644,7 @@ StructExprStruct::as_string () const for (const auto &attr : inner_attrs) str += "\n " + attr.as_string (); } +#endif return str; } @@ -3689,12 +3668,14 @@ StructExprFieldWithVal::as_string () const std::string StructExprFieldIdentifierValue::as_string () const { + // TODO: rewrite to work with non-linearisable exprs return field_name + " : " + StructExprFieldWithVal::as_string (); } std::string StructExprFieldIndexValue::as_string () const { + // TODO: rewrite to work with non-linearisable exprs return std::to_string (index) + " : " + StructExprFieldWithVal::as_string (); } @@ -3726,7 +3707,7 @@ StructExprStructFields::as_string () const std::string EnumExprStruct::as_string () const { - std::string str ("StructExprStruct (or subclass): "); + std::string str ("EnumExprStruct (or subclass): "); str += "\n Path: " + get_enum_variant_path ().as_string (); @@ -3754,12 +3735,14 @@ EnumExprFieldWithVal::as_string () const std::string EnumExprFieldIdentifierValue::as_string () const { + // TODO: rewrite to work with non-linearisable exprs return field_name + " : " + EnumExprFieldWithVal::as_string (); } std::string EnumExprFieldIndexValue::as_string () const { + // TODO: rewrite to work with non-linearisable exprs return std::to_string (index) + " : " + EnumExprFieldWithVal::as_string (); } @@ -3767,6 +3750,8 @@ std::string EnumItem::as_string () const { // outer attributes + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str = "outer attributes: "; if (outer_attrs.empty ()) { @@ -3779,6 +3764,7 @@ EnumItem::as_string () const for (const auto &attr : outer_attrs) str += "\n " + attr.as_string (); } +#endif str += "\n" + variant_name; @@ -3816,7 +3802,11 @@ EnumItemTuple::as_string () const std::string TupleField::as_string () const { + // TODO: rewrite to work with non-linearisable exprs + // outer attributes + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str = "outer attributes: "; if (outer_attrs.empty ()) { @@ -3827,15 +3817,12 @@ TupleField::as_string () const /* note that this does not print them with "outer attribute" syntax - * just the body */ for (const auto &attr : outer_attrs) - { str += "\n " + attr.as_string (); - } } +#endif if (has_visibility ()) - { str += "\n" + visibility.as_string (); - } str += " " + field_type->as_string (); @@ -3873,7 +3860,10 @@ EnumItemStruct::as_string () const std::string StructField::as_string () const { + // TODO: rewrite to work with non-linearisable exprs // outer attributes + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str = "outer attributes: "; if (outer_attrs.empty ()) { @@ -3884,15 +3874,12 @@ StructField::as_string () const /* note that this does not print them with "outer attribute" syntax - * just the body */ for (const auto &attr : outer_attrs) - { str += "\n " + attr.as_string (); - } } +#endif if (has_visibility ()) - { str += "\n" + visibility.as_string (); - } str += " " + field_name + " : " + field_type->as_string (); @@ -3902,6 +3889,7 @@ StructField::as_string () const std::string EnumItemDiscriminant::as_string () const { + // TODO: rewrite to work with non-linearisable exprs std::string str = EnumItem::as_string (); // add equal and expression @@ -3939,6 +3927,8 @@ std::string ExternalStaticItem::as_string () const { // outer attributes + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str = "outer attributes: "; if (outer_attrs.empty ()) { @@ -3951,6 +3941,7 @@ ExternalStaticItem::as_string () const for (const auto &attr : outer_attrs) str += "\n " + attr.as_string (); } +#endif // start visibility on new line and with a space str += "\n" + visibility.as_string () + " "; @@ -3973,6 +3964,8 @@ std::string ExternalFunctionItem::as_string () const { // outer attributes + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str = "outer attributes: "; if (outer_attrs.empty ()) { @@ -3985,6 +3978,7 @@ ExternalFunctionItem::as_string () const for (const auto &attr : outer_attrs) str += "\n " + attr.as_string (); } +#endif // start visibility on new line and with a space str += "\n" + visibility.as_string () + " "; @@ -4011,7 +4005,7 @@ ExternalFunctionItem::as_string () const stderr, "something really terrible has gone wrong - null pointer " "generic param in external function item."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + param->as_string (); @@ -4061,6 +4055,8 @@ ExternalFunctionItem::as_string () const std::string NamedFunctionParam::as_string () const { + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str = "outer attributes: "; if (!has_outer_attrs ()) @@ -4072,6 +4068,7 @@ NamedFunctionParam::as_string () const for (const auto &attr : outer_attrs) str += "\n " + attr.as_string (); } +#endif str += "\n" + name; @@ -4098,6 +4095,8 @@ attr.as_string(); std::string TraitItemFunc::as_string () const { + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str = "outer attributes: "; if (outer_attrs.empty ()) { @@ -4108,22 +4107,17 @@ TraitItemFunc::as_string () const /* note that this does not print them with "outer attribute" syntax - * just the body */ for (const auto &attr : outer_attrs) - { str += "\n " + attr.as_string (); - } } +#endif str += "\n" + decl.as_string (); str += "\n Definition (block expr): "; if (has_definition ()) - { str += block_expr->as_string (); - } else - { str += "none"; - } return str; } @@ -4150,7 +4144,7 @@ TraitFunctionDecl::as_string () const stderr, "something really terrible has gone wrong - null pointer " "generic param in trait function decl."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + param->as_string (); @@ -4161,9 +4155,7 @@ TraitFunctionDecl::as_string () const if (has_params ()) { for (const auto ¶m : function_params) - { str += "\n " + param.as_string (); - } } else { @@ -4172,23 +4164,15 @@ TraitFunctionDecl::as_string () const str += "\n Return type: "; if (has_return_type ()) - { str += return_type->as_string (); - } else - { str += "none (void)"; - } str += "\n Where clause: "; if (has_where_clause ()) - { str += where_clause.as_string (); - } else - { str += "none"; - } return str; } @@ -4196,6 +4180,8 @@ TraitFunctionDecl::as_string () const std::string TraitItemMethod::as_string () const { + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str = "outer attributes: "; if (outer_attrs.empty ()) { @@ -4206,22 +4192,17 @@ TraitItemMethod::as_string () const /* note that this does not print them with "outer attribute" syntax - * just the body */ for (const auto &attr : outer_attrs) - { str += "\n " + attr.as_string (); - } } +#endif str += "\n" + decl.as_string (); str += "\n Definition (block expr): "; if (has_definition ()) - { str += block_expr->as_string (); - } else - { str += "none"; - } return str; } @@ -4248,7 +4229,7 @@ TraitMethodDecl::as_string () const stderr, "something really terrible has gone wrong - null pointer " "generic param in trait function decl."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + param->as_string (); @@ -4261,9 +4242,7 @@ TraitMethodDecl::as_string () const if (has_params ()) { for (const auto ¶m : function_params) - { str += "\n " + param.as_string (); - } } else { @@ -4272,23 +4251,15 @@ TraitMethodDecl::as_string () const str += "\n Return type: "; if (has_return_type ()) - { str += return_type->as_string (); - } else - { str += "none (void)"; - } str += "\n Where clause: "; if (has_where_clause ()) - { str += where_clause.as_string (); - } else - { str += "none"; - } return str; } @@ -4296,6 +4267,9 @@ TraitMethodDecl::as_string () const std::string TraitItemConst::as_string () const { + // TODO: rewrite to work with non-linearisable exprs + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str = "outer attributes: "; if (outer_attrs.empty ()) { @@ -4306,17 +4280,14 @@ TraitItemConst::as_string () const /* note that this does not print them with "outer attribute" syntax - * just the body */ for (const auto &attr : outer_attrs) - { str += "\n " + attr.as_string (); - } } +#endif str += "\nconst " + name + " : " + type->as_string (); if (has_expression ()) - { str += " = " + expr->as_string (); - } return str; } @@ -4324,6 +4295,8 @@ TraitItemConst::as_string () const std::string TraitItemType::as_string () const { + std::string str = append_attributes (outer_attrs, OUTER); +#if 0 std::string str = "outer attributes: "; if (outer_attrs.empty ()) { @@ -4334,10 +4307,9 @@ TraitItemType::as_string () const /* note that this does not print them with "outer attribute" syntax - * just the body */ for (const auto &attr : outer_attrs) - { str += "\n " + attr.as_string (); - } } +#endif str += "\ntype " + name; @@ -4357,7 +4329,7 @@ TraitItemType::as_string () const stderr, "something really terrible has gone wrong - null pointer " "type param bound in trait item type."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + bound->as_string (); @@ -4370,6 +4342,7 @@ TraitItemType::as_string () const std::string SelfParam::as_string () const { + // TODO: rewrite to allow non-linearisable types if (is_error ()) { return "error"; @@ -4382,9 +4355,7 @@ SelfParam::as_string () const std::string str; if (is_mut) - { str += "mut "; - } str += "self : "; @@ -4398,9 +4369,7 @@ SelfParam::as_string () const std::string str = "&" + lifetime.as_string () + " "; if (is_mut) - { str += "mut "; - } str += "self"; @@ -4412,9 +4381,7 @@ SelfParam::as_string () const std::string str = "&"; if (is_mut) - { str += " mut "; - } str += "self"; @@ -4426,9 +4393,7 @@ SelfParam::as_string () const std::string str; if (is_mut) - { str += "mut "; - } str += "self"; @@ -4440,6 +4405,7 @@ SelfParam::as_string () const std::string ArrayElemsCopied::as_string () const { + // TODO: rewrite to allow non-linearisable exprs return elem_to_copy->as_string () + "; " + num_copies->as_string (); } @@ -4453,9 +4419,7 @@ LifetimeWhereClauseItem::as_string () const str += "\nLifetime bounds: "; for (const auto &bound : lifetime_bounds) - { str += "\n " + bound.as_string (); - } return str; } @@ -4472,9 +4436,7 @@ TypeBoundWhereClauseItem::as_string () const else { for (const auto &for_lifetime : for_lifetimes) - { str += "\n " + for_lifetime.as_string (); - } } str += "\nType: " + bound_type->as_string (); @@ -4485,9 +4447,7 @@ TypeBoundWhereClauseItem::as_string () const { // debug null pointer check if (bound == nullptr) - { - return "nullptr_POINTER_MARK - type param bounds"; - } + return "NULL_POINTER_MARK - type param bounds"; str += "\n " + bound->as_string (); } @@ -4508,7 +4468,7 @@ ArrayElemsValues::as_string () const fprintf (stderr, "something really terrible has gone wrong - null pointer " "expr in array elems values."); - return "nullptr_POINTER_MARK"; + return "NULL_POINTER_MARK"; } str += "\n " + expr->as_string (); @@ -4520,6 +4480,7 @@ ArrayElemsValues::as_string () const std::string MaybeNamedParam::as_string () const { + // TODO: rewrite to allow using non-linearisable types in dump std::string str; switch (param_kind) @@ -4838,7 +4799,7 @@ MacroParser::parse_path_meta_item () "failed to parse literal in attribute"); return nullptr; } - LiteralExpr expr (std::move (lit), locus); + LiteralExpr expr (std::move (lit), {}, locus); // stream_pos++; /* shouldn't be required anymore due to parsing literal actually * skipping the token */ @@ -4893,9 +4854,8 @@ MacroParser::parse_meta_item_seq () meta_items.push_back (std::move (inner)); if (peek_token ()->get_id () != COMMA) - { break; - } + skip_token (); } @@ -5056,7 +5016,7 @@ std::unique_ptr MacroParser::parse_meta_item_lit () { Location locus = peek_token ()->get_locus (); - LiteralExpr lit_expr (parse_literal (), locus); + LiteralExpr lit_expr (parse_literal (), {}, locus); return std::unique_ptr ( new MetaItemLitExpr (std::move (lit_expr))); } @@ -5294,7 +5254,7 @@ Attribute MetaNameValueStr::to_attribute () const { LiteralExpr lit_expr (str, Literal::LitType::STRING, - PrimitiveCoreType::CORETYPE_UNKNOWN, Location ()); + PrimitiveCoreType::CORETYPE_UNKNOWN, {}, Location ()); return Attribute (SimplePath::from_str (ident), std::unique_ptr ( new AttrInputLiteral (std::move (lit_expr)))); diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index b7baab4..9c7c567 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -846,14 +846,7 @@ class ExprWithoutBlock; // Base expression AST node - abstract class Expr { - // TODO: move outer attribute data to derived classes? - std::vector outer_attrs; - public: - // TODO: this mutable getter seems really dodgy. Think up better way. - const std::vector &get_outer_attrs () const { return outer_attrs; } - std::vector &get_outer_attrs () { return outer_attrs; } - // Unique pointer custom clone function std::unique_ptr clone_expr () const { @@ -869,8 +862,7 @@ public: * overrided in subclasses of ExprWithoutBlock */ virtual ExprWithoutBlock *as_expr_without_block () const { return nullptr; } - // TODO: make pure virtual if move out outer attributes to derived classes - virtual std::string as_string () const; + virtual std::string as_string () const = 0; virtual ~Expr () {} @@ -890,9 +882,7 @@ public: protected: // Constructor - Expr (std::vector outer_attribs = std::vector ()) - : outer_attrs (std::move (outer_attribs)), - node_id (Analysis::Mappings::get ()->get_next_node_id ()) + Expr () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} // Clone function implementation as pure virtual method @@ -900,10 +890,7 @@ protected: // TODO: think of less hacky way to implement this kind of thing // Sets outer attributes. - void set_outer_attrs (std::vector outer_attrs_to_set) - { - outer_attrs = std::move (outer_attrs_to_set); - } + virtual void set_outer_attrs (std::vector) = 0; NodeId node_id; }; @@ -912,12 +899,6 @@ protected: class ExprWithoutBlock : public Expr { protected: - // Constructor - ExprWithoutBlock (std::vector outer_attribs - = std::vector ()) - : Expr (std::move (outer_attribs)) - {} - // pure virtual clone implementation virtual ExprWithoutBlock *clone_expr_without_block_impl () const = 0; @@ -952,15 +933,13 @@ public: */ class IdentifierExpr : public ExprWithoutBlock { + std::vector outer_attrs; Identifier ident; Location locus; public: - IdentifierExpr (Identifier ident, Location locus = Location (), - std::vector outer_attrs - = std::vector ()) - : ExprWithoutBlock (std::move (outer_attrs)), ident (std::move (ident)), - locus (locus) + IdentifierExpr (Identifier ident, std::vector outer_attrs, Location locus) + : outer_attrs (std::move (outer_attrs)), ident (std::move (ident)), locus (locus) {} std::string as_string () const override { return ident; } @@ -982,6 +961,11 @@ public: void mark_for_strip () override { ident = {}; } bool is_marked_for_strip () const override { return ident.empty (); } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: // Clone method implementation IdentifierExpr *clone_expr_without_block_impl () const final override @@ -1677,22 +1661,7 @@ public: // Base path expression AST node - abstract class PathExpr : public ExprWithoutBlock -{ -protected: - PathExpr (std::vector outer_attribs) - : ExprWithoutBlock (std::move (outer_attribs)) - {} - -public: - // TODO: think of a better and less hacky way to allow this - - /* Replaces the outer attributes of this path expression with the given outer - * attributes. */ - void replace_outer_attrs (std::vector outer_attrs) - { - set_outer_attrs (std::move (outer_attrs)); - } -}; +{}; } // namespace AST } // namespace Rust diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index cfb4228..5177785 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -13,12 +13,7 @@ namespace AST { // AST node for an expression with an accompanying block - abstract class ExprWithBlock : public Expr { - // TODO: should this mean that a BlockExpr should be a member variable? protected: - ExprWithBlock (std::vector outer_attrs = std::vector ()) - : Expr (std::move (outer_attrs)) - {} - // pure virtual clone implementation virtual ExprWithBlock *clone_expr_with_block_impl () const = 0; @@ -41,6 +36,7 @@ public: // Literals? Or literal base? class LiteralExpr : public ExprWithoutBlock { + std::vector outer_attrs; Literal literal; Location locus; @@ -50,15 +46,15 @@ public: Literal::LitType get_lit_type () const { return literal.get_lit_type (); } LiteralExpr (std::string value_as_string, Literal::LitType type, - PrimitiveCoreType type_hint, Location locus, - std::vector outer_attrs = std::vector ()) - : ExprWithoutBlock (std::move (outer_attrs)), + PrimitiveCoreType type_hint, + std::vector outer_attrs, Location locus) + : outer_attrs (std::move (outer_attrs)), literal (std::move (value_as_string), type, type_hint), locus (locus) {} - LiteralExpr (Literal literal, Location locus, - std::vector outer_attrs = std::vector ()) - : ExprWithoutBlock (std::move (outer_attrs)), literal (std::move (literal)), + LiteralExpr (Literal literal, + std::vector outer_attrs, Location locus) + : outer_attrs (std::move (outer_attrs)), literal (std::move (literal)), locus (locus) {} @@ -79,6 +75,11 @@ public: void mark_for_strip () override { literal = Literal::create_error (); } bool is_marked_for_strip () const override { return literal.is_error (); } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -196,20 +197,21 @@ public: Location locus; protected: - /* Variable must be protected to allow derived classes to use it as a first - * class citizen */ + /* Variables must be protected to allow derived classes to use them as first + * class citizens */ + std::vector outer_attrs; std::unique_ptr main_or_left_expr; // Constructor (only for initialisation of expr purposes) OperatorExpr (std::unique_ptr main_or_left_expr, std::vector outer_attribs, Location locus) - : ExprWithoutBlock (std::move (outer_attribs)), locus (locus), + : locus (locus), outer_attrs (std::move (outer_attribs)), main_or_left_expr (std::move (main_or_left_expr)) {} // Copy constructor (only for initialisation of expr purposes) OperatorExpr (OperatorExpr const &other) - : ExprWithoutBlock (other), locus (other.locus) + : locus (other.locus), outer_attrs (other.outer_attrs) { // guard to prevent null dereference (only required if error state) if (other.main_or_left_expr != nullptr) @@ -221,7 +223,7 @@ protected: { ExprWithoutBlock::operator= (other); locus = other.locus; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; // guard to prevent null dereference (only required if error state) if (other.main_or_left_expr != nullptr) @@ -246,6 +248,11 @@ public: { return main_or_left_expr == nullptr; } + + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } }; /* Unary prefix & or &mut (or && and &&mut) borrow operator. Cannot be @@ -859,6 +866,7 @@ protected: // Expression in parentheses (i.e. like literally just any 3 + (2 * 6)) class GroupedExpr : public ExprWithoutBlock { + std::vector outer_attrs; std::vector inner_attrs; std::unique_ptr expr_in_parens; Location locus; @@ -869,17 +877,22 @@ public: const std::vector &get_inner_attrs () const { return inner_attrs; } std::vector &get_inner_attrs () { return inner_attrs; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + GroupedExpr (std::unique_ptr parenthesised_expr, std::vector inner_attribs, std::vector outer_attribs, Location locus) - : ExprWithoutBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), inner_attrs (std::move (inner_attribs)), expr_in_parens (std::move (parenthesised_expr)), locus (locus) {} // Copy constructor includes clone for expr_in_parens GroupedExpr (GroupedExpr const &other) - : ExprWithoutBlock (other), inner_attrs (other.inner_attrs), + : ExprWithoutBlock (other), outer_attrs (other.outer_attrs), inner_attrs (other.inner_attrs), locus (other.locus) { // guard to prevent null dereference (only required if error state) @@ -893,7 +906,7 @@ public: ExprWithoutBlock::operator= (other); inner_attrs = other.inner_attrs; locus = other.locus; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; // guard to prevent null dereference (only required if error state) if (other.expr_in_parens != nullptr) @@ -1090,9 +1103,9 @@ protected: // Array definition-ish expression class ArrayExpr : public ExprWithoutBlock { + std::vector outer_attrs; std::vector inner_attrs; std::unique_ptr internal_elements; - Location locus; // TODO: find another way to store this to save memory? @@ -1104,6 +1117,11 @@ public: const std::vector &get_inner_attrs () const { return inner_attrs; } std::vector &get_inner_attrs () { return inner_attrs; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + // Returns whether array expr has array elems or if it is just empty. bool has_array_elems () const { return internal_elements != nullptr; } @@ -1111,14 +1129,14 @@ public: ArrayExpr (std::unique_ptr array_elems, std::vector inner_attribs, std::vector outer_attribs, Location locus) - : ExprWithoutBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), inner_attrs (std::move (inner_attribs)), internal_elements (std::move (array_elems)), locus (locus) {} // Copy constructor requires cloning ArrayElems for polymorphism to hold ArrayExpr (ArrayExpr const &other) - : ExprWithoutBlock (other), inner_attrs (other.inner_attrs), + : ExprWithoutBlock (other), outer_attrs (other.outer_attrs), inner_attrs (other.inner_attrs), locus (other.locus), marked_for_strip (other.marked_for_strip) { if (other.has_array_elems ()) @@ -1132,7 +1150,7 @@ public: inner_attrs = other.inner_attrs; locus = other.locus; marked_for_strip = other.marked_for_strip; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; if (other.has_array_elems ()) internal_elements = other.internal_elements->clone_array_elems (); @@ -1178,9 +1196,9 @@ protected: * implementation */ class ArrayIndexExpr : public ExprWithoutBlock { + std::vector outer_attrs; std::unique_ptr array_expr; std::unique_ptr index_expr; - Location locus; public: @@ -1189,14 +1207,14 @@ public: ArrayIndexExpr (std::unique_ptr array_expr, std::unique_ptr array_index_expr, std::vector outer_attribs, Location locus) - : ExprWithoutBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), array_expr (std::move (array_expr)), index_expr (std::move (array_index_expr)), locus (locus) {} // Copy constructor requires special cloning due to unique_ptr ArrayIndexExpr (ArrayIndexExpr const &other) - : ExprWithoutBlock (other), locus (other.locus) + : ExprWithoutBlock (other), outer_attrs (other.outer_attrs), locus (other.locus) { // guard to prevent null dereference (only required if error state) if (other.array_expr != nullptr) @@ -1209,7 +1227,7 @@ public: ArrayIndexExpr &operator= (ArrayIndexExpr const &other) { ExprWithoutBlock::operator= (other); - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; locus = other.locus; // guard to prevent null dereference (only required if error state) @@ -1259,6 +1277,11 @@ public: return index_expr; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1271,11 +1294,9 @@ protected: // AST representation of a tuple class TupleExpr : public ExprWithoutBlock { + std::vector outer_attrs; std::vector inner_attrs; - std::vector > tuple_elems; - // replaces (inlined version of) TupleElements - Location locus; // TODO: find another way to store this to save memory? @@ -1287,17 +1308,22 @@ public: const std::vector &get_inner_attrs () const { return inner_attrs; } std::vector &get_inner_attrs () { return inner_attrs; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + TupleExpr (std::vector > tuple_elements, std::vector inner_attribs, std::vector outer_attribs, Location locus) - : ExprWithoutBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), inner_attrs (std::move (inner_attribs)), tuple_elems (std::move (tuple_elements)), locus (locus) {} // copy constructor with vector clone TupleExpr (TupleExpr const &other) - : ExprWithoutBlock (other), inner_attrs (other.inner_attrs), + : ExprWithoutBlock (other), outer_attrs (other.outer_attrs), inner_attrs (other.inner_attrs), locus (other.locus), marked_for_strip (other.marked_for_strip) { tuple_elems.reserve (other.tuple_elems.size ()); @@ -1309,6 +1335,7 @@ public: TupleExpr &operator= (TupleExpr const &other) { ExprWithoutBlock::operator= (other); + outer_attrs = other.outer_attrs; inner_attrs = other.inner_attrs; locus = other.locus; marked_for_strip = other.marked_for_strip; @@ -1359,6 +1386,7 @@ protected: // AST representation of a tuple indexing expression class TupleIndexExpr : public ExprWithoutBlock { + std::vector outer_attrs; std::unique_ptr tuple_expr; // TupleIndex is a decimal int literal with no underscores or suffix TupleIndex tuple_index; @@ -1374,13 +1402,13 @@ public: TupleIndexExpr (std::unique_ptr tuple_expr, TupleIndex index, std::vector outer_attribs, Location locus) - : ExprWithoutBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), tuple_expr (std::move (tuple_expr)), tuple_index (index), locus (locus) {} // Copy constructor requires a clone for tuple_expr TupleIndexExpr (TupleIndexExpr const &other) - : ExprWithoutBlock (other), tuple_index (other.tuple_index), + : ExprWithoutBlock (other), outer_attrs (other.outer_attrs), tuple_index (other.tuple_index), locus (other.locus) { // guard to prevent null dereference (only required if error state) @@ -1394,7 +1422,7 @@ public: ExprWithoutBlock::operator= (other); tuple_index = other.tuple_index; locus = other.locus; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; // guard to prevent null dereference (only required if error state) if (other.tuple_expr != nullptr) @@ -1425,6 +1453,11 @@ public: return tuple_expr; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1437,13 +1470,14 @@ protected: // Base struct/tuple/union value creator AST node (abstract) class StructExpr : public ExprWithoutBlock { + std::vector outer_attrs; PathInExpression struct_name; protected: // Protected constructor to allow initialising struct_name StructExpr (PathInExpression struct_path, std::vector outer_attribs) - : ExprWithoutBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), struct_name (std::move (struct_path)) {} @@ -1459,6 +1493,11 @@ public: struct_name = PathInExpression::create_error (); } bool is_marked_for_strip () const override { return struct_name.is_error (); } + + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } }; // Actual AST node of the struct creator (with no fields). Not abstract! @@ -1936,13 +1975,14 @@ protected: // Base AST node representing creation of an enum variant instance - abstract class EnumVariantExpr : public ExprWithoutBlock { + std::vector outer_attrs; PathInExpression enum_variant_path; protected: // Protected constructor for initialising enum_variant_path EnumVariantExpr (PathInExpression path_to_enum_variant, std::vector outer_attribs) - : ExprWithoutBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), enum_variant_path (std::move (path_to_enum_variant)) {} @@ -1962,6 +2002,11 @@ public: { return enum_variant_path.is_error (); } + + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } }; /* Base AST node for a single enum expression field (in enum instance creation) @@ -2183,7 +2228,6 @@ protected: class EnumExprTuple : public EnumVariantExpr { std::vector > values; - Location locus; public: @@ -2282,10 +2326,9 @@ class Function; // Function call expression AST node class CallExpr : public ExprWithoutBlock { + std::vector outer_attrs; std::unique_ptr function; - // inlined form of CallParams std::vector > params; - Location locus; public: @@ -2296,14 +2339,14 @@ public: CallExpr (std::unique_ptr function_expr, std::vector > function_params, std::vector outer_attribs, Location locus) - : ExprWithoutBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), function (std::move (function_expr)), params (std::move (function_params)), locus (locus) {} // copy constructor requires clone CallExpr (CallExpr const &other) - : ExprWithoutBlock (other), locus (other.locus) + : ExprWithoutBlock (other), outer_attrs (other.outer_attrs), locus (other.locus) { // guard to prevent null dereference (only required if error state) if (other.function != nullptr) @@ -2319,7 +2362,7 @@ public: { ExprWithoutBlock::operator= (other); locus = other.locus; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; // guard to prevent null dereference (only required if error state) if (other.function != nullptr) @@ -2373,6 +2416,11 @@ public: return function; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -2385,11 +2433,10 @@ protected: // Method call expression AST node class MethodCallExpr : public ExprWithoutBlock { + std::vector outer_attrs; std::unique_ptr receiver; PathExprSegment method_name; - // inlined form of CallParams std::vector > params; - Location locus; public: @@ -2399,7 +2446,7 @@ public: PathExprSegment method_path, std::vector > method_params, std::vector outer_attribs, Location locus) - : ExprWithoutBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), receiver (std::move (call_receiver)), method_name (std::move (method_path)), params (std::move (method_params)), locus (locus) @@ -2407,7 +2454,7 @@ public: // copy constructor required due to cloning MethodCallExpr (MethodCallExpr const &other) - : ExprWithoutBlock (other), method_name (other.method_name), + : ExprWithoutBlock (other), outer_attrs (other.outer_attrs), method_name (other.method_name), locus (other.locus) { // guard to prevent null dereference (only required if error state) @@ -2425,7 +2472,7 @@ public: ExprWithoutBlock::operator= (other); method_name = other.method_name; locus = other.locus; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; // guard to prevent null dereference (only required if error state) if (other.receiver != nullptr) @@ -2470,6 +2517,11 @@ public: const PathExprSegment &get_method_name () const { return method_name; } PathExprSegment &get_method_name () { return method_name; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -2483,9 +2535,9 @@ protected: // Struct or union field access expression AST node class FieldAccessExpr : public ExprWithoutBlock { + std::vector outer_attrs; std::unique_ptr receiver; Identifier field; - Location locus; public: @@ -2494,14 +2546,14 @@ public: FieldAccessExpr (std::unique_ptr field_access_receiver, Identifier field_name, std::vector outer_attribs, Location locus) - : ExprWithoutBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), receiver (std::move (field_access_receiver)), field (std::move (field_name)), locus (locus) {} // Copy constructor required due to unique_ptr cloning FieldAccessExpr (FieldAccessExpr const &other) - : ExprWithoutBlock (other), field (other.field), locus (other.locus) + : ExprWithoutBlock (other), outer_attrs (other.outer_attrs), field (other.field), locus (other.locus) { // guard to prevent null dereference (only required if error state) if (other.receiver != nullptr) @@ -2514,7 +2566,7 @@ public: ExprWithoutBlock::operator= (other); field = other.field; locus = other.locus; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; // guard to prevent null dereference (only required if error state) if (other.receiver != nullptr) @@ -2547,6 +2599,11 @@ public: Identifier get_field_name () const { return field; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -2645,17 +2702,15 @@ public: // Base closure definition expression AST node - abstract class ClosureExpr : public ExprWithoutBlock { + std::vector outer_attrs; bool has_move; std::vector params; // may be empty - /* also note a double pipe "||" can be used for empty params - does not need a - * space */ - Location locus; protected: ClosureExpr (std::vector closure_params, bool has_move, std::vector outer_attribs, Location locus) - : ExprWithoutBlock (std::move (outer_attribs)), has_move (has_move), + : outer_attrs (std::move (outer_attribs)), has_move (has_move), params (std::move (closure_params)), locus (locus) {} @@ -2668,6 +2723,11 @@ public: // TODO: this mutable getter seems really dodgy. Think up better way. const std::vector &get_params () const { return params; } std::vector &get_params () { return params; } + + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } }; // Represents a non-type-specified closure expression AST node @@ -2746,13 +2806,10 @@ protected: // A block AST node class BlockExpr : public ExprWithBlock { + std::vector outer_attrs; std::vector inner_attrs; - - // bool has_statements; std::vector > statements; - // bool has_expr; - std::unique_ptr expr; // inlined from Statements - + std::unique_ptr expr; Location locus; bool marked_for_strip = false; @@ -2769,7 +2826,7 @@ public: std::unique_ptr block_expr, std::vector inner_attribs, std::vector outer_attribs, Location locus) - : ExprWithBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), inner_attrs (std::move (inner_attribs)), statements (std::move (block_statements)), expr (std::move (block_expr)), locus (locus) @@ -2777,7 +2834,7 @@ public: // Copy constructor with clone BlockExpr (BlockExpr const &other) - : ExprWithBlock (other), inner_attrs (other.inner_attrs), + : ExprWithBlock (other), outer_attrs (other.outer_attrs), inner_attrs (other.inner_attrs), locus (other.locus), marked_for_strip (other.marked_for_strip) { // guard to protect from null pointer dereference @@ -2796,7 +2853,7 @@ public: inner_attrs = other.inner_attrs; locus = other.locus; marked_for_strip = other.marked_for_strip; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; // guard to protect from null pointer dereference if (other.expr != nullptr) @@ -2861,6 +2918,11 @@ public: // Removes the tail expression from the block. void strip_tail_expr () { expr = nullptr; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -2970,7 +3032,7 @@ protected: // AST node representing continue expression within loops class ContinueExpr : public ExprWithoutBlock { - // bool has_label; + std::vector outer_attrs; Lifetime label; Location locus; @@ -2984,10 +3046,9 @@ public: bool has_label () const { return !label.is_error (); } // Constructor for a ContinueExpr with a label. - ContinueExpr (Location locus, Lifetime label = Lifetime::error (), - std::vector outer_attribs - = std::vector ()) - : ExprWithoutBlock (std::move (outer_attribs)), label (std::move (label)), + ContinueExpr (Lifetime label, + std::vector outer_attribs, Location locus) + : outer_attrs (std::move (outer_attribs)), label (std::move (label)), locus (locus) {} @@ -3000,6 +3061,11 @@ public: void mark_for_strip () override { marked_for_strip = true; } bool is_marked_for_strip () const override { return marked_for_strip; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -3013,12 +3079,9 @@ protected: // AST node representing break expression within loops class BreakExpr : public ExprWithoutBlock { - // bool has_label; + std::vector outer_attrs; Lifetime label; - - // bool has_break_expr; std::unique_ptr break_expr; - Location locus; // TODO: find another way to store this to save memory? @@ -3035,17 +3098,17 @@ public: bool has_break_expr () const { return break_expr != nullptr; } // Constructor for a break expression - BreakExpr (Location locus, Lifetime break_label = Lifetime::error (), - std::unique_ptr expr_in_break = nullptr, - std::vector outer_attribs = std::vector ()) - : ExprWithoutBlock (std::move (outer_attribs)), + BreakExpr (Lifetime break_label, + std::unique_ptr expr_in_break, + std::vector outer_attribs, Location locus) + : outer_attrs (std::move (outer_attribs)), label (std::move (break_label)), break_expr (std::move (expr_in_break)), locus (locus) {} // Copy constructor defined to use clone for unique pointer BreakExpr (BreakExpr const &other) - : ExprWithoutBlock (other), label (other.label), locus (other.locus), + : ExprWithoutBlock (other), outer_attrs (other.outer_attrs), label (other.label), locus (other.locus), marked_for_strip (other.marked_for_strip) { // guard to protect from null pointer dereference @@ -3060,7 +3123,7 @@ public: label = other.label; locus = other.locus; marked_for_strip = other.marked_for_strip; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; // guard to protect from null pointer dereference if (other.break_expr != nullptr) @@ -3091,6 +3154,11 @@ public: return break_expr; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -3107,13 +3175,14 @@ class RangeExpr : public ExprWithoutBlock protected: // outer attributes not allowed before range expressions - RangeExpr (Location locus) - : ExprWithoutBlock (std::vector ()), locus (locus) - {} + RangeExpr (Location locus) : locus (locus) {} public: Location get_locus () const { return locus; } Location get_locus_slow () const final override { return get_locus (); } + + // should never be called - error if called + void set_outer_attrs (std::vector new_attrs) override { rust_assert (false); } }; // Range from (inclusive) and to (exclusive) expression AST node object @@ -3501,8 +3570,8 @@ protected: // Return expression AST node representation class ReturnExpr : public ExprWithoutBlock { + std::vector outer_attrs; std::unique_ptr return_expr; - Location locus; // TODO: find another way to store this to save memory? @@ -3516,15 +3585,15 @@ public: bool has_returned_expr () const { return return_expr != nullptr; } // Constructor for ReturnExpr. - ReturnExpr (Location locus, std::unique_ptr returned_expr = nullptr, - std::vector outer_attribs = std::vector ()) - : ExprWithoutBlock (std::move (outer_attribs)), + ReturnExpr (std::unique_ptr returned_expr, + std::vector outer_attribs, Location locus) + : outer_attrs (std::move (outer_attribs)), return_expr (std::move (returned_expr)), locus (locus) {} // Copy constructor with clone ReturnExpr (ReturnExpr const &other) - : ExprWithoutBlock (other), locus (other.locus), + : ExprWithoutBlock (other), outer_attrs (other.outer_attrs), locus (other.locus), marked_for_strip (other.marked_for_strip) { // guard to protect from null pointer dereference @@ -3538,7 +3607,7 @@ public: ExprWithoutBlock::operator= (other); locus = other.locus; marked_for_strip = other.marked_for_strip; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; // guard to protect from null pointer dereference if (other.return_expr != nullptr) @@ -3569,6 +3638,11 @@ public: return return_expr; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -3584,6 +3658,7 @@ class MacroInvocation; // An unsafe block AST node class UnsafeBlockExpr : public ExprWithBlock { + std::vector outer_attrs; // Or just have it extend BlockExpr std::unique_ptr expr; Location locus; @@ -3593,13 +3668,13 @@ public: UnsafeBlockExpr (std::unique_ptr block_expr, std::vector outer_attribs, Location locus) - : ExprWithBlock (std::move (outer_attribs)), expr (std::move (block_expr)), + : outer_attrs (std::move (outer_attribs)), expr (std::move (block_expr)), locus (locus) {} // Copy constructor with clone UnsafeBlockExpr (UnsafeBlockExpr const &other) - : ExprWithBlock (other), locus (other.locus) + : ExprWithBlock (other), outer_attrs (other.outer_attrs), locus (other.locus) { // guard to prevent null dereference (only required if error state) if (other.expr != nullptr) @@ -3611,7 +3686,7 @@ public: { ExprWithBlock::operator= (other); locus = other.locus; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; // guard to prevent null dereference (only required if error state) if (other.expr != nullptr) @@ -3642,6 +3717,11 @@ public: return expr; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -3679,9 +3759,8 @@ class BaseLoopExpr : public ExprWithBlock { protected: // protected to allow subclasses better use of them - // bool has_loop_label; + std::vector outer_attrs; LoopLabel loop_label; - std::unique_ptr loop_block; private: @@ -3693,14 +3772,14 @@ protected: LoopLabel loop_label = LoopLabel::error (), std::vector outer_attribs = std::vector ()) - : ExprWithBlock (std::move (outer_attribs)), + : outer_attrs (std::move (outer_attribs)), loop_label (std::move (loop_label)), loop_block (std::move (loop_block)), locus (locus) {} // Copy constructor for BaseLoopExpr with clone BaseLoopExpr (BaseLoopExpr const &other) - : ExprWithBlock (other), loop_label (other.loop_label), locus (other.locus) + : ExprWithBlock (other), outer_attrs (other.outer_attrs), loop_label (other.loop_label), locus (other.locus) { // guard to prevent null dereference (only required if error state) if (other.loop_block != nullptr) @@ -3713,7 +3792,7 @@ protected: ExprWithBlock::operator= (other); loop_label = other.loop_label; locus = other.locus; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; // guard to prevent null dereference (only required if error state) if (other.loop_block != nullptr) @@ -3744,6 +3823,11 @@ public: rust_assert (loop_block != nullptr); return loop_block; } + + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } }; // 'Loop' expression (i.e. the infinite loop) AST node @@ -3989,7 +4073,6 @@ class IfExpr : public ExprWithBlock { std::unique_ptr condition; std::unique_ptr if_block; - Location locus; public: @@ -3997,8 +4080,7 @@ public: IfExpr (std::unique_ptr condition, std::unique_ptr if_block, Location locus) - : ExprWithBlock (std::vector ()), - condition (std::move (condition)), if_block (std::move (if_block)), + : condition (std::move (condition)), if_block (std::move (if_block)), locus (locus) {} // outer attributes are never allowed on IfExprs @@ -4079,6 +4161,9 @@ public: return if_block == nullptr && condition == nullptr; } + // this should never be called + void set_outer_attrs (std::vector new_attrs) override { rust_assert (false); } + protected: // Base clone function but still concrete as concrete base class virtual IfExpr *clone_if_expr_impl () const { return new IfExpr (*this); } @@ -4209,7 +4294,6 @@ protected: // Basic "if let" expression AST node with no else class IfLetExpr : public ExprWithBlock { - // MatchArmPatterns patterns; std::vector > match_arm_patterns; // inlined std::unique_ptr value; std::unique_ptr if_block; @@ -4221,8 +4305,7 @@ public: IfLetExpr (std::vector > match_arm_patterns, std::unique_ptr value, std::unique_ptr if_block, Location locus) - : ExprWithBlock (std::vector ()), - match_arm_patterns (std::move (match_arm_patterns)), + : match_arm_patterns (std::move (match_arm_patterns)), value (std::move (value)), if_block (std::move (if_block)), locus (locus) {} // outer attributes not allowed on if let exprs either @@ -4315,6 +4398,9 @@ public: return match_arm_patterns; } + // this should never be called + void set_outer_attrs (std::vector new_attrs) override { rust_assert (false); } + protected: /* Use covariance to implement clone function as returning this object rather * than base (or rather this or any derived object) */ @@ -4826,15 +4912,10 @@ protected: // Match expression AST node class MatchExpr : public ExprWithBlock { + std::vector outer_attrs; std::unique_ptr branch_value; std::vector inner_attrs; - - // bool has_match_arms; - // MatchArms match_arms; - // std::vector > match_arms; // inlined from - // MatchArms std::vector match_arms; - Location locus; public: @@ -4844,11 +4925,10 @@ public: bool has_match_arms () const { return !match_arms.empty (); } MatchExpr (std::unique_ptr branch_value, - // std::vector > match_arms, std::vector match_arms, std::vector inner_attrs, std::vector outer_attrs, Location locus) - : ExprWithBlock (std::move (outer_attrs)), + : outer_attrs (std::move (outer_attrs)), branch_value (std::move (branch_value)), inner_attrs (std::move (inner_attrs)), match_arms (std::move (match_arms)), locus (locus) @@ -4856,16 +4936,12 @@ public: // Copy constructor requires clone due to unique_ptr MatchExpr (MatchExpr const &other) - : ExprWithBlock (other), inner_attrs (other.inner_attrs), + : ExprWithBlock (other), outer_attrs (other.outer_attrs), inner_attrs (other.inner_attrs), match_arms (other.match_arms), locus (other.locus) { // guard to prevent null dereference (only required if error state) if (other.branch_value != nullptr) branch_value = other.branch_value->clone_expr (); - - /*match_arms.reserve (other.match_arms.size ()); - for (const auto &e : other.match_arms) - match_arms.push_back (e->clone_match_case ());*/ } // Overloaded assignment operator to clone due to unique_ptr @@ -4874,7 +4950,7 @@ public: ExprWithBlock::operator= (other); inner_attrs = other.inner_attrs; match_arms = other.match_arms; - // outer_attrs = other.outer_attrs; + outer_attrs = other.outer_attrs; locus = other.locus; // guard to prevent null dereference (only required if error state) @@ -4883,10 +4959,6 @@ public: else branch_value = nullptr; - /*match_arms.reserve (other.match_arms.size ()); - for (const auto &e : other.match_arms) - match_arms.push_back (e->clone_match_case ());*/ - return *this; } @@ -4907,6 +4979,11 @@ public: const std::vector &get_inner_attrs () const { return inner_attrs; } std::vector &get_inner_attrs () { return inner_attrs; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + // TODO: is this better? Or is a "vis_block" better? std::unique_ptr &get_scrutinee_expr () { @@ -4929,6 +5006,7 @@ protected: // Await expression AST node (pseudo-member variable access) class AwaitExpr : public ExprWithoutBlock { + std::vector outer_attrs; std::unique_ptr awaited_expr; Location locus; @@ -4936,13 +5014,13 @@ public: // TODO: ensure outer attributes are actually allowed AwaitExpr (std::unique_ptr awaited_expr, std::vector outer_attrs, Location locus) - : ExprWithoutBlock (std::move (outer_attrs)), + : outer_attrs (std::move (outer_attrs)), awaited_expr (std::move (awaited_expr)), locus (locus) {} // copy constructor with clone AwaitExpr (AwaitExpr const &other) - : ExprWithoutBlock (other), locus (other.locus) + : ExprWithoutBlock (other), outer_attrs (other.outer_attrs), locus (other.locus) { // guard to prevent null dereference (only required if error state) if (other.awaited_expr != nullptr) @@ -4953,6 +5031,7 @@ public: AwaitExpr &operator= (AwaitExpr const &other) { ExprWithoutBlock::operator= (other); + outer_attrs = other.outer_attrs; locus = other.locus; // guard to prevent null dereference (only required if error state) @@ -4986,6 +5065,11 @@ public: return awaited_expr; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -4999,6 +5083,7 @@ protected: class AsyncBlockExpr : public ExprWithBlock { // TODO: should this extend BlockExpr rather than be a composite of it? + std::vector outer_attrs; bool has_move; std::unique_ptr block_expr; Location locus; @@ -5006,13 +5091,13 @@ class AsyncBlockExpr : public ExprWithBlock public: AsyncBlockExpr (std::unique_ptr block_expr, bool has_move, std::vector outer_attrs, Location locus) - : ExprWithBlock (std::move (outer_attrs)), has_move (has_move), + : outer_attrs (std::move (outer_attrs)), has_move (has_move), block_expr (std::move (block_expr)), locus (locus) {} // copy constructor with clone AsyncBlockExpr (AsyncBlockExpr const &other) - : ExprWithBlock (other), has_move (other.has_move), locus (other.locus) + : ExprWithBlock (other), outer_attrs (other.outer_attrs), has_move (other.has_move), locus (other.locus) { // guard to prevent null dereference (only required if error state) if (other.block_expr != nullptr) @@ -5023,6 +5108,7 @@ public: AsyncBlockExpr &operator= (AsyncBlockExpr const &other) { ExprWithBlock::operator= (other); + outer_attrs = other.outer_attrs; has_move = other.has_move; locus = other.locus; @@ -5057,6 +5143,11 @@ public: return block_expr; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index b5fb858..79d513f 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -360,6 +360,7 @@ class MacroInvocation : public TypeNoBounds, public Pattern, public ExprWithoutBlock { + std::vector outer_attrs; /*SimplePath path; DelimTokenTree token_tree;*/ MacroInvocData invoc_data; @@ -375,7 +376,7 @@ public: {}*/ MacroInvocation (MacroInvocData invoc_data, std::vector outer_attrs, Location locus) - : ExprWithoutBlock (std::move (outer_attrs)), + : outer_attrs (std::move (outer_attrs)), invoc_data (std::move (invoc_data)), locus (locus) {} @@ -391,6 +392,11 @@ public: return invoc_data.is_marked_for_strip (); } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h index 2b7c973..b30dffc 100644 --- a/gcc/rust/ast/rust-path.h +++ b/gcc/rust/ast/rust-path.h @@ -311,9 +311,9 @@ public: * arguments) */ class PathInExpression : public PathPattern, public PathExpr { + std::vector outer_attrs; bool has_opening_scope_resolution; Location locus; - NodeId _node_id; public: @@ -321,12 +321,10 @@ public: // Constructor PathInExpression (std::vector path_segments, - Location locus = Location (), - bool has_opening_scope_resolution = false, - std::vector outer_attrs - = std::vector ()) + std::vector outer_attrs, Location locus, + bool has_opening_scope_resolution = false) : PathPattern (std::move (path_segments)), - PathExpr (std::move (outer_attrs)), + outer_attrs (std::move (outer_attrs)), has_opening_scope_resolution (has_opening_scope_resolution), locus (locus), _node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} @@ -334,7 +332,7 @@ public: // Creates an error state path in expression. static PathInExpression create_error () { - return PathInExpression (std::vector ()); + return PathInExpression ({}, {}, Location ()); } // Returns whether path in expression is in an error state. @@ -360,10 +358,16 @@ public: // Invalid if path is empty (error state), so base stripping on that. void mark_for_strip () override { remove_all_segments (); } bool is_marked_for_strip () const override { return is_error (); } - bool opening_scope_resolution () { return has_opening_scope_resolution; } + + bool opening_scope_resolution () const { return has_opening_scope_resolution; } NodeId get_node_id () const override { return _node_id; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -835,6 +839,7 @@ public: * allows specifying trait functions) */ class QualifiedPathInExpression : public PathPattern, public PathExpr { + std::vector outer_attrs; QualifiedPathType path_type; Location locus; @@ -843,11 +848,10 @@ public: QualifiedPathInExpression (QualifiedPathType qual_path_type, std::vector path_segments, - Location locus = Location (), - std::vector outer_attrs - = std::vector ()) + std::vector outer_attrs, + Location locus) : PathPattern (std::move (path_segments)), - PathExpr (std::move (outer_attrs)), + outer_attrs (std::move (outer_attrs)), path_type (std::move (qual_path_type)), locus (locus) {} @@ -861,7 +865,7 @@ public: static QualifiedPathInExpression create_error () { return QualifiedPathInExpression (QualifiedPathType::create_error (), - std::vector ()); + {}, {}, Location ()); } Location get_locus () const { return locus; } @@ -883,6 +887,11 @@ public: return path_type; } + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } + + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 39bba4f..2f4c010 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -1529,13 +1529,12 @@ public: } void visit (AST::IfExpr &expr) override { - // initial strip test based on outer attrs - expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) - { - expr.mark_for_strip (); - return; - } + // TODO: for if expressions, are attributes allowed if it is part of an expression statement? + // if so, probably have to add a "does_expr_allow_attrs()" method to Expr and then don't move attrs. + // otherwise, that may be useful anyway for better error messages. + + // NOTE: IfExpr literally doesn't support outer attrs, so no strip code + // TODO: is this still true? can't find info on page // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); @@ -1555,13 +1554,7 @@ public: } void visit (AST::IfExprConseqElse &expr) override { - // initial strip test based on outer attrs - expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) - { - expr.mark_for_strip (); - return; - } + // NOTE: IfExpr literally doesn't support outer attrs, so no strip code // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); @@ -1589,13 +1582,7 @@ public: } void visit (AST::IfExprConseqIf &expr) override { - // initial strip test based on outer attrs - expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) - { - expr.mark_for_strip (); - return; - } + // NOTE: IfExpr literally doesn't support outer attrs, so no strip code // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); @@ -1623,13 +1610,7 @@ public: } void visit (AST::IfExprConseqIfLet &expr) override { - // initial strip test based on outer attrs - expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) - { - expr.mark_for_strip (); - return; - } + // NOTE: IfExpr literally doesn't support outer attrs, so no strip code // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); @@ -1658,13 +1639,7 @@ public: } void visit (AST::IfLetExpr &expr) override { - // initial strip test based on outer attrs - expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) - { - expr.mark_for_strip (); - return; - } + // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code for (auto &pattern : expr.get_patterns ()) { @@ -1692,13 +1667,7 @@ public: } void visit (AST::IfLetExprConseqElse &expr) override { - // initial strip test based on outer attrs - expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) - { - expr.mark_for_strip (); - return; - } + // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code for (auto &pattern : expr.get_patterns ()) { @@ -1734,13 +1703,7 @@ public: } void visit (AST::IfLetExprConseqIf &expr) override { - // initial strip test based on outer attrs - expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) - { - expr.mark_for_strip (); - return; - } + // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code for (auto &pattern : expr.get_patterns ()) { @@ -1776,13 +1739,7 @@ public: } void visit (AST::IfLetExprConseqIfLet &expr) override { - // initial strip test based on outer attrs - expander.expand_cfg_attrs (expr.get_outer_attrs ()); - if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) - { - expr.mark_for_strip (); - return; - } + // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code for (auto &pattern : expr.get_patterns ()) { diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 87e02dd..a0d2af9 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -791,7 +791,7 @@ Parser::parse_attr_input () } // create actual LiteralExpr - AST::LiteralExpr lit_expr (t->get_str (), lit_type, t->get_type_hint (), + AST::LiteralExpr lit_expr (t->get_str (), lit_type, t->get_type_hint (), {}, t->get_locus ()); std::unique_ptr attr_input_lit ( @@ -6361,9 +6361,8 @@ Parser::parse_path_in_expression () segments.shrink_to_fit (); - return AST::PathInExpression (std::move (segments), locus, - has_opening_scope_resolution, - std::vector ()); + return AST::PathInExpression (std::move (segments), {}, locus, + has_opening_scope_resolution); } /* Parses a single path in expression path segment (including generic @@ -6476,8 +6475,7 @@ Parser::parse_qualified_path_in_expression ( // FIXME: outer attr parsing return AST::QualifiedPathInExpression (std::move (qual_path_type), - std::move (segments), locus, - std::vector ()); + std::move (segments), {}, locus); } // Parses the type syntactical construction at the start of a qualified path. @@ -7377,8 +7375,7 @@ Parser::parse_literal_expr ( // create literal based on stuff in switch return std::unique_ptr ( new AST::LiteralExpr (std::move (literal_value), std::move (type), - t->get_type_hint (), t->get_locus (), - std::move (outer_attrs))); + t->get_type_hint (), std::move (outer_attrs), t->get_locus ())); } // Parses a return expression (including any expression to return). @@ -7406,8 +7403,8 @@ Parser::parse_return_expr ( // FIXME: ensure this doesn't ruin the middle of any expressions or anything return std::unique_ptr ( - new AST::ReturnExpr (locus, std::move (returned_expr), - std::move (outer_attrs))); + new AST::ReturnExpr (std::move (returned_expr), + std::move (outer_attrs), locus)); } /* Parses a break expression (including any label to break to AND any return @@ -7442,8 +7439,8 @@ Parser::parse_break_expr ( std::unique_ptr return_expr = parse_expr (); return std::unique_ptr ( - new AST::BreakExpr (locus, std::move (label), std::move (return_expr), - std::move (outer_attrs))); + new AST::BreakExpr (std::move (label), std::move (return_expr), + std::move (outer_attrs), locus)); } // Parses a continue expression (including any label to continue from). @@ -7474,7 +7471,7 @@ Parser::parse_continue_expr ( } return std::unique_ptr ( - new AST::ContinueExpr (locus, std::move (label), std::move (outer_attrs))); + new AST::ContinueExpr (std::move (label), std::move (outer_attrs), locus)); } // Parses a loop label used in loop expressions. @@ -11488,7 +11485,7 @@ Parser::parse_path_based_stmt_or_expr ( // lexer.skip_token(); // HACK: add outer attrs to path - path.replace_outer_attrs (std::move (outer_attrs)); + path.set_outer_attrs (std::move (outer_attrs)); expr = std::unique_ptr ( new AST::PathInExpression (std::move (path))); } @@ -11539,7 +11536,7 @@ Parser::parse_path_based_stmt_or_expr ( // lexer.skip_token(); // HACK: replace outer attributes in path - path.replace_outer_attrs (std::move (outer_attrs)); + path.set_outer_attrs (std::move (outer_attrs)); std::unique_ptr expr ( new AST::PathInExpression (std::move (path))); @@ -12047,11 +12044,11 @@ Parser::null_denotation ( /* HACK: may have to become permanent, but this is my * current identifier expression */ return std::unique_ptr ( - new AST::IdentifierExpr (tok->get_str (), + new AST::IdentifierExpr (tok->get_str (), {}, tok->get_locus ())); } // HACK: add outer attrs to path - path.replace_outer_attrs (std::move (outer_attrs)); + path.set_outer_attrs (std::move (outer_attrs)); return std::unique_ptr ( new AST::PathInExpression (std::move (path))); } @@ -12069,11 +12066,11 @@ Parser::null_denotation ( /* HACK: may have to become permanent, but this is my * current identifier expression */ return std::unique_ptr ( - new AST::IdentifierExpr (tok->get_str (), + new AST::IdentifierExpr (tok->get_str (), {}, tok->get_locus ())); } // HACK: add outer attrs to path - path.replace_outer_attrs (std::move (outer_attrs)); + path.set_outer_attrs (std::move (outer_attrs)); return std::unique_ptr ( new AST::PathInExpression (std::move (path))); } @@ -12087,10 +12084,10 @@ Parser::null_denotation ( /* HACK: may have to become permanent, but this is my current * identifier expression */ return std::unique_ptr ( - new AST::IdentifierExpr (tok->get_str (), tok->get_locus ())); + new AST::IdentifierExpr (tok->get_str (), {}, tok->get_locus ())); } // HACK: add outer attrs to path - path.replace_outer_attrs (std::move (outer_attrs)); + path.set_outer_attrs (std::move (outer_attrs)); return std::unique_ptr ( new AST::PathInExpression (std::move (path))); } @@ -12105,33 +12102,34 @@ Parser::null_denotation ( // HACK: add outer attrs to path AST::QualifiedPathInExpression path = parse_qualified_path_in_expression (true); - path.replace_outer_attrs (std::move (outer_attrs)); + path.set_outer_attrs (std::move (outer_attrs)); return std::unique_ptr ( new AST::QualifiedPathInExpression (std::move (path))); } + // FIXME: for literal exprs, should outer attrs be passed in or just ignored? case INT_LITERAL: // we should check the range, but ignore for now // encode as int? return std::unique_ptr ( new AST::LiteralExpr (tok->get_str (), AST::Literal::INT, - tok->get_type_hint (), tok->get_locus ())); + tok->get_type_hint (), {}, tok->get_locus ())); case FLOAT_LITERAL: // encode as float? return std::unique_ptr ( new AST::LiteralExpr (tok->get_str (), AST::Literal::FLOAT, - tok->get_type_hint (), tok->get_locus ())); + tok->get_type_hint (), {}, tok->get_locus ())); case STRING_LITERAL: return std::unique_ptr ( new AST::LiteralExpr (tok->get_str (), AST::Literal::STRING, - tok->get_type_hint (), tok->get_locus ())); + tok->get_type_hint (), {}, tok->get_locus ())); case TRUE_LITERAL: return std::unique_ptr ( new AST::LiteralExpr ("true", AST::Literal::BOOL, tok->get_type_hint (), - tok->get_locus ())); + {}, tok->get_locus ())); case FALSE_LITERAL: return std::unique_ptr ( new AST::LiteralExpr ("false", AST::Literal::BOOL, - tok->get_type_hint (), tok->get_locus ())); + tok->get_type_hint (), {}, tok->get_locus ())); case LEFT_PAREN: { // have to parse whole expression if inside brackets /* recursively invoke parse_expression with lowest priority possible as * it it were a top-level expression. */ @@ -12170,7 +12168,7 @@ Parser::null_denotation ( ParseRestrictions entered_from_unary; entered_from_unary.entered_from_unary = true; std::unique_ptr expr - = parse_expr (LBP_UNARY_MINUS, std::vector (), + = parse_expr (LBP_UNARY_MINUS, {}, entered_from_unary); if (expr == nullptr) @@ -12195,7 +12193,7 @@ Parser::null_denotation ( ParseRestrictions entered_from_unary; entered_from_unary.entered_from_unary = true; std::unique_ptr expr - = parse_expr (LBP_UNARY_EXCLAM, std::vector (), + = parse_expr (LBP_UNARY_EXCLAM, {}, entered_from_unary); if (expr == nullptr) @@ -12222,7 +12220,7 @@ Parser::null_denotation ( entered_from_unary.entered_from_unary = true; entered_from_unary.can_be_struct_expr = false; std::unique_ptr expr - = parse_expr (LBP_UNARY_ASTERISK, std::vector (), + = parse_expr (LBP_UNARY_ASTERISK, {}, entered_from_unary); // FIXME: allow outer attributes on expression return std::unique_ptr ( @@ -12245,13 +12243,13 @@ Parser::null_denotation ( { lexer.skip_token (); expr - = parse_expr (LBP_UNARY_AMP_MUT, std::vector (), + = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary); is_mut_borrow = true; } else { - expr = parse_expr (LBP_UNARY_AMP, std::vector (), + expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary); } @@ -12272,13 +12270,13 @@ Parser::null_denotation ( { lexer.skip_token (); expr - = parse_expr (LBP_UNARY_AMP_MUT, std::vector (), + = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary); is_mut_borrow = true; } else { - expr = parse_expr (LBP_UNARY_AMP, std::vector (), + expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary); } @@ -12317,7 +12315,7 @@ Parser::null_denotation ( if (tok->get_id () == SELF && path.is_single_segment ()) { // HACK: add outer attrs to path - path.replace_outer_attrs (std::move (outer_attrs)); + path.set_outer_attrs (std::move (outer_attrs)); return std::unique_ptr ( new AST::PathInExpression (std::move (path))); } @@ -12347,7 +12345,7 @@ Parser::null_denotation ( { // assume path is returned // HACK: add outer attributes to path - path.replace_outer_attrs (std::move (outer_attrs)); + path.set_outer_attrs (std::move (outer_attrs)); return std::unique_ptr ( new AST::PathInExpression (std::move (path))); } @@ -12360,7 +12358,7 @@ Parser::null_denotation ( { // assume path is returned // HACK: add outer attributes to path - path.replace_outer_attrs (std::move (outer_attrs)); + path.set_outer_attrs (std::move (outer_attrs)); return std::unique_ptr ( new AST::PathInExpression (std::move (path))); } @@ -12369,7 +12367,7 @@ Parser::null_denotation ( default: // assume path is returned // HACK: add outer attributes to path - path.replace_outer_attrs (std::move (outer_attrs)); + path.set_outer_attrs (std::move (outer_attrs)); return std::unique_ptr ( new AST::PathInExpression (std::move (path))); } @@ -14115,9 +14113,8 @@ Parser::parse_struct_expr_tuple_partial ( exprs.push_back (std::move (expr)); if (lexer.peek_token ()->get_id () != COMMA) - { break; - } + lexer.skip_token (); t = lexer.peek_token (); @@ -14243,7 +14240,7 @@ Parser::parse_path_in_expression_pratt (const_TokenPtr tok) "current token (just about to return path to null denotation): '%s'\n", lexer.peek_token ()->get_token_description ()); - return AST::PathInExpression (std::move (segments), tok->get_locus (), false); + return AST::PathInExpression (std::move (segments), {}, tok->get_locus (), false); } // Parses a closure expression with pratt parsing (from null denotation). -- cgit v1.1 From a48d424e4c55824f56d922d895c2a25f10347683 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Wed, 3 Feb 2021 12:06:29 +0800 Subject: Fixed compile error --- gcc/rust/resolve/rust-ast-resolve.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gcc') diff --git a/gcc/rust/resolve/rust-ast-resolve.cc b/gcc/rust/resolve/rust-ast-resolve.cc index c6d7148..6731f13 100644 --- a/gcc/rust/resolve/rust-ast-resolve.cc +++ b/gcc/rust/resolve/rust-ast-resolve.cc @@ -331,7 +331,7 @@ ResolveStructExprField::visit (AST::StructExprFieldIndexValue &field) void ResolveStructExprField::visit (AST::StructExprFieldIdentifier &field) { - AST::IdentifierExpr expr (field.get_field_name (), field.get_locus ()); + AST::IdentifierExpr expr (field.get_field_name (), {}, field.get_locus ()); expr.set_node_id (field.get_node_id ()); ResolveExpr::go (&expr, field.get_node_id ()); -- cgit v1.1 From 14aacf472dd089804302c5a2f41c1fc37ba5e824 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Wed, 3 Feb 2021 13:48:20 +0800 Subject: Removed testcase that apparently didn't compile on CI --- gcc/testsuite/rust.test/compilable/arrays3.rs | 66 --------------------------- 1 file changed, 66 deletions(-) delete mode 100644 gcc/testsuite/rust.test/compilable/arrays3.rs (limited to 'gcc') diff --git a/gcc/testsuite/rust.test/compilable/arrays3.rs b/gcc/testsuite/rust.test/compilable/arrays3.rs deleted file mode 100644 index 974e346..0000000 --- a/gcc/testsuite/rust.test/compilable/arrays3.rs +++ /dev/null @@ -1,66 +0,0 @@ -fn main() { - // const, inferred array - let const_inferred_array = [0, 1, 2, 3]; - // const, inferred, copied array - let const_inferred_copied_array = [0; 4]; - - // const index of const, inferred array - let const_inferred_index = const_inferred_array[1]; - // variable index of const, inferred array - let some_var = 3; - let const_inferred_index_var = const_inferred_array[some_var]; - - // const index of const, inferred, copied array - let const_inferred_copied_index = const_inferred_copied_array[1]; - // variable index of const, inferred array - let const_inferred_copied_index_var = const_inferred_copied_array[some_var]; - - // mut, inferred array - let mut mut_inferred_array = [0, 1, 2, 3]; - // mut, inferred, copied array - let mut mut_inferred_copied_array = [0; 6]; - - // const index of mut, inferred array - let mut_inferred_index = mut_inferred_array[1]; - // variable index of mut, inferred array - let mut_inferred_index_var = mut_inferred_array[some_var]; - - // const index of mut, inferred, copied array - let mut_inferred_copied_index = mut_inferred_copied_array[1]; - // variable index of mut, inferred array - let mut_inferred_copied_index_var = mut_inferred_copied_array[some_var]; - - // const, typed array - let const_typed_array: [i32; 5] = [0, 1, 2, 3, 4]; - // const, typed, copied array - let const_typed_copied_array: [i32; 4] = [2; 4]; - - // const index of const, typed array - let const_typed_index = const_typed_array[1]; - // variable index of const, typed array - let const_typed_index_var = const_typed_array[some_var]; - - // const index of const, typed, copied array - let const_typed_copied_index = const_typed_copied_array[1]; - // variable index of const, typed array - let const_typed_copied_index_var = const_typed_copied_array[some_var]; - - // mut, typed array - let mut mut_typed_array: [i32; 4] = [0, 1, 2, 3]; - // mut, typed, copied array - let mut mut_typed_copied_array: [i32; 4] = [0; 4]; - - // const index of mut, typed array - let mut_typed_index = mut_typed_array[1]; - // variable index of mut, typed array - let mut_typed_index_var = mut_typed_array[some_var]; - - // const index of mut, typed, copied array - let mut_typed_copied_index = mut_typed_copied_array[1]; - // variable index of mut, typed array - let mut_typed_copied_index_var = mut_typed_copied_array[some_var]; - - - // index + 1 expression - let some_thing = mut_inferred_copied_array[some_var + 1]; -} \ No newline at end of file -- cgit v1.1 From 877e7ac6c72608950fbe2ffde04142bbfb01b29d Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Wed, 3 Feb 2021 18:23:00 +0800 Subject: Readded outer attributes to if and if let exprs --- gcc/rust/ast/rust-expr.h | 52 +++++++++++++++----------- gcc/rust/expand/rust-macro-expand.cc | 71 +++++++++++++++++++++++++++++------- gcc/rust/parse/rust-parse-impl.h | 28 ++++++-------- 3 files changed, 101 insertions(+), 50 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index b9a6c88..74a6473 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -4075,6 +4075,7 @@ class IfLetExpr; // Base if expression with no "else" or "if let" AST node class IfExpr : public ExprWithBlock { + std::vector outer_attrs; std::unique_ptr condition; std::unique_ptr if_block; Location locus; @@ -4083,14 +4084,14 @@ public: std::string as_string () const override; IfExpr (std::unique_ptr condition, std::unique_ptr if_block, - Location locus) - : condition (std::move (condition)), if_block (std::move (if_block)), + std::vector outer_attrs, Location locus) + : outer_attrs (std::move (outer_attrs)), condition (std::move (condition)), if_block (std::move (if_block)), locus (locus) {} // outer attributes are never allowed on IfExprs // Copy constructor with clone - IfExpr (IfExpr const &other) : ExprWithBlock (other), locus (other.locus) + IfExpr (IfExpr const &other) : ExprWithBlock (other), outer_attrs (other.outer_attrs), locus (other.locus) { // guard to prevent null dereference (only required if error state) if (other.condition != nullptr) @@ -4103,6 +4104,7 @@ public: IfExpr &operator= (IfExpr const &other) { ExprWithBlock::operator= (other); + outer_attrs = other.outer_attrs; locus = other.locus; // guard to prevent null dereference (only required if error state) @@ -4165,8 +4167,11 @@ public: return if_block == nullptr && condition == nullptr; } - // this should never be called - void set_outer_attrs (std::vector new_attrs) override { rust_assert (false); } + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + + // TODO: this mutable getter seems really dodgy. Think up better way. + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } protected: // Base clone function but still concrete as concrete base class @@ -4190,8 +4195,8 @@ public: IfExprConseqElse (std::unique_ptr condition, std::unique_ptr if_block, - std::unique_ptr else_block, Location locus) - : IfExpr (std::move (condition), std::move (if_block), locus), + std::unique_ptr else_block, std::vector outer_attrs, Location locus) + : IfExpr (std::move (condition), std::move (if_block), std::move (outer_attrs), locus), else_block (std::move (else_block)) {} // again, outer attributes not allowed @@ -4246,8 +4251,8 @@ public: IfExprConseqIf (std::unique_ptr condition, std::unique_ptr if_block, - std::unique_ptr conseq_if_expr, Location locus) - : IfExpr (std::move (condition), std::move (if_block), locus), + std::unique_ptr conseq_if_expr, std::vector outer_attrs, Location locus) + : IfExpr (std::move (condition), std::move (if_block), std::move (outer_attrs), locus), conseq_if_expr (std::move (conseq_if_expr)) {} // outer attributes not allowed @@ -4298,6 +4303,7 @@ protected: // Basic "if let" expression AST node with no else class IfLetExpr : public ExprWithBlock { + std::vector outer_attrs; std::vector > match_arm_patterns; // inlined std::unique_ptr value; std::unique_ptr if_block; @@ -4307,7 +4313,7 @@ public: std::string as_string () const override; IfLetExpr (std::vector > match_arm_patterns, - std::unique_ptr value, std::unique_ptr if_block, + std::unique_ptr value, std::unique_ptr if_block, std::vector outer_attrs, Location locus) : match_arm_patterns (std::move (match_arm_patterns)), value (std::move (value)), if_block (std::move (if_block)), locus (locus) @@ -4316,7 +4322,7 @@ public: // copy constructor with clone IfLetExpr (IfLetExpr const &other) - : ExprWithBlock (other), locus (other.locus) + : ExprWithBlock (other), outer_attrs (other.outer_attrs), locus (other.locus) { // guard to prevent null dereference (only required if error state) if (other.value != nullptr) @@ -4333,6 +4339,7 @@ public: IfLetExpr &operator= (IfLetExpr const &other) { ExprWithBlock::operator= (other); + outer_attrs = other.outer_attrs; locus = other.locus; // guard to prevent null dereference (only required if error state) @@ -4402,8 +4409,11 @@ public: return match_arm_patterns; } - // this should never be called - void set_outer_attrs (std::vector new_attrs) override { rust_assert (false); } + void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + + // TODO: this mutable getter seems really dodgy. Think up better way. + const std::vector &get_outer_attrs () const { return outer_attrs; } + std::vector &get_outer_attrs () { return outer_attrs; } protected: /* Use covariance to implement clone function as returning this object rather @@ -4430,9 +4440,9 @@ public: IfExprConseqIfLet (std::unique_ptr condition, std::unique_ptr if_block, - std::unique_ptr conseq_if_let_expr, + std::unique_ptr conseq_if_let_expr, std::vector outer_attrs, Location locus) - : IfExpr (std::move (condition), std::move (if_block), locus), + : IfExpr (std::move (condition), std::move (if_block), std::move (outer_attrs), locus), if_let_expr (std::move (conseq_if_let_expr)) {} // outer attributes not allowed @@ -4487,9 +4497,9 @@ public: IfLetExprConseqElse ( std::vector > match_arm_patterns, std::unique_ptr value, std::unique_ptr if_block, - std::unique_ptr else_block, Location locus) + std::unique_ptr else_block, std::vector outer_attrs, Location locus) : IfLetExpr (std::move (match_arm_patterns), std::move (value), - std::move (if_block), locus), + std::move (if_block), std::move (outer_attrs), locus), else_block (std::move (else_block)) {} // outer attributes not allowed @@ -4546,9 +4556,9 @@ public: IfLetExprConseqIf (std::vector > match_arm_patterns, std::unique_ptr value, std::unique_ptr if_block, - std::unique_ptr if_expr, Location locus) + std::unique_ptr if_expr, std::vector outer_attrs, Location locus) : IfLetExpr (std::move (match_arm_patterns), std::move (value), - std::move (if_block), locus), + std::move (if_block), std::move (outer_attrs), locus), if_expr (std::move (if_expr)) {} // again, outer attributes not allowed @@ -4604,9 +4614,9 @@ public: IfLetExprConseqIfLet ( std::vector > match_arm_patterns, std::unique_ptr value, std::unique_ptr if_block, - std::unique_ptr if_let_expr, Location locus) + std::unique_ptr if_let_expr, std::vector outer_attrs, Location locus) : IfLetExpr (std::move (match_arm_patterns), std::move (value), - std::move (if_block), locus), + std::move (if_block), std::move (outer_attrs), locus), if_let_expr (std::move (if_let_expr)) {} // outer attributes not allowed diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 2f4c010..cfd587b 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -1529,12 +1529,15 @@ public: } void visit (AST::IfExpr &expr) override { - // TODO: for if expressions, are attributes allowed if it is part of an expression statement? - // if so, probably have to add a "does_expr_allow_attrs()" method to Expr and then don't move attrs. - // otherwise, that may be useful anyway for better error messages. + // rust playground test shows that IfExpr does support outer attrs, at least when used as statement - // NOTE: IfExpr literally doesn't support outer attrs, so no strip code - // TODO: is this still true? can't find info on page + // initial strip test based on outer attrs + expander.expand_cfg_attrs (expr.get_outer_attrs ()); + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) + { + expr.mark_for_strip (); + return; + } // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); @@ -1554,7 +1557,13 @@ public: } void visit (AST::IfExprConseqElse &expr) override { - // NOTE: IfExpr literally doesn't support outer attrs, so no strip code + // initial strip test based on outer attrs + expander.expand_cfg_attrs (expr.get_outer_attrs ()); + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) + { + expr.mark_for_strip (); + return; + } // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); @@ -1582,7 +1591,13 @@ public: } void visit (AST::IfExprConseqIf &expr) override { - // NOTE: IfExpr literally doesn't support outer attrs, so no strip code + // initial strip test based on outer attrs + expander.expand_cfg_attrs (expr.get_outer_attrs ()); + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) + { + expr.mark_for_strip (); + return; + } // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); @@ -1610,7 +1625,13 @@ public: } void visit (AST::IfExprConseqIfLet &expr) override { - // NOTE: IfExpr literally doesn't support outer attrs, so no strip code + // initial strip test based on outer attrs + expander.expand_cfg_attrs (expr.get_outer_attrs ()); + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) + { + expr.mark_for_strip (); + return; + } // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); @@ -1639,8 +1660,14 @@ public: } void visit (AST::IfLetExpr &expr) override { - // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code - + // initial strip test based on outer attrs + expander.expand_cfg_attrs (expr.get_outer_attrs ()); + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) + { + expr.mark_for_strip (); + return; + } + for (auto &pattern : expr.get_patterns ()) { pattern->accept_vis (*this); @@ -1667,7 +1694,13 @@ public: } void visit (AST::IfLetExprConseqElse &expr) override { - // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code + // initial strip test based on outer attrs + expander.expand_cfg_attrs (expr.get_outer_attrs ()); + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) + { + expr.mark_for_strip (); + return; + } for (auto &pattern : expr.get_patterns ()) { @@ -1703,7 +1736,13 @@ public: } void visit (AST::IfLetExprConseqIf &expr) override { - // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code + // initial strip test based on outer attrs + expander.expand_cfg_attrs (expr.get_outer_attrs ()); + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) + { + expr.mark_for_strip (); + return; + } for (auto &pattern : expr.get_patterns ()) { @@ -1739,7 +1778,13 @@ public: } void visit (AST::IfLetExprConseqIfLet &expr) override { - // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code + // initial strip test based on outer attrs + expander.expand_cfg_attrs (expr.get_outer_attrs ()); + if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) + { + expr.mark_for_strip (); + return; + } for (auto &pattern : expr.get_patterns ()) { diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index ffe4e91..3bea914 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -7503,10 +7503,8 @@ Parser::parse_loop_label () template std::unique_ptr Parser::parse_if_expr ( - std::vector outer_attrs ATTRIBUTE_UNUSED) + std::vector outer_attrs) { - // TODO: make having outer attributes an error? - Location locus = lexer.peek_token ()->get_locus (); skip_token (IF); @@ -7525,7 +7523,7 @@ Parser::parse_if_expr ( ParseRestrictions no_struct_expr; no_struct_expr.can_be_struct_expr = false; std::unique_ptr condition - = parse_expr (std::vector (), no_struct_expr); + = parse_expr ({}, no_struct_expr); if (condition == nullptr) { rust_error_at (lexer.peek_token ()->get_locus (), @@ -7550,7 +7548,7 @@ Parser::parse_if_expr ( { // single selection - end of if expression return std::unique_ptr ( - new AST::IfExpr (std::move (condition), std::move (if_body), locus)); + new AST::IfExpr (std::move (condition), std::move (if_body), std::move (outer_attrs), locus)); } else { @@ -7579,7 +7577,7 @@ Parser::parse_if_expr ( return std::unique_ptr ( new AST::IfExprConseqElse (std::move (condition), std::move (if_body), - std::move (else_body), locus)); + std::move (else_body), std::move (outer_attrs), locus)); } case IF: { // multiple selection - else if or else if let @@ -7601,7 +7599,7 @@ Parser::parse_if_expr ( return std::unique_ptr ( new AST::IfExprConseqIfLet (std::move (condition), std::move (if_body), - std::move (if_let_expr), locus)); + std::move (if_let_expr), std::move (outer_attrs), locus)); } else { @@ -7619,7 +7617,7 @@ Parser::parse_if_expr ( return std::unique_ptr ( new AST::IfExprConseqIf (std::move (condition), std::move (if_body), - std::move (if_expr), locus)); + std::move (if_expr), std::move (outer_attrs), locus)); } } default: @@ -7639,10 +7637,8 @@ Parser::parse_if_expr ( template std::unique_ptr Parser::parse_if_let_expr ( - std::vector outer_attrs ATTRIBUTE_UNUSED) + std::vector outer_attrs) { - // TODO: make having outer attributes an error? - Location locus = lexer.peek_token ()->get_locus (); skip_token (IF); @@ -7679,7 +7675,7 @@ Parser::parse_if_let_expr ( ParseRestrictions no_struct_expr; no_struct_expr.can_be_struct_expr = false; std::unique_ptr scrutinee_expr - = parse_expr (std::vector (), no_struct_expr); + = parse_expr ({}, no_struct_expr); if (scrutinee_expr == nullptr) { rust_error_at ( @@ -7709,7 +7705,7 @@ Parser::parse_if_let_expr ( return std::unique_ptr ( new AST::IfLetExpr (std::move (match_arm_patterns), std::move (scrutinee_expr), std::move (if_let_body), - locus)); + std::move (outer_attrs), locus)); } else { @@ -7739,7 +7735,7 @@ Parser::parse_if_let_expr ( new AST::IfLetExprConseqElse (std::move (match_arm_patterns), std::move (scrutinee_expr), std::move (if_let_body), - std::move (else_body), locus)); + std::move (else_body), std::move (outer_attrs), locus)); } case IF: { // multiple selection - else if or else if let @@ -7761,7 +7757,7 @@ Parser::parse_if_let_expr ( return std::unique_ptr ( new AST::IfLetExprConseqIfLet ( std::move (match_arm_patterns), std::move (scrutinee_expr), - std::move (if_let_body), std::move (if_let_expr), locus)); + std::move (if_let_body), std::move (if_let_expr), std::move (outer_attrs), locus)); } else { @@ -7780,7 +7776,7 @@ Parser::parse_if_let_expr ( new AST::IfLetExprConseqIf (std::move (match_arm_patterns), std::move (scrutinee_expr), std::move (if_let_body), - std::move (if_expr), locus)); + std::move (if_expr), std::move (outer_attrs), locus)); } } default: -- cgit v1.1 From 5c7dacd845ffb4b9a101308bc14241e7e9b939f8 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Fri, 5 Feb 2021 21:49:35 +0800 Subject: Added some debug for cfg stripping and improved dump messages --- gcc/rust/expand/rust-macro-expand.cc | 7 ++++++- gcc/rust/rust-session-manager.cc | 11 +++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index cfd587b..29fc58b 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -1504,7 +1504,7 @@ public: return; } - // TODO: strip sub-patterns of pattern + // strip sub-patterns of pattern auto &pattern = expr.get_pattern (); pattern->accept_vis (*this); if (pattern->is_marked_for_strip ()) @@ -3283,7 +3283,12 @@ MacroExpander::fails_cfg_with_expand (std::vector &attrs) const attr.parse_attr_to_meta_item (); if (!attr.check_cfg_predicate (session)) + { + // DEBUG + fprintf (stderr, "cfg predicate failed for attribute: \033[0;31m'%s'\033[0m\n", attr.as_string ()); + return true; + } } } return false; diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index 281049d..dcdbd49 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -363,7 +363,8 @@ Session::enable_dump (std::string arg) rust_error_at ( Location (), "dumping all is not supported as of now. choose %, %, " - "or %"); + "%, %, %, %," + " %, or %"); return false; } else if (arg == "lex") @@ -406,15 +407,17 @@ Session::enable_dump (std::string arg) else if (arg == "") { rust_error_at (Location (), "dump option was not given a name. choose " - "%, %, or %"); + "%, %, %, %, " + "%, %, %, or %"); return false; } else { rust_error_at ( Location (), - "dump option %qs was unrecognised. choose %, %, or " - "%", + "dump option %qs was unrecognised. choose %, %, " + "%, %, %, %," + " %, or %", arg.c_str ()); return false; } -- cgit v1.1 From d0e10517881b190de1724b29257a3e408b203d43 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Sat, 6 Feb 2021 15:51:57 +0800 Subject: Fixed bad cfg predicate string interpolation --- gcc/rust/expand/rust-macro-expand.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gcc') diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 29fc58b..cd37673 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -3285,7 +3285,7 @@ MacroExpander::fails_cfg_with_expand (std::vector &attrs) const if (!attr.check_cfg_predicate (session)) { // DEBUG - fprintf (stderr, "cfg predicate failed for attribute: \033[0;31m'%s'\033[0m\n", attr.as_string ()); + fprintf (stderr, "cfg predicate failed for attribute: \033[0;31m'%s'\033[0m\n", attr.as_string ().c_str ()); return true; } -- cgit v1.1 From 3571e997473c87dcdb2ca590951acbd698382a33 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Sat, 6 Feb 2021 16:25:10 +0800 Subject: Added more debug for cfg predicate checking --- gcc/rust/expand/rust-macro-expand.cc | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'gcc') diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index cd37673..e9f2e2a 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -3282,12 +3282,19 @@ MacroExpander::fails_cfg_with_expand (std::vector &attrs) const if (!attr.is_parsed_to_meta_item ()) attr.parse_attr_to_meta_item (); + // DEBUG + if (!attr.is_parsed_to_meta_item ()) + fprintf (stderr, "failed to parse attr to meta item, right before cfg predicate check\n"); + if (!attr.check_cfg_predicate (session)) { // DEBUG fprintf (stderr, "cfg predicate failed for attribute: \033[0;31m'%s'\033[0m\n", attr.as_string ().c_str ()); return true; + } else { + // DEBUG + fprintf (stderr, "cfg predicate succeeded for attribute: \033[0;31m'%s'\033[0m\n", attr.as_string ().c_str ()); } } } -- cgit v1.1 From feaa2074d8cece0510c49c45a2456a3f56b02381 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Sat, 6 Feb 2021 18:10:57 +0800 Subject: Added even more debug for cfg predicate checking --- gcc/rust/ast/rust-ast-full-test.cc | 6 +++++- gcc/rust/ast/rust-ast.h | 1 + gcc/rust/expand/rust-macro-expand.cc | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index 53007e7..aa17d60 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -5359,8 +5359,12 @@ Attribute::check_cfg_predicate (const Session &session) const /* assume that cfg predicate actually can exist, i.e. attribute has cfg or * cfg_attr path */ if (!has_attr_input () - || (path.as_string () != "cfg" && path.as_string () != "cfg_attr")) + || (path.as_string () != "cfg" && path.as_string () != "cfg_attr")) { + // DEBUG message + fprintf (stderr, "tried to check cfg predicate on attr that either has no input or invalid path. attr: '%s'\n", as_string ().c_str ()); + return false; + } // assume that it has already been parsed if (!is_parsed_to_meta_item ()) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index faecfba..0922045 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -733,6 +733,7 @@ public: bool check_cfg_predicate (const Session &) const override { // this should never be called - should be converted first + rust_assert (false); return false; } diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index e9f2e2a..df99d75 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -3285,6 +3285,8 @@ MacroExpander::fails_cfg_with_expand (std::vector &attrs) const // DEBUG if (!attr.is_parsed_to_meta_item ()) fprintf (stderr, "failed to parse attr to meta item, right before cfg predicate check\n"); + else + fprintf(stderr, "attr has been successfully parsed to meta item, right before cfg predicate check\n"); if (!attr.check_cfg_predicate (session)) { -- cgit v1.1 From 83ca72858469584de7d235054209dd214818ca1a Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Sat, 6 Feb 2021 18:30:14 +0800 Subject: Added more debug for cfg expansion --- gcc/rust/ast/rust-ast-full-test.cc | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index aa17d60..3e5fe07 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -5030,6 +5030,9 @@ AttrInputMetaItemContainer::check_cfg_predicate (const Session &session) const if (items.empty ()) return false; + // DEBUG + fprintf (stderr, "asked to check cfg of attrinputmetaitemcontainer - delegating to first item. container: '%s'\n", as_string ().c_str ()); + return items[0]->check_cfg_predicate (session); /*for (const auto &inner_item : items) @@ -5229,6 +5232,9 @@ MetaItemPath::check_cfg_predicate (const Session &session) const bool MetaNameValueStr::check_cfg_predicate (const Session &session) const { + // DEBUG + fprintf (stderr, "checked key-value pair for cfg: '%s', '%s' - is%s in target data\n", ident.c_str (), str.c_str (), session.options.target_data.has_key_value_pair (ident, str) ? "" : " not"); + return session.options.target_data.has_key_value_pair (ident, str); } -- cgit v1.1 From 27eef5b7a065e8ea05ac575c4b364bb5dbc44e46 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Sat, 6 Feb 2021 20:22:33 +0800 Subject: Rewrote MetaNameValueStr to store raw string rather than quoted string --- gcc/rust/ast/rust-ast-full-test.cc | 55 +++++++++++--------------------------- gcc/rust/ast/rust-macro.h | 3 ++- 2 files changed, 17 insertions(+), 41 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index 3e5fe07..47d6c89 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -150,6 +150,15 @@ std::string append_attributes (std::vector attrs, AttrMode mode) { return str; } +// Removes the beginning and end quotes of a quoted string. +std::string +unquote_string (std::string input) +{ + rust_assert (input.front () == '"'); + rust_assert (input.back () == '"'); + return input.substr (1, input.length () - 2); +} + std::string Crate::as_string () const { @@ -158,13 +167,10 @@ Crate::as_string () const std::string str ("Crate: "); // add utf8bom and shebang if (has_utf8bom) - { str += "\n has utf8bom"; - } + if (has_shebang) - { str += "\n has shebang"; - } // inner attributes str += "\n inner attributes: "; @@ -177,9 +183,7 @@ Crate::as_string () const /* note that this does not print them with "inner attribute" syntax - * just the body */ for (const auto &attr : inner_attrs) - { str += "\n " + attr.as_string (); - } } // items @@ -311,9 +315,7 @@ SimplePath::as_string () const { std::string path; if (has_opening_scope_resolution) - { path = "::"; - } // crappy hack because doing proper for loop would be more code bool first_time = true; @@ -377,9 +379,7 @@ VisItem::as_string () const } if (has_visibility ()) - { - str = visibility.as_string () + " "; - } + str += visibility.as_string () + " "; return str; } @@ -426,9 +426,7 @@ ModuleBodied::as_string () const /* note that this does not print them with "inner attribute" syntax - * just the body */ for (const auto &attr : inner_attrs) - { str += "\n " + attr.as_string (); - } } // items @@ -474,9 +472,7 @@ StaticItem::as_string () const str += indent_spaces (stay) + "static"; if (has_mut) - { str += " mut"; - } str += " " + name; @@ -509,9 +505,7 @@ ExternCrate::as_string () const str += "extern crate " + referenced_crate; if (has_as_clause ()) - { str += " as " + as_clause_name; - } return str; } @@ -556,20 +550,14 @@ TupleStruct::as_string () const else { for (const auto &field : fields) - { str += "\n " + field.as_string (); - } } str += "\n Where clause: "; if (has_where_clause ()) - { str += where_clause.as_string (); - } else - { str += "none"; - } return str; } @@ -637,13 +625,9 @@ InherentImpl::as_string () const str += "\n Where clause: "; if (has_where_clause ()) - { str += where_clause.as_string (); - } else - { str += "none"; - } // inner attributes str += "\n inner attributes: "; @@ -670,9 +654,7 @@ InherentImpl::as_string () const else { for (const auto &item : impl_items) - { str += "\n " + item->as_string (); - } } return str; @@ -775,13 +757,9 @@ StructStruct::as_string () const str += "\n Where clause: "; if (has_where_clause ()) - { str += where_clause.as_string (); - } else - { str += "none"; - } // struct fields str += "\n Struct fields: "; @@ -796,9 +774,7 @@ StructStruct::as_string () const else { for (const auto &field : fields) - { str += "\n " + field.as_string (); - } } return str; @@ -950,13 +926,9 @@ Enum::as_string () const str += "\n Where clause: "; if (has_where_clause ()) - { str += where_clause.as_string (); - } else - { str += "none"; - } // items str += "\n Items: "; @@ -4677,8 +4649,11 @@ MacroParser::parse_meta_item_inner () skip_token (2); + // remove the quotes from the string value + std::string raw_value = unquote_string (std::move (value)); + return std::unique_ptr ( - new MetaNameValueStr (std::move (ident), std::move (value))); + new MetaNameValueStr (std::move (ident), std::move (raw_value))); } else { diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 79d513f..e23be14 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -537,6 +537,7 @@ protected: class MetaNameValueStr : public MetaItem { Identifier ident; + // NOTE: str stored without quotes std::string str; public: @@ -544,7 +545,7 @@ public: : ident (std::move (ident)), str (std::move (str)) {} - std::string as_string () const override { return ident + " = " + str; } + std::string as_string () const override { return ident + " = \"" + str + "\""; } void accept_vis (ASTVisitor &vis) override; -- cgit v1.1 From 8f97711dce5c48e46a9a64c5517fa6ae10db5e04 Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Sun, 7 Feb 2021 15:03:07 +0800 Subject: General cleanup of AST and macro expansion-related things --- gcc/rust/ast/rust-ast-full-test.cc | 874 ++++++++--------------------------- gcc/rust/ast/rust-ast.h | 24 +- gcc/rust/ast/rust-macro.h | 176 ++----- gcc/rust/expand/rust-macro-expand.cc | 105 +++-- gcc/rust/expand/rust-macro-expand.h | 5 +- gcc/rust/parse/rust-parse-impl.h | 121 ++--- gcc/rust/rust-session-manager.cc | 14 +- 7 files changed, 396 insertions(+), 923 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index 47d6c89..e1308b7 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -109,14 +109,17 @@ frag_spec_to_str (MacroFragSpec frag_spec) } } -enum AttrMode +enum AttrMode { OUTER, INNER }; -std::string get_mode_dump_desc (AttrMode mode) { - switch (mode) { +std::string +get_mode_dump_desc (AttrMode mode) +{ + switch (mode) + { case OUTER: return "outer attributes"; case INNER: @@ -124,35 +127,38 @@ std::string get_mode_dump_desc (AttrMode mode) { default: gcc_unreachable (); return ""; - } + } } // Adds lines below adding attributes -std::string append_attributes (std::vector attrs, AttrMode mode) { +std::string +append_attributes (std::vector attrs, AttrMode mode) +{ indent_spaces (enter); - std::string str = "\n" + indent_spaces (stay) + get_mode_dump_desc (mode) + ": "; - //str += "\n" + indent_spaces (stay) + "inner attributes: "; + std::string str + = "\n" + indent_spaces (stay) + get_mode_dump_desc (mode) + ": "; + // str += "\n" + indent_spaces (stay) + "inner attributes: "; if (attrs.empty ()) { str += "none"; } else { - /* note that this does not print them with outer or "inner attribute" + /* note that this does not print them with outer or "inner attribute" * syntax - just prints the body */ for (const auto &attr : attrs) str += "\n" + indent_spaces (stay) + attr.as_string (); } indent_spaces (out); - + return str; } -// Removes the beginning and end quotes of a quoted string. +// Removes the beginning and end quotes of a quoted string. std::string -unquote_string (std::string input) +unquote_string (std::string input) { rust_assert (input.front () == '"'); rust_assert (input.back () == '"'); @@ -167,24 +173,13 @@ Crate::as_string () const std::string str ("Crate: "); // add utf8bom and shebang if (has_utf8bom) - str += "\n has utf8bom"; + str += "\n has utf8bom"; if (has_shebang) - str += "\n has shebang"; + str += "\n has shebang"; // inner attributes - str += "\n inner attributes: "; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - str += "\n " + attr.as_string (); - } + str += append_attributes (inner_attrs, INNER); // items str += "\n items: "; @@ -315,7 +310,7 @@ SimplePath::as_string () const { std::string path; if (has_opening_scope_resolution) - path = "::"; + path = "::"; // crappy hack because doing proper for loop would be more code bool first_time = true; @@ -379,28 +374,11 @@ VisItem::as_string () const } if (has_visibility ()) - str += visibility.as_string () + " "; + str += visibility.as_string () + " "; return str; } -// Creates a string that reflects the outer attributes stored. -/*std::string -Item::as_string () const -{ - std::string str; - - if (!outer_attrs.empty ()) - { - for (const auto &attr : outer_attrs) - { - str += attr.as_string () + "\n"; - } - } - - return str; -}*/ - std::string Module::as_string () const { @@ -416,18 +394,7 @@ ModuleBodied::as_string () const std::string str = Module::as_string (); // inner attributes - str += "\n inner attributes: "; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - str += "\n " + attr.as_string (); - } + str += append_attributes (inner_attrs, INNER); // items str += "\n items: "; @@ -472,7 +439,7 @@ StaticItem::as_string () const str += indent_spaces (stay) + "static"; if (has_mut) - str += " mut"; + str += " mut"; str += " " + name; @@ -505,7 +472,7 @@ ExternCrate::as_string () const str += "extern crate " + referenced_crate; if (has_as_clause ()) - str += " as " + as_clause_name; + str += " as " + as_clause_name; return str; } @@ -550,14 +517,14 @@ TupleStruct::as_string () const else { for (const auto &field : fields) - str += "\n " + field.as_string (); + str += "\n " + field.as_string (); } str += "\n Where clause: "; if (has_where_clause ()) - str += where_clause.as_string (); + str += where_clause.as_string (); else - str += "none"; + str += "none"; return str; } @@ -625,25 +592,12 @@ InherentImpl::as_string () const str += "\n Where clause: "; if (has_where_clause ()) - str += where_clause.as_string (); + str += where_clause.as_string (); else - str += "none"; + str += "none"; // inner attributes - str += "\n inner attributes: "; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - { - str += "\n " + attr.as_string (); - } - } + str += append_attributes (inner_attrs, INNER); // inherent impl items str += "\n Inherent impl items: "; @@ -654,7 +608,7 @@ InherentImpl::as_string () const else { for (const auto &item : impl_items) - str += "\n " + item->as_string (); + str += "\n " + item->as_string (); } return str; @@ -757,9 +711,9 @@ StructStruct::as_string () const str += "\n Where clause: "; if (has_where_clause ()) - str += where_clause.as_string (); + str += where_clause.as_string (); else - str += "none"; + str += "none"; // struct fields str += "\n Struct fields: "; @@ -774,7 +728,7 @@ StructStruct::as_string () const else { for (const auto &field : fields) - str += "\n " + field.as_string (); + str += "\n " + field.as_string (); } return str; @@ -926,9 +880,9 @@ Enum::as_string () const str += "\n Where clause: "; if (has_where_clause ()) - str += where_clause.as_string (); + str += where_clause.as_string (); else - str += "none"; + str += "none"; // items str += "\n Items: "; @@ -963,7 +917,7 @@ Trait::as_string () const std::string str = VisItem::as_string (); if (has_unsafe) - str += "unsafe "; + str += "unsafe "; str += "trait " + name; @@ -1016,9 +970,9 @@ Trait::as_string () const str += "\n Where clause: "; if (!has_where_clause ()) - str += "none"; + str += "none"; else - str += where_clause.as_string (); + str += where_clause.as_string (); str += "\n Trait items: "; if (!has_trait_items ()) @@ -1079,9 +1033,9 @@ Union::as_string () const str += "\n Where clause: "; if (has_where_clause ()) - str += where_clause.as_string (); + str += where_clause.as_string (); else - str += "none"; + str += "none"; // struct fields str += "\n Struct fields (variants): "; @@ -1092,7 +1046,7 @@ Union::as_string () const else { for (const auto &field : variants) - str += "\n " + field.as_string (); + str += "\n " + field.as_string (); } return str; @@ -1213,34 +1167,12 @@ BlockExpr::as_string () const { std::string istr = indent_spaces (enter); std::string str = istr + "BlockExpr:\n" + istr; - + // get outer attributes - str += "{\n" + indent_spaces (stay) + "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n" + indent_spaces (stay) + attr.as_string (); - } + str += append_attributes (outer_attrs, OUTER); // inner attributes - str += "\n" + indent_spaces (stay) + "inner attributes: "; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - str += "\n" + indent_spaces (stay) + attr.as_string (); - } + str += append_attributes (inner_attrs, INNER); // statements str += "\n" + indent_spaces (stay) + "statements: "; @@ -1273,7 +1205,7 @@ BlockExpr::as_string () const else str += "\n" + expr->as_string (); - str += "\n" + indent_spaces (out) + "}"; + str += "\n" + indent_spaces (out); return str; } @@ -1316,18 +1248,7 @@ TraitImpl::as_string () const str += where_clause.as_string (); // inner attributes - str += "\n inner attributes: "; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - str += "\n " + attr.as_string (); - } + str += append_attributes (inner_attrs, INNER); str += "\n trait impl items: "; if (!has_impl_items ()) @@ -1383,50 +1304,11 @@ TypeAlias::as_string () const std::string MacroInvocationSemi::as_string () const { - std::string str; - - // get outer attrs - if (!outer_attrs.empty ()) - { - for (const auto &attr : outer_attrs) - str += attr.as_string () + "\n"; - } - - str += invoc_data.as_string (); - -#if 0 - str += "\n" + path.as_string () + "!"; - - std::string tok_trees; - if (token_trees.empty ()) - { - tok_trees = "none"; - } - else - { - auto i = token_trees.begin (); - auto e = token_trees.end (); + std::string str = "MacroInvocationSemi: "; - // DEBUG: null pointer check - if (i == e) - { - fprintf (stderr, - "something really terrible has gone wrong - null pointer " - "token tree in macro invocation semi."); - return "NULL_POINTER_MARK"; - } - - std::string s; - for (; i != e; i++) - { - s += (*i)->as_string (); - if (e != i + 1) - s += ", "; - } + str += append_attributes (outer_attrs, OUTER); - tok_trees += get_string_in_delims (s, delim_type); - } -#endif + str += "\n" + invoc_data.as_string (); return str; } @@ -1438,21 +1320,9 @@ ExternBlock::as_string () const str += "extern "; if (has_abi ()) - str += "\"" + abi + "\" "; + str += "\"" + abi + "\" "; - // inner attributes - str += "\n inner attributes: "; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - str += "\n " + attr.as_string (); - } + str += append_attributes (inner_attrs, INNER); str += "\n external items: "; if (!has_extern_items ()) @@ -1488,11 +1358,7 @@ MacroRulesDefinition::as_string () const std::string str; // get outer attrs - if (!outer_attrs.empty ()) - { - for (const auto &attr : outer_attrs) - str += attr.as_string () + "\n"; - } + str += append_attributes (outer_attrs, OUTER); str += "macro_rules!"; @@ -1531,9 +1397,13 @@ MacroRulesDefinition::as_string () const std::string MacroInvocation::as_string () const { - /*return "MacroInvocation: " + path.as_string () + "!" - + token_tree.as_string ();*/ - return "MacroInvocation: " + invoc_data.as_string (); + std::string str = "MacroInvocation: "; + + str += append_attributes (outer_attrs, OUTER); + + str += "\n " + invoc_data.as_string (); + + return str; } std::string @@ -1591,22 +1461,6 @@ ClosureExpr::as_string () const str += append_attributes (outer_attrs, OUTER); -#if 0 - // outer attributes - str += "\n outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif - str += "\n Has move: "; if (has_move) str += "true"; @@ -1674,7 +1528,7 @@ QualifiedPathInExpression::as_string () const std::string BorrowExpr::as_string () const { - /* TODO: find way to incorporate outer attrs - may have to represent in + /* TODO: find way to incorporate outer attrs - may have to represent in * different style (i.e. something more like BorrowExpr: \n outer attrs) */ std::string str ("&"); @@ -1693,7 +1547,7 @@ BorrowExpr::as_string () const std::string ReturnExpr::as_string () const { - /* TODO: find way to incorporate outer attrs - may have to represent in + /* TODO: find way to incorporate outer attrs - may have to represent in * different style (i.e. something more like BorrowExpr: \n outer attrs) */ std::string str ("return "); @@ -1713,20 +1567,6 @@ GroupedExpr::as_string () const str += append_attributes (outer_attrs, OUTER); // inner attributes -#if 0 - str += "\n inner attributes: "; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - str += "\n " + attr.as_string (); - } -#endif str += append_attributes (inner_attrs, INNER); str += "\n Expr in parens: " + expr_in_parens->as_string (); @@ -1821,7 +1661,7 @@ AsyncBlockExpr::as_string () const std::string str = "AsyncBlockExpr: "; // get outer attributes - //str += "\n " + Expr::as_string (); + // str += "\n " + Expr::as_string (); str += append_attributes (outer_attrs, OUTER); str += "\n Has move: "; @@ -1871,7 +1711,7 @@ MethodCallExpr::as_string () const std::string str = "MethodCallExpr: "; str += append_attributes (outer_attrs, OUTER); - + str += "\n Object (receiver) expr: \n"; str += receiver->as_string (); @@ -1958,15 +1798,14 @@ RangeToInclExpr::as_string () const std::string UnsafeBlockExpr::as_string () const { - std::string istr = indent_spaces (enter); - std::string str = istr + "UnsafeBlockExpr:"; - str += istr + "{"; + std::string str = "UnsafeBlockExpr:" + indent_spaces (enter); // get outer attributes - //str += "\n" + indent_spaces (stay) + Expr::as_string (); str += append_attributes (outer_attrs, OUTER); - return str + "\n" + indent_spaces (out) + "}\n" + expr->as_string (); + str += indent_spaces (stay) + expr->as_string () + "\n" + indent_spaces (out); + + return str; } std::string @@ -1982,9 +1821,9 @@ ClosureExprInner::as_string () const std::string IfExpr::as_string () const { - /* TODO: can if exprs have outer attributes? i thought no, but can't find it - * in docs */ - std::string str ("IfExpr: "); + std::string str = "IfExpr: "; + + str += append_attributes (outer_attrs, OUTER); str += "\n Condition expr: " + condition->as_string (); @@ -2026,9 +1865,9 @@ IfExprConseqIfLet::as_string () const std::string IfLetExpr::as_string () const { - /* TODO: can if let exprs have outer attributes? i thought no, but can't find - * it in docs */ - std::string str ("IfLetExpr: "); + std::string str = "IfLetExpr: "; + + str += append_attributes (outer_attrs, OUTER); str += "\n Condition match arm patterns: "; if (match_arm_patterns.empty ()) @@ -2218,7 +2057,7 @@ CallExpr::as_string () const std::string str = "CallExpr: "; str += append_attributes (outer_attrs, OUTER); - + str += "\n Function expr: "; str += function->as_string (); @@ -2319,20 +2158,6 @@ ArrayExpr::as_string () const // inner attributes str += append_attributes (inner_attrs, INNER); - #if 0 - str += "\n inner attributes: "; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - str += "\n " + attr.as_string (); - } - #endif str += "\n Array elems: "; if (!has_array_elems ()) @@ -2376,20 +2201,6 @@ MatchArm::as_string () const { // outer attributes std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str = "Outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif str += "\nPatterns: "; if (match_arm_patterns.empty ()) @@ -2422,26 +2233,6 @@ MatchCase::as_string () const return str; } -/*std::string -MatchCaseBlockExpr::as_string () const -{ - std::string str = MatchCase::as_string (); - - str += "\n Block expr: " + block_expr->as_string (); - - return str; -} - -std::string -MatchCaseExpr::as_string () const -{ - std::string str = MatchCase::as_string (); - - str += "\n Expr: " + expr->as_string (); - - return str; -}*/ - std::string MatchExpr::as_string () const { @@ -2453,20 +2244,6 @@ MatchExpr::as_string () const // inner attributes str += append_attributes (inner_attrs, INNER); -#if 0 - str += "\n inner attributes: "; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - str += "\n " + attr.as_string (); - } -#endif // match arms str += "\n Match arms: "; @@ -2492,20 +2269,6 @@ TupleExpr::as_string () const // inner attributes str += append_attributes (inner_attrs, INNER); -#if 0 - str += "\n inner attributes: "; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - str += "\n " + attr.as_string (); - } -#endif str += "\n Tuple elements: "; if (tuple_elems.empty ()) @@ -2515,7 +2278,7 @@ TupleExpr::as_string () const else { for (const auto &elem : tuple_elems) - str += "\n " + elem->as_string (); + str += "\n " + elem->as_string (); } return str; @@ -2529,9 +2292,9 @@ ExprStmtWithoutBlock::as_string () const str += indent_spaces (stay); if (expr == nullptr) - str += "none (this shouldn't happen and is probably an error)"; + str += "none (this shouldn't happen and is probably an error)"; else - str += expr->as_string (); + str += expr->as_string (); indent_spaces (out); return str; @@ -2565,13 +2328,13 @@ FunctionQualifiers::as_string () const } if (has_unsafe) - str += "unsafe "; + str += "unsafe "; if (has_extern) { str += "extern"; if (extern_abi != "") - str += " \"" + extern_abi + "\""; + str += " \"" + extern_abi + "\""; } return str; @@ -2584,9 +2347,9 @@ TraitBound::as_string () const str += "\n Has opening question mark: "; if (opening_question_mark) - str += "true"; + str += "true"; else - str += "false"; + str += "false"; str += "\n For lifetimes: "; if (!has_for_lifetimes ()) @@ -2596,7 +2359,7 @@ TraitBound::as_string () const else { for (const auto &lifetime : for_lifetimes) - str += "\n " + lifetime.as_string (); + str += "\n " + lifetime.as_string (); } str += "\n Type path: " + type_path.as_string (); @@ -2635,7 +2398,7 @@ MacroMatcher::as_string () const else { for (const auto &match : matches) - str += "\n " + match->as_string (); + str += "\n " + match->as_string (); } return str; @@ -2648,9 +2411,9 @@ LifetimeParam::as_string () const str += "\n Outer attribute: "; if (!has_outer_attribute ()) - str += "none"; + str += "none"; else - str += outer_attr.as_string (); + str += outer_attr.as_string (); str += "\n Lifetime: " + lifetime.as_string (); @@ -2662,7 +2425,7 @@ LifetimeParam::as_string () const else { for (const auto &bound : lifetime_bounds) - str += "\n " + bound.as_string (); + str += "\n " + bound.as_string (); } return str; @@ -2677,12 +2440,12 @@ MacroMatchFragment::as_string () const std::string QualifiedPathInType::as_string () const { - /* TODO: this may need adjusting if segments (e.g. with functions) can't be + /* TODO: this may need adjusting if segments (e.g. with functions) can't be * literalised */ std::string str = path_type.as_string (); for (const auto &segment : segments) - str += "::" + segment->as_string (); + str += "::" + segment->as_string (); return str; } @@ -2700,14 +2463,14 @@ MacroMatchRepetition::as_string () const else { for (const auto &match : matches) - str += "\n " + match->as_string (); + str += "\n " + match->as_string (); } str += "\n Sep: "; if (!has_sep ()) - str += "none"; + str += "none"; else - str += sep->as_string (); + str += sep->as_string (); str += "\n Op: "; switch (op) @@ -2735,7 +2498,7 @@ std::string Lifetime::as_string () const { if (is_error ()) - return "error lifetime"; + return "error lifetime"; switch (lifetime_type) { @@ -2753,15 +2516,15 @@ Lifetime::as_string () const std::string TypePath::as_string () const { - /* TODO: this may need to be rewritten if a segment (e.g. function) can't be + /* TODO: this may need to be rewritten if a segment (e.g. function) can't be * literalised */ std::string str; if (has_opening_scope_resolution) - str = "::"; + str = "::"; for (const auto &segment : segments) - str += segment->as_string () + "::"; + str += segment->as_string () + "::"; // kinda hack - remove last 2 '::' characters str.erase (str.length () - 2); @@ -2776,9 +2539,9 @@ TypeParam::as_string () const str += "\n Outer attribute: "; if (!has_outer_attribute ()) - str += "none"; + str += "none"; else - str += outer_attr.as_string (); + str += outer_attr.as_string (); str += "\n Identifier: " + type_representation; @@ -2790,14 +2553,14 @@ TypeParam::as_string () const else { for (const auto &bound : type_param_bounds) - str += "\n " + bound->as_string (); + str += "\n " + bound->as_string (); } str += "\n Type: "; if (!has_type ()) - str += "none"; + str += "none"; else - str += type->as_string (); + str += type->as_string (); return str; } @@ -2806,7 +2569,7 @@ SimplePath PathPattern::convert_to_simple_path (bool with_opening_scope_resolution) const { if (!has_segments ()) - return SimplePath::create_empty (); + return SimplePath::create_empty (); // create vector of reserved size (to minimise reallocations) std::vector simple_segments; @@ -2817,7 +2580,7 @@ PathPattern::convert_to_simple_path (bool with_opening_scope_resolution) const // return empty path if doesn't meet simple path segment requirements if (segment.is_error () || segment.has_generic_args () || segment.as_string () == "Self") - return SimplePath::create_empty (); + return SimplePath::create_empty (); // create segment and add to vector std::string segment_str = segment.as_string (); @@ -2828,9 +2591,9 @@ PathPattern::convert_to_simple_path (bool with_opening_scope_resolution) const // kind of a HACK to get locus depending on opening scope resolution Location locus = Linemap::unknown_location (); if (with_opening_scope_resolution) - locus = simple_segments[0].get_locus () - 2; // minus 2 chars for :: + locus = simple_segments[0].get_locus () - 2; // minus 2 chars for :: else - locus = simple_segments[0].get_locus (); + locus = simple_segments[0].get_locus (); // FIXME: this hack probably doesn't actually work return SimplePath (std::move (simple_segments), with_opening_scope_resolution, @@ -2841,7 +2604,7 @@ SimplePath TypePath::as_simple_path () const { if (segments.empty ()) - return SimplePath::create_empty (); + return SimplePath::create_empty (); // create vector of reserved size (to minimise reallocations) std::vector simple_segments; @@ -2852,7 +2615,7 @@ TypePath::as_simple_path () const // return empty path if doesn't meet simple path segment requirements if (segment == nullptr || segment->is_error () || !segment->is_ident_only () || segment->as_string () == "Self") - return SimplePath::create_empty (); + return SimplePath::create_empty (); // create segment and add to vector std::string segment_str = segment->as_string (); @@ -2870,7 +2633,7 @@ PathExprSegment::as_string () const // TODO: rewrite dump to work with non-literalisable types std::string ident_str = segment_name.as_string (); if (has_generic_args ()) - ident_str += "::<" + generic_args.as_string () + ">"; + ident_str += "::<" + generic_args.as_string () + ">"; return ident_str; } @@ -2941,9 +2704,9 @@ ForLoopExpr::as_string () const str += "\n Label: "; if (!has_loop_label ()) - str += "none"; + str += "none"; else - str += loop_label.as_string (); + str += loop_label.as_string (); str += "\n Pattern: " + pattern->as_string (); @@ -2959,9 +2722,9 @@ RangePattern::as_string () const { // TODO: maybe rewrite to work with non-linearisable bounds if (has_ellipsis_syntax) - return lower->as_string () + "..." + upper->as_string (); + return lower->as_string () + "..." + upper->as_string (); else - return lower->as_string () + "..=" + upper->as_string (); + return lower->as_string () + "..=" + upper->as_string (); } std::string @@ -2970,7 +2733,7 @@ RangePatternBoundLiteral::as_string () const std::string str; if (has_minus) - str += "-"; + str += "-"; str += literal.as_string (); @@ -2983,7 +2746,7 @@ SlicePattern::as_string () const std::string str ("SlicePattern: "); for (const auto &pattern : items) - str += "\n " + pattern->as_string (); + str += "\n " + pattern->as_string (); return str; } @@ -2994,7 +2757,7 @@ TuplePatternItemsMultiple::as_string () const std::string str; for (const auto &pattern : patterns) - str += "\n " + pattern->as_string (); + str += "\n " + pattern->as_string (); return str; } @@ -3012,7 +2775,7 @@ TuplePatternItemsRanged::as_string () const else { for (const auto &lower : lower_patterns) - str += "\n " + lower->as_string (); + str += "\n " + lower->as_string (); } str += "\n Upper patterns: "; @@ -3023,7 +2786,7 @@ TuplePatternItemsRanged::as_string () const else { for (const auto &upper : upper_patterns) - str += "\n " + upper->as_string (); + str += "\n " + upper->as_string (); } return str; @@ -3040,20 +2803,6 @@ StructPatternField::as_string () const { // outer attributes std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str ("Outer attributes: "); - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif return str; } @@ -3066,10 +2815,10 @@ StructPatternFieldIdent::as_string () const str += "\n"; if (has_ref) - str += "ref "; + str += "ref "; if (has_mut) - str += "mut "; + str += "mut "; str += ident; @@ -3114,14 +2863,14 @@ StructPatternElements::as_string () const else { for (const auto &field : fields) - str += "\n " + field->as_string (); + str += "\n " + field->as_string (); } str += "\n Etc: "; if (has_struct_pattern_etc) - str += "true"; + str += "true"; else - str += "false"; + str += "false"; return str; } @@ -3135,9 +2884,9 @@ StructPattern::as_string () const str += "\n Struct pattern elems: "; if (!has_struct_pattern_elems ()) - str += "none"; + str += "none"; else - str += elems.as_string (); + str += elems.as_string (); return str; } @@ -3148,7 +2897,7 @@ LiteralPattern::as_string () const std::string str; if (has_minus) - str += "-"; + str += "-"; return str + lit.as_string (); } @@ -3160,10 +2909,10 @@ ReferencePattern::as_string () const std::string str ("&"); if (has_two_amps) - str += "&"; + str += "&"; if (is_mut) - str += "mut "; + str += "mut "; str += pattern->as_string (); @@ -3177,15 +2926,15 @@ IdentifierPattern::as_string () const std::string str; if (is_ref) - str += "ref "; + str += "ref "; if (is_mut) - str += "mut "; + str += "mut "; str += variable_ident; if (has_pattern_to_bind ()) - str += " @ " + to_bind->as_string (); + str += " @ " + to_bind->as_string (); return str; } @@ -3196,7 +2945,7 @@ TupleStructItemsNoRange::as_string () const std::string str; for (const auto &pattern : patterns) - str += "\n " + pattern->as_string (); + str += "\n " + pattern->as_string (); return str; } @@ -3213,7 +2962,7 @@ TupleStructItemsRange::as_string () const else { for (const auto &lower : lower_patterns) - str += "\n " + lower->as_string (); + str += "\n " + lower->as_string (); } str += "\n Upper patterns: "; @@ -3224,7 +2973,7 @@ TupleStructItemsRange::as_string () const else { for (const auto &upper : upper_patterns) - str += "\n " + upper->as_string (); + str += "\n " + upper->as_string (); } return str; @@ -3251,37 +3000,14 @@ LetStmt::as_string () const str += "\n" + indent_spaces (stay) + "let " + variables_pattern->as_string (); if (has_type ()) - str += " : " + type->as_string (); + str += " : " + type->as_string (); if (has_init_expr ()) - str += " = " + init_expr->as_string (); + str += " = " + init_expr->as_string (); return str; } -#if 0 -// Used to get outer attributes for expressions. -std::string -Expr::as_string () const -{ - // outer attributes - std::string str = "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } - - return str; -} -#endif - // hopefully definition here will prevent circular dependency issue TraitBound * TypePath::to_trait_bound (bool in_parens) const @@ -3315,7 +3041,7 @@ ImplTraitType::as_string () const else { for (const auto &bound : type_param_bounds) - str += "\n " + bound->as_string (); + str += "\n " + bound->as_string (); } return str; @@ -3328,10 +3054,10 @@ ReferenceType::as_string () const std::string str ("&"); if (has_lifetime ()) - str += lifetime.as_string () + " "; + str += lifetime.as_string () + " "; if (has_mut) - str += "mut "; + str += "mut "; str += type->as_string (); @@ -3367,9 +3093,9 @@ TraitObjectType::as_string () const std::string str ("TraitObjectType: \n Has dyn dispatch: "); if (has_dyn) - str += "true"; + str += "true"; else - str += "false"; + str += "false"; str += "\n TypeParamBounds: "; if (type_param_bounds.empty ()) @@ -3379,7 +3105,7 @@ TraitObjectType::as_string () const else { for (const auto &bound : type_param_bounds) - str += "\n " + bound->as_string (); + str += "\n " + bound->as_string (); } return str; @@ -3397,7 +3123,7 @@ BareFunctionType::as_string () const else { for (const auto &for_lifetime : for_lifetimes) - str += "\n " + for_lifetime.as_string (); + str += "\n " + for_lifetime.as_string (); } str += "\n Qualifiers: " + function_qualifiers.as_string (); @@ -3410,20 +3136,20 @@ BareFunctionType::as_string () const else { for (const auto ¶m : params) - str += "\n " + param.as_string (); + str += "\n " + param.as_string (); } str += "\n Is variadic: "; if (is_variadic) - str += "true"; + str += "true"; else - str += "false"; + str += "false"; str += "\n Return type: "; if (!has_return_type ()) - str += "none (void)"; + str += "none (void)"; else - str += return_type->as_string (); + str += return_type->as_string (); return str; } @@ -3449,9 +3175,9 @@ TraitObjectTypeOneBound::as_string () const std::string str ("TraitObjectTypeOneBound: \n Has dyn dispatch: "); if (has_dyn) - str += "true"; + str += "true"; else - str += "false"; + str += "false"; str += "\n TraitBound: " + trait_bound.as_string (); @@ -3480,7 +3206,7 @@ TypePathFunction::as_string () const str += ")"; if (has_return_type ()) - str += " -> " + return_type->as_string (); + str += " -> " + return_type->as_string (); return str; } @@ -3560,7 +3286,7 @@ StructExprTuple::as_string () const // debug - null pointer check if (*i == nullptr) - return "ERROR_MARK_STRING - nullptr struct expr tuple field"; + return "ERROR_MARK_STRING - nullptr struct expr tuple field"; str += '('; for (; i != e; i++) @@ -3575,18 +3301,7 @@ StructExprTuple::as_string () const indent_spaces (enter); indent_spaces (enter); // inner attributes - str += "\n" + indent_spaces (stay) + "inner attributes:"; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - str += "\n" + indent_spaces (stay) + attr.as_string (); - } + str += append_attributes (inner_attrs, INNER); indent_spaces (out); indent_spaces (out); @@ -3603,20 +3318,6 @@ StructExprStruct::as_string () const // inner attributes str += append_attributes (inner_attrs, INNER); -#if 0 - str += "\n inner attributes: "; - if (inner_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "inner attribute" syntax - - * just the body */ - for (const auto &attr : inner_attrs) - str += "\n " + attr.as_string (); - } -#endif return str; } @@ -3723,20 +3424,6 @@ EnumItem::as_string () const { // outer attributes std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str = "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif str += "\n" + variant_name; @@ -3778,23 +3465,9 @@ TupleField::as_string () const // outer attributes std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str = "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif if (has_visibility ()) - str += "\n" + visibility.as_string (); + str += "\n" + visibility.as_string (); str += " " + field_type->as_string (); @@ -3835,23 +3508,9 @@ StructField::as_string () const // TODO: rewrite to work with non-linearisable exprs // outer attributes std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str = "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif if (has_visibility ()) - str += "\n" + visibility.as_string (); + str += "\n" + visibility.as_string (); str += " " + field_name + " : " + field_type->as_string (); @@ -3870,50 +3529,11 @@ EnumItemDiscriminant::as_string () const return str; } -#if 0 -std::string -ExternalItem::as_string () const -{ - // outer attributes - std::string str = "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } - - // start visibility on new line and with a space - str += "\n" + visibility.as_string () + " "; - - return str; -} -#endif - std::string ExternalStaticItem::as_string () const { // outer attributes std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str = "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif // start visibility on new line and with a space str += "\n" + visibility.as_string () + " "; @@ -3937,20 +3557,6 @@ ExternalFunctionItem::as_string () const { // outer attributes std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str = "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif // start visibility on new line and with a space str += "\n" + visibility.as_string () + " "; @@ -4028,19 +3634,6 @@ std::string NamedFunctionParam::as_string () const { std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str = "outer attributes: "; - - if (!has_outer_attrs ()) - { - str += "none"; - } - else - { - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif str += "\n" + name; @@ -4049,47 +3642,18 @@ NamedFunctionParam::as_string () const return str; } -/*std::string TraitItem::as_string() const { - // outer attributes - std::string str = "outer attributes: "; - if (outer_attrs.empty()) { - str += "none"; - } else { - // note that this does not print them with "outer attribute" syntax - -just the body for (const auto& attr : outer_attrs) { str += "\n " + -attr.as_string(); - } - } - - return str; -}*/ - std::string TraitItemFunc::as_string () const { std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str = "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif str += "\n" + decl.as_string (); str += "\n Definition (block expr): "; if (has_definition ()) - str += block_expr->as_string (); + str += block_expr->as_string (); else - str += "none"; + str += "none"; return str; } @@ -4127,7 +3691,7 @@ TraitFunctionDecl::as_string () const if (has_params ()) { for (const auto ¶m : function_params) - str += "\n " + param.as_string (); + str += "\n " + param.as_string (); } else { @@ -4136,15 +3700,15 @@ TraitFunctionDecl::as_string () const str += "\n Return type: "; if (has_return_type ()) - str += return_type->as_string (); + str += return_type->as_string (); else - str += "none (void)"; + str += "none (void)"; str += "\n Where clause: "; if (has_where_clause ()) - str += where_clause.as_string (); + str += where_clause.as_string (); else - str += "none"; + str += "none"; return str; } @@ -4153,28 +3717,14 @@ std::string TraitItemMethod::as_string () const { std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str = "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif str += "\n" + decl.as_string (); str += "\n Definition (block expr): "; if (has_definition ()) - str += block_expr->as_string (); + str += block_expr->as_string (); else - str += "none"; + str += "none"; return str; } @@ -4214,7 +3764,7 @@ TraitMethodDecl::as_string () const if (has_params ()) { for (const auto ¶m : function_params) - str += "\n " + param.as_string (); + str += "\n " + param.as_string (); } else { @@ -4223,15 +3773,15 @@ TraitMethodDecl::as_string () const str += "\n Return type: "; if (has_return_type ()) - str += return_type->as_string (); + str += return_type->as_string (); else - str += "none (void)"; + str += "none (void)"; str += "\n Where clause: "; if (has_where_clause ()) - str += where_clause.as_string (); + str += where_clause.as_string (); else - str += "none"; + str += "none"; return str; } @@ -4241,25 +3791,11 @@ TraitItemConst::as_string () const { // TODO: rewrite to work with non-linearisable exprs std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str = "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif str += "\nconst " + name + " : " + type->as_string (); if (has_expression ()) - str += " = " + expr->as_string (); + str += " = " + expr->as_string (); return str; } @@ -4268,20 +3804,6 @@ std::string TraitItemType::as_string () const { std::string str = append_attributes (outer_attrs, OUTER); -#if 0 - std::string str = "outer attributes: "; - if (outer_attrs.empty ()) - { - str += "none"; - } - else - { - /* note that this does not print them with "outer attribute" syntax - - * just the body */ - for (const auto &attr : outer_attrs) - str += "\n " + attr.as_string (); - } -#endif str += "\ntype " + name; @@ -4327,7 +3849,7 @@ SelfParam::as_string () const std::string str; if (is_mut) - str += "mut "; + str += "mut "; str += "self : "; @@ -4341,7 +3863,7 @@ SelfParam::as_string () const std::string str = "&" + lifetime.as_string () + " "; if (is_mut) - str += "mut "; + str += "mut "; str += "self"; @@ -4353,7 +3875,7 @@ SelfParam::as_string () const std::string str = "&"; if (is_mut) - str += " mut "; + str += " mut "; str += "self"; @@ -4365,7 +3887,7 @@ SelfParam::as_string () const std::string str; if (is_mut) - str += "mut "; + str += "mut "; str += "self"; @@ -4391,7 +3913,7 @@ LifetimeWhereClauseItem::as_string () const str += "\nLifetime bounds: "; for (const auto &bound : lifetime_bounds) - str += "\n " + bound.as_string (); + str += "\n " + bound.as_string (); return str; } @@ -4408,7 +3930,7 @@ TypeBoundWhereClauseItem::as_string () const else { for (const auto &for_lifetime : for_lifetimes) - str += "\n " + for_lifetime.as_string (); + str += "\n " + for_lifetime.as_string (); } str += "\nType: " + bound_type->as_string (); @@ -4419,7 +3941,7 @@ TypeBoundWhereClauseItem::as_string () const { // debug null pointer check if (bound == nullptr) - return "NULL_POINTER_MARK - type param bounds"; + return "NULL_POINTER_MARK - type param bounds"; str += "\n " + bound->as_string (); } @@ -4649,8 +4171,8 @@ MacroParser::parse_meta_item_inner () skip_token (2); - // remove the quotes from the string value - std::string raw_value = unquote_string (std::move (value)); + // remove the quotes from the string value + std::string raw_value = unquote_string (std::move (value)); return std::unique_ptr ( new MetaNameValueStr (std::move (ident), std::move (raw_value))); @@ -4829,7 +4351,7 @@ MacroParser::parse_meta_item_seq () meta_items.push_back (std::move (inner)); if (peek_token ()->get_id () != COMMA) - break; + break; skip_token (); } @@ -5006,7 +4528,10 @@ AttrInputMetaItemContainer::check_cfg_predicate (const Session &session) const return false; // DEBUG - fprintf (stderr, "asked to check cfg of attrinputmetaitemcontainer - delegating to first item. container: '%s'\n", as_string ().c_str ()); + fprintf (stderr, + "asked to check cfg of attrinputmetaitemcontainer - delegating to " + "first item. container: '%s'\n", + as_string ().c_str ()); return items[0]->check_cfg_predicate (session); @@ -5208,7 +4733,12 @@ bool MetaNameValueStr::check_cfg_predicate (const Session &session) const { // DEBUG - fprintf (stderr, "checked key-value pair for cfg: '%s', '%s' - is%s in target data\n", ident.c_str (), str.c_str (), session.options.target_data.has_key_value_pair (ident, str) ? "" : " not"); + fprintf (stderr, + "checked key-value pair for cfg: '%s', '%s' - is%s in target data\n", + ident.c_str (), str.c_str (), + session.options.target_data.has_key_value_pair (ident, str) + ? "" + : " not"); return session.options.target_data.has_key_value_pair (ident, str); } @@ -5340,11 +4870,15 @@ Attribute::check_cfg_predicate (const Session &session) const /* assume that cfg predicate actually can exist, i.e. attribute has cfg or * cfg_attr path */ if (!has_attr_input () - || (path.as_string () != "cfg" && path.as_string () != "cfg_attr")) { - // DEBUG message - fprintf (stderr, "tried to check cfg predicate on attr that either has no input or invalid path. attr: '%s'\n", as_string ().c_str ()); + || (path.as_string () != "cfg" && path.as_string () != "cfg_attr")) + { + // DEBUG message + fprintf (stderr, + "tried to check cfg predicate on attr that either has no input " + "or invalid path. attr: '%s'\n", + as_string ().c_str ()); - return false; + return false; } // assume that it has already been parsed @@ -5833,18 +5367,6 @@ IfLetExprConseqIfLet::accept_vis (ASTVisitor &vis) vis.visit (*this); } -/*void -MatchCaseBlockExpr::accept_vis (ASTVisitor &vis) -{ - vis.visit (*this); -} - -void -MatchCaseExpr::accept_vis (ASTVisitor &vis) -{ - vis.visit (*this); -}*/ - void MatchExpr::accept_vis (ASTVisitor &vis) { diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 0922045..b80e7a3 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -220,7 +220,10 @@ protected: /* Use covariance to implement clone function as returning this object rather * than base */ - Token *clone_token_tree_impl () const final override { return clone_token_impl (); } + Token *clone_token_tree_impl () const final override + { + return clone_token_impl (); + } /* Use covariance to implement clone function as returning this object rather * than base */ @@ -823,8 +826,6 @@ public: return std::unique_ptr (clone_item_impl ()); } - std::string as_string () const = 0; - /* Adds crate names to the vector passed by reference, if it can * (polymorphism). TODO: remove, unused. */ virtual void @@ -885,8 +886,7 @@ public: protected: // Constructor - Expr () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) - {} + Expr () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} // Clone function implementation as pure virtual method virtual Expr *clone_expr_impl () const = 0; @@ -941,8 +941,10 @@ class IdentifierExpr : public ExprWithoutBlock Location locus; public: - IdentifierExpr (Identifier ident, std::vector outer_attrs, Location locus) - : outer_attrs (std::move (outer_attrs)), ident (std::move (ident)), locus (locus) + IdentifierExpr (Identifier ident, std::vector outer_attrs, + Location locus) + : outer_attrs (std::move (outer_attrs)), ident (std::move (ident)), + locus (locus) {} std::string as_string () const override { return ident; } @@ -967,7 +969,10 @@ public: const std::vector &get_outer_attrs () const { return outer_attrs; } std::vector &get_outer_attrs () { return outer_attrs; } - void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + void set_outer_attrs (std::vector new_attrs) override + { + outer_attrs = std::move (new_attrs); + } protected: // Clone method implementation @@ -1670,7 +1675,8 @@ public: // Base path expression AST node - abstract class PathExpr : public ExprWithoutBlock -{}; +{ +}; } // namespace AST } // namespace Rust diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index e23be14..29a1cf1 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -316,7 +316,7 @@ class MacroRulesDefinition : public MacroItem Location locus; - /* NOTE: in rustc, macro definitions are considered (and parsed as) a type + /* NOTE: in rustc, macro definitions are considered (and parsed as) a type * of macro, whereas here they are considered part of the language itself. * I am not aware of the implications of this decision. The rustc spec does * mention that using the same parser for macro definitions and invocations @@ -395,7 +395,10 @@ public: const std::vector &get_outer_attrs () const { return outer_attrs; } std::vector &get_outer_attrs () { return outer_attrs; } - void set_outer_attrs (std::vector new_attrs) override { outer_attrs = std::move (new_attrs); } + void set_outer_attrs (std::vector new_attrs) override + { + outer_attrs = std::move (new_attrs); + } protected: /* Use covariance to implement clone function as returning this object rather @@ -545,7 +548,10 @@ public: : ident (std::move (ident)), str (std::move (str)) {} - std::string as_string () const override { return ident + " = \"" + str + "\""; } + std::string as_string () const override + { + return ident + " = \"" + str + "\""; + } void accept_vis (ASTVisitor &vis) override; @@ -626,138 +632,44 @@ protected: } }; -/* A "tagged union" describing a single AST node. Due to technical difficulties - * with unions, this is actually a struct and so wastes space. FIXME */ -/* +/* Should be a tagged union to save space but implemented as struct due to + * technical difficulties. TODO: fix + * Basically, a single AST node used inside an AST fragment. */ struct SingleASTNode { - public: - enum tag_types { - EXPR, - STMT, - ITEM, - TYPE, - PATTERN, - TRAIT_ITEM, - INHERENT_IMPL_ITEM, - TRAIT_IMPL_ITEM, - EXTERNAL_ITEM - // TODO: other types like inside macro_rules? - }; - - SingleASTNode (std::unique_ptr expr) : tag (EXPR), expr (std::move (expr)) { - - } - - SingleASTNode (std::unique_ptr stmt) : tag (STMT), stmt (std::move (stmt)) { - - } - - SingleASTNode (std::unique_ptr item) : tag (ITEM), item (std::move (item)) { - - } - - SingleASTNode (std::unique_ptr type) : tag (TYPE), type (std::move (type)) { - - } - - SingleASTNode (std::unique_ptr pattern) : tag (PATTERN), pattern (std::move (pattern)) { - - } - - SingleASTNode (std::unique_ptr trait_item) : tag (TRAIT_ITEM), trait_item (std::move (trait_item)) { - - } - - SingleASTNode (std::unique_ptr inherent_impl_item) : tag (INHERENT_IMPL_ITEM), inherent_impl_item (std::move (inherent_impl_item)) { - - } - - SingleASTNode (std::unique_ptr trait_impl_item) : tag (TRAIT_IMPL_ITEM), trait_impl_item (std::move (trait_impl_item)) { - - } - - SingleASTNode (std::unique_ptr external_item) : tag (EXTERNAL_ITEM), external_item (std::move (external_item)) { - - } - - // destructor definition is required - ~SingleASTNode () { - switch (tag) { - case EXPR: - expr.~unique_ptr (); - break; - case STMT: - stmt.~unique_ptr (); - break; - case ITEM: - item.~unique_ptr (); - break; - case TYPE: - type.~unique_ptr (); - break; - case PATTERN: - pattern.~unique_ptr (); - break; - case TRAIT_ITEM: - trait_item.~unique_ptr (); - break; - case INHERENT_IMPL_ITEM: - inherent_impl_item.~unique_ptr (); - break; - case TRAIT_IMPL_ITEM: - trait_impl_item.~unique_ptr (); - break; - case EXTERNAL_ITEM: - external_item.~unique_ptr (); - break; - default: - // should not happen - gcc_unreachable (); - break; - } - } - - private: - tag_types tag; - - union { - std::unique_ptr expr; - std::unique_ptr stmt; - std::unique_ptr item; - std::unique_ptr type; - std::unique_ptr pattern; - std::unique_ptr trait_item; - std::unique_ptr inherent_impl_item; - std::unique_ptr trait_impl_item; - std::unique_ptr external_item; - }; -}; -*/ -struct SingleASTNode { - std::unique_ptr expr; - std::unique_ptr stmt; - std::unique_ptr item; - std::unique_ptr type; - std::unique_ptr pattern; - std::unique_ptr trait_item; - std::unique_ptr inherent_impl_item; - std::unique_ptr trait_impl_item; - std::unique_ptr external_item; - - SingleASTNode (std::unique_ptr expr) : expr (std::move (expr)) {} - SingleASTNode (std::unique_ptr stmt) : stmt (std::move (stmt)) {} - SingleASTNode (std::unique_ptr item) : item (std::move (item)) {} - SingleASTNode (std::unique_ptr type) : type (std::move (type)) {} - SingleASTNode (std::unique_ptr pattern) : pattern (std::move (pattern)) {} - SingleASTNode (std::unique_ptr trait_item) : trait_item (std::move (trait_item)) {} - SingleASTNode (std::unique_ptr inherent_impl_item) : inherent_impl_item (std::move (inherent_impl_item)) {} - SingleASTNode (std::unique_ptr trait_impl_item) : trait_impl_item (std::move (trait_impl_item)) {} - SingleASTNode (std::unique_ptr external_item) : external_item (std::move (external_item)) {} + std::unique_ptr expr; + std::unique_ptr stmt; + std::unique_ptr item; + std::unique_ptr type; + std::unique_ptr pattern; + std::unique_ptr trait_item; + std::unique_ptr inherent_impl_item; + std::unique_ptr trait_impl_item; + std::unique_ptr external_item; + + SingleASTNode (std::unique_ptr expr) : expr (std::move (expr)) {} + SingleASTNode (std::unique_ptr stmt) : stmt (std::move (stmt)) {} + SingleASTNode (std::unique_ptr item) : item (std::move (item)) {} + SingleASTNode (std::unique_ptr type) : type (std::move (type)) {} + SingleASTNode (std::unique_ptr pattern) + : pattern (std::move (pattern)) + {} + SingleASTNode (std::unique_ptr trait_item) + : trait_item (std::move (trait_item)) + {} + SingleASTNode (std::unique_ptr inherent_impl_item) + : inherent_impl_item (std::move (inherent_impl_item)) + {} + SingleASTNode (std::unique_ptr trait_impl_item) + : trait_impl_item (std::move (trait_impl_item)) + {} + SingleASTNode (std::unique_ptr external_item) + : external_item (std::move (external_item)) + {} }; -/* Basically, a "fragment" that can be incorporated into the AST, created as - * a result of macro expansion. Really annoying to work with due to the fact +/* Basically, a "fragment" that can be incorporated into the AST, created as + * a result of macro expansion. Really annoying to work with due to the fact * that macros can really expand to anything. As such, horrible representation * at the moment. */ struct ASTFragment @@ -765,7 +677,7 @@ struct ASTFragment private: /* basic idea: essentially, a vector of tagged unions of different AST node * types. Now, this could actually be stored without a tagged union if the - * different AST node types had a unified parent, but that would create + * different AST node types had a unified parent, but that would create * issues with the diamond problem or significant performance penalties. So * a tagged union had to be used instead. A vector is used to represent the * ability for a macro to expand to two statements, for instance. */ diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index df99d75..b2a0bb5 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -18,7 +18,6 @@ #include "rust-macro-expand.h" #include "rust-ast-full.h" -// is full really required? #include "rust-ast-visitor.h" #include "rust-diagnostics.h" @@ -1529,7 +1528,8 @@ public: } void visit (AST::IfExpr &expr) override { - // rust playground test shows that IfExpr does support outer attrs, at least when used as statement + // rust playground test shows that IfExpr does support outer attrs, at least + // when used as statement // initial strip test based on outer attrs expander.expand_cfg_attrs (expr.get_outer_attrs ()); @@ -1667,7 +1667,7 @@ public: expr.mark_for_strip (); return; } - + for (auto &pattern : expr.get_patterns ()) { pattern->accept_vis (*this); @@ -3183,36 +3183,48 @@ MacroExpander::expand_cfg_macro (AST::MacroInvocData &invoc) return AST::Literal ("false", AST::Literal::BOOL, CORETYPE_BOOL); } -AST::ASTFragment -MacroExpander::expand_decl_macro (AST::MacroInvocData &invoc, AST::MacroRulesDefinition &rules_def) { +AST::ASTFragment +MacroExpander::expand_decl_macro (AST::MacroInvocData &invoc, + AST::MacroRulesDefinition &rules_def) +{ // ensure that both invocation and rules are in a valid state rust_assert (!invoc.is_marked_for_strip ()); rust_assert (!rules_def.is_marked_for_strip ()); rust_assert (rules_def.get_macro_rules ().size () > 0); - /* probably something here about parsing invoc and rules def token trees to token stream. if not, - * how would parser handle the captures of exprs and stuff? on the other hand, token trees may be - * kind of useful in rules def as creating a point where recursion can occur (like having - * "compare_macro_match" and then it calling itself when it finds delimiters) */ - - /* find matching rule to invoc token tree, based on macro rule's matcher. if none exist, error. - * - specifically, check each matcher in order. if one fails to match, move onto next. */ - /* TODO: does doing this require parsing expressions and whatever in the invoc? if so, might as - * well save the results if referenced using $ or whatever. If not, do another pass saving them. - * Except this is probably useless as different rules could have different starting points for exprs - * or whatever. Decision trees could avoid this, but they have their own issues. */ - /* TODO: will need to modify the parser so that it can essentially "catch" errors - maybe - * "try_parse_expr" or whatever methods. */ - // this technically creates a back-tracking parser - this will be the implementation style - - /* then, after results are saved, generate the macro output from the transcriber token tree. if i - * understand this correctly, the macro invocation gets replaced by the transcriber tokens, except - * with substitutions made (e.g. for $i variables) */ - - /* TODO: it is probably better to modify AST::Token to store a pointer to a Lexer::Token (rather - * than being converted) - i.e. not so much have AST::Token as a Token but rather a TokenContainer - * (as it is another type of TokenTree). This will prevent re-conversion of Tokens between each type - * all the time, while still allowing the heterogenous storage of token trees. */ + /* probably something here about parsing invoc and rules def token trees to + * token stream. if not, how would parser handle the captures of exprs and + * stuff? on the other hand, token trees may be kind of useful in rules def as + * creating a point where recursion can occur (like having + * "compare_macro_match" and then it calling itself when it finds delimiters) + */ + + /* find matching rule to invoc token tree, based on macro rule's matcher. if + * none exist, error. + * - specifically, check each matcher in order. if one fails to match, move + * onto next. */ + /* TODO: does doing this require parsing expressions and whatever in the + * invoc? if so, might as well save the results if referenced using $ or + * whatever. If not, do another pass saving them. Except this is probably + * useless as different rules could have different starting points for exprs + * or whatever. Decision trees could avoid this, but they have their own + * issues. */ + /* TODO: will need to modify the parser so that it can essentially "catch" + * errors - maybe "try_parse_expr" or whatever methods. */ + // this technically creates a back-tracking parser - this will be the + // implementation style + + /* then, after results are saved, generate the macro output from the + * transcriber token tree. if i understand this correctly, the macro + * invocation gets replaced by the transcriber tokens, except with + * substitutions made (e.g. for $i variables) */ + + /* TODO: it is probably better to modify AST::Token to store a pointer to a + * Lexer::Token (rather than being converted) - i.e. not so much have + * AST::Token as a Token but rather a TokenContainer (as it is another type of + * TokenTree). This will prevent re-conversion of Tokens between each type + * all the time, while still allowing the heterogenous storage of token trees. + */ } void @@ -3275,6 +3287,7 @@ MacroExpander::fails_cfg (const std::vector &attrs) const bool MacroExpander::fails_cfg_with_expand (std::vector &attrs) const { + // TODO: maybe have something that strips cfg attributes that evaluate true? for (auto &attr : attrs) { if (attr.get_path () == "cfg") @@ -3282,22 +3295,32 @@ MacroExpander::fails_cfg_with_expand (std::vector &attrs) const if (!attr.is_parsed_to_meta_item ()) attr.parse_attr_to_meta_item (); - // DEBUG - if (!attr.is_parsed_to_meta_item ()) - fprintf (stderr, "failed to parse attr to meta item, right before cfg predicate check\n"); - else - fprintf(stderr, "attr has been successfully parsed to meta item, right before cfg predicate check\n"); + // DEBUG + if (!attr.is_parsed_to_meta_item ()) + fprintf (stderr, "failed to parse attr to meta item, right before " + "cfg predicate check\n"); + else + fprintf (stderr, "attr has been successfully parsed to meta item, " + "right before cfg predicate check\n"); if (!attr.check_cfg_predicate (session)) - { - // DEBUG - fprintf (stderr, "cfg predicate failed for attribute: \033[0;31m'%s'\033[0m\n", attr.as_string ().c_str ()); + { + // DEBUG + fprintf ( + stderr, + "cfg predicate failed for attribute: \033[0;31m'%s'\033[0m\n", + attr.as_string ().c_str ()); - return true; - } else { - // DEBUG - fprintf (stderr, "cfg predicate succeeded for attribute: \033[0;31m'%s'\033[0m\n", attr.as_string ().c_str ()); - } + return true; + } + else + { + // DEBUG + fprintf (stderr, + "cfg predicate succeeded for attribute: " + "\033[0;31m'%s'\033[0m\n", + attr.as_string ().c_str ()); + } } } return false; diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index ef2fb4e..2e9b88b 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -60,8 +60,9 @@ struct MacroExpander // should this be public or private? void expand_invoc (std::unique_ptr &invoc); - // Expands a single declarative macro. - AST::ASTFragment expand_decl_macro (AST::MacroInvocData &invoc, AST::MacroRulesDefinition &rules_def); + // Expands a single declarative macro. + AST::ASTFragment expand_decl_macro (AST::MacroInvocData &invoc, + AST::MacroRulesDefinition &rules_def); void expand_cfg_attrs (std::vector &attrs); bool fails_cfg (const std::vector &attr) const; diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 3bea914..cc30f1b 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -789,8 +789,8 @@ Parser::parse_attr_input () } // create actual LiteralExpr - AST::LiteralExpr lit_expr (t->get_str (), lit_type, t->get_type_hint (), {}, - t->get_locus ()); + AST::LiteralExpr lit_expr (t->get_str (), lit_type, t->get_type_hint (), + {}, t->get_locus ()); std::unique_ptr attr_input_lit ( new AST::AttrInputLiteral (std::move (lit_expr))); @@ -1554,8 +1554,9 @@ Parser::parse_macro_invocation_semi ( t = lexer.peek_token (); } - - AST::DelimTokenTree delim_tok_tree (delim_type, std::move (token_trees), tok_tree_locus); + + 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 @@ -1572,7 +1573,7 @@ Parser::parse_macro_invocation_semi ( { // as this is the end, allow recovery (probably) - may change return std::unique_ptr ( - new AST::MacroInvocationSemi (std::move (invoc_data), + new AST::MacroInvocationSemi (std::move (invoc_data), std::move (outer_attrs), macro_locus)); } @@ -1585,7 +1586,7 @@ Parser::parse_macro_invocation_semi ( lexer.peek_token ()->get_token_description ()); return std::unique_ptr ( - new AST::MacroInvocationSemi (std::move (invoc_data), + new AST::MacroInvocationSemi (std::move (invoc_data), std::move (outer_attrs), macro_locus)); } else @@ -1634,7 +1635,7 @@ Parser::parse_macro_invocation ( return std::unique_ptr ( new AST::MacroInvocation (AST::MacroInvocData (std::move (macro_path), - std::move (delim_tok_tree)), + std::move (delim_tok_tree)), std::move (outer_attrs), macro_locus)); } @@ -7373,7 +7374,8 @@ Parser::parse_literal_expr ( // create literal based on stuff in switch return std::unique_ptr ( new AST::LiteralExpr (std::move (literal_value), std::move (type), - t->get_type_hint (), std::move (outer_attrs), t->get_locus ())); + t->get_type_hint (), std::move (outer_attrs), + t->get_locus ())); } // Parses a return expression (including any expression to return). @@ -7401,8 +7403,8 @@ Parser::parse_return_expr ( // FIXME: ensure this doesn't ruin the middle of any expressions or anything return std::unique_ptr ( - new AST::ReturnExpr (std::move (returned_expr), - std::move (outer_attrs), locus)); + new AST::ReturnExpr (std::move (returned_expr), std::move (outer_attrs), + locus)); } /* Parses a break expression (including any label to break to AND any return @@ -7522,8 +7524,7 @@ Parser::parse_if_expr ( * parsed */ ParseRestrictions no_struct_expr; no_struct_expr.can_be_struct_expr = false; - std::unique_ptr condition - = parse_expr ({}, no_struct_expr); + std::unique_ptr condition = parse_expr ({}, no_struct_expr); if (condition == nullptr) { rust_error_at (lexer.peek_token ()->get_locus (), @@ -7548,7 +7549,8 @@ Parser::parse_if_expr ( { // single selection - end of if expression return std::unique_ptr ( - new AST::IfExpr (std::move (condition), std::move (if_body), std::move (outer_attrs), locus)); + new AST::IfExpr (std::move (condition), std::move (if_body), + std::move (outer_attrs), locus)); } else { @@ -7577,7 +7579,8 @@ Parser::parse_if_expr ( return std::unique_ptr ( new AST::IfExprConseqElse (std::move (condition), std::move (if_body), - std::move (else_body), std::move (outer_attrs), locus)); + std::move (else_body), + std::move (outer_attrs), locus)); } case IF: { // multiple selection - else if or else if let @@ -7599,7 +7602,8 @@ Parser::parse_if_expr ( return std::unique_ptr ( new AST::IfExprConseqIfLet (std::move (condition), std::move (if_body), - std::move (if_let_expr), std::move (outer_attrs), locus)); + std::move (if_let_expr), + std::move (outer_attrs), locus)); } else { @@ -7617,7 +7621,8 @@ Parser::parse_if_expr ( return std::unique_ptr ( new AST::IfExprConseqIf (std::move (condition), std::move (if_body), - std::move (if_expr), std::move (outer_attrs), locus)); + std::move (if_expr), + std::move (outer_attrs), locus)); } } default: @@ -7674,8 +7679,7 @@ Parser::parse_if_let_expr ( // parse expression (required) - HACK to prevent struct expr being parsed ParseRestrictions no_struct_expr; no_struct_expr.can_be_struct_expr = false; - std::unique_ptr scrutinee_expr - = parse_expr ({}, no_struct_expr); + std::unique_ptr scrutinee_expr = parse_expr ({}, no_struct_expr); if (scrutinee_expr == nullptr) { rust_error_at ( @@ -7735,7 +7739,8 @@ Parser::parse_if_let_expr ( new AST::IfLetExprConseqElse (std::move (match_arm_patterns), std::move (scrutinee_expr), std::move (if_let_body), - std::move (else_body), std::move (outer_attrs), locus)); + std::move (else_body), + std::move (outer_attrs), locus)); } case IF: { // multiple selection - else if or else if let @@ -7757,7 +7762,8 @@ Parser::parse_if_let_expr ( return std::unique_ptr ( new AST::IfLetExprConseqIfLet ( std::move (match_arm_patterns), std::move (scrutinee_expr), - std::move (if_let_body), std::move (if_let_expr), std::move (outer_attrs), locus)); + std::move (if_let_body), std::move (if_let_expr), + std::move (outer_attrs), locus)); } else { @@ -7776,7 +7782,8 @@ Parser::parse_if_let_expr ( new AST::IfLetExprConseqIf (std::move (match_arm_patterns), std::move (scrutinee_expr), std::move (if_let_body), - std::move (if_expr), std::move (outer_attrs), locus)); + std::move (if_expr), + std::move (outer_attrs), locus)); } } default: @@ -8837,8 +8844,10 @@ Parser::parse_type () AST::DelimTokenTree tok_tree = parse_delim_token_tree (); return std::unique_ptr ( - new AST::MacroInvocation (AST::MacroInvocData (std::move (macro_path), - std::move (tok_tree)), {}, locus)); + new AST::MacroInvocation ( + AST::MacroInvocData (std::move (macro_path), + std::move (tok_tree)), + {}, locus)); } case PLUS: { // type param bounds @@ -9638,8 +9647,10 @@ Parser::parse_type_no_bounds () AST::DelimTokenTree tok_tree = parse_delim_token_tree (); return std::unique_ptr ( - new AST::MacroInvocation (AST::MacroInvocData (std::move (macro_path), - std::move (tok_tree)), {}, 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 @@ -11411,9 +11422,10 @@ Parser::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), + 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)); + AST::MacroInvocData invoc_data (std::move (macro_path), + std::move (delim_tok_tree)); if (lexer.peek_token ()->get_id () == SEMICOLON) { @@ -11436,13 +11448,12 @@ Parser::parse_path_based_stmt_or_expr ( else { // tokens don't match opening delimiters, so produce error - rust_error_at (t2->get_locus (), - "unexpected token %qs - expecting closing delimiter %qs (for a " - "macro invocation)", - t2->get_token_description (), - (type == AST::PARENS - ? ")" - : (type == AST::SQUARE ? "]" : "}"))); + rust_error_at ( + t2->get_locus (), + "unexpected token %qs - expecting closing delimiter %qs (for a " + "macro invocation)", + t2->get_token_description (), + (type == AST::PARENS ? ")" : (type == AST::SQUARE ? "]" : "}"))); return ExprOrStmt::create_error (); } } @@ -11711,14 +11722,15 @@ Parser::parse_macro_invocation_maybe_semi ( 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)); + 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 stmt ( - new AST::MacroInvocationSemi (std::move (invoc_data), + new AST::MacroInvocationSemi (std::move (invoc_data), std::move (outer_attrs), macro_locus)); return ExprOrStmt (std::move (stmt)); @@ -12078,7 +12090,8 @@ Parser::null_denotation ( /* HACK: may have to become permanent, but this is my current * identifier expression */ return std::unique_ptr ( - new AST::IdentifierExpr (tok->get_str (), {}, tok->get_locus ())); + new AST::IdentifierExpr (tok->get_str (), {}, + tok->get_locus ())); } // HACK: add outer attrs to path path.set_outer_attrs (std::move (outer_attrs)); @@ -12100,7 +12113,8 @@ Parser::null_denotation ( return std::unique_ptr ( new AST::QualifiedPathInExpression (std::move (path))); } - // FIXME: for literal exprs, should outer attrs be passed in or just ignored? + // FIXME: for literal exprs, should outer attrs be passed in or just + // ignored? case INT_LITERAL: // we should check the range, but ignore for now // encode as int? @@ -12162,8 +12176,7 @@ Parser::null_denotation ( ParseRestrictions entered_from_unary; entered_from_unary.entered_from_unary = true; std::unique_ptr expr - = parse_expr (LBP_UNARY_MINUS, {}, - entered_from_unary); + = parse_expr (LBP_UNARY_MINUS, {}, entered_from_unary); if (expr == nullptr) return nullptr; @@ -12187,8 +12200,7 @@ Parser::null_denotation ( ParseRestrictions entered_from_unary; entered_from_unary.entered_from_unary = true; std::unique_ptr expr - = parse_expr (LBP_UNARY_EXCLAM, {}, - entered_from_unary); + = parse_expr (LBP_UNARY_EXCLAM, {}, entered_from_unary); if (expr == nullptr) return nullptr; @@ -12214,8 +12226,7 @@ Parser::null_denotation ( entered_from_unary.entered_from_unary = true; entered_from_unary.can_be_struct_expr = false; std::unique_ptr expr - = parse_expr (LBP_UNARY_ASTERISK, {}, - entered_from_unary); + = parse_expr (LBP_UNARY_ASTERISK, {}, entered_from_unary); // FIXME: allow outer attributes on expression return std::unique_ptr ( new AST::DereferenceExpr (std::move (expr), std::move (outer_attrs), @@ -12235,15 +12246,12 @@ Parser::null_denotation ( if (lexer.peek_token ()->get_id () == MUT) { lexer.skip_token (); - expr - = parse_expr (LBP_UNARY_AMP_MUT, {}, - entered_from_unary); + expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary); is_mut_borrow = true; } else { - expr = parse_expr (LBP_UNARY_AMP, {}, - entered_from_unary); + expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary); } // FIXME: allow outer attributes on expression @@ -12262,15 +12270,12 @@ Parser::null_denotation ( if (lexer.peek_token ()->get_id () == MUT) { lexer.skip_token (); - expr - = parse_expr (LBP_UNARY_AMP_MUT, {}, - entered_from_unary); + expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary); is_mut_borrow = true; } else { - expr = parse_expr (LBP_UNARY_AMP, {}, - entered_from_unary); + expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary); } // FIXME: allow outer attributes on expression @@ -13939,7 +13944,8 @@ Parser::parse_macro_invocation_partial ( Location macro_locus = converted_path.get_locus (); return std::unique_ptr ( - new AST::MacroInvocation (AST::MacroInvocData (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)); } @@ -14106,7 +14112,7 @@ Parser::parse_struct_expr_tuple_partial ( exprs.push_back (std::move (expr)); if (lexer.peek_token ()->get_id () != COMMA) - break; + break; lexer.skip_token (); @@ -14233,7 +14239,8 @@ Parser::parse_path_in_expression_pratt (const_TokenPtr tok) "current token (just about to return path to null denotation): '%s'\n", lexer.peek_token ()->get_token_description ()); - return AST::PathInExpression (std::move (segments), {}, tok->get_locus (), false); + return AST::PathInExpression (std::move (segments), {}, tok->get_locus (), + false); } // Parses a closure expression with pratt parsing (from null denotation). diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index 1d31135..2d15a59 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -365,7 +365,7 @@ Session::enable_dump (std::string arg) Location (), "dumping all is not supported as of now. choose %, %, " "%, %, %, %," - " %, or %"); + " %, or %"); return false; } else if (arg == "lex") @@ -407,9 +407,11 @@ Session::enable_dump (std::string arg) } else if (arg == "") { - rust_error_at (Location (), "dump option was not given a name. choose " - "%, %, %, %, " - "%, %, %, or %"); + rust_error_at ( + Location (), + "dump option was not given a name. choose " + "%, %, %, %, " + "%, %, %, or %"); return false; } else @@ -417,8 +419,8 @@ Session::enable_dump (std::string arg) rust_error_at ( Location (), "dump option %qs was unrecognised. choose %, %, " - "%, %, %, %," - " %, or %", + "%, %, %, %," + " %, or %", arg.c_str ()); return false; } -- cgit v1.1 From cfd1d805ff5921480d9badd7d215e1b1deb33aca Mon Sep 17 00:00:00 2001 From: SimplyTheOther Date: Tue, 9 Feb 2021 16:40:23 +0800 Subject: Unified Tokens partially in preparation for macro expansion --- gcc/rust/ast/rust-ast-full-test.cc | 50 ++++++------- gcc/rust/ast/rust-ast.h | 141 ++++++++----------------------------- gcc/rust/ast/rust-macro.h | 12 +--- gcc/rust/lex/rust-token.h | 14 ---- gcc/rust/parse/rust-parse-impl.h | 25 +++---- 5 files changed, 63 insertions(+), 179 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index e1308b7..2c196a0 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -290,13 +290,17 @@ DelimTokenTree::as_string () const std::string Token::as_string () const { - /* FIXME: only works when not identifier or literal or whatever, i.e. when - * doesn't store string value */ - // return get_token_description(token_id); + if (tok_ref->has_str ()) + { + std::string str = tok_ref->get_str (); - // maybe fixed - stores everything as string though, so storage-inefficient - std::string quote = is_string_lit () ? "\"" : ""; - return quote + str + quote; + std::string quote = is_string_lit () ? "\"" : ""; + return quote + str + quote; + } + else + { + return tok_ref->get_token_description (); + } } std::string @@ -4375,9 +4379,10 @@ DelimTokenTree::to_token_stream () const std::vector > tokens; // simulate presence of delimiters + const_TokenPtr left_paren + = Rust::Token::make (LEFT_PAREN, Linemap::unknown_location ()); tokens.push_back ( - std::unique_ptr (new Token (LEFT_PAREN, Linemap::unknown_location (), - "", CORETYPE_UNKNOWN))); + std::unique_ptr (new Token (std::move (left_paren)))); for (const auto &tree : token_trees) { @@ -4387,9 +4392,10 @@ DelimTokenTree::to_token_stream () const std::make_move_iterator (stream.end ())); } - tokens.push_back (std::unique_ptr ( - new Token (RIGHT_PAREN, Linemap::unknown_location (), "", - CORETYPE_UNKNOWN))); + const_TokenPtr right_paren + = Rust::Token::make (RIGHT_PAREN, Linemap::unknown_location ()); + tokens.push_back ( + std::unique_ptr (new Token (std::move (right_paren)))); tokens.shrink_to_fit (); @@ -4545,8 +4551,7 @@ AttrInputMetaItemContainer::check_cfg_predicate (const Session &session) const } bool -MetaItemLitExpr::check_cfg_predicate ( - const Session &session ATTRIBUTE_UNUSED) const +MetaItemLitExpr::check_cfg_predicate (const Session &) const { /* as far as I can tell, a literal expr can never be a valid cfg body, so * false */ @@ -4653,10 +4658,7 @@ bool MetaListPaths::check_path_exists_in_cfg (const Session &session, const SimplePath &path) const { - auto it = session.options.target_data.features.find (path.as_string ()); - if (it != session.options.target_data.features.end ()) - return true; - return false; + return session.options.target_data.has_key (path.as_string ()); } bool @@ -4710,10 +4712,7 @@ MetaItemSeq::check_cfg_predicate (const Session &session) const bool MetaWord::check_cfg_predicate (const Session &session) const { - auto it = session.options.target_data.features.find (ident); - if (it != session.options.target_data.features.end ()) - return true; - return false; + return session.options.target_data.has_key (ident); } bool @@ -4723,10 +4722,7 @@ MetaItemPath::check_cfg_predicate (const Session &session) const * relating to SimplePath being identifier. Currently, it would return true * if path as identifier existed, and if the path in string form existed * (though this shouldn't occur). */ - auto it = session.options.target_data.features.find (path.as_string ()); - if (it != session.options.target_data.features.end ()) - return true; - return false; + return session.options.target_data.has_key (path.as_string ()); } bool @@ -4853,8 +4849,8 @@ AttrInputMetaItemContainer::separate_cfg_attrs () const Attribute attr = (*it)->to_attribute (); if (attr.is_empty ()) { - // TODO should this be an error that causes us to chuck out - // everything? + /* TODO should this be an error that causes us to chuck out + * everything? */ continue; } attrs.push_back (std::move (attr)); diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index b80e7a3..14816e2 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -129,6 +129,7 @@ class Token : public TokenTree, public MacroMatch { // A token is a kind of token tree (except delimiter tokens) // A token is a kind of MacroMatch (except $ and delimiter tokens) +#if 0 // TODO: improve member variables - current ones are the same as lexer token // Token kind. TokenId token_id; @@ -138,6 +139,13 @@ class Token : public TokenTree, public MacroMatch std::string str; // Token type hint (if any). PrimitiveCoreType type_hint; +#endif + + const_TokenPtr tok_ref; + + /* new idea: wrapper around const_TokenPtr used for heterogeneuous storage in + * token trees. rather than convert back and forth when parsing macros, just + * wrap it. */ public: // Unique pointer custom clone function @@ -146,6 +154,7 @@ public: return std::unique_ptr (clone_token_impl ()); } +#if 0 /* constructor from general text - avoid using if lexer const_TokenPtr is * available */ Token (TokenId token_id, Location locus, std::string str, @@ -153,8 +162,11 @@ public: : token_id (token_id), locus (locus), str (std::move (str)), type_hint (type_hint) {} +#endif + // not doable with new implementation - will have to make a const_TokenPtr // Constructor from lexer const_TokenPtr +#if 0 /* TODO: find workaround for std::string being nullptr - probably have to * introduce new method in lexer Token, or maybe make conversion method * there */ @@ -188,10 +200,12 @@ public: lexer_token_ptr->get_token_description ()); } } +#endif + Token (const_TokenPtr lexer_tok_ptr) : tok_ref (std::move (lexer_tok_ptr)) {} bool is_string_lit () const { - switch (token_id) + switch (get_id ()) { case STRING_LITERAL: case BYTE_STRING_LITERAL: @@ -208,11 +222,14 @@ public: // Return copy of itself but in token stream form. std::vector > to_token_stream () const override; - TokenId get_id () const { return token_id; } + TokenId get_id () const { return tok_ref->get_id (); } - Location get_locus () const { return locus; } + Location get_locus () const { return tok_ref->get_locus (); } - PrimitiveCoreType get_type_hint () const { return type_hint; } + PrimitiveCoreType get_type_hint () const { return tok_ref->get_type_hint (); } + + // Get a new token pointer copy. + const_TokenPtr get_tok_ptr () const { return tok_ref; } protected: // No virtual for now as not polymorphic but can be in future @@ -1190,15 +1207,8 @@ protected: class LifetimeParam : public GenericParam { Lifetime lifetime; - - // bool has_lifetime_bounds; - // LifetimeBounds lifetime_bounds; - std::vector lifetime_bounds; // inlined LifetimeBounds - - // bool has_outer_attribute; - // std::unique_ptr outer_attr; + std::vector lifetime_bounds; Attribute outer_attr; - Location locus; public: @@ -1211,45 +1221,21 @@ public: // Creates an error state lifetime param. static LifetimeParam create_error () { - return LifetimeParam (Lifetime::error ()); + return LifetimeParam (Lifetime::error (), {}, Attribute::create_empty (), Location ()); } // Returns whether the lifetime param is in an error state. bool is_error () const { return lifetime.is_error (); } // Constructor - LifetimeParam (Lifetime lifetime, Location locus = Location (), - std::vector lifetime_bounds - = std::vector (), - Attribute outer_attr = Attribute::create_empty ()) + LifetimeParam (Lifetime lifetime, + std::vector lifetime_bounds, + Attribute outer_attr, Location locus) : lifetime (std::move (lifetime)), lifetime_bounds (std::move (lifetime_bounds)), outer_attr (std::move (outer_attr)), locus (locus) {} - // TODO: remove copy and assignment operator definitions - not required - - // Copy constructor with clone - LifetimeParam (LifetimeParam const &other) - : lifetime (other.lifetime), lifetime_bounds (other.lifetime_bounds), - outer_attr (other.outer_attr), locus (other.locus) - {} - - // Overloaded assignment operator to clone attribute - LifetimeParam &operator= (LifetimeParam const &other) - { - lifetime = other.lifetime; - lifetime_bounds = other.lifetime_bounds; - outer_attr = other.outer_attr; - locus = other.locus; - - return *this; - } - - // move constructors - LifetimeParam (LifetimeParam &&other) = default; - LifetimeParam &operator= (LifetimeParam &&other) = default; - std::string as_string () const override; void accept_vis (ASTVisitor &vis) override; @@ -1263,28 +1249,13 @@ protected: } }; -// A macro item AST node - potentially abstract base class +// A macro item AST node - abstract base class class MacroItem : public Item -{ - /*public: - std::string as_string() const;*/ - // std::vector outer_attrs; - -protected: - /*MacroItem (std::vector outer_attribs) - : outer_attrs (std::move (outer_attribs)) - {}*/ -}; +{}; // Item used in trait declarations - abstract base class class TraitItem { - // bool has_outer_attrs; - // TODO: remove and rely on virtual functions and VisItem-derived attributes? - // std::vector outer_attrs; - - // NOTE: all children should have outer attributes - protected: // Clone function implementation as pure virtual method virtual TraitItem *clone_trait_item_impl () const = 0; @@ -1468,69 +1439,18 @@ 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, - 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), - TraitImplItem (other), outer_attrs (other.outer_attrs), path (other.path), - delim_type (other.delim_type), locus (other.locus) - { - 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) - { - MacroItem::operator= (other); - TraitItem::operator= (other); - InherentImplItem::operator= (other); - TraitImplItem::operator= (other); - outer_attrs = other.outer_attrs; - path = other.path; - delim_type = other.delim_type; - locus = other.locus; - - token_trees.reserve (other.token_trees.size ()); - for (const auto &e : other.token_trees) - 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; // Clones this macro invocation semi. @@ -1540,11 +1460,6 @@ 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 { diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 29a1cf1..51220e4 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -361,19 +361,12 @@ class MacroInvocation : public TypeNoBounds, public ExprWithoutBlock { std::vector outer_attrs; - /*SimplePath path; - DelimTokenTree token_tree;*/ MacroInvocData invoc_data; Location locus; public: std::string as_string () const override; - /*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) : outer_attrs (std::move (outer_attrs)), @@ -694,11 +687,8 @@ public: struct MacroParser { private: + // TODO: might as well rewrite to use lexer tokens std::vector > token_stream; - /* probably have to make this mutable (mutable int stream_pos) otherwise const - * has to be removed up to DelimTokenTree or further ok since this changing - * would have an effect on the results of the methods run (i.e. not logically - * const), the parsing methods shouldn't be const */ int stream_pos; public: diff --git a/gcc/rust/lex/rust-token.h b/gcc/rust/lex/rust-token.h index fab736a..6070244 100644 --- a/gcc/rust/lex/rust-token.h +++ b/gcc/rust/lex/rust-token.h @@ -318,13 +318,6 @@ public: } // Makes and returns a new TokenPtr of type INT_LITERAL. - /*static TokenPtr make_int (Location locus, const std::string &str) - { - //return TokenPtr (new Token (INT_LITERAL, locus, str)); - return std::make_shared(INT_LITERAL, locus, str); - }*/ - - // Makes and returns a new TokenPtr of type INT_LITERAL. static TokenPtr make_int (Location locus, const std::string &str, PrimitiveCoreType type_hint = CORETYPE_UNKNOWN) { @@ -333,13 +326,6 @@ public: } // Makes and returns a new TokenPtr of type FLOAT_LITERAL. - /*static TokenPtr make_float (Location locus, const std::string &str) - { - return TokenPtr (new Token (FLOAT_LITERAL, locus, str)); - return std::make_shared(FLOAT_LITERAL, locus, str); - }*/ - - // Makes and returns a new TokenPtr of type FLOAT_LITERAL. static TokenPtr make_float (Location locus, const std::string &str, PrimitiveCoreType type_hint = CORETYPE_UNKNOWN) { diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index cc30f1b..4a9f3a3 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -972,8 +972,7 @@ Parser::parse_token_tree () default: // parse token itself as TokenTree lexer.skip_token (); - // TODO: fix that token constructor, possibly with c++11 features - return std::unique_ptr (new AST::Token (t)); + return std::unique_ptr (new AST::Token (std::move (t))); } } @@ -1805,7 +1804,7 @@ Parser::parse_macro_match () default: // just the token lexer.skip_token (); - return std::unique_ptr (new AST::Token (t)); + return std::unique_ptr (new AST::Token (std::move (t))); } } @@ -1913,7 +1912,7 @@ Parser::parse_macro_match_repetition () break; default: // separator does exist - separator = std::unique_ptr (new AST::Token (t)); + separator = std::unique_ptr (new AST::Token (std::move (t))); lexer.skip_token (); break; } @@ -2665,9 +2664,9 @@ Parser::parse_generic_params () } std::unique_ptr param ( - new AST::LifetimeParam (std::move (lifetime), locus, + new AST::LifetimeParam (std::move (lifetime), std::move (lifetime_bounds), - std::move (outer_attr))); + std::move (outer_attr), locus)); generic_params.push_back (std::move (param)); if (lexer.peek_token ()->get_id () != COMMA) @@ -2841,9 +2840,9 @@ Parser::parse_generic_params (EndTokenPred is_end_token) } std::unique_ptr param ( - new AST::LifetimeParam (std::move (lifetime), locus, + new AST::LifetimeParam (std::move (lifetime), std::move (lifetime_bounds), - std::move (outer_attr))); + std::move (outer_attr), locus)); generic_params.push_back (std::move (param)); if (lexer.peek_token ()->get_id () != COMMA) @@ -3166,9 +3165,9 @@ Parser::parse_lifetime_param () // TODO: have end token passed in? } - return AST::LifetimeParam (std::move (lifetime), lifetime_tok->get_locus (), + return AST::LifetimeParam (std::move (lifetime), std::move (lifetime_bounds), - std::move (outer_attr)); + std::move (outer_attr), lifetime_tok->get_locus ()); } // Parses type generic parameters. Will also consume any trailing comma. @@ -3693,13 +3692,11 @@ Parser::parse_trait_bound () lexer.skip_token (); } - // parse for lifetimes, if it exists (although empty for lifetimes is ok to - // handle this) + /* parse for lifetimes, if it exists (although empty for lifetimes is ok to + * handle this) */ std::vector for_lifetimes; if (lexer.peek_token ()->get_id () == FOR) - { for_lifetimes = parse_for_lifetimes (); - } // handle TypePath AST::TypePath type_path = parse_type_path (); -- cgit v1.1