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
|
// slang-check-stmt.cpp
#include "slang-check-impl.h"
// This file implements semantic checking logic related to statements.
namespace Slang
{
namespace
{
/// RAII-like type for establishing an "outer" statement during nested checks.
///
/// The `SemanticsStmtVisitor` maintains a linked list of outer statements
/// using `OuterStmtInfo` records stored on the recursive call stack during
/// checking. This type creates a sub-`SemanticsStmtVisitor` that has one
/// additional outer statement added to the stack of outer statements.
///
/// The outer statements are used to validate and resolve things like
/// the target of `break` or `continue` statements.
///
struct WithOuterStmt : public SemanticsStmtVisitor
{
public:
WithOuterStmt(SemanticsStmtVisitor* visitor, Stmt* outerStmt)
: SemanticsStmtVisitor(*visitor)
{
m_parentFunc = visitor->m_parentFunc;
m_outerStmt.next = visitor->m_outerStmts;
m_outerStmt.stmt = outerStmt;
m_outerStmts = &m_outerStmt;
}
private:
OuterStmtInfo m_outerStmt;
};
}
void SemanticsVisitor::checkStmt(Stmt* stmt, FunctionDeclBase* parentDecl, OuterStmtInfo* outerStmts)
{
if (!stmt) return;
dispatchStmt(stmt, parentDecl, outerStmts);
checkModifiers(stmt);
}
void SemanticsStmtVisitor::visitDeclStmt(DeclStmt* stmt)
{
// When we encounter a declaration during statement checking,
// we expect that it hasn't been checked yet (because otherwise
// it would be referenced before its declaration point), but
// we will bottleneck through the `ensureDecl()` path anyway,
// to unify with the rest of semantic checking.
//
// TODO: This logic might not suffice for something like a
// local `struct` declaration, where it would have members
// that need to be recursively checked.
//
ensureDeclBase(stmt->decl, DeclCheckState::Checked);
}
void SemanticsStmtVisitor::visitBlockStmt(BlockStmt* stmt)
{
checkStmt(stmt->body);
}
void SemanticsStmtVisitor::visitSeqStmt(SeqStmt* stmt)
{
for(auto ss : stmt->stmts)
{
checkStmt(ss);
}
}
void SemanticsStmtVisitor::checkStmt(Stmt* stmt)
{
SemanticsVisitor::checkStmt(stmt, m_parentFunc, m_outerStmts);
}
template<typename T>
T* SemanticsStmtVisitor::FindOuterStmt()
{
for(auto outerStmtInfo = m_outerStmts; outerStmtInfo; outerStmtInfo = outerStmtInfo->next)
{
auto outerStmt = outerStmtInfo->stmt;
auto found = as<T>(outerStmt);
if (found)
return found;
}
return nullptr;
}
void SemanticsStmtVisitor::visitBreakStmt(BreakStmt *stmt)
{
auto outer = FindOuterStmt<BreakableStmt>();
if (!outer)
{
getSink()->diagnose(stmt, Diagnostics::breakOutsideLoop);
}
stmt->parentStmt = outer;
}
void SemanticsStmtVisitor::visitContinueStmt(ContinueStmt *stmt)
{
auto outer = FindOuterStmt<LoopStmt>();
if (!outer)
{
getSink()->diagnose(stmt, Diagnostics::continueOutsideLoop);
}
stmt->parentStmt = outer;
}
Expr* SemanticsVisitor::checkPredicateExpr(Expr* expr)
{
Expr* e = expr;
e = CheckTerm(e);
e = coerce(m_astBuilder->getBoolType(), e);
return e;
}
void SemanticsStmtVisitor::visitDoWhileStmt(DoWhileStmt *stmt)
{
WithOuterStmt subContext(this, stmt);
stmt->predicate = checkPredicateExpr(stmt->predicate);
subContext.checkStmt(stmt->statement);
}
void SemanticsStmtVisitor::visitForStmt(ForStmt *stmt)
{
WithOuterStmt subContext(this, stmt);
checkStmt(stmt->initialStatement);
if (stmt->predicateExpression)
{
stmt->predicateExpression = checkPredicateExpr(stmt->predicateExpression);
}
if (stmt->sideEffectExpression)
{
stmt->sideEffectExpression = CheckExpr(stmt->sideEffectExpression);
}
subContext.checkStmt(stmt->statement);
}
Expr* SemanticsVisitor::checkExpressionAndExpectIntegerConstant(Expr* expr, IntVal** outIntVal)
{
expr = CheckExpr(expr);
auto intVal = CheckIntegerConstantExpression(expr);
if (outIntVal)
*outIntVal = intVal;
return expr;
}
void SemanticsStmtVisitor::visitCompileTimeForStmt(CompileTimeForStmt* stmt)
{
WithOuterStmt subContext(this, stmt);
stmt->varDecl->type.type = m_astBuilder->getIntType();
addModifier(stmt->varDecl, m_astBuilder->create<ConstModifier>());
stmt->varDecl->setCheckState(DeclCheckState::Checked);
IntVal* rangeBeginVal = nullptr;
IntVal* rangeEndVal = nullptr;
if (stmt->rangeBeginExpr)
{
stmt->rangeBeginExpr = checkExpressionAndExpectIntegerConstant(stmt->rangeBeginExpr, &rangeBeginVal);
}
else
{
ConstantIntVal* rangeBeginConst = m_astBuilder->create<ConstantIntVal>();
rangeBeginConst->value = 0;
rangeBeginVal = rangeBeginConst;
}
stmt->rangeEndExpr = checkExpressionAndExpectIntegerConstant(stmt->rangeEndExpr, &rangeEndVal);
stmt->rangeBeginVal = rangeBeginVal;
stmt->rangeEndVal = rangeEndVal;
subContext.checkStmt(stmt->body);
}
void SemanticsStmtVisitor::visitSwitchStmt(SwitchStmt* stmt)
{
WithOuterStmt subContext(this, stmt);
// TODO(tfoley): need to coerce condition to an integral type...
stmt->condition = CheckExpr(stmt->condition);
subContext.checkStmt(stmt->body);
// TODO(tfoley): need to check that all case tags are unique
// TODO(tfoley): check that there is at most one `default` clause
}
void SemanticsStmtVisitor::visitCaseStmt(CaseStmt* stmt)
{
// TODO(tfoley): Need to coerce to type being switch on,
// and ensure that value is a compile-time constant
auto expr = CheckExpr(stmt->expr);
auto switchStmt = FindOuterStmt<SwitchStmt>();
if (!switchStmt)
{
getSink()->diagnose(stmt, Diagnostics::caseOutsideSwitch);
}
else
{
// TODO: need to do some basic matching to ensure the type
// for the `case` is consistent with the type for the `switch`...
}
stmt->expr = expr;
stmt->parentStmt = switchStmt;
}
void SemanticsStmtVisitor::visitDefaultStmt(DefaultStmt* stmt)
{
auto switchStmt = FindOuterStmt<SwitchStmt>();
if (!switchStmt)
{
getSink()->diagnose(stmt, Diagnostics::defaultOutsideSwitch);
}
stmt->parentStmt = switchStmt;
}
void SemanticsStmtVisitor::visitIfStmt(IfStmt *stmt)
{
stmt->predicate = checkPredicateExpr(stmt->predicate);
checkStmt(stmt->positiveStatement);
checkStmt(stmt->negativeStatement);
}
void SemanticsStmtVisitor::visitUnparsedStmt(UnparsedStmt*)
{
// Nothing to do
}
void SemanticsStmtVisitor::visitEmptyStmt(EmptyStmt*)
{
// Nothing to do
}
void SemanticsStmtVisitor::visitDiscardStmt(DiscardStmt*)
{
// Nothing to do
}
void SemanticsStmtVisitor::visitReturnStmt(ReturnStmt *stmt)
{
auto function = getParentFunc();
if (!stmt->expression)
{
if (function && !function->returnType.equals(m_astBuilder->getVoidType()))
{
getSink()->diagnose(stmt, Diagnostics::returnNeedsExpression);
}
}
else
{
stmt->expression = CheckTerm(stmt->expression);
if (!stmt->expression->type->equals(m_astBuilder->getErrorType()))
{
if (function)
{
stmt->expression = coerce(function->returnType.Ptr(), stmt->expression);
}
else
{
// TODO(tfoley): this case currently gets triggered for member functions,
// which aren't being checked consistently (because of the whole symbol
// table idea getting in the way).
// getSink()->diagnose(stmt, Diagnostics::unimplemented, "case for return stmt");
}
}
}
}
void SemanticsStmtVisitor::visitWhileStmt(WhileStmt *stmt)
{
WithOuterStmt subContext(this, stmt);
stmt->predicate = checkPredicateExpr(stmt->predicate);
subContext.checkStmt(stmt->statement);
}
void SemanticsStmtVisitor::visitExpressionStmt(ExpressionStmt *stmt)
{
stmt->expression = CheckExpr(stmt->expression);
}
void SemanticsStmtVisitor::visitGpuForeachStmt(GpuForeachStmt*stmt)
{
stmt->device = CheckExpr(stmt->device);
stmt->gridDims = CheckExpr(stmt->gridDims);
ensureDeclBase(stmt->dispatchThreadID, DeclCheckState::Checked);
WithOuterStmt subContext(this, stmt);
stmt->kernelCall = subContext.CheckExpr(stmt->kernelCall);
return;
}
}
|