forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve_unformed.cpp
More file actions
317 lines (304 loc) · 12.2 KB
/
Copy pathresolve_unformed.cpp
File metadata and controls
317 lines (304 loc) · 12.2 KB
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
// 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_unformed.h"
#include <unordered_map>
#include "common/check.h"
#include "explorer/ast/ast.h"
#include "explorer/ast/expression.h"
#include "explorer/ast/pattern.h"
#include "explorer/common/nonnull.h"
using llvm::cast;
namespace Carbon {
auto FlowFacts::TakeAction(Nonnull<const AstNode*> node, ActionType action,
SourceLocation source_loc, const std::string& name)
-> ErrorOr<Success> {
switch (action) {
case ActionType::AddInit: {
AddFact(node, FormedState::MustBeFormed);
break;
}
case ActionType::AddUninit: {
AddFact(node, FormedState::Unformed);
break;
}
case ActionType::Form: {
// TODO: Use CARBON_CHECK when we are able to handle global variables.
auto entry = facts_.find(node);
if (entry != facts_.end() &&
entry->second.formed_state == FormedState::Unformed) {
entry->second.formed_state = FormedState::MayBeFormed;
}
break;
}
case ActionType::Check: {
// TODO: @slaterlatiao add all available value nodes to flow facts and use
// CARBON_CHECK on the following line.
auto entry = facts_.find(node);
if (entry != facts_.end() &&
entry->second.formed_state == FormedState::Unformed) {
return CompilationError(source_loc)
<< "use of uninitialized variable " << name;
}
break;
}
case ActionType::None:
break;
}
return Success();
}
// Traverses the sub-AST rooted at the given node, resolving the formed/unformed
// states of local variables within it and updating the flow facts.
static auto ResolveUnformed(Nonnull<const Expression*> expression,
FlowFacts& flow_facts, FlowFacts::ActionType action)
-> ErrorOr<Success>;
static auto ResolveUnformed(Nonnull<const Pattern*> pattern,
FlowFacts& flow_facts, FlowFacts::ActionType action)
-> ErrorOr<Success>;
static auto ResolveUnformed(Nonnull<const Statement*> statement,
FlowFacts& flow_facts, FlowFacts::ActionType action)
-> ErrorOr<Success>;
static auto ResolveUnformed(Nonnull<const Declaration*> declaration)
-> ErrorOr<Success>;
static auto ResolveUnformed(Nonnull<const Expression*> expression,
FlowFacts& flow_facts, FlowFacts::ActionType action)
-> ErrorOr<Success> {
switch (expression->kind()) {
case ExpressionKind::IdentifierExpression: {
auto& identifier = cast<IdentifierExpression>(*expression);
CARBON_RETURN_IF_ERROR(
flow_facts.TakeAction(&identifier.value_node().base(), action,
identifier.source_loc(), identifier.name()));
break;
}
case ExpressionKind::CallExpression: {
auto& call = cast<CallExpression>(*expression);
CARBON_RETURN_IF_ERROR(
ResolveUnformed(&call.argument(), flow_facts, action));
break;
}
case ExpressionKind::TupleLiteral:
for (Nonnull<const Expression*> field :
cast<TupleLiteral>(*expression).fields()) {
CARBON_RETURN_IF_ERROR(ResolveUnformed(field, flow_facts, action));
}
break;
case ExpressionKind::OperatorExpression: {
auto& opt_exp = cast<OperatorExpression>(*expression);
if (opt_exp.op() == Operator::AddressOf) {
CARBON_CHECK(opt_exp.arguments().size() == 1)
<< "OperatorExpression with op & can only have 1 argument";
CARBON_RETURN_IF_ERROR(
// When a variable is taken address of, defer the unformed check to
// runtime. A more sound analysis can be implemented when a
// points-to analysis is available.
ResolveUnformed(opt_exp.arguments().front(), flow_facts,
FlowFacts::ActionType::Form));
} else {
for (Nonnull<const Expression*> operand : opt_exp.arguments()) {
CARBON_RETURN_IF_ERROR(ResolveUnformed(operand, flow_facts, action));
}
}
break;
}
case ExpressionKind::StructLiteral:
for (const FieldInitializer& init :
cast<StructLiteral>(*expression).fields()) {
CARBON_RETURN_IF_ERROR(ResolveUnformed(&init.expression(), flow_facts,
FlowFacts::ActionType::Check));
}
break;
case ExpressionKind::SimpleMemberAccessExpression:
CARBON_RETURN_IF_ERROR(ResolveUnformed(
&cast<SimpleMemberAccessExpression>(*expression).object(), flow_facts,
FlowFacts::ActionType::Check));
break;
case ExpressionKind::DotSelfExpression:
case ExpressionKind::IntLiteral:
case ExpressionKind::BoolLiteral:
case ExpressionKind::BoolTypeLiteral:
case ExpressionKind::IntTypeLiteral:
case ExpressionKind::StringLiteral:
case ExpressionKind::StringTypeLiteral:
case ExpressionKind::TypeTypeLiteral:
case ExpressionKind::ContinuationTypeLiteral:
case ExpressionKind::ValueLiteral:
case ExpressionKind::IndexExpression:
case ExpressionKind::CompoundMemberAccessExpression:
case ExpressionKind::IfExpression:
case ExpressionKind::WhereExpression:
case ExpressionKind::StructTypeLiteral:
case ExpressionKind::IntrinsicExpression:
case ExpressionKind::UnimplementedExpression:
case ExpressionKind::FunctionTypeLiteral:
case ExpressionKind::ArrayTypeLiteral:
case ExpressionKind::InstantiateImpl:
break;
}
return Success();
}
static auto ResolveUnformed(Nonnull<const Pattern*> pattern,
FlowFacts& flow_facts, FlowFacts::ActionType action)
-> ErrorOr<Success> {
switch (pattern->kind()) {
case PatternKind::BindingPattern: {
auto& binding_pattern = cast<BindingPattern>(*pattern);
CARBON_RETURN_IF_ERROR(flow_facts.TakeAction(&binding_pattern, action,
binding_pattern.source_loc(),
binding_pattern.name()));
} break;
case PatternKind::TuplePattern:
for (Nonnull<const Pattern*> field :
cast<TuplePattern>(*pattern).fields()) {
CARBON_RETURN_IF_ERROR(ResolveUnformed(field, flow_facts, action));
}
break;
case PatternKind::GenericBinding:
case PatternKind::AlternativePattern:
case PatternKind::ExpressionPattern:
case PatternKind::AutoPattern:
case PatternKind::VarPattern:
case PatternKind::AddrPattern:
// do nothing
break;
}
return Success();
}
static auto ResolveUnformed(Nonnull<const Statement*> statement,
FlowFacts& flow_facts, FlowFacts::ActionType action)
-> ErrorOr<Success> {
switch (statement->kind()) {
case StatementKind::Block: {
auto& block = cast<Block>(*statement);
for (auto* block_statement : block.statements()) {
CARBON_RETURN_IF_ERROR(
ResolveUnformed(block_statement, flow_facts, action));
}
break;
}
case StatementKind::VariableDefinition: {
auto& def = cast<VariableDefinition>(*statement);
if (def.has_init()) {
CARBON_RETURN_IF_ERROR(ResolveUnformed(&def.pattern(), flow_facts,
FlowFacts::ActionType::AddInit));
CARBON_RETURN_IF_ERROR(ResolveUnformed(&def.init(), flow_facts,
FlowFacts::ActionType::Check));
} else {
CARBON_RETURN_IF_ERROR(ResolveUnformed(
&def.pattern(), flow_facts, FlowFacts::ActionType::AddUninit));
}
break;
}
case StatementKind::ReturnVar: {
auto& ret_var = cast<ReturnVar>(*statement);
auto& binding_pattern = cast<BindingPattern>(ret_var.value_node().base());
CARBON_RETURN_IF_ERROR(
flow_facts.TakeAction(&binding_pattern, FlowFacts::ActionType::Check,
ret_var.source_loc(), binding_pattern.name()));
break;
}
case StatementKind::ReturnExpression: {
auto& ret_exp_stmt = cast<ReturnExpression>(*statement);
CARBON_RETURN_IF_ERROR(ResolveUnformed(&ret_exp_stmt.expression(),
flow_facts,
FlowFacts::ActionType::Check));
break;
}
case StatementKind::Assign: {
auto& assign = cast<Assign>(*statement);
if (assign.lhs().kind() == ExpressionKind::IdentifierExpression) {
CARBON_RETURN_IF_ERROR(ResolveUnformed(&assign.lhs(), flow_facts,
FlowFacts::ActionType::Form));
} else {
// TODO: Support checking non-identifier lhs expression.
CARBON_RETURN_IF_ERROR(ResolveUnformed(&assign.lhs(), flow_facts,
FlowFacts::ActionType::None));
}
CARBON_RETURN_IF_ERROR(ResolveUnformed(&assign.rhs(), flow_facts,
FlowFacts::ActionType::Check));
break;
}
case StatementKind::ExpressionStatement: {
auto& exp_stmt = cast<ExpressionStatement>(*statement);
CARBON_RETURN_IF_ERROR(
ResolveUnformed(&exp_stmt.expression(), flow_facts, action));
break;
}
case StatementKind::If: {
auto& if_stmt = cast<If>(*statement);
CARBON_RETURN_IF_ERROR(ResolveUnformed(&if_stmt.condition(), flow_facts,
FlowFacts::ActionType::Check));
CARBON_RETURN_IF_ERROR(
ResolveUnformed(&if_stmt.then_block(), flow_facts, action));
if (if_stmt.else_block().has_value()) {
CARBON_RETURN_IF_ERROR(
ResolveUnformed(*if_stmt.else_block(), flow_facts, action));
}
break;
}
case StatementKind::While: {
auto& while_stmt = cast<While>(*statement);
CARBON_RETURN_IF_ERROR(ResolveUnformed(
&while_stmt.condition(), flow_facts, FlowFacts::ActionType::Check));
CARBON_RETURN_IF_ERROR(
ResolveUnformed(&while_stmt.body(), flow_facts, action));
break;
}
case StatementKind::Match: {
auto& match = cast<Match>(*statement);
CARBON_RETURN_IF_ERROR(ResolveUnformed(&match.expression(), flow_facts,
FlowFacts::ActionType::Check));
for (auto& clause : match.clauses()) {
CARBON_RETURN_IF_ERROR(ResolveUnformed(&clause.pattern(), flow_facts,
FlowFacts::ActionType::Check));
CARBON_RETURN_IF_ERROR(
ResolveUnformed(&clause.statement(), flow_facts, action));
}
break;
}
case StatementKind::Break:
case StatementKind::Continue:
case StatementKind::Continuation:
case StatementKind::Run:
case StatementKind::Await:
case StatementKind::For:
// do nothing
break;
}
return Success();
}
static auto ResolveUnformed(Nonnull<const Declaration*> declaration)
-> ErrorOr<Success> {
switch (declaration->kind()) {
// Checks formed/unformed state intraprocedurally.
// Can be extended to an interprocedural analysis when a call graph is
// available.
case DeclarationKind::FunctionDeclaration: {
auto& function = cast<FunctionDeclaration>(*declaration);
if (function.body().has_value()) {
FlowFacts flow_facts;
CARBON_RETURN_IF_ERROR(ResolveUnformed(*function.body(), flow_facts,
FlowFacts::ActionType::None));
}
break;
}
case DeclarationKind::ClassDeclaration:
case DeclarationKind::InterfaceDeclaration:
case DeclarationKind::ImplDeclaration:
case DeclarationKind::ChoiceDeclaration:
case DeclarationKind::VariableDeclaration:
case DeclarationKind::AssociatedConstantDeclaration:
case DeclarationKind::SelfDeclaration:
case DeclarationKind::AliasDeclaration:
// do nothing
break;
}
return Success();
}
auto ResolveUnformed(const AST& ast) -> ErrorOr<Success> {
for (auto declaration : ast.declarations) {
CARBON_RETURN_IF_ERROR(ResolveUnformed(declaration));
}
return Success();
}
} // namespace Carbon