forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.h
484 lines (391 loc) · 14.9 KB
/
statement.h
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_EXPLORER_AST_STATEMENT_H_
#define CARBON_EXPLORER_AST_STATEMENT_H_
#include <utility>
#include <vector>
#include "common/ostream.h"
#include "explorer/ast/ast_node.h"
#include "explorer/ast/expression.h"
#include "explorer/ast/pattern.h"
#include "explorer/ast/return_term.h"
#include "explorer/ast/static_scope.h"
#include "explorer/ast/value_category.h"
#include "explorer/common/arena.h"
#include "explorer/common/source_location.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Compiler.h"
namespace Carbon {
class FunctionDeclaration;
class Statement : public AstNode {
public:
~Statement() override = 0;
void Print(llvm::raw_ostream& out) const override { PrintDepth(-1, out); }
void PrintID(llvm::raw_ostream& out) const override { PrintDepth(1, out); }
void PrintDepth(int depth, llvm::raw_ostream& out) const;
static auto classof(const AstNode* node) {
return InheritsFromStatement(node->kind());
}
// Returns the enumerator corresponding to the most-derived type of this
// object.
auto kind() const -> StatementKind {
return static_cast<StatementKind>(root_kind());
}
protected:
Statement(AstNodeKind kind, SourceLocation source_loc)
: AstNode(kind, source_loc) {}
};
class Block : public Statement {
public:
Block(SourceLocation source_loc, std::vector<Nonnull<Statement*>> statements)
: Statement(AstNodeKind::Block, source_loc),
statements_(std::move(statements)) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromBlock(node->kind());
}
auto statements() const -> llvm::ArrayRef<Nonnull<const Statement*>> {
return statements_;
}
auto statements() -> llvm::MutableArrayRef<Nonnull<Statement*>> {
return statements_;
}
private:
std::vector<Nonnull<Statement*>> statements_;
};
class ExpressionStatement : public Statement {
public:
ExpressionStatement(SourceLocation source_loc,
Nonnull<Expression*> expression)
: Statement(AstNodeKind::ExpressionStatement, source_loc),
expression_(expression) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromExpressionStatement(node->kind());
}
auto expression() const -> const Expression& { return *expression_; }
auto expression() -> Expression& { return *expression_; }
private:
Nonnull<Expression*> expression_;
};
class Assign : public Statement {
public:
Assign(SourceLocation source_loc, Nonnull<Expression*> lhs,
Nonnull<Expression*> rhs)
: Statement(AstNodeKind::Assign, source_loc), lhs_(lhs), rhs_(rhs) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromAssign(node->kind());
}
auto lhs() const -> const Expression& { return *lhs_; }
auto lhs() -> Expression& { return *lhs_; }
auto rhs() const -> const Expression& { return *rhs_; }
auto rhs() -> Expression& { return *rhs_; }
// Can only be called by type-checking, if a conversion was required.
void set_rhs(Nonnull<Expression*> rhs) { rhs_ = rhs; }
private:
Nonnull<Expression*> lhs_;
Nonnull<Expression*> rhs_;
};
class VariableDefinition : public Statement {
public:
enum DefinitionType {
Var,
Returned,
};
VariableDefinition(SourceLocation source_loc, Nonnull<Pattern*> pattern,
std::optional<Nonnull<Expression*>> init,
ValueCategory value_category, DefinitionType def_type)
: Statement(AstNodeKind::VariableDefinition, source_loc),
pattern_(pattern),
init_(init),
value_category_(value_category),
def_type_(def_type) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromVariableDefinition(node->kind());
}
auto pattern() const -> const Pattern& { return *pattern_; }
auto pattern() -> Pattern& { return *pattern_; }
auto init() const -> const Expression& {
CARBON_CHECK(has_init());
return **init_;
}
auto init() -> Expression& {
CARBON_CHECK(has_init());
return **init_;
}
auto has_init() const -> bool { return init_.has_value(); }
// Can only be called by type-checking, if a conversion was required.
void set_init(Nonnull<Expression*> init) {
CARBON_CHECK(has_init()) << "should not add a new initializer";
init_ = init;
}
auto value_category() const -> ValueCategory { return value_category_; }
auto is_returned() const -> bool { return def_type_ == Returned; };
private:
Nonnull<Pattern*> pattern_;
std::optional<Nonnull<Expression*>> init_;
ValueCategory value_category_;
const DefinitionType def_type_;
};
class If : public Statement {
public:
If(SourceLocation source_loc, Nonnull<Expression*> condition,
Nonnull<Block*> then_block, std::optional<Nonnull<Block*>> else_block)
: Statement(AstNodeKind::If, source_loc),
condition_(condition),
then_block_(then_block),
else_block_(else_block) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromIf(node->kind());
}
auto condition() const -> const Expression& { return *condition_; }
auto condition() -> Expression& { return *condition_; }
auto then_block() const -> const Block& { return *then_block_; }
auto then_block() -> Block& { return *then_block_; }
auto else_block() const -> std::optional<Nonnull<const Block*>> {
return else_block_;
}
auto else_block() -> std::optional<Nonnull<Block*>> { return else_block_; }
// Can only be called by type-checking, if a conversion was required.
void set_condition(Nonnull<Expression*> condition) { condition_ = condition; }
private:
Nonnull<Expression*> condition_;
Nonnull<Block*> then_block_;
std::optional<Nonnull<Block*>> else_block_;
};
class Return : public Statement {
public:
static auto classof(const AstNode* node) -> bool {
return InheritsFromReturn(node->kind());
}
// The AST node representing the function body this statement returns from.
// Can only be called after ResolveControlFlow has visited this node.
//
// Note that this function does not represent an edge in the tree
// structure of the AST: the return value is not a child of this node,
// but an ancestor.
auto function() const -> const FunctionDeclaration& { return **function_; }
auto function() -> FunctionDeclaration& { return **function_; }
// Can only be called once, by ResolveControlFlow.
void set_function(Nonnull<FunctionDeclaration*> function) {
CARBON_CHECK(!function_.has_value());
function_ = function;
}
protected:
Return(AstNodeKind node_kind, SourceLocation source_loc)
: Statement(node_kind, source_loc) {}
private:
std::optional<Nonnull<FunctionDeclaration*>> function_;
};
class ReturnVar : public Return {
public:
explicit ReturnVar(SourceLocation source_loc)
: Return(AstNodeKind::ReturnVar, source_loc) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromReturnVar(node->kind());
}
// Returns the value node of the BindingPattern of the returned var
// definition. Cannot be called before name resolution.
auto value_node() const -> const ValueNodeView& { return *value_node_; }
// Can only be called once, by ResolveNames.
void set_value_node(ValueNodeView value_node) {
CARBON_CHECK(!value_node_.has_value());
value_node_ = value_node;
}
private:
// The value node of the BindingPattern of the returned var definition.
std::optional<ValueNodeView> value_node_;
};
class ReturnExpression : public Return {
public:
ReturnExpression(Nonnull<Arena*> arena, SourceLocation source_loc)
: ReturnExpression(source_loc, arena->New<TupleLiteral>(source_loc),
true) {}
ReturnExpression(SourceLocation source_loc, Nonnull<Expression*> expression,
bool is_omitted_expression)
: Return(AstNodeKind::ReturnExpression, source_loc),
expression_(expression),
is_omitted_expression_(is_omitted_expression) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromReturnExpression(node->kind());
}
auto expression() const -> const Expression& { return *expression_; }
auto expression() -> Expression& { return *expression_; }
auto is_omitted_expression() const -> bool { return is_omitted_expression_; }
// Can only be called by type-checking, if a conversion was required.
void set_expression(Nonnull<Expression*> expression) {
expression_ = expression;
}
private:
Nonnull<Expression*> expression_;
bool is_omitted_expression_;
};
class While : public Statement {
public:
While(SourceLocation source_loc, Nonnull<Expression*> condition,
Nonnull<Block*> body)
: Statement(AstNodeKind::While, source_loc),
condition_(condition),
body_(body) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromWhile(node->kind());
}
auto condition() const -> const Expression& { return *condition_; }
auto condition() -> Expression& { return *condition_; }
auto body() const -> const Block& { return *body_; }
auto body() -> Block& { return *body_; }
// Can only be called by type-checking, if a conversion was required.
void set_condition(Nonnull<Expression*> condition) { condition_ = condition; }
private:
Nonnull<Expression*> condition_;
Nonnull<Block*> body_;
};
class Break : public Statement {
public:
explicit Break(SourceLocation source_loc)
: Statement(AstNodeKind::Break, source_loc) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromBreak(node->kind());
}
// The AST node representing the loop this statement breaks out of.
// Can only be called after ResolveControlFlow has visited this node.
//
// Note that this function does not represent an edge in the tree
// structure of the AST: the return value is not a child of this node,
// but an ancestor.
auto loop() const -> const Statement& { return **loop_; }
// Can only be called once, by ResolveControlFlow.
void set_loop(Nonnull<const Statement*> loop) {
CARBON_CHECK(!loop_.has_value());
loop_ = loop;
}
private:
std::optional<Nonnull<const Statement*>> loop_;
};
class Continue : public Statement {
public:
explicit Continue(SourceLocation source_loc)
: Statement(AstNodeKind::Continue, source_loc) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromContinue(node->kind());
}
// The AST node representing the loop this statement continues.
// Can only be called after ResolveControlFlow has visited this node.
//
// Note that this function does not represent an edge in the tree
// structure of the AST: the return value is not a child of this node,
// but an ancestor.
auto loop() const -> const Statement& { return **loop_; }
// Can only be called once, by ResolveControlFlow.
void set_loop(Nonnull<const Statement*> loop) {
CARBON_CHECK(!loop_.has_value());
loop_ = loop;
}
private:
std::optional<Nonnull<const Statement*>> loop_;
};
class Match : public Statement {
public:
class Clause {
public:
Clause(Nonnull<Pattern*> pattern, Nonnull<Statement*> statement)
: pattern_(pattern), statement_(statement) {}
auto pattern() const -> const Pattern& { return *pattern_; }
auto pattern() -> Pattern& { return *pattern_; }
auto statement() const -> const Statement& { return *statement_; }
auto statement() -> Statement& { return *statement_; }
private:
Nonnull<Pattern*> pattern_;
Nonnull<Statement*> statement_;
};
Match(SourceLocation source_loc, Nonnull<Expression*> expression,
std::vector<Clause> clauses)
: Statement(AstNodeKind::Match, source_loc),
expression_(expression),
clauses_(std::move(clauses)) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromMatch(node->kind());
}
auto expression() const -> const Expression& { return *expression_; }
auto expression() -> Expression& { return *expression_; }
auto clauses() const -> llvm::ArrayRef<Clause> { return clauses_; }
auto clauses() -> llvm::MutableArrayRef<Clause> { return clauses_; }
// Can only be called by type-checking, if a conversion was required.
void set_expression(Nonnull<Expression*> expression) {
expression_ = expression;
}
private:
Nonnull<Expression*> expression_;
std::vector<Clause> clauses_;
};
// A continuation statement.
//
// __continuation <continuation_variable> {
// <body>
// }
class Continuation : public Statement {
public:
using ImplementsCarbonValueNode = void;
Continuation(SourceLocation source_loc, std::string name,
Nonnull<Block*> body)
: Statement(AstNodeKind::Continuation, source_loc),
name_(std::move(name)),
body_(body) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromContinuation(node->kind());
}
auto name() const -> const std::string& { return name_; }
auto body() const -> const Block& { return *body_; }
auto body() -> Block& { return *body_; }
// The static type of the continuation. Cannot be called before typechecking.
//
// This will always be ContinuationType, but we must set it dynamically in
// the typechecker because this code can't depend on ContinuationType.
auto static_type() const -> const Value& { return **static_type_; }
// Sets the static type of the continuation. Can only be called once,
// during typechecking.
void set_static_type(Nonnull<const Value*> type) {
CARBON_CHECK(!static_type_.has_value());
static_type_ = type;
}
auto value_category() const -> ValueCategory { return ValueCategory::Var; }
auto constant_value() const -> std::optional<Nonnull<const Value*>> {
return std::nullopt;
}
auto symbolic_identity() const -> std::optional<Nonnull<const Value*>> {
return std::nullopt;
}
private:
std::string name_;
Nonnull<Block*> body_;
std::optional<Nonnull<const Value*>> static_type_;
};
// A run statement.
//
// __run <argument>;
class Run : public Statement {
public:
Run(SourceLocation source_loc, Nonnull<Expression*> argument)
: Statement(AstNodeKind::Run, source_loc), argument_(argument) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromRun(node->kind());
}
auto argument() const -> const Expression& { return *argument_; }
auto argument() -> Expression& { return *argument_; }
// Can only be called by type-checking, if a conversion was required.
void set_argument(Nonnull<Expression*> argument) { argument_ = argument; }
private:
Nonnull<Expression*> argument_;
};
// An await statement.
//
// __await;
class Await : public Statement {
public:
explicit Await(SourceLocation source_loc)
: Statement(AstNodeKind::Await, source_loc) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromAwait(node->kind());
}
};
} // namespace Carbon
#endif // CARBON_EXPLORER_AST_STATEMENT_H_