Debian Codegraph @

gcc/rust/expand/rust-macro-builtins.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 #ifndef RUST_MACRO_BUILTINS_H
20 #define RUST_MACRO_BUILTINS_H
21
22 #include "rust-ast.h"
23 #include "rust-builtin-ast-nodes.h"
24 #include "rust-ast-fragment.h"
25 #include "rust-location.h"
26 #include "bi-map.h"
27
28 namespace Rust {
29
30 // FIXME: Add a BuiltinMacro class which contains a name (or should it?), a
31 // transcriber and extra info if necessary
32 // then make a global map<string, BuiltinMacro>
33
34 //
35 // All builtin macros possible
36 //
37 enum class BuiltinMacro
38 {
39 Assert,
40 File,
41 Line,
42 Column,
43 IncludeBytes,
44 IncludeStr,
45 Stringify,
46 CompileError,
47 Concat,
48 Env,
49 OptionEnv,
50 Cfg,
51 Include,
52 FormatArgs,
53 FormatArgsNl,
54 ConcatIdents,
55 ModulePath,
56 Asm,
57 LlvmAsm,
58 GlobalAsm,
59 LogSyntax,
60 TraceMacros,
61 Test,
62 Bench,
63 TestCase,
64 GlobalAllocator,
65 CfgAccessible,
66 RustcDecodable,
67 RustcEncodable,
68 Clone,
69 Copy,
70 Debug,
71 Default,
72 Eq,
73 PartialEq,
74 Ord,
75 PartialOrd,
76 Hash,
77 };
78
79 tl::optional<BuiltinMacro>
80 builtin_macro_from_string (const std::string &identifier);
81
82 //
83 // This class provides a list of builtin macros implemented by the compiler.
84 // The functions defined are called "builtin transcribers" in that they
85 // replace the transcribing part of a macro definition.
86 //
87 // Like regular macro transcribers, they are responsible for building and
88 // returning an AST fragment: basically a vector of AST nodes put together.
89 //
90 // Unlike regular declarative macros where each match arm has its own
91 // associated transcriber, builtin transcribers are responsible for handling
92 // all match arms of the macro. This means that you should take extra care
93 // when implementing a builtin containing multiple match arms: You will
94 // probably need to do some lookahead in order to determine which match arm
95 // the user intended to use.
96 //
97 // An example of this is the `assert!()` macro:
98 //
99 // ```
100 // macro_rules! assert {
101 // ($cond:expr $(,)?) => {{ ... }};
102 // ($cond : expr, $ ($arg : tt) +) = > {{ ... }};
103 // }
104 // ```
105 //
106 // If more tokens exist beyond the optional comma, they need to be handled as
107 // a token-tree for a custom panic message.
108 //
109 // These builtin macros with empty transcribers are defined in the standard
110 // library. They are marked with a special attribute,
111 // `#[rustc_builtin_macro]`. When this attribute is present on a macro
112 // definition, the compiler should look for an associated transcriber in the
113 // mappings. Meaning that you must remember to insert your transcriber in the
114 // `builtin_macros` map of the `Mappings`.
115 //
116 // This map is built as a static variable in the `insert_macro_def()` method
117 // of the `Mappings` class.
118
119 class MacroBuiltin
120 {
121 public:
122 static const BiMap<std::string, BuiltinMacro> builtins;
123 static std::unordered_map<std::string, AST::MacroTranscriberFunc>
124 builtin_transcribers;
125
126 static tl::optional<AST::Fragment> assert_handler (location_t invoc_locus,
127 AST::MacroInvocData &invoc,
128 AST::InvocKind semicolon);
129
130 static tl::optional<AST::Fragment> file_handler (location_t invoc_locus,
131 AST::MacroInvocData &invoc,
132 AST::InvocKind semicolon);
133
134 static tl::optional<AST::Fragment> column_handler (location_t invoc_locus,
135 AST::MacroInvocData &invoc,
136 AST::InvocKind semicolon);
137
138 static tl::optional<AST::Fragment>
139 include_bytes_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
140 AST::InvocKind semicolon);
141
142 static tl::optional<AST::Fragment>
143 include_str_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
144 AST::InvocKind semicolon);
145
146 static tl::optional<AST::Fragment>
147 stringify_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
148 AST::InvocKind semicolon);
149
150 static tl::optional<AST::Fragment>
151 compile_error_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
152 AST::InvocKind semicolon);
153
154 static tl::optional<AST::Fragment> concat_handler (location_t invoc_locus,
155 AST::MacroInvocData &invoc,
156 AST::InvocKind semicolon);
157
158 static tl::optional<AST::Fragment> env_handler (location_t invoc_locus,
159 AST::MacroInvocData &invoc,
160 AST::InvocKind semicolon);
161
162 static tl::optional<AST::Fragment>
163 option_env_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
164 AST::InvocKind semicolon);
165
166 static tl::optional<AST::Fragment> cfg_handler (location_t invoc_locus,
167 AST::MacroInvocData &invoc,
168 AST::InvocKind semicolon);
169
170 static tl::optional<AST::Fragment>
171 include_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
172 AST::InvocKind semicolon);
173
174 static tl::optional<AST::Fragment> line_handler (location_t invoc_locus,
175 AST::MacroInvocData &invoc,
176 AST::InvocKind semicolon);
177
178 static tl::optional<AST::Fragment> asm_handler (location_t invoc_locus,
179 AST::MacroInvocData &invoc,
180 AST::InvocKind semicolon,
181 AST::AsmKind is_global_asm);
182
183 static tl::optional<AST::Fragment>
184 format_args_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
185 AST::InvocKind semicolon, AST::FormatArgs::Newline nl);
186
187 static tl::optional<AST::Fragment> sorry (location_t invoc_locus,
188 AST::MacroInvocData &invoc,
189 AST::InvocKind semicolon);
190
191 /* Builtin procedural macros do not work directly on tokens, but still need a
192 * builtin transcriber to be considered proper builtin macros */
193 static tl::optional<AST::Fragment>
194 proc_macro_builtin (location_t, AST::MacroInvocData &, AST::InvocKind);
195 };
196 } // namespace Rust
197
198 namespace std {
199 template <> struct hash<Rust::BuiltinMacro>
200 {
201 size_t operator() (const Rust::BuiltinMacro &macro) const noexcept
202 {
203 return hash<std::underlying_type<Rust::BuiltinMacro>::type> () (
204 static_cast<std::underlying_type<Rust::BuiltinMacro>::type> (macro));
205 }
206 };
207 } // namespace std
208
209 #endif // RUST_MACRO_BUILTINS_H