diff options
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-macro.h | 1 | ||||
-rw-r--r-- | gcc/rust/expand/rust-macro-expand.cc | 21 | ||||
-rw-r--r-- | gcc/rust/expand/rust-macro-expand.h | 7 | ||||
-rw-r--r-- | gcc/rust/expand/rust-macro-substitute-ctx.cc | 44 | ||||
-rw-r--r-- | gcc/rust/expand/rust-macro-substitute-ctx.h | 6 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/macro9.rs | 17 | ||||
-rw-r--r-- | gcc/testsuite/rust/execute/torture/macros19.rs | 14 | ||||
-rw-r--r-- | gcc/testsuite/rust/execute/torture/macros20.rs | 14 |
8 files changed, 103 insertions, 21 deletions
diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 2b39624..d625ad2d 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -206,6 +206,7 @@ public: } MacroRepOp get_op () const { return op; } + const std::unique_ptr<MacroRepSep> &get_sep () const { return sep; } std::vector<std::unique_ptr<MacroMatch> > &get_matches () { return matches; } protected: diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 26f584d..b15e783 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -3598,12 +3598,13 @@ MacroExpander::match_token (Parser<MacroInvocLexer> &parser, AST::Token &token) } bool -MacroExpander::match_n_matches ( - Parser<MacroInvocLexer> &parser, - std::vector<std::unique_ptr<AST::MacroMatch>> &matches, size_t &match_amount, - size_t lo_bound, size_t hi_bound) +MacroExpander::match_n_matches (Parser<MacroInvocLexer> &parser, + AST::MacroMatchRepetition &rep, + size_t &match_amount, size_t lo_bound, + size_t hi_bound) { match_amount = 0; + auto &matches = rep.get_matches (); const MacroInvocLexer &source = parser.get_token_source (); while (true) @@ -3614,6 +3615,12 @@ MacroExpander::match_n_matches ( if (t_id == RIGHT_PAREN || t_id == RIGHT_SQUARE || t_id == RIGHT_CURLY) break; + // Skip parsing a separator on the first match, otherwise consume it. + // If it isn't present, this is an error + if (rep.has_sep () && match_amount > 0) + if (!match_token (parser, *rep.get_sep ())) + break; + bool valid_current_match = false; for (auto &match : matches) { @@ -3688,17 +3695,17 @@ MacroExpander::match_repetition (Parser<MacroInvocLexer> &parser, case AST::MacroMatchRepetition::MacroRepOp::ANY: lo_str = "0"; hi_str = "+inf"; - res = match_n_matches (parser, rep.get_matches (), match_amount); + res = match_n_matches (parser, rep, match_amount); break; case AST::MacroMatchRepetition::MacroRepOp::ONE_OR_MORE: lo_str = "1"; hi_str = "+inf"; - res = match_n_matches (parser, rep.get_matches (), match_amount, 1); + res = match_n_matches (parser, rep, match_amount, 1); break; case AST::MacroMatchRepetition::MacroRepOp::ZERO_OR_ONE: lo_str = "0"; hi_str = "1"; - res = match_n_matches (parser, rep.get_matches (), match_amount, 0, 1); + res = match_n_matches (parser, rep, match_amount, 0, 1); break; default: gcc_unreachable (); diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index c56ece6..88d0e6e 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -192,7 +192,7 @@ struct MacroExpander * Match any amount of matches * * @param parser Parser to use for matching - * @param matches All consecutive matches to identify + * @param rep Repetition to try and match * @param match_amount Reference in which to store the ammount of succesful * and valid matches * @@ -209,9 +209,8 @@ struct MacroExpander * otherwise */ bool match_n_matches (Parser<MacroInvocLexer> &parser, - std::vector<std::unique_ptr<AST::MacroMatch>> &matches, - size_t &match_amount, size_t lo_bound = 0, - size_t hi_bound = 0); + AST::MacroMatchRepetition &rep, size_t &match_amount, + size_t lo_bound = 0, size_t hi_bound = 0); void push_context (ContextType t) { context.push_back (t); } diff --git a/gcc/rust/expand/rust-macro-substitute-ctx.cc b/gcc/rust/expand/rust-macro-substitute-ctx.cc index 3d07b48..f9f2005 100644 --- a/gcc/rust/expand/rust-macro-substitute-ctx.cc +++ b/gcc/rust/expand/rust-macro-substitute-ctx.cc @@ -33,7 +33,9 @@ SubstituteCtx::substitute_metavar (std::unique_ptr<AST::Token> &metavar) } std::vector<std::unique_ptr<AST::Token>> -SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end) +SubstituteCtx::substitute_repetition ( + size_t pattern_start, size_t pattern_end, + std::unique_ptr<AST::Token> separator_token) { rust_assert (pattern_end < macro.size ()); @@ -117,6 +119,11 @@ SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end) auto substitute_context = SubstituteCtx (input, new_macro, sub_map); auto new_tokens = substitute_context.substitute_tokens (); + // Skip the first repetition, but add the separator to the expanded + // tokens if it is present + if (i != 0 && separator_token) + expanded.emplace_back (separator_token->clone_token ()); + for (auto &new_token : new_tokens) expanded.emplace_back (new_token->clone_token ()); } @@ -127,6 +134,13 @@ SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end) return expanded; } +static bool +is_rep_op (std::unique_ptr<AST::Token> &tok) +{ + auto id = tok->get_id (); + return id == QUESTION_MARK || id == ASTERISK || id == PLUS; +} + std::pair<std::vector<std::unique_ptr<AST::Token>>, size_t> SubstituteCtx::substitute_token (size_t token_idx) { @@ -148,20 +162,34 @@ SubstituteCtx::substitute_token (size_t token_idx) pattern_end++) ; + std::unique_ptr<AST::Token> separator_token = nullptr; + // FIXME: Can this go out of bounds? + auto &post_pattern_token = macro.at (pattern_end + 1); + if (!is_rep_op (post_pattern_token)) + separator_token = post_pattern_token->clone_token (); + + // Amount of tokens to skip + auto to_skip = 0; + // Parentheses + to_skip += 2; + // Repetition operator + to_skip += 1; + // Separator + if (separator_token) + to_skip += 1; + // FIXME: This skips whitespaces... Is that okay?? - // FIXME: Is there any existing parsing function that allows us to parse - // a macro pattern? + // FIXME: Is there any existing parsing function that allows us to + // parse a macro pattern? // FIXME: Add error handling in the case we haven't found a matching // closing delimiter // FIXME: We need to parse the repetition token now - return { - substitute_repetition (pattern_start, pattern_end), - // + 2 for the opening and closing parentheses which are mandatory - // + 1 for the repetitor (+, *, ?) - pattern_end - pattern_start + 3}; + return {substitute_repetition (pattern_start, pattern_end, + std::move (separator_token)), + pattern_end - pattern_start + to_skip}; } // TODO: We need to check if the $ was alone. In that case, do // not error out: Simply act as if there was an empty identifier diff --git a/gcc/rust/expand/rust-macro-substitute-ctx.h b/gcc/rust/expand/rust-macro-substitute-ctx.h index d51fb81..ed83926 100644 --- a/gcc/rust/expand/rust-macro-substitute-ctx.h +++ b/gcc/rust/expand/rust-macro-substitute-ctx.h @@ -49,12 +49,14 @@ public: * Substitute a macro repetition by its given fragments * * @param pattern_start Start index of the pattern tokens - * @param pattern_end Index Amount of tokens in the pattern + * @param pattern_end End index of the patterns tokens + * @param separator Optional separator to include when expanding tokens * * @return A vector containing the repeated pattern */ std::vector<std::unique_ptr<AST::Token>> - substitute_repetition (size_t pattern_start, size_t pattern_end); + substitute_repetition (size_t pattern_start, size_t pattern_end, + std::unique_ptr<AST::Token> separator); /** * Substitute a given token by its appropriate representation diff --git a/gcc/testsuite/rust/compile/macro9.rs b/gcc/testsuite/rust/compile/macro9.rs new file mode 100644 index 0000000..9a59089 --- /dev/null +++ b/gcc/testsuite/rust/compile/macro9.rs @@ -0,0 +1,17 @@ +macro_rules! add { + ($e:expr, $($es:expr),*) => { + $e + add!($($es),*) + }; + ($e:expr) => { + $e + }; +} + +fn main() -> i32 { + let a = add!(15 2 9); // { dg-error "Failed to match any rule within macro" } + let b = add!(15); + let b = add!(15 14); // { dg-error "Failed to match any rule within macro" } + let b = add!(15, 14,); // { dg-error "Failed to match any rule within macro" } + + 0 +} diff --git a/gcc/testsuite/rust/execute/torture/macros19.rs b/gcc/testsuite/rust/execute/torture/macros19.rs new file mode 100644 index 0000000..4732545 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/macros19.rs @@ -0,0 +1,14 @@ +macro_rules! add { + ($e:expr, $($es:expr),*) => { + $e + add!($($es),*) + }; + ($e:expr) => { + $e + }; +} + +fn main() -> i32 { + let a = add!(15, 2, 9); // 26 + + a - 26 +} diff --git a/gcc/testsuite/rust/execute/torture/macros20.rs b/gcc/testsuite/rust/execute/torture/macros20.rs new file mode 100644 index 0000000..fc116d0 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/macros20.rs @@ -0,0 +1,14 @@ +macro_rules! add { + ($e:expr big_tok $($es:expr) big_tok *) => { + $e + add!($($es) big_tok *) + }; + ($e:expr) => { + $e + }; +} + +fn main() -> i32 { + let a = add!(15 big_tok 2 big_tok 9); // 26 + + a - 26 +} |