blob: 8932f98ce6de82c7c982a6bb315c3b4905ada73c [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for statements.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/Stmt.h"
16#include "clang/AST/Expr.h"
17#include "clang/Parse/Scope.h"
18#include "clang/Basic/Diagnostic.h"
19#include "clang/Basic/LangOptions.h"
20#include "clang/Lex/IdentifierTable.h"
21
22using namespace clang;
23
24Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) {
25 Expr *E = static_cast<Expr*>(expr);
Steve Naroff1b8a46c2007-07-27 22:15:19 +000026 assert(E && "ParseExprStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +000027
28 // Exprs are statements, so there is no need to do a conversion here. However,
29 // diagnose some potentially bad code.
30 if (!E->hasLocalSideEffect())
31 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
32
33 return E;
34}
35
36
37Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
38 return new NullStmt(SemiLoc);
39}
40
41Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
42 if (decl)
43 return new DeclStmt(static_cast<Decl *>(decl));
44 else
45 return true; // error
46}
47
48Action::StmtResult
49Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
50 StmtTy **Elts, unsigned NumElts) {
51 return new CompoundStmt((Stmt**)Elts, NumElts);
52}
53
54Action::StmtResult
55Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
56 SourceLocation DotDotDotLoc, ExprTy *rhsval,
57 SourceLocation ColonLoc, StmtTy *subStmt) {
58 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
59 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
60 assert((LHSVal != 0) && "missing expression in case statement");
61
62 SourceLocation ExpLoc;
63 // C99 6.8.4.2p3: The expression shall be an integer constant.
64 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
65 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
66 LHSVal->getSourceRange());
67 return SubStmt;
68 }
69
70 // GCC extension: The expression shall be an integer constant.
71 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
72 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
73 RHSVal->getSourceRange());
74 return SubStmt;
75 }
76
77 if (SwitchStack.empty()) {
78 Diag(CaseLoc, diag::err_case_not_in_switch);
79 return SubStmt;
80 }
81
82 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
83 SwitchStack.back()->addSwitchCase(CS);
84 return CS;
85}
86
87Action::StmtResult
88Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
89 StmtTy *subStmt, Scope *CurScope) {
90 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
91
92 if (SwitchStack.empty()) {
93 Diag(DefaultLoc, diag::err_default_not_in_switch);
94 return SubStmt;
95 }
96
97 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
98 SwitchStack.back()->addSwitchCase(DS);
99
100 return DS;
101}
102
103Action::StmtResult
104Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
105 SourceLocation ColonLoc, StmtTy *subStmt) {
106 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
107 // Look up the record for this label identifier.
108 LabelStmt *&LabelDecl = LabelMap[II];
109
110 // If not forward referenced or defined already, just create a new LabelStmt.
111 if (LabelDecl == 0)
112 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
113
114 assert(LabelDecl->getID() == II && "Label mismatch!");
115
116 // Otherwise, this label was either forward reference or multiply defined. If
117 // multiply defined, reject it now.
118 if (LabelDecl->getSubStmt()) {
119 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
120 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
121 return SubStmt;
122 }
123
124 // Otherwise, this label was forward declared, and we just found its real
125 // definition. Fill in the forward definition and return it.
126 LabelDecl->setIdentLoc(IdentLoc);
127 LabelDecl->setSubStmt(SubStmt);
128 return LabelDecl;
129}
130
131Action::StmtResult
132Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
133 StmtTy *ThenVal, SourceLocation ElseLoc,
134 StmtTy *ElseVal) {
135 Expr *condExpr = (Expr *)CondVal;
136 assert(condExpr && "ParseIfStmt(): missing expression");
137
138 DefaultFunctionArrayConversion(condExpr);
139 QualType condType = condExpr->getType();
140
141 if (!condType->isScalarType()) // C99 6.8.4.1p1
142 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
143 condType.getAsString(), condExpr->getSourceRange());
144
145 return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
146}
147
148Action::StmtResult
149Sema::StartSwitchStmt(ExprTy *Cond) {
150 SwitchStmt *SS = new SwitchStmt((Expr*)Cond);
151 SwitchStack.push_back(SS);
152 return SS;
153}
154
155Action::StmtResult
156Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
157 Stmt *BodyStmt = (Stmt*)Body;
158
159 SwitchStmt *SS = SwitchStack.back();
160 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
161
162 SS->setBody(BodyStmt);
163 SwitchStack.pop_back();
164
165 Expr *condExpr = SS->getCond();
166 QualType condType = condExpr->getType();
167
168 if (!condType->isIntegerType()) { // C99 6.8.4.2p1
169 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
170 condType.getAsString(), condExpr->getSourceRange());
171 return false;
172 }
173
174 DefaultStmt *CurDefaultStmt = 0;
175
176 // FIXME: The list of cases is backwards and needs to be reversed.
177 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
178 SC = SC->getNextSwitchCase()) {
179 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
180 if (CurDefaultStmt) {
181 Diag(DS->getDefaultLoc(),
182 diag::err_multiple_default_labels_defined);
183 Diag(CurDefaultStmt->getDefaultLoc(),
184 diag::err_first_label);
185
186 // FIXME: Remove the default statement from the switch block
187 // so that we'll return a valid AST.
188 } else {
189 CurDefaultStmt = DS;
190 }
191
192 // FIXME: Check that case values are unique here.
193 }
194 }
195
196 return SS;
197}
198
199Action::StmtResult
200Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
201 Expr *condExpr = (Expr *)Cond;
202 assert(condExpr && "ParseWhileStmt(): missing expression");
203
204 DefaultFunctionArrayConversion(condExpr);
205 QualType condType = condExpr->getType();
206
207 if (!condType->isScalarType()) // C99 6.8.5p2
208 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
209 condType.getAsString(), condExpr->getSourceRange());
210
211 return new WhileStmt(condExpr, (Stmt*)Body);
212}
213
214Action::StmtResult
215Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
216 SourceLocation WhileLoc, ExprTy *Cond) {
217 Expr *condExpr = (Expr *)Cond;
218 assert(condExpr && "ParseDoStmt(): missing expression");
219
220 DefaultFunctionArrayConversion(condExpr);
221 QualType condType = condExpr->getType();
222
223 if (!condType->isScalarType()) // C99 6.8.5p2
224 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
225 condType.getAsString(), condExpr->getSourceRange());
226
227 return new DoStmt((Stmt*)Body, condExpr);
228}
229
230Action::StmtResult
231Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
232 StmtTy *First, ExprTy *Second, ExprTy *Third,
233 SourceLocation RParenLoc, StmtTy *Body) {
234 if (First) {
235 // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
236 // declaration support to create a DeclStmt node. Once this is done,
237 // we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
238 }
239 if (Second) {
240 Expr *testExpr = (Expr *)Second;
241 DefaultFunctionArrayConversion(testExpr);
242 QualType testType = testExpr->getType();
243
244 if (!testType->isScalarType()) // C99 6.8.5p2
245 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
246 testType.getAsString(), testExpr->getSourceRange());
247 }
248 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
249}
250
251
252Action::StmtResult
253Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
254 IdentifierInfo *LabelII) {
255 // Look up the record for this label identifier.
256 LabelStmt *&LabelDecl = LabelMap[LabelII];
257
258 // If we haven't seen this label yet, create a forward reference.
259 if (LabelDecl == 0)
260 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
261
262 return new GotoStmt(LabelDecl);
263}
264
265Action::StmtResult
266Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
267 ExprTy *DestExp) {
268 // FIXME: Verify that the operand is convertible to void*.
269
270 return new IndirectGotoStmt((Expr*)DestExp);
271}
272
273Action::StmtResult
274Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
275 Scope *S = CurScope->getContinueParent();
276 if (!S) {
277 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
278 Diag(ContinueLoc, diag::err_continue_not_in_loop);
279 return true;
280 }
281
282 return new ContinueStmt();
283}
284
285Action::StmtResult
286Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
287 Scope *S = CurScope->getBreakParent();
288 if (!S) {
289 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
290 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
291 return true;
292 }
293
294 return new BreakStmt();
295}
296
297
298Action::StmtResult
299Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
300 Expr *RetValExp = static_cast<Expr *>(rex);
301 QualType lhsType = CurFunctionDecl->getResultType();
302
303 if (lhsType->isVoidType()) {
304 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
305 Diag(ReturnLoc, diag::ext_return_has_expr,
306 CurFunctionDecl->getIdentifier()->getName(),
307 RetValExp->getSourceRange());
308 return new ReturnStmt(RetValExp);
309 } else {
310 if (!RetValExp) {
311 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
312 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
313 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
314 else // C90 6.6.6.4p4
315 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
316 return new ReturnStmt((Expr*)0);
317 }
318 }
319 // we have a non-void function with an expression, continue checking
320 QualType rhsType = RetValExp->getType();
321
322 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
323 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
324 // function return.
325 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
326 RetValExp);
Ted Kremenek235e1ad2007-08-14 18:14:14 +0000327
Chris Lattner4b009652007-07-25 00:24:17 +0000328 // decode the result (notice that extensions still return a type).
329 switch (result) {
330 case Compatible:
331 break;
332 case Incompatible:
333 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
334 lhsType.getAsString(), rhsType.getAsString(),
335 RetValExp->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000336 break;
337 case PointerFromInt:
338 // check for null pointer constant (C99 6.3.2.3p3)
339 if (!RetValExp->isNullPointerConstant(Context)) {
340 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
341 lhsType.getAsString(), rhsType.getAsString(),
342 RetValExp->getSourceRange());
343 }
344 break;
345 case IntFromPointer:
346 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
347 lhsType.getAsString(), rhsType.getAsString(),
348 RetValExp->getSourceRange());
349 break;
350 case IncompatiblePointer:
351 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
352 lhsType.getAsString(), rhsType.getAsString(),
353 RetValExp->getSourceRange());
354 break;
355 case CompatiblePointerDiscardsQualifiers:
356 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
357 lhsType.getAsString(), rhsType.getAsString(),
358 RetValExp->getSourceRange());
359 break;
360 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000361
362 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
363
Chris Lattner4b009652007-07-25 00:24:17 +0000364 return new ReturnStmt((Expr*)RetValExp);
365}
366