Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 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 AST_BUILDER_H
20 : : #define AST_BUILDER_H
21 : :
22 : : #include "rust-ast-full.h"
23 : :
24 : : namespace Rust {
25 : : namespace AST {
26 : :
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 */
31 : : class AstBuilder
32 : : {
33 : : public:
34 : 24 : AstBuilder (location_t loc) : loc (loc) {}
35 : :
36 : : /* Create an identifier expression (`variable`) */
37 : : std::unique_ptr<Expr> identifier (std::string name);
38 : :
39 : : /* Create a tuple index expression (`receiver.0`) */
40 : : std::unique_ptr<Expr> tuple_idx (std::string receiver, int idx);
41 : :
42 : : /* Create a reference to an expression (`&of`) */
43 : : std::unique_ptr<Expr> ref (std::unique_ptr<Expr> &&of, bool mut = false);
44 : :
45 : : /* Create a dereference of an expression (`*of`) */
46 : : std::unique_ptr<Expr> deref (std::unique_ptr<Expr> &&of);
47 : :
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);
51 : :
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);
57 : :
58 : : /**
59 : : * Create a call expression to a function, struct or enum variant, given its
60 : : * arguments (`path(arg0, arg1, arg2)`)
61 : : */
62 : : std::unique_ptr<Expr> call (std::unique_ptr<Expr> &&path,
63 : : std::vector<std::unique_ptr<Expr>> &&args);
64 : :
65 : : /* Empty function qualifiers, with no specific qualifiers */
66 : : FunctionQualifiers fn_qualifiers ();
67 : :
68 : : /* Create a single path segment from one string */
69 : : PathExprSegment path_segment (std::string seg);
70 : :
71 : : /* And similarly for type path segments */
72 : : std::unique_ptr<TypePathSegment> type_path_segment (std::string seg);
73 : :
74 : : /* Create a Type from a single string - the most basic kind of type in our AST
75 : : */
76 : : std::unique_ptr<Type> single_type_path (std::string type);
77 : :
78 : : /**
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
82 : : */
83 : : PathInExpression path_in_expression (std::vector<std::string> &&segments);
84 : :
85 : : /* Create a struct expression for unit structs (`S`) */
86 : : std::unique_ptr<Expr> struct_expr_struct (std::string struct_name);
87 : :
88 : : /**
89 : : * Create an expression for struct instantiation with fields (`S { a, b: c }`)
90 : : */
91 : : std::unique_ptr<Expr>
92 : : struct_expr (std::string struct_name,
93 : : std::vector<std::unique_ptr<StructExprField>> &&fields);
94 : :
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);
98 : :
99 : : /* Create a field access expression (`instance.field`) */
100 : : std::unique_ptr<Expr> field_access (std::unique_ptr<Expr> &&instance,
101 : : std::string field);
102 : :
103 : : /* Create a wildcard pattern (`_`) */
104 : : std::unique_ptr<Pattern> wildcard ();
105 : :
106 : : private:
107 : : /**
108 : : * Location of the generated AST nodes
109 : : */
110 : : location_t loc;
111 : : };
112 : :
113 : : } // namespace AST
114 : : } // namespace Rust
115 : :
116 : : #endif // AST_BUILDER_H
|