1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
// Copyright (C) 2020-2024 Free Software Foundation, Inc.
// This file is part of GCC.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#ifndef RUST_MACRO_BUILTINS_H
#define RUST_MACRO_BUILTINS_H
#include "rust-ast.h"
#include "rust-ast-fragment.h"
#include "rust-location.h"
#include "bi-map.h"
namespace Rust {
// FIXME: Add a BuiltinMacro class which contains a name (or should it?), a
// transcriber and extra info if necessary
// then make a global map<string, BuiltinMacro>
/**
* All builtin macros possible
*/
enum class BuiltinMacro
{
Assert,
File,
Line,
Column,
IncludeBytes,
IncludeStr,
Stringify,
CompileError,
Concat,
Env,
OptionEnv,
Cfg,
Include,
FormatArgs,
FormatArgsNl,
ConcatIdents,
ModulePath,
Asm,
LlvmAsm,
GlobalAsm,
LogSyntax,
TraceMacros,
Test,
Bench,
TestCase,
GlobalAllocator,
CfgAccessible,
RustcDecodable,
RustcEncodable,
Clone,
Copy,
Debug,
Default,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
};
BuiltinMacro
builtin_macro_from_string (const std::string &identifier);
/**
* This class provides a list of builtin macros implemented by the compiler.
* The functions defined are called "builtin transcribers" in that they replace
* the transcribing part of a macro definition.
*
* Like regular macro transcribers, they are responsible for building and
* returning an AST fragment: basically a vector of AST nodes put together.
*
* Unlike regular declarative macros where each match arm has its own associated
* transcriber, builtin transcribers are responsible for handling all match arms
* of the macro. This means that you should take extra care when implementing a
* builtin containing multiple match arms: You will probably need to do some
* lookahead in order to determine which match arm the user intended to use.
*
* An example of this is the `assert!()` macro:
*
* ```
* macro_rules! assert {
* ($cond:expr $(,)?) => {{ ... }};
* ($cond : expr, $ ($arg : tt) +) = > {{ ... }};
* }
* ```
*
* If more tokens exist beyond the optional comma, they need to be handled as
* a token-tree for a custom panic message.
*
* These builtin macros with empty transcribers are defined in the standard
* library. They are marked with a special attribute, `#[rustc_builtin_macro]`.
* When this attribute is present on a macro definition, the compiler should
* look for an associated transcriber in the mappings. Meaning that you must
* remember to insert your transcriber in the `builtin_macros` map of the
*`Mappings`.
*
* This map is built as a static variable in the `insert_macro_def()` method
* of the `Mappings` class.
*/
class MacroBuiltin
{
public:
static const BiMap<std::string, BuiltinMacro> builtins;
static std::unordered_map<std::string, AST::MacroTranscriberFunc>
builtin_transcribers;
static tl::optional<AST::Fragment>
assert_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment> file_handler (location_t invoc_locus,
AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment>
column_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment>
include_bytes_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment>
include_str_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment>
stringify_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment>
compile_error_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment>
concat_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment> env_handler (location_t invoc_locus,
AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment> cfg_handler (location_t invoc_locus,
AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment>
include_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment> line_handler (location_t invoc_locus,
AST::MacroInvocData &invoc);
static tl::optional<AST::Fragment> sorry (location_t invoc_locus,
AST::MacroInvocData &invoc);
/* Builtin procedural macros do not work directly on tokens, but still need a
* builtin transcriber to be considered proper builtin macros */
static tl::optional<AST::Fragment> proc_macro_builtin (location_t,
AST::MacroInvocData &);
};
} // namespace Rust
namespace std {
template <> struct hash<Rust::BuiltinMacro>
{
size_t operator() (const Rust::BuiltinMacro ¯o) const noexcept
{
return hash<std::underlying_type<Rust::BuiltinMacro>::type> () (
static_cast<std::underlying_type<Rust::BuiltinMacro>::type> (macro));
}
};
} // namespace std
#endif // RUST_MACRO_BUILTINS_H
|