]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/rust/ast/rust-ast-builder.h
1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
22 #include "rust-ast-full.h"
27 // TODO: Use this builder when expanding regular macros
28 /* Builder class with helper methods to create AST nodes. This builder is
29 * tailored towards generating multiple AST nodes from a single location, and
30 * may not be suitable to other purposes */
34 AstBuilder (location_t loc
) : loc (loc
) {}
36 /* Create an identifier expression (`variable`) */
37 std::unique_ptr
<Expr
> identifier (std::string name
);
39 /* Create a tuple index expression (`receiver.0`) */
40 std::unique_ptr
<Expr
> tuple_idx (std::string receiver
, int idx
);
42 /* Create a reference to an expression (`&of`) */
43 std::unique_ptr
<Expr
> ref (std::unique_ptr
<Expr
> &&of
, bool mut
= false);
45 /* Create a dereference of an expression (`*of`) */
46 std::unique_ptr
<Expr
> deref (std::unique_ptr
<Expr
> &&of
);
48 /* Create a block with an optional tail expression */
49 std::unique_ptr
<Expr
> block (std::vector
<std::unique_ptr
<Stmt
>> &&stmts
,
50 std::unique_ptr
<Expr
> &&tail_expr
= nullptr);
52 /* Create a let binding with an optional type and initializer (`let <name> :
53 * <type> = <init>`) */
54 std::unique_ptr
<Stmt
> let (std::unique_ptr
<Pattern
> pattern
,
55 std::unique_ptr
<Type
> type
= nullptr,
56 std::unique_ptr
<Expr
> init
= nullptr);
59 * Create a call expression to a function, struct or enum variant, given its
60 * arguments (`path(arg0, arg1, arg2)`)
62 std::unique_ptr
<Expr
> call (std::unique_ptr
<Expr
> &&path
,
63 std::vector
<std::unique_ptr
<Expr
>> &&args
);
65 /* Empty function qualifiers, with no specific qualifiers */
66 FunctionQualifiers
fn_qualifiers ();
68 /* Create a single path segment from one string */
69 PathExprSegment
path_segment (std::string seg
);
71 /* And similarly for type path segments */
72 std::unique_ptr
<TypePathSegment
> type_path_segment (std::string seg
);
74 /* Create a Type from a single string - the most basic kind of type in our AST
76 std::unique_ptr
<Type
> single_type_path (std::string type
);
79 * Create a path in expression from multiple segments (`Clone::clone`). You
80 * do not need to separate the segments using `::`, you can simply provide a
81 * vector of strings to the functions which will get turned into path segments
83 PathInExpression
path_in_expression (std::vector
<std::string
> &&segments
);
85 /* Create a struct expression for unit structs (`S`) */
86 std::unique_ptr
<Expr
> struct_expr_struct (std::string struct_name
);
89 * Create an expression for struct instantiation with fields (`S { a, b: c }`)
92 struct_expr (std::string struct_name
,
93 std::vector
<std::unique_ptr
<StructExprField
>> &&fields
);
95 /* Create a field expression for struct instantiation (`field_name: value`) */
96 std::unique_ptr
<StructExprField
>
97 struct_expr_field (std::string field_name
, std::unique_ptr
<Expr
> &&value
);
99 /* Create a field access expression (`instance.field`) */
100 std::unique_ptr
<Expr
> field_access (std::unique_ptr
<Expr
> &&instance
,
103 /* Create a wildcard pattern (`_`) */
104 std::unique_ptr
<Pattern
> wildcard ();
108 * Location of the generated AST nodes
116 #endif // AST_BUILDER_H