Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #include "rust-ast.h"
20 : #include "rust-macro-expand.h"
21 : #include "rust-macro.h"
22 :
23 : namespace Rust {
24 : class SubstituteCtx
25 : {
26 : std::vector<std::unique_ptr<AST::Token>> &input;
27 : std::vector<std::unique_ptr<AST::Token>> ¯o;
28 : std::map<std::string, MatchedFragmentContainer *> &fragments;
29 : AST::MacroRulesDefinition &definition;
30 : // Macro invocation location
31 : location_t origin;
32 :
33 : /**
34 : * Find the repetition amount to use when expanding a repetition, and
35 : * check that all fragments used respect that repetition amount
36 : *
37 : * @param pattern_start Start of the repetition pattern
38 : * @param pattern_end End of the repetition pattern
39 : * @param repeat_amount Reference to fill with the matched repetition amount
40 : */
41 : bool check_repetition_amount (size_t pattern_start, size_t pattern_end,
42 : size_t &repeat_amount);
43 :
44 : public:
45 6529 : SubstituteCtx (std::vector<std::unique_ptr<AST::Token>> &input,
46 : std::vector<std::unique_ptr<AST::Token>> ¯o,
47 : std::map<std::string, MatchedFragmentContainer *> &fragments,
48 : AST::MacroRulesDefinition &definition, location_t origin)
49 6529 : : input (input), macro (macro), fragments (fragments),
50 6529 : definition (definition), origin (origin)
51 : {}
52 :
53 : /**
54 : * Special-case the $crate metavar to expand to the name of the crate in which
55 : * the macro was defined.
56 : *
57 : * https://doc.rust-lang.org/reference/macros-by-example.html#r-macro.decl.hygiene.crate
58 : *
59 : *
60 : * @param expanded Reference to a vector upon which expanded tokens will be
61 : * pushed
62 : *
63 : * @return True if the substitution succeeded
64 : */
65 : bool
66 : substitute_dollar_crate (std::vector<std::unique_ptr<AST::Token>> &expanded);
67 :
68 : /**
69 : * Substitute a metavariable by its given fragment in a transcribing context,
70 : * i.e. replacing $var with the associated fragment.
71 : *
72 : * @param metavar Metavariable to try and replace
73 : * @param expanded Reference to a vector upon which expanded tokens will be
74 : * pushed
75 : *
76 : * @return True if the substitution succeeded
77 : */
78 : bool substitute_metavar (std::unique_ptr<AST::Token> &metavar,
79 : std::vector<std::unique_ptr<AST::Token>> &expanded);
80 :
81 : /**
82 : * Substitute a macro repetition by its given fragments
83 : *
84 : * @param pattern_start Start index of the pattern tokens
85 : * @param pattern_end End index of the patterns tokens
86 : * @param separator Optional separator to include when expanding tokens
87 : *
88 : * @return A vector containing the repeated pattern
89 : */
90 : std::vector<std::unique_ptr<AST::Token>>
91 : substitute_repetition (size_t pattern_start, size_t pattern_end,
92 : std::unique_ptr<AST::Token> separator);
93 :
94 : /**
95 : * Substitute a given token by its appropriate representation
96 : *
97 : * @param token_idx Current token to try and substitute
98 : *
99 : * @return A token containing the associated fragment expanded into tokens if
100 : * any, or the cloned token if no fragment was associated, as well as the
101 : * amount of tokens that should be skipped before the next invocation. Since
102 : * this function may consume more than just one token, it is important to skip
103 : * ahead of the input to avoid mis-substitutions
104 : */
105 : std::pair<std::vector<std::unique_ptr<AST::Token>>, size_t>
106 : substitute_token (size_t token_idx);
107 :
108 : /**
109 : * Substitute all tokens by their appropriate representation
110 : *
111 : * @return A vector containing the substituted tokens
112 : */
113 : std::vector<std::unique_ptr<AST::Token>> substitute_tokens ();
114 : };
115 : } // namespace Rust
|