blob: 9cca2c24f02cbe77e35c55501944bb3aaeda8549 [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"
Chris Lattner3429a812007-08-23 05:46:52 +000015#include "clang/AST/ASTContext.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/Expr.h"
Chris Lattner3429a812007-08-23 05:46:52 +000017#include "clang/AST/Stmt.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/Parse/Scope.h"
19#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/LangOptions.h"
21#include "clang/Lex/IdentifierTable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022using 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 return E;
28}
29
30
31Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
32 return new NullStmt(SemiLoc);
33}
34
35Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
36 if (decl)
37 return new DeclStmt(static_cast<Decl *>(decl));
38 else
39 return true; // error
40}
41
42Action::StmtResult
43Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnerf2b07572007-08-31 21:49:55 +000044 StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
Chris Lattner3ea3b662007-08-27 04:29:41 +000045 Stmt **Elts = reinterpret_cast<Stmt**>(elts);
46 // If we're in C89 mode, check that we don't have any decls after stmts. If
47 // so, emit an extension diagnostic.
48 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
49 // Note that __extension__ can be around a decl.
50 unsigned i = 0;
51 // Skip over all declarations.
52 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
53 /*empty*/;
54
55 // We found the end of the list or a statement. Scan for another declstmt.
56 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
57 /*empty*/;
58
59 if (i != NumElts) {
60 Decl *D = cast<DeclStmt>(Elts[i])->getDecl();
61 Diag(D->getLocation(), diag::ext_mixed_decls_code);
62 }
63 }
Chris Lattnerf2b07572007-08-31 21:49:55 +000064 // Warn about unused expressions in statements.
65 for (unsigned i = 0; i != NumElts; ++i) {
66 Expr *E = dyn_cast<Expr>(Elts[i]);
67 if (!E) continue;
68
69 // Warn about expressions with unused results.
70 if (E->hasLocalSideEffect() || E->getType()->isVoidType())
71 continue;
72
73 // The last expr in a stmt expr really is used.
74 if (isStmtExpr && i == NumElts-1)
75 continue;
76
77 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
78 /// a context where the result is unused. Emit a diagnostic to warn about
79 /// this.
80 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
81 Diag(BO->getOperatorLoc(), diag::warn_unused_expr,
82 BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange());
83 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
84 Diag(UO->getOperatorLoc(), diag::warn_unused_expr,
85 UO->getSubExpr()->getSourceRange());
86 else
87 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
88 }
89
Steve Naroff5d2fff82007-08-31 23:28:33 +000090 return new CompoundStmt(Elts, NumElts, L, R);
Chris Lattner4b009652007-07-25 00:24:17 +000091}
92
93Action::StmtResult
94Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
95 SourceLocation DotDotDotLoc, ExprTy *rhsval,
96 SourceLocation ColonLoc, StmtTy *subStmt) {
97 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
98 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
99 assert((LHSVal != 0) && "missing expression in case statement");
100
101 SourceLocation ExpLoc;
102 // C99 6.8.4.2p3: The expression shall be an integer constant.
103 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
104 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
105 LHSVal->getSourceRange());
106 return SubStmt;
107 }
108
109 // GCC extension: The expression shall be an integer constant.
110 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
111 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
112 RHSVal->getSourceRange());
Chris Lattner3429a812007-08-23 05:46:52 +0000113 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner4b009652007-07-25 00:24:17 +0000114 }
115
116 if (SwitchStack.empty()) {
117 Diag(CaseLoc, diag::err_case_not_in_switch);
118 return SubStmt;
119 }
120
Steve Naroff5d2fff82007-08-31 23:28:33 +0000121 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000122 SwitchStack.back()->addSwitchCase(CS);
123 return CS;
124}
125
126Action::StmtResult
127Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
128 StmtTy *subStmt, Scope *CurScope) {
129 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
130
131 if (SwitchStack.empty()) {
132 Diag(DefaultLoc, diag::err_default_not_in_switch);
133 return SubStmt;
134 }
135
136 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
137 SwitchStack.back()->addSwitchCase(DS);
138
139 return DS;
140}
141
142Action::StmtResult
143Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
144 SourceLocation ColonLoc, StmtTy *subStmt) {
145 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
146 // Look up the record for this label identifier.
147 LabelStmt *&LabelDecl = LabelMap[II];
148
149 // If not forward referenced or defined already, just create a new LabelStmt.
150 if (LabelDecl == 0)
151 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
152
153 assert(LabelDecl->getID() == II && "Label mismatch!");
154
155 // Otherwise, this label was either forward reference or multiply defined. If
156 // multiply defined, reject it now.
157 if (LabelDecl->getSubStmt()) {
158 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
159 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
160 return SubStmt;
161 }
162
163 // Otherwise, this label was forward declared, and we just found its real
164 // definition. Fill in the forward definition and return it.
165 LabelDecl->setIdentLoc(IdentLoc);
166 LabelDecl->setSubStmt(SubStmt);
167 return LabelDecl;
168}
169
170Action::StmtResult
171Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
172 StmtTy *ThenVal, SourceLocation ElseLoc,
173 StmtTy *ElseVal) {
174 Expr *condExpr = (Expr *)CondVal;
175 assert(condExpr && "ParseIfStmt(): missing expression");
176
177 DefaultFunctionArrayConversion(condExpr);
178 QualType condType = condExpr->getType();
179
180 if (!condType->isScalarType()) // C99 6.8.4.1p1
181 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
182 condType.getAsString(), condExpr->getSourceRange());
183
Steve Naroff5d2fff82007-08-31 23:28:33 +0000184 return new IfStmt(IfLoc, condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattner4b009652007-07-25 00:24:17 +0000185}
186
187Action::StmtResult
Chris Lattner3429a812007-08-23 05:46:52 +0000188Sema::StartSwitchStmt(ExprTy *cond) {
189 Expr *Cond = static_cast<Expr*>(cond);
190
191 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
192 UsualUnaryConversions(Cond);
193
194 SwitchStmt *SS = new SwitchStmt(Cond);
Chris Lattner4b009652007-07-25 00:24:17 +0000195 SwitchStack.push_back(SS);
196 return SS;
197}
198
Chris Lattner3429a812007-08-23 05:46:52 +0000199/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
200/// the specified width and sign. If an overflow occurs, detect it and emit
201/// the specified diagnostic.
202void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
203 unsigned NewWidth, bool NewSign,
204 SourceLocation Loc,
205 unsigned DiagID) {
206 // Perform a conversion to the promoted condition type if needed.
207 if (NewWidth > Val.getBitWidth()) {
208 // If this is an extension, just do it.
209 llvm::APSInt OldVal(Val);
210 Val.extend(NewWidth);
211
212 // If the input was signed and negative and the output is unsigned,
213 // warn.
214 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
215 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
216
217 Val.setIsSigned(NewSign);
218 } else if (NewWidth < Val.getBitWidth()) {
219 // If this is a truncation, check for overflow.
220 llvm::APSInt ConvVal(Val);
221 ConvVal.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000222 ConvVal.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000223 ConvVal.extend(Val.getBitWidth());
Chris Lattner5c039602007-08-23 22:08:35 +0000224 ConvVal.setIsSigned(Val.isSigned());
Chris Lattner3429a812007-08-23 05:46:52 +0000225 if (ConvVal != Val)
226 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
227
228 // Regardless of whether a diagnostic was emitted, really do the
229 // truncation.
230 Val.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000231 Val.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000232 } else if (NewSign != Val.isSigned()) {
233 // Convert the sign to match the sign of the condition. This can cause
234 // overflow as well: unsigned(INTMIN)
235 llvm::APSInt OldVal(Val);
236 Val.setIsSigned(NewSign);
237
238 if (Val.isNegative()) // Sign bit changes meaning.
239 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
240 }
241}
242
Chris Lattner0ab833c2007-08-23 18:29:20 +0000243namespace {
244 struct CaseCompareFunctor {
245 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
246 const llvm::APSInt &RHS) {
247 return LHS.first < RHS;
248 }
Chris Lattner2157f272007-09-03 18:31:57 +0000249 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
250 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
251 return LHS.first < RHS.first;
252 }
Chris Lattner0ab833c2007-08-23 18:29:20 +0000253 bool operator()(const llvm::APSInt &LHS,
254 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
255 return LHS < RHS.first;
256 }
257 };
258}
259
Chris Lattner4b009652007-07-25 00:24:17 +0000260Action::StmtResult
261Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
262 Stmt *BodyStmt = (Stmt*)Body;
263
264 SwitchStmt *SS = SwitchStack.back();
265 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
266
Steve Naroffa610eab2007-09-01 21:08:38 +0000267 SS->setBody(BodyStmt, SwitchLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000268 SwitchStack.pop_back();
269
Chris Lattner3429a812007-08-23 05:46:52 +0000270 Expr *CondExpr = SS->getCond();
271 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000272
Chris Lattner3429a812007-08-23 05:46:52 +0000273 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner4b009652007-07-25 00:24:17 +0000274 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattner3429a812007-08-23 05:46:52 +0000275 CondType.getAsString(), CondExpr->getSourceRange());
276 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000277 }
Chris Lattner3429a812007-08-23 05:46:52 +0000278
279 // Get the bitwidth of the switched-on value before promotions. We must
280 // convert the integer case values to this width before comparison.
281 unsigned CondWidth = Context.getTypeSize(CondType, SwitchLoc);
282 bool CondIsSigned = CondType->isSignedIntegerType();
283
284 // Accumulate all of the case values in a vector so that we can sort them
285 // and detect duplicates. This vector contains the APInt for the case after
286 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000287 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
288 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000289
290 // Keep track of any GNU case ranges we see. The APSInt is the low value.
291 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
292
293 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000294
Chris Lattner1a4066d2007-08-23 06:23:56 +0000295 bool CaseListIsErroneous = false;
296
297 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000298 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000299
Chris Lattner4b009652007-07-25 00:24:17 +0000300 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000301 if (TheDefaultStmt) {
302 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
303 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Chris Lattner4b009652007-07-25 00:24:17 +0000304
Chris Lattner3429a812007-08-23 05:46:52 +0000305 // FIXME: Remove the default statement from the switch block so that
306 // we'll return a valid AST. This requires recursing down the
307 // AST and finding it, not something we are set up to do right now. For
308 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000309 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000310 }
Chris Lattner3429a812007-08-23 05:46:52 +0000311 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000312
Chris Lattner3429a812007-08-23 05:46:52 +0000313 } else {
314 CaseStmt *CS = cast<CaseStmt>(SC);
315
316 // We already verified that the expression has a i-c-e value (C99
317 // 6.8.4.2p3) - get that value now.
318 llvm::APSInt LoVal(32);
319 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
320
321 // Convert the value to the same width/sign as the condition.
322 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
323 CS->getLHS()->getLocStart(),
324 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000325
Chris Lattner1a4066d2007-08-23 06:23:56 +0000326 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000327 if (CS->getRHS())
328 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000329 else
330 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000331 }
332 }
333
Chris Lattner1a4066d2007-08-23 06:23:56 +0000334 // Sort all the scalar case values so we can easily detect duplicates.
335 std::stable_sort(CaseVals.begin(), CaseVals.end());
Chris Lattner3429a812007-08-23 05:46:52 +0000336
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000337 if (!CaseVals.empty()) {
338 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
339 if (CaseVals[i].first == CaseVals[i+1].first) {
340 // If we have a duplicate, report it.
341 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
342 diag::err_duplicate_case, CaseVals[i].first.toString());
343 Diag(CaseVals[i].second->getLHS()->getLocStart(),
344 diag::err_duplicate_case_prev);
345 // FIXME: We really want to remove the bogus case stmt from the substmt,
346 // but we have no way to do this right now.
347 CaseListIsErroneous = true;
348 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000349 }
350 }
Chris Lattner3429a812007-08-23 05:46:52 +0000351
Chris Lattner1a4066d2007-08-23 06:23:56 +0000352 // Detect duplicate case ranges, which usually don't exist at all in the first
353 // place.
354 if (!CaseRanges.empty()) {
355 // Sort all the case ranges by their low value so we can easily detect
356 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000357 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000358
359 // Scan the ranges, computing the high values and removing empty ranges.
360 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000361 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000362 CaseStmt *CR = CaseRanges[i].second;
363 llvm::APSInt HiVal(32);
364 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
365
366 // Convert the value to the same width/sign as the condition.
367 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
368 CR->getRHS()->getLocStart(),
369 diag::warn_case_value_overflow);
370
Chris Lattner7443e0f2007-08-23 17:48:14 +0000371 // If the low value is bigger than the high value, the case is empty.
372 if (CaseRanges[i].first > HiVal) {
373 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
374 SourceRange(CR->getLHS()->getLocStart(),
375 CR->getRHS()->getLocEnd()));
376 CaseRanges.erase(CaseRanges.begin()+i);
377 --i, --e;
378 continue;
379 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000380 HiVals.push_back(HiVal);
381 }
382
383 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000384 // ranges. Since the range list is sorted, we only need to compare case
385 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000386 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000387 llvm::APSInt &CRLo = CaseRanges[i].first;
388 llvm::APSInt &CRHi = HiVals[i];
389 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000390
Chris Lattner0ab833c2007-08-23 18:29:20 +0000391 // Check to see whether the case range overlaps with any singleton cases.
392 CaseStmt *OverlapStmt = 0;
393 llvm::APSInt OverlapVal(32);
394
395 // Find the smallest value >= the lower bound. If I is in the case range,
396 // then we have overlap.
397 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
398 CaseVals.end(), CRLo,
399 CaseCompareFunctor());
400 if (I != CaseVals.end() && I->first < CRHi) {
401 OverlapVal = I->first; // Found overlap with scalar.
402 OverlapStmt = I->second;
403 }
404
405 // Find the smallest value bigger than the upper bound.
406 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
407 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
408 OverlapVal = (I-1)->first; // Found overlap with scalar.
409 OverlapStmt = (I-1)->second;
410 }
411
412 // Check to see if this case stmt overlaps with the subsequent case range.
413 if (i && CRLo <= HiVals[i-1]) {
414 OverlapVal = HiVals[i-1]; // Found overlap with range.
415 OverlapStmt = CaseRanges[i-1].second;
416 }
417
418 if (OverlapStmt) {
419 // If we have a duplicate, report it.
420 Diag(CR->getLHS()->getLocStart(),
421 diag::err_duplicate_case, OverlapVal.toString());
422 Diag(OverlapStmt->getLHS()->getLocStart(),
423 diag::err_duplicate_case_prev);
424 // FIXME: We really want to remove the bogus case stmt from the substmt,
425 // but we have no way to do this right now.
426 CaseListIsErroneous = true;
427 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000428 }
429 }
Chris Lattner3429a812007-08-23 05:46:52 +0000430
Chris Lattner1a4066d2007-08-23 06:23:56 +0000431 // FIXME: If the case list was broken is some way, we don't have a good system
432 // to patch it up. Instead, just return the whole substmt as broken.
433 if (CaseListIsErroneous)
434 return true;
Chris Lattner3429a812007-08-23 05:46:52 +0000435
Chris Lattner4b009652007-07-25 00:24:17 +0000436 return SS;
437}
438
439Action::StmtResult
440Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
441 Expr *condExpr = (Expr *)Cond;
442 assert(condExpr && "ParseWhileStmt(): missing expression");
443
444 DefaultFunctionArrayConversion(condExpr);
445 QualType condType = condExpr->getType();
446
447 if (!condType->isScalarType()) // C99 6.8.5p2
448 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
449 condType.getAsString(), condExpr->getSourceRange());
450
Steve Naroff5d2fff82007-08-31 23:28:33 +0000451 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000452}
453
454Action::StmtResult
455Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
456 SourceLocation WhileLoc, ExprTy *Cond) {
457 Expr *condExpr = (Expr *)Cond;
458 assert(condExpr && "ParseDoStmt(): missing expression");
459
460 DefaultFunctionArrayConversion(condExpr);
461 QualType condType = condExpr->getType();
462
463 if (!condType->isScalarType()) // C99 6.8.5p2
464 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
465 condType.getAsString(), condExpr->getSourceRange());
466
Steve Naroff5d2fff82007-08-31 23:28:33 +0000467 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000468}
469
470Action::StmtResult
471Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000472 StmtTy *first, ExprTy *second, ExprTy *third,
473 SourceLocation RParenLoc, StmtTy *body) {
474 Stmt *First = static_cast<Stmt*>(first);
475 Expr *Second = static_cast<Expr*>(second);
476 Expr *Third = static_cast<Expr*>(third);
477 Stmt *Body = static_cast<Stmt*>(body);
478
Chris Lattner06611052007-08-28 05:03:08 +0000479 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
480 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
481 // identifiers for objects having storage class 'auto' or 'register'.
482 for (Decl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
483 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D);
484 if (BVD && !BVD->hasLocalStorage())
485 BVD = 0;
486 if (BVD == 0)
487 Diag(D->getLocation(), diag::err_non_variable_decl_in_for);
488 // FIXME: mark decl erroneous!
489 }
Chris Lattner4b009652007-07-25 00:24:17 +0000490 }
491 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000492 DefaultFunctionArrayConversion(Second);
493 QualType SecondType = Second->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000494
Chris Lattner3332fbd2007-08-28 04:55:47 +0000495 if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattner4b009652007-07-25 00:24:17 +0000496 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000497 SecondType.getAsString(), Second->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000498 }
Steve Naroff5d2fff82007-08-31 23:28:33 +0000499 return new ForStmt(First, Second, Third, Body, ForLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000500}
501
502
503Action::StmtResult
504Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
505 IdentifierInfo *LabelII) {
506 // Look up the record for this label identifier.
507 LabelStmt *&LabelDecl = LabelMap[LabelII];
508
509 // If we haven't seen this label yet, create a forward reference.
510 if (LabelDecl == 0)
511 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
512
Ted Kremeneka65ad462007-09-06 17:11:52 +0000513 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000514}
515
516Action::StmtResult
517Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
518 ExprTy *DestExp) {
519 // FIXME: Verify that the operand is convertible to void*.
520
521 return new IndirectGotoStmt((Expr*)DestExp);
522}
523
524Action::StmtResult
525Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
526 Scope *S = CurScope->getContinueParent();
527 if (!S) {
528 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
529 Diag(ContinueLoc, diag::err_continue_not_in_loop);
530 return true;
531 }
532
Steve Naroffc32a20d2007-08-31 23:49:30 +0000533 return new ContinueStmt(ContinueLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000534}
535
536Action::StmtResult
537Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
538 Scope *S = CurScope->getBreakParent();
539 if (!S) {
540 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
541 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
542 return true;
543 }
544
Steve Naroffc32a20d2007-08-31 23:49:30 +0000545 return new BreakStmt(BreakLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000546}
547
548
549Action::StmtResult
550Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
551 Expr *RetValExp = static_cast<Expr *>(rex);
552 QualType lhsType = CurFunctionDecl->getResultType();
553
554 if (lhsType->isVoidType()) {
555 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
556 Diag(ReturnLoc, diag::ext_return_has_expr,
557 CurFunctionDecl->getIdentifier()->getName(),
558 RetValExp->getSourceRange());
Steve Naroffc32a20d2007-08-31 23:49:30 +0000559 return new ReturnStmt(ReturnLoc, RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000560 } else {
561 if (!RetValExp) {
562 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
563 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
564 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
565 else // C90 6.6.6.4p4
566 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
Steve Naroffc32a20d2007-08-31 23:49:30 +0000567 return new ReturnStmt(ReturnLoc, (Expr*)0);
Chris Lattner4b009652007-07-25 00:24:17 +0000568 }
569 }
570 // we have a non-void function with an expression, continue checking
571 QualType rhsType = RetValExp->getType();
572
573 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
574 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
575 // function return.
576 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
577 RetValExp);
Ted Kremenek235e1ad2007-08-14 18:14:14 +0000578
Chris Lattner4b009652007-07-25 00:24:17 +0000579 // decode the result (notice that extensions still return a type).
580 switch (result) {
581 case Compatible:
582 break;
583 case Incompatible:
584 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
585 lhsType.getAsString(), rhsType.getAsString(),
586 RetValExp->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000587 break;
588 case PointerFromInt:
589 // check for null pointer constant (C99 6.3.2.3p3)
590 if (!RetValExp->isNullPointerConstant(Context)) {
591 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
592 lhsType.getAsString(), rhsType.getAsString(),
593 RetValExp->getSourceRange());
594 }
595 break;
596 case IntFromPointer:
597 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
598 lhsType.getAsString(), rhsType.getAsString(),
599 RetValExp->getSourceRange());
600 break;
601 case IncompatiblePointer:
602 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
603 lhsType.getAsString(), rhsType.getAsString(),
604 RetValExp->getSourceRange());
605 break;
606 case CompatiblePointerDiscardsQualifiers:
607 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
608 lhsType.getAsString(), rhsType.getAsString(),
609 RetValExp->getSourceRange());
610 break;
611 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000612
613 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
614
Steve Naroffc32a20d2007-08-31 23:49:30 +0000615 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000616}
617