Skip to content

Commit 3d8b9f9

Browse files
committed
ast-builder: Add more methods
This commit adds new methods for building pattern nodes, match expressions and more precise call expressions. gcc/rust/ChangeLog: * ast/rust-ast-builder.cc: Add new functions. * ast/rust-ast-builder.h: Declare them.
1 parent 74f3333 commit 3d8b9f9

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

gcc/rust/ast/rust-ast-builder.cc

+91
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "rust-ast-builder-type.h"
2121
#include "rust-common.h"
2222
#include "rust-expr.h"
23+
#include "rust-path.h"
2324
#include "rust-token.h"
2425
#include "rust-make-unique.h"
2526

@@ -42,6 +43,33 @@ Builder::call (std::unique_ptr<Expr> &&path,
4243
new CallExpr (std::move (path), std::move (args), {}, loc));
4344
}
4445

46+
std::unique_ptr<Expr>
47+
Builder::call (std::unique_ptr<Path> &&path,
48+
std::vector<std::unique_ptr<Expr>> &&args) const
49+
{
50+
return call (std::unique_ptr<Expr> (
51+
new PathInExpression (std::move (path), {}, loc)),
52+
std::move (args));
53+
}
54+
55+
std::unique_ptr<Expr>
56+
Builder::call (std::unique_ptr<Expr> &&path, std::unique_ptr<Expr> &&arg) const
57+
{
58+
auto args = std::vector<std::unique_ptr<Expr>> ();
59+
args.emplace_back (std::move (arg));
60+
61+
return call (std::move (path), std::move (args));
62+
}
63+
64+
std::unique_ptr<Expr>
65+
Builder::call (std::unique_ptr<Path> &&path, std::unique_ptr<Expr> &&arg) const
66+
{
67+
auto args = std::vector<std::unique_ptr<Expr>> ();
68+
args.emplace_back (std::move (arg));
69+
70+
return call (std::move (path), std::move (args));
71+
}
72+
4573
std::unique_ptr<Expr>
4674
Builder::array (std::vector<std::unique_ptr<Expr>> &&members) const
4775
{
@@ -56,6 +84,13 @@ Builder::identifier (std::string name) const
5684
return std::unique_ptr<Expr> (new IdentifierExpr (name, {}, loc));
5785
}
5886

87+
std::unique_ptr<Pattern>
88+
Builder::identifier_pattern (std::string name, bool mut) const
89+
{
90+
return std::unique_ptr<Pattern> (
91+
new IdentifierPattern (name, loc, false, mut));
92+
}
93+
5994
std::unique_ptr<Expr>
6095
Builder::tuple_idx (std::string receiver, int idx) const
6196
{
@@ -117,6 +152,22 @@ Builder::path_in_expression (std::vector<std::string> &&segments) const
117152
return PathInExpression (std::move (path_segments), {}, loc);
118153
}
119154

155+
PathInExpression
156+
Builder::path_in_expression (LangItem::Kind lang_item) const
157+
{
158+
return PathInExpression (lang_item, {}, loc);
159+
}
160+
161+
std::unique_ptr<Expr>
162+
Builder::block (std::unique_ptr<Stmt> &&stmt,
163+
std::unique_ptr<Expr> &&tail_expr) const
164+
{
165+
auto stmts = std::vector<std::unique_ptr<Stmt>> ();
166+
stmts.emplace_back (std::move (stmt));
167+
168+
return block (std::move (stmts), std::move (tail_expr));
169+
}
170+
120171
std::unique_ptr<Expr>
121172
Builder::block (std::vector<std::unique_ptr<Stmt>> &&stmts,
122173
std::unique_ptr<Expr> &&tail_expr) const
@@ -189,6 +240,46 @@ Builder::wildcard () const
189240
return std::unique_ptr<Pattern> (new WildcardPattern (loc));
190241
}
191242

243+
std::unique_ptr<Path>
244+
Builder::lang_item_path (LangItem::Kind kind) const
245+
{
246+
return std::unique_ptr<Path> (new LangItemPath (kind, loc));
247+
}
248+
249+
std::unique_ptr<Expr>
250+
Builder::match (std::unique_ptr<Expr> &&scrutinee,
251+
std::vector<MatchCase> &&cases)
252+
{
253+
return std::unique_ptr<Expr> (
254+
new MatchExpr (std::move (scrutinee), std::move (cases), {}, {}, loc));
255+
}
256+
257+
MatchArm
258+
Builder::match_arm (std::unique_ptr<Pattern> &&pattern)
259+
{
260+
auto patterns = std::vector<std::unique_ptr<Pattern>> ();
261+
patterns.emplace_back (std::move (pattern));
262+
263+
return MatchArm (std::move (patterns), loc);
264+
}
265+
266+
MatchCase
267+
Builder::match_case (std::unique_ptr<Pattern> &&pattern,
268+
std::unique_ptr<Expr> &&expr)
269+
{
270+
return MatchCase (match_arm (std::move (pattern)), std::move (expr));
271+
}
272+
273+
std::unique_ptr<Expr>
274+
Builder::loop (std::vector<std::unique_ptr<Stmt>> &&stmts)
275+
{
276+
auto block = std::unique_ptr<BlockExpr> (
277+
new BlockExpr (std::move (stmts), nullptr, {}, {}, LoopLabel::error (), loc,
278+
loc));
279+
280+
return std::unique_ptr<Expr> (new LoopExpr (std::move (block), loc));
281+
}
282+
192283
std::unique_ptr<Type>
193284
Builder::new_type (Type &type)
194285
{

gcc/rust/ast/rust-ast-builder.h

+30
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define AST_BUILDER_H
2121

2222
#include "rust-ast-full.h"
23+
#include "rust-expr.h"
2324

2425
namespace Rust {
2526
namespace AST {
@@ -38,6 +39,8 @@ class Builder
3839

3940
/* Create an identifier expression (`variable`) */
4041
std::unique_ptr<Expr> identifier (std::string name) const;
42+
std::unique_ptr<Pattern> identifier_pattern (std::string name,
43+
bool mut = false) const;
4144

4245
/* Create a tuple index expression (`receiver.0`) */
4346
std::unique_ptr<Expr> tuple_idx (std::string receiver, int idx) const;
@@ -53,6 +56,9 @@ class Builder
5356
std::unique_ptr<Expr> block (std::vector<std::unique_ptr<Stmt>> &&stmts,
5457
std::unique_ptr<Expr> &&tail_expr
5558
= nullptr) const;
59+
std::unique_ptr<Expr> block (std::unique_ptr<Stmt> &&stmt,
60+
std::unique_ptr<Expr> &&tail_expr
61+
= nullptr) const;
5662

5763
/* Create a let binding with an optional type and initializer (`let <name> :
5864
* <type> = <init>`) */
@@ -66,6 +72,12 @@ class Builder
6672
*/
6773
std::unique_ptr<Expr> call (std::unique_ptr<Expr> &&path,
6874
std::vector<std::unique_ptr<Expr>> &&args) const;
75+
std::unique_ptr<Expr> call (std::unique_ptr<Path> &&path,
76+
std::vector<std::unique_ptr<Expr>> &&args) const;
77+
std::unique_ptr<Expr> call (std::unique_ptr<Expr> &&path,
78+
std::unique_ptr<Expr> &&arg) const;
79+
std::unique_ptr<Expr> call (std::unique_ptr<Path> &&path,
80+
std::unique_ptr<Expr> &&arg) const;
6981

7082
/**
7183
* Create an array expression (`[member0, member1, member2]`)
@@ -100,6 +112,11 @@ class Builder
100112
PathInExpression
101113
path_in_expression (std::vector<std::string> &&segments) const;
102114

115+
/**
116+
* Create a path in expression from a lang item.
117+
*/
118+
PathInExpression path_in_expression (LangItem::Kind lang_item) const;
119+
103120
/* Create a struct expression for unit structs (`S`) */
104121
std::unique_ptr<Expr> struct_expr_struct (std::string struct_name) const;
105122

@@ -122,6 +139,19 @@ class Builder
122139
/* Create a wildcard pattern (`_`) */
123140
std::unique_ptr<Pattern> wildcard () const;
124141

142+
/* Create a lang item path usable as a general path */
143+
std::unique_ptr<Path> lang_item_path (LangItem::Kind) const;
144+
145+
/* Create match expressions and their components */
146+
std::unique_ptr<Expr> match (std::unique_ptr<Expr> &&scrutinee,
147+
std::vector<MatchCase> &&cases);
148+
MatchArm match_arm (std::unique_ptr<Pattern> &&pattern);
149+
MatchCase match_case (std::unique_ptr<Pattern> &&pattern,
150+
std::unique_ptr<Expr> &&expr);
151+
152+
/* Create a loop expression */
153+
std::unique_ptr<Expr> loop (std::vector<std::unique_ptr<Stmt>> &&stmts);
154+
125155
static std::unique_ptr<Type> new_type (Type &type);
126156

127157
static std::unique_ptr<GenericParam>

0 commit comments

Comments
 (0)