forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve_control_flow.cpp
246 lines (225 loc) · 9.11 KB
/
resolve_control_flow.cpp
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
// 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
#include "explorer/interpreter/resolve_control_flow.h"
#include "explorer/ast/declaration.h"
#include "explorer/ast/return_term.h"
#include "explorer/ast/statement.h"
#include "explorer/base/error_builders.h"
#include "explorer/base/print_as_id.h"
#include "llvm/Support/Casting.h"
using llvm::cast;
namespace Carbon {
// Aggregate information about a function being analyzed.
struct FunctionData {
// The function declaration.
Nonnull<CallableDeclaration*> declaration;
// True if the function has a deduced return type, and we've already seen
// a `return` statement in its body.
bool saw_return_in_auto = false;
};
// Resolves control-flow edges such as `Return::function()` and `Break::loop()`
// in the AST rooted at `statement`. `loop` is the innermost loop that
// statically encloses `statement`, or nullopt if there is no such loop.
// `function` carries information about the function body that `statement`
// belongs to, and that information may be updated by this call. `function`
// can be nullopt if `statement` does not belong to a function body, for
// example if it is part of a continuation body instead.
static auto ResolveControlFlow(Nonnull<TraceStream*> trace_stream,
Nonnull<Statement*> statement,
std::optional<Nonnull<const Statement*>> loop,
std::optional<Nonnull<FunctionData*>> function)
-> ErrorOr<Success> {
SetFileContext set_file_ctx(*trace_stream, statement->source_loc());
switch (statement->kind()) {
case StatementKind::ReturnVar:
case StatementKind::ReturnExpression: {
if (!function.has_value()) {
return ProgramError(statement->source_loc())
<< "return is not within a function body";
}
const ReturnTerm& function_return =
(*function)->declaration->return_term();
if (function_return.is_auto()) {
if ((*function)->saw_return_in_auto) {
return ProgramError(statement->source_loc())
<< "Only one return is allowed in a function with an `auto` "
"return type.";
}
(*function)->saw_return_in_auto = true;
}
auto& ret = cast<Return>(*statement);
ret.set_function((*function)->declaration);
if (statement->kind() == StatementKind::ReturnVar &&
function_return.is_omitted()) {
return ProgramError(statement->source_loc())
<< *statement
<< " should not provide a return value, to match the function's "
"signature.";
}
if (statement->kind() == StatementKind::ReturnExpression) {
auto& ret_exp = cast<ReturnExpression>(*statement);
if (ret_exp.is_omitted_expression() != function_return.is_omitted()) {
return ProgramError(ret_exp.source_loc())
<< ret_exp << " should"
<< (function_return.is_omitted() ? " not" : "")
<< " provide a return value, to match the function's "
"signature.";
}
}
if (trace_stream->is_enabled()) {
trace_stream->Result()
<< "flow-resolved return statement `" << *statement << "` in `"
<< PrintAsID(*((*function)->declaration)) << "` ("
<< statement->source_loc() << ")\n";
}
return Success();
}
case StatementKind::Break:
if (!loop.has_value()) {
return ProgramError(statement->source_loc())
<< "break is not within a loop body";
}
cast<Break>(*statement).set_loop(*loop);
if (trace_stream->is_enabled()) {
trace_stream->Result()
<< "flow-resolved break statement `" << *statement << "` for `"
<< PrintAsID(**loop) << "`\n";
}
return Success();
case StatementKind::Continue:
if (!loop.has_value()) {
return ProgramError(statement->source_loc())
<< "continue is not within a loop body";
}
cast<Continue>(*statement).set_loop(*loop);
if (trace_stream->is_enabled()) {
trace_stream->Result()
<< "flow-resolved continue statement `" << *statement << "` in `"
<< PrintAsID(**loop) << "` (" << statement->source_loc() << ")\n";
}
return Success();
case StatementKind::If: {
auto& if_stmt = cast<If>(*statement);
CARBON_RETURN_IF_ERROR(ResolveControlFlow(
trace_stream, &if_stmt.then_block(), loop, function));
if (if_stmt.else_block().has_value()) {
CARBON_RETURN_IF_ERROR(ResolveControlFlow(
trace_stream, *if_stmt.else_block(), loop, function));
}
return Success();
}
case StatementKind::Block: {
auto& block = cast<Block>(*statement);
for (auto* block_statement : block.statements()) {
CARBON_RETURN_IF_ERROR(
ResolveControlFlow(trace_stream, block_statement, loop, function));
}
return Success();
}
case StatementKind::For: {
CARBON_RETURN_IF_ERROR(ResolveControlFlow(
trace_stream, &cast<For>(*statement).body(), statement, function));
if (trace_stream->is_enabled()) {
trace_stream->Result()
<< "flow-resolved for statement `" << PrintAsID(*statement) << "` ("
<< statement->source_loc() << ")\n";
}
return Success();
}
case StatementKind::While:
CARBON_RETURN_IF_ERROR(ResolveControlFlow(
trace_stream, &cast<While>(*statement).body(), statement, function));
if (trace_stream->is_enabled()) {
trace_stream->Result()
<< "flow-resolved while statement `" << PrintAsID(*statement)
<< "` (" << statement->source_loc() << ")\n";
}
return Success();
case StatementKind::Match: {
auto& match = cast<Match>(*statement);
for (Match::Clause& clause : match.clauses()) {
CARBON_RETURN_IF_ERROR(ResolveControlFlow(
trace_stream, &clause.statement(), loop, function));
}
return Success();
}
case StatementKind::ExpressionStatement:
case StatementKind::Assign:
case StatementKind::IncrementDecrement:
case StatementKind::VariableDefinition:
return Success();
}
}
auto ResolveControlFlow(Nonnull<TraceStream*> trace_stream,
Nonnull<Declaration*> declaration) -> ErrorOr<Success> {
switch (declaration->kind()) {
case DeclarationKind::DestructorDeclaration:
case DeclarationKind::FunctionDeclaration: {
auto& callable = cast<CallableDeclaration>(*declaration);
if (callable.body().has_value()) {
FunctionData data = {.declaration = &callable};
CARBON_RETURN_IF_ERROR(ResolveControlFlow(
trace_stream, *callable.body(), std::nullopt, &data));
}
break;
}
case DeclarationKind::ClassDeclaration: {
auto& class_decl = cast<ClassDeclaration>(*declaration);
for (Nonnull<Declaration*> member : class_decl.members()) {
CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, member));
}
break;
}
case DeclarationKind::MixinDeclaration: {
auto& mixin_decl = cast<MixinDeclaration>(*declaration);
for (Nonnull<Declaration*> member : mixin_decl.members()) {
CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, member));
}
break;
}
case DeclarationKind::InterfaceDeclaration:
case DeclarationKind::ConstraintDeclaration: {
auto& iface_decl = cast<ConstraintTypeDeclaration>(*declaration);
for (Nonnull<Declaration*> member : iface_decl.members()) {
CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, member));
}
break;
}
case DeclarationKind::ImplDeclaration: {
auto& impl_decl = cast<ImplDeclaration>(*declaration);
for (Nonnull<Declaration*> member : impl_decl.members()) {
CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, member));
}
break;
}
case DeclarationKind::MatchFirstDeclaration: {
auto& match_first_decl = cast<MatchFirstDeclaration>(*declaration);
for (Nonnull<Declaration*> impl : match_first_decl.impl_declarations()) {
CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, impl));
}
break;
}
case DeclarationKind::NamespaceDeclaration:
case DeclarationKind::ChoiceDeclaration:
case DeclarationKind::VariableDeclaration:
case DeclarationKind::InterfaceExtendDeclaration:
case DeclarationKind::InterfaceRequireDeclaration:
case DeclarationKind::AssociatedConstantDeclaration:
case DeclarationKind::SelfDeclaration:
case DeclarationKind::AliasDeclaration:
case DeclarationKind::MixDeclaration:
case DeclarationKind::ExtendBaseDeclaration:
// do nothing
break;
}
return Success();
}
auto ResolveControlFlow(Nonnull<TraceStream*> trace_stream, AST& ast)
-> ErrorOr<Success> {
for (auto* declaration : ast.declarations) {
CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, declaration));
}
return Success();
}
} // namespace Carbon