Debian Codegraph @

gcc/rust/expand/rust-macro-substitute-ctx.h

raw
1 // Copyright (C) 2020-2025 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>> &macro;
28 std::map<std::string, MatchedFragmentContainer *> &fragments;
29 AST::MacroRulesDefinition &definition;
30
31 /**
32 * Find the repetition amount to use when expanding a repetition, and
33 * check that all fragments used respect that repetition amount
34 *
35 * @param pattern_start Start of the repetition pattern
36 * @param pattern_end End of the repetition pattern
37 * @param repeat_amount Reference to fill with the matched repetition amount
38 */
39 bool check_repetition_amount (size_t pattern_start, size_t pattern_end,
40 size_t &repeat_amount);
41
42 public:
43 SubstituteCtx (std::vector<std::unique_ptr<AST::Token>> &input,
44 std::vector<std::unique_ptr<AST::Token>> &macro,
45 std::map<std::string, MatchedFragmentContainer *> &fragments,
46 AST::MacroRulesDefinition &definition)
47 : input (input), macro (macro), fragments (fragments),
48 definition (definition)
49 {}
50
51 /**
52 * Special-case the $crate metavar to expand to the name of the crate in which
53 * the macro was defined.
54 *
55 * https://doc.rust-lang.org/reference/macros-by-example.html#r-macro.decl.hygiene.crate
56 *
57 *
58 * @param expanded Reference to a vector upon which expanded tokens will be
59 * pushed
60 *
61 * @return True if the substitution succeeded
62 */
63 bool
64 substitute_dollar_crate (std::vector<std::unique_ptr<AST::Token>> &expanded);
65
66 /**
67 * Substitute a metavariable by its given fragment in a transcribing context,
68 * i.e. replacing $var with the associated fragment.
69 *
70 * @param metavar Metavariable to try and replace
71 * @param expanded Reference to a vector upon which expanded tokens will be
72 * pushed
73 *
74 * @return True if the substitution succeeded
75 */
76 bool substitute_metavar (std::unique_ptr<AST::Token> &metavar,
77 std::vector<std::unique_ptr<AST::Token>> &expanded);
78
79 /**
80 * Substitute a macro repetition by its given fragments
81 *
82 * @param pattern_start Start index of the pattern tokens
83 * @param pattern_end End index of the patterns tokens
84 * @param separator Optional separator to include when expanding tokens
85 *
86 * @return A vector containing the repeated pattern
87 */
88 std::vector<std::unique_ptr<AST::Token>>
89 substitute_repetition (size_t pattern_start, size_t pattern_end,
90 std::unique_ptr<AST::Token> separator);
91
92 /**
93 * Substitute a given token by its appropriate representation
94 *
95 * @param token_idx Current token to try and substitute
96 *
97 * @return A token containing the associated fragment expanded into tokens if
98 * any, or the cloned token if no fragment was associated, as well as the
99 * amount of tokens that should be skipped before the next invocation. Since
100 * this function may consume more than just one token, it is important to skip
101 * ahead of the input to avoid mis-substitutions
102 */
103 std::pair<std::vector<std::unique_ptr<AST::Token>>, size_t>
104 substitute_token (size_t token_idx);
105
106 /**
107 * Substitute all tokens by their appropriate representation
108 *
109 * @return A vector containing the substituted tokens
110 */
111 std::vector<std::unique_ptr<AST::Token>> substitute_tokens ();
112 };
113 } // namespace Rust