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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
// Copyright (C) 2020-2025 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 AST_BUILDER_H
#define AST_BUILDER_H
#include "rust-ast-full.h"
#include "rust-expr.h"
#include "rust-ast.h"
#include "rust-item.h"
#include "rust-operators.h"
namespace Rust {
namespace AST {
template <typename T>
std::vector<std::unique_ptr<T>>
vec (std::unique_ptr<T> &&t)
{
auto v = std::vector<std::unique_ptr<T>> ();
v.emplace_back (std::move (t));
return v;
}
template <typename T>
std::vector<std::unique_ptr<T>>
vec (std::unique_ptr<T> &&t1, std::unique_ptr<T> &&t2)
{
auto v = std::vector<std::unique_ptr<T>> ();
v.emplace_back (std::move (t1));
v.emplace_back (std::move (t2));
return v;
}
/* Pointer-ify something */
template <typename T>
static std::unique_ptr<T>
ptrify (T value)
{
return std::unique_ptr<T> (new T (value));
}
// TODO: Use this builder when expanding regular macros
/* Builder class with helper methods to create AST nodes. This builder is
* tailored towards generating multiple AST nodes from a single location, and
* may not be suitable to other purposes */
class Builder
{
public:
Builder (location_t loc) : loc (loc) {}
/* Create an expression statement from an expression */
std::unique_ptr<Stmt> statementify (std::unique_ptr<Expr> &&value,
bool semicolon_followed = true) const;
/* Create a string literal expression ("content") */
std::unique_ptr<Expr> literal_string (std::string &&content) const;
/* Create a boolean literal expression (true) */
std::unique_ptr<Expr> literal_bool (bool b) const;
/* Create an identifier expression (`variable`) */
std::unique_ptr<Expr> identifier (std::string name) const;
std::unique_ptr<Pattern> identifier_pattern (std::string name,
bool mut = false) const;
/* Create a tuple index expression (`receiver.0`) */
std::unique_ptr<Expr> tuple_idx (std::string receiver, int idx) const;
/* Create a tuple expression (`(a1, a2, a3)`) */
std::unique_ptr<Expr> tuple (std::vector<std::unique_ptr<Expr>> &&values
= {}) const;
/* Create a reference to an expression (`&of`) */
std::unique_ptr<Expr> ref (std::unique_ptr<Expr> &&of,
bool mut = false) const;
/* Create a dereference of an expression (`*of`) */
std::unique_ptr<Expr> deref (std::unique_ptr<Expr> &&of) const;
/* Build a comparison expression (`lhs == rhs`) */
std::unique_ptr<Expr> comparison_expr (std::unique_ptr<Expr> &&lhs,
std::unique_ptr<Expr> &&rhs,
ComparisonOperator op) const;
/* Build a lazy boolean operator expression (`lhs && rhs`) */
std::unique_ptr<Expr> boolean_operation (std::unique_ptr<Expr> &&lhs,
std::unique_ptr<Expr> &&rhs,
LazyBooleanOperator op) const;
/* Create a block with an optional tail expression */
std::unique_ptr<BlockExpr> block (std::vector<std::unique_ptr<Stmt>> &&stmts,
std::unique_ptr<Expr> &&tail_expr
= nullptr) const;
std::unique_ptr<BlockExpr> block (tl::optional<std::unique_ptr<Stmt>> &&stmt,
std::unique_ptr<Expr> &&tail_expr
= nullptr) const;
/* Create an empty block */
std::unique_ptr<BlockExpr> block () const;
/* Create an early return expression with an optional expression */
std::unique_ptr<Expr> return_expr (std::unique_ptr<Expr> &&to_return
= nullptr);
/* Create a let binding with an optional type and initializer (`let <name> :
* <type> = <init>`) */
std::unique_ptr<Stmt> let (std::unique_ptr<Pattern> &&pattern,
std::unique_ptr<Type> &&type = nullptr,
std::unique_ptr<Expr> &&init = nullptr) const;
/**
* Create a call expression to a function, struct or enum variant, given its
* arguments (`path(arg0, arg1, arg2)`)
*/
std::unique_ptr<Expr> call (std::unique_ptr<Expr> &&path,
std::vector<std::unique_ptr<Expr>> &&args
= {}) const;
std::unique_ptr<Expr> call (std::unique_ptr<Expr> &&path,
std::unique_ptr<Expr> &&arg) const;
/**
* Create an array expression (`[member0, member1, member2]`)
*/
std::unique_ptr<Expr>
array (std::vector<std::unique_ptr<Expr>> &&members) const;
/* Create a qualified path in expression (`<type as Trait>::seg::expr`) */
std::unique_ptr<Expr>
qualified_path_in_expression (std::unique_ptr<Type> &&type, TypePath trait,
PathExprSegment segment) const;
std::unique_ptr<Expr>
qualified_path_in_expression (std::unique_ptr<Type> &&type, TypePath trait,
std::vector<PathExprSegment> &&segments
= {}) const;
/* Self parameter for a function definition (`&self`) */
std::unique_ptr<Param> self_ref_param (bool mutability = false) const;
/* A regular named function parameter for a definition (`a: type`) */
std::unique_ptr<Param> function_param (std::unique_ptr<Pattern> &&pattern,
std::unique_ptr<Type> &&type) const;
/* Empty function qualifiers, with no specific qualifiers */
FunctionQualifiers fn_qualifiers () const;
std::unique_ptr<Function>
function (std::string function_name,
std::vector<std::unique_ptr<Param>> params,
std::unique_ptr<Type> return_type, std::unique_ptr<BlockExpr> block,
std::vector<std::unique_ptr<GenericParam>> generic_params = {},
FunctionQualifiers qualifiers
= FunctionQualifiers (UNKNOWN_LOCATION, Async::No, Const::No,
Unsafety::Normal),
WhereClause where_clause = WhereClause::create_empty (),
Visibility visibility = Visibility::create_private ()) const;
/* Create a single path segment from one string */
PathExprSegment path_segment (std::string seg) const;
/* And similarly for type path segments */
std::unique_ptr<TypePathSegment> type_path_segment (std::string seg) const;
std::unique_ptr<TypePathSegment>
type_path_segment (LangItem::Kind lang_item) const;
std::unique_ptr<TypePathSegment>
type_path_segment_generic (std::string seg, GenericArgs args) const;
std::unique_ptr<TypePathSegment>
type_path_segment_generic (LangItem::Kind lang_item, GenericArgs args) const;
/* Create a Type from a single string - the most basic kind of type in our AST
*/
std::unique_ptr<Type> single_type_path (std::string type) const;
std::unique_ptr<Type> single_type_path (LangItem::Kind lang_item) const;
std::unique_ptr<Type> single_generic_type_path (std::string type,
GenericArgs args) const;
std::unique_ptr<Type> single_generic_type_path (LangItem::Kind lang_item,
GenericArgs args) const;
TypePath type_path (std::vector<std::unique_ptr<TypePathSegment>> &&segment,
bool opening_scope = false) const;
TypePath type_path (std::vector<std::string> &&segments,
bool opening_scope = false) const;
TypePath type_path (std::unique_ptr<TypePathSegment> &&segment) const;
TypePath type_path (std::string type) const;
TypePath type_path (LangItem::Kind lang_item) const;
std::unique_ptr<Type>
reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
bool mutability = false) const;
/**
* Create a path in expression from multiple segments (`Clone::clone`). You
* do not need to separate the segments using `::`, you can simply provide a
* vector of strings to the functions which will get turned into path segments
*/
PathInExpression path_in_expression (std::vector<std::string> &&segments,
bool opening_scope = false) const;
/**
* Create a path in expression from a lang item.
*/
PathInExpression path_in_expression (LangItem::Kind lang_item) const;
/* Create the path to an enum's variant (`Result::Ok`) */
PathInExpression variant_path (const std::string &enum_path,
const std::string &variant) const;
/* Create a new struct */
std::unique_ptr<Stmt>
struct_struct (std::string struct_name,
std::vector<std::unique_ptr<GenericParam>> &&generics,
std::vector<StructField> &&fields);
/* Create a struct expression for unit structs (`S`) */
std::unique_ptr<Expr> struct_expr_struct (std::string struct_name) const;
/**
* Create an expression for struct instantiation with fields (`S { a, b: c }`)
* Named tuple expressions (`S(a, b, c)`) are call expressions and can thus be
* constructed with `call`
*/
std::unique_ptr<Expr>
struct_expr (std::string struct_name,
std::vector<std::unique_ptr<StructExprField>> &&fields) const;
std::unique_ptr<Expr>
struct_expr (PathInExpression struct_name,
std::vector<std::unique_ptr<StructExprField>> &&fields) const;
/* Create a field expression for struct instantiation (`field_name: value`) */
std::unique_ptr<StructExprField>
struct_expr_field (std::string field_name,
std::unique_ptr<Expr> &&value) const;
/* Create a field access expression (`instance.field`) */
std::unique_ptr<Expr> field_access (std::unique_ptr<Expr> &&instance,
std::string field) const;
/* Create a wildcard pattern (`_`) */
std::unique_ptr<Pattern> wildcard () const;
/* Create a reference pattern (`&pattern`) */
std::unique_ptr<Pattern> ref_pattern (std::unique_ptr<Pattern> &&inner) const;
/* Create a lang item path usable as a general path */
std::unique_ptr<Path> lang_item_path (LangItem::Kind) const;
/* Create match expressions and their components */
std::unique_ptr<Expr> match (std::unique_ptr<Expr> &&scrutinee,
std::vector<MatchCase> &&cases);
MatchArm match_arm (std::unique_ptr<Pattern> &&pattern);
MatchCase match_case (std::unique_ptr<Pattern> &&pattern,
std::unique_ptr<Expr> &&expr);
/* Create a loop expression */
std::unique_ptr<Expr> loop (std::vector<std::unique_ptr<Stmt>> &&stmts);
std::unique_ptr<TypeParamBound> trait_bound (TypePath bound);
std::unique_ptr<Item>
trait_impl (TypePath trait_path, std::unique_ptr<Type> target,
std::vector<std::unique_ptr<AssociatedItem>> trait_items = {},
std::vector<std::unique_ptr<GenericParam>> generics = {},
WhereClause where_clause = WhereClause::create_empty (),
Visibility visibility = Visibility::create_private ()) const;
std::unique_ptr<GenericParam>
generic_type_param (std::string type_representation,
std::vector<std::unique_ptr<TypeParamBound>> &&bounds,
std::unique_ptr<Type> &&type = nullptr);
static std::unique_ptr<Type> new_type (Type &type);
static std::unique_ptr<GenericParam>
new_lifetime_param (LifetimeParam ¶m);
static std::unique_ptr<GenericParam> new_type_param (
TypeParam ¶m,
std::vector<std::unique_ptr<TypeParamBound>> extra_trait_bounds = {});
static Lifetime new_lifetime (const Lifetime &lifetime);
static GenericArgs new_generic_args (GenericArgs &args);
private:
/**
* Location of the generated AST nodes
*/
location_t loc;
};
} // namespace AST
} // namespace Rust
#endif // AST_BUILDER_H
|