diff options
author | SimplyTheOther <simplytheother@gmail.com> | 2021-01-08 15:08:14 +0800 |
---|---|---|
committer | SimplyTheOther <simplytheother@gmail.com> | 2021-01-08 15:08:14 +0800 |
commit | b5f86dca7e6b53ca3701ef01ae36070a760dff78 (patch) | |
tree | 9068741ba3ed05bafa7ad98989881198618e28b1 /gcc | |
parent | faf78e75e4ec3c989e452d47dc37a0be1706bf08 (diff) | |
download | gcc-b5f86dca7e6b53ca3701ef01ae36070a760dff78.zip gcc-b5f86dca7e6b53ca3701ef01ae36070a760dff78.tar.gz gcc-b5f86dca7e6b53ca3701ef01ae36070a760dff78.tar.bz2 |
Added final to some overriden methods for optimisation, planned macro parsing
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-ast.h | 26 | ||||
-rw-r--r-- | gcc/rust/ast/rust-expr.h | 61 | ||||
-rw-r--r-- | gcc/rust/ast/rust-item.h | 12 | ||||
-rw-r--r-- | gcc/rust/ast/rust-macro.h | 21 | ||||
-rw-r--r-- | gcc/rust/ast/rust-path.h | 26 | ||||
-rw-r--r-- | gcc/rust/expand/rust-macro-expand.cc | 31 |
6 files changed, 113 insertions, 64 deletions
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<Attribute> inner_attrs; std::unique_ptr<Expr> 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<ClosureParam> &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<AST::Attribute> &attrs) const void MacroExpander::expand_cfg_attrs (std::vector<AST::Attribute> &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") |