blob: 7b544a280740699e26c053c94e31a26fcc53c117 [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) {
Steve Naroffcb597472007-09-13 21:41:19 +000036 if (decl) {
37 ScopedDecl *SD = dyn_cast<ScopedDecl>(static_cast<Decl *>(decl));
38 assert(SD && "Sema::ParseDeclStmt(): expected ScopedDecl");
39 return new DeclStmt(SD);
40 } else
Chris Lattner4b009652007-07-25 00:24:17 +000041 return true; // error
42}
43
44Action::StmtResult
45Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnerf2b07572007-08-31 21:49:55 +000046 StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
Chris Lattner3ea3b662007-08-27 04:29:41 +000047 Stmt **Elts = reinterpret_cast<Stmt**>(elts);
48 // If we're in C89 mode, check that we don't have any decls after stmts. If
49 // so, emit an extension diagnostic.
50 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
51 // Note that __extension__ can be around a decl.
52 unsigned i = 0;
53 // Skip over all declarations.
54 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
55 /*empty*/;
56
57 // We found the end of the list or a statement. Scan for another declstmt.
58 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
59 /*empty*/;
60
61 if (i != NumElts) {
Steve Naroffcb597472007-09-13 21:41:19 +000062 ScopedDecl *D = cast<DeclStmt>(Elts[i])->getDecl();
Chris Lattner3ea3b662007-08-27 04:29:41 +000063 Diag(D->getLocation(), diag::ext_mixed_decls_code);
64 }
65 }
Chris Lattnerf2b07572007-08-31 21:49:55 +000066 // Warn about unused expressions in statements.
67 for (unsigned i = 0; i != NumElts; ++i) {
68 Expr *E = dyn_cast<Expr>(Elts[i]);
69 if (!E) continue;
70
71 // Warn about expressions with unused results.
72 if (E->hasLocalSideEffect() || E->getType()->isVoidType())
73 continue;
74
75 // The last expr in a stmt expr really is used.
76 if (isStmtExpr && i == NumElts-1)
77 continue;
78
79 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
80 /// a context where the result is unused. Emit a diagnostic to warn about
81 /// this.
82 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
83 Diag(BO->getOperatorLoc(), diag::warn_unused_expr,
84 BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange());
85 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
86 Diag(UO->getOperatorLoc(), diag::warn_unused_expr,
87 UO->getSubExpr()->getSourceRange());
88 else
89 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
90 }
91
Steve Naroff5d2fff82007-08-31 23:28:33 +000092 return new CompoundStmt(Elts, NumElts, L, R);
Chris Lattner4b009652007-07-25 00:24:17 +000093}
94
95Action::StmtResult
96Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
97 SourceLocation DotDotDotLoc, ExprTy *rhsval,
98 SourceLocation ColonLoc, StmtTy *subStmt) {
99 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
100 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
101 assert((LHSVal != 0) && "missing expression in case statement");
102
103 SourceLocation ExpLoc;
104 // C99 6.8.4.2p3: The expression shall be an integer constant.
105 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
106 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
107 LHSVal->getSourceRange());
108 return SubStmt;
109 }
110
111 // GCC extension: The expression shall be an integer constant.
112 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
113 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
114 RHSVal->getSourceRange());
Chris Lattner3429a812007-08-23 05:46:52 +0000115 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner4b009652007-07-25 00:24:17 +0000116 }
117
118 if (SwitchStack.empty()) {
119 Diag(CaseLoc, diag::err_case_not_in_switch);
120 return SubStmt;
121 }
122
Steve Naroff5d2fff82007-08-31 23:28:33 +0000123 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000124 SwitchStack.back()->addSwitchCase(CS);
125 return CS;
126}
127
128Action::StmtResult
129Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
130 StmtTy *subStmt, Scope *CurScope) {
131 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
132
133 if (SwitchStack.empty()) {
134 Diag(DefaultLoc, diag::err_default_not_in_switch);
135 return SubStmt;
136 }
137
138 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
139 SwitchStack.back()->addSwitchCase(DS);
140
141 return DS;
142}
143
144Action::StmtResult
145Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
146 SourceLocation ColonLoc, StmtTy *subStmt) {
147 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
148 // Look up the record for this label identifier.
149 LabelStmt *&LabelDecl = LabelMap[II];
150
151 // If not forward referenced or defined already, just create a new LabelStmt.
152 if (LabelDecl == 0)
153 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
154
155 assert(LabelDecl->getID() == II && "Label mismatch!");
156
157 // Otherwise, this label was either forward reference or multiply defined. If
158 // multiply defined, reject it now.
159 if (LabelDecl->getSubStmt()) {
160 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
161 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
162 return SubStmt;
163 }
164
165 // Otherwise, this label was forward declared, and we just found its real
166 // definition. Fill in the forward definition and return it.
167 LabelDecl->setIdentLoc(IdentLoc);
168 LabelDecl->setSubStmt(SubStmt);
169 return LabelDecl;
170}
171
172Action::StmtResult
173Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
174 StmtTy *ThenVal, SourceLocation ElseLoc,
175 StmtTy *ElseVal) {
176 Expr *condExpr = (Expr *)CondVal;
177 assert(condExpr && "ParseIfStmt(): missing expression");
178
179 DefaultFunctionArrayConversion(condExpr);
180 QualType condType = condExpr->getType();
181
182 if (!condType->isScalarType()) // C99 6.8.4.1p1
183 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
184 condType.getAsString(), condExpr->getSourceRange());
185
Steve Naroff5d2fff82007-08-31 23:28:33 +0000186 return new IfStmt(IfLoc, condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattner4b009652007-07-25 00:24:17 +0000187}
188
189Action::StmtResult
Chris Lattner3429a812007-08-23 05:46:52 +0000190Sema::StartSwitchStmt(ExprTy *cond) {
191 Expr *Cond = static_cast<Expr*>(cond);
192
193 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
194 UsualUnaryConversions(Cond);
195
196 SwitchStmt *SS = new SwitchStmt(Cond);
Chris Lattner4b009652007-07-25 00:24:17 +0000197 SwitchStack.push_back(SS);
198 return SS;
199}
200
Chris Lattner3429a812007-08-23 05:46:52 +0000201/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
202/// the specified width and sign. If an overflow occurs, detect it and emit
203/// the specified diagnostic.
204void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
205 unsigned NewWidth, bool NewSign,
206 SourceLocation Loc,
207 unsigned DiagID) {
208 // Perform a conversion to the promoted condition type if needed.
209 if (NewWidth > Val.getBitWidth()) {
210 // If this is an extension, just do it.
211 llvm::APSInt OldVal(Val);
212 Val.extend(NewWidth);
213
214 // If the input was signed and negative and the output is unsigned,
215 // warn.
216 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
217 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
218
219 Val.setIsSigned(NewSign);
220 } else if (NewWidth < Val.getBitWidth()) {
221 // If this is a truncation, check for overflow.
222 llvm::APSInt ConvVal(Val);
223 ConvVal.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000224 ConvVal.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000225 ConvVal.extend(Val.getBitWidth());
Chris Lattner5c039602007-08-23 22:08:35 +0000226 ConvVal.setIsSigned(Val.isSigned());
Chris Lattner3429a812007-08-23 05:46:52 +0000227 if (ConvVal != Val)
228 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
229
230 // Regardless of whether a diagnostic was emitted, really do the
231 // truncation.
232 Val.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000233 Val.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000234 } else if (NewSign != Val.isSigned()) {
235 // Convert the sign to match the sign of the condition. This can cause
236 // overflow as well: unsigned(INTMIN)
237 llvm::APSInt OldVal(Val);
238 Val.setIsSigned(NewSign);
239
240 if (Val.isNegative()) // Sign bit changes meaning.
241 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
242 }
243}
244
Chris Lattner0ab833c2007-08-23 18:29:20 +0000245namespace {
246 struct CaseCompareFunctor {
247 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
248 const llvm::APSInt &RHS) {
249 return LHS.first < RHS;
250 }
Chris Lattner2157f272007-09-03 18:31:57 +0000251 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
252 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
253 return LHS.first < RHS.first;
254 }
Chris Lattner0ab833c2007-08-23 18:29:20 +0000255 bool operator()(const llvm::APSInt &LHS,
256 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
257 return LHS < RHS.first;
258 }
259 };
260}
261
Chris Lattner4b009652007-07-25 00:24:17 +0000262Action::StmtResult
263Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
264 Stmt *BodyStmt = (Stmt*)Body;
265
266 SwitchStmt *SS = SwitchStack.back();
267 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
268
Steve Naroffa610eab2007-09-01 21:08:38 +0000269 SS->setBody(BodyStmt, SwitchLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000270 SwitchStack.pop_back();
271
Chris Lattner3429a812007-08-23 05:46:52 +0000272 Expr *CondExpr = SS->getCond();
273 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000274
Chris Lattner3429a812007-08-23 05:46:52 +0000275 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner4b009652007-07-25 00:24:17 +0000276 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattner3429a812007-08-23 05:46:52 +0000277 CondType.getAsString(), CondExpr->getSourceRange());
278 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000279 }
Chris Lattner3429a812007-08-23 05:46:52 +0000280
281 // Get the bitwidth of the switched-on value before promotions. We must
282 // convert the integer case values to this width before comparison.
283 unsigned CondWidth = Context.getTypeSize(CondType, SwitchLoc);
284 bool CondIsSigned = CondType->isSignedIntegerType();
285
286 // Accumulate all of the case values in a vector so that we can sort them
287 // and detect duplicates. This vector contains the APInt for the case after
288 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000289 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
290 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000291
292 // Keep track of any GNU case ranges we see. The APSInt is the low value.
293 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
294
295 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000296
Chris Lattner1a4066d2007-08-23 06:23:56 +0000297 bool CaseListIsErroneous = false;
298
299 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000300 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000301
Chris Lattner4b009652007-07-25 00:24:17 +0000302 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000303 if (TheDefaultStmt) {
304 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
305 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Chris Lattner4b009652007-07-25 00:24:17 +0000306
Chris Lattner3429a812007-08-23 05:46:52 +0000307 // FIXME: Remove the default statement from the switch block so that
308 // we'll return a valid AST. This requires recursing down the
309 // AST and finding it, not something we are set up to do right now. For
310 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000311 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000312 }
Chris Lattner3429a812007-08-23 05:46:52 +0000313 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000314
Chris Lattner3429a812007-08-23 05:46:52 +0000315 } else {
316 CaseStmt *CS = cast<CaseStmt>(SC);
317
318 // We already verified that the expression has a i-c-e value (C99
319 // 6.8.4.2p3) - get that value now.
320 llvm::APSInt LoVal(32);
321 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
322
323 // Convert the value to the same width/sign as the condition.
324 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
325 CS->getLHS()->getLocStart(),
326 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000327
Chris Lattner1a4066d2007-08-23 06:23:56 +0000328 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000329 if (CS->getRHS())
330 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000331 else
332 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000333 }
334 }
335
Chris Lattner1a4066d2007-08-23 06:23:56 +0000336 // Sort all the scalar case values so we can easily detect duplicates.
337 std::stable_sort(CaseVals.begin(), CaseVals.end());
Chris Lattner3429a812007-08-23 05:46:52 +0000338
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000339 if (!CaseVals.empty()) {
340 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
341 if (CaseVals[i].first == CaseVals[i+1].first) {
342 // If we have a duplicate, report it.
343 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
344 diag::err_duplicate_case, CaseVals[i].first.toString());
345 Diag(CaseVals[i].second->getLHS()->getLocStart(),
346 diag::err_duplicate_case_prev);
347 // FIXME: We really want to remove the bogus case stmt from the substmt,
348 // but we have no way to do this right now.
349 CaseListIsErroneous = true;
350 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000351 }
352 }
Chris Lattner3429a812007-08-23 05:46:52 +0000353
Chris Lattner1a4066d2007-08-23 06:23:56 +0000354 // Detect duplicate case ranges, which usually don't exist at all in the first
355 // place.
356 if (!CaseRanges.empty()) {
357 // Sort all the case ranges by their low value so we can easily detect
358 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000359 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000360
361 // Scan the ranges, computing the high values and removing empty ranges.
362 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000363 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000364 CaseStmt *CR = CaseRanges[i].second;
365 llvm::APSInt HiVal(32);
366 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
367
368 // Convert the value to the same width/sign as the condition.
369 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
370 CR->getRHS()->getLocStart(),
371 diag::warn_case_value_overflow);
372
Chris Lattner7443e0f2007-08-23 17:48:14 +0000373 // If the low value is bigger than the high value, the case is empty.
374 if (CaseRanges[i].first > HiVal) {
375 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
376 SourceRange(CR->getLHS()->getLocStart(),
377 CR->getRHS()->getLocEnd()));
378 CaseRanges.erase(CaseRanges.begin()+i);
379 --i, --e;
380 continue;
381 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000382 HiVals.push_back(HiVal);
383 }
384
385 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000386 // ranges. Since the range list is sorted, we only need to compare case
387 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000388 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000389 llvm::APSInt &CRLo = CaseRanges[i].first;
390 llvm::APSInt &CRHi = HiVals[i];
391 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000392
Chris Lattner0ab833c2007-08-23 18:29:20 +0000393 // Check to see whether the case range overlaps with any singleton cases.
394 CaseStmt *OverlapStmt = 0;
395 llvm::APSInt OverlapVal(32);
396
397 // Find the smallest value >= the lower bound. If I is in the case range,
398 // then we have overlap.
399 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
400 CaseVals.end(), CRLo,
401 CaseCompareFunctor());
402 if (I != CaseVals.end() && I->first < CRHi) {
403 OverlapVal = I->first; // Found overlap with scalar.
404 OverlapStmt = I->second;
405 }
406
407 // Find the smallest value bigger than the upper bound.
408 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
409 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
410 OverlapVal = (I-1)->first; // Found overlap with scalar.
411 OverlapStmt = (I-1)->second;
412 }
413
414 // Check to see if this case stmt overlaps with the subsequent case range.
415 if (i && CRLo <= HiVals[i-1]) {
416 OverlapVal = HiVals[i-1]; // Found overlap with range.
417 OverlapStmt = CaseRanges[i-1].second;
418 }
419
420 if (OverlapStmt) {
421 // If we have a duplicate, report it.
422 Diag(CR->getLHS()->getLocStart(),
423 diag::err_duplicate_case, OverlapVal.toString());
424 Diag(OverlapStmt->getLHS()->getLocStart(),
425 diag::err_duplicate_case_prev);
426 // FIXME: We really want to remove the bogus case stmt from the substmt,
427 // but we have no way to do this right now.
428 CaseListIsErroneous = true;
429 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000430 }
431 }
Chris Lattner3429a812007-08-23 05:46:52 +0000432
Chris Lattner1a4066d2007-08-23 06:23:56 +0000433 // FIXME: If the case list was broken is some way, we don't have a good system
434 // to patch it up. Instead, just return the whole substmt as broken.
435 if (CaseListIsErroneous)
436 return true;
Chris Lattner3429a812007-08-23 05:46:52 +0000437
Chris Lattner4b009652007-07-25 00:24:17 +0000438 return SS;
439}
440
441Action::StmtResult
442Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
443 Expr *condExpr = (Expr *)Cond;
444 assert(condExpr && "ParseWhileStmt(): missing expression");
445
446 DefaultFunctionArrayConversion(condExpr);
447 QualType condType = condExpr->getType();
448
449 if (!condType->isScalarType()) // C99 6.8.5p2
450 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
451 condType.getAsString(), condExpr->getSourceRange());
452
Steve Naroff5d2fff82007-08-31 23:28:33 +0000453 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000454}
455
456Action::StmtResult
457Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
458 SourceLocation WhileLoc, ExprTy *Cond) {
459 Expr *condExpr = (Expr *)Cond;
460 assert(condExpr && "ParseDoStmt(): missing expression");
461
462 DefaultFunctionArrayConversion(condExpr);
463 QualType condType = condExpr->getType();
464
465 if (!condType->isScalarType()) // C99 6.8.5p2
466 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
467 condType.getAsString(), condExpr->getSourceRange());
468
Steve Naroff5d2fff82007-08-31 23:28:33 +0000469 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000470}
471
472Action::StmtResult
473Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000474 StmtTy *first, ExprTy *second, ExprTy *third,
475 SourceLocation RParenLoc, StmtTy *body) {
476 Stmt *First = static_cast<Stmt*>(first);
477 Expr *Second = static_cast<Expr*>(second);
478 Expr *Third = static_cast<Expr*>(third);
479 Stmt *Body = static_cast<Stmt*>(body);
480
Chris Lattner06611052007-08-28 05:03:08 +0000481 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
482 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
483 // identifiers for objects having storage class 'auto' or 'register'.
Steve Naroff2591e1b2007-09-13 23:52:58 +0000484 for (ScopedDecl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattner06611052007-08-28 05:03:08 +0000485 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D);
486 if (BVD && !BVD->hasLocalStorage())
487 BVD = 0;
488 if (BVD == 0)
Steve Naroffcb597472007-09-13 21:41:19 +0000489 Diag(dyn_cast<ScopedDecl>(D)->getLocation(),
490 diag::err_non_variable_decl_in_for);
Chris Lattner06611052007-08-28 05:03:08 +0000491 // FIXME: mark decl erroneous!
492 }
Chris Lattner4b009652007-07-25 00:24:17 +0000493 }
494 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000495 DefaultFunctionArrayConversion(Second);
496 QualType SecondType = Second->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000497
Chris Lattner3332fbd2007-08-28 04:55:47 +0000498 if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattner4b009652007-07-25 00:24:17 +0000499 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000500 SecondType.getAsString(), Second->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000501 }
Steve Naroff5d2fff82007-08-31 23:28:33 +0000502 return new ForStmt(First, Second, Third, Body, ForLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000503}
504
505
506Action::StmtResult
507Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
508 IdentifierInfo *LabelII) {
509 // Look up the record for this label identifier.
510 LabelStmt *&LabelDecl = LabelMap[LabelII];
511
512 // If we haven't seen this label yet, create a forward reference.
513 if (LabelDecl == 0)
514 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
515
Ted Kremeneka65ad462007-09-06 17:11:52 +0000516 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000517}
518
519Action::StmtResult
520Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
521 ExprTy *DestExp) {
522 // FIXME: Verify that the operand is convertible to void*.
523
524 return new IndirectGotoStmt((Expr*)DestExp);
525}
526
527Action::StmtResult
528Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
529 Scope *S = CurScope->getContinueParent();
530 if (!S) {
531 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
532 Diag(ContinueLoc, diag::err_continue_not_in_loop);
533 return true;
534 }
535
Steve Naroffc32a20d2007-08-31 23:49:30 +0000536 return new ContinueStmt(ContinueLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000537}
538
539Action::StmtResult
540Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
541 Scope *S = CurScope->getBreakParent();
542 if (!S) {
543 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
544 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
545 return true;
546 }
547
Steve Naroffc32a20d2007-08-31 23:49:30 +0000548 return new BreakStmt(BreakLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000549}
550
551
552Action::StmtResult
553Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
554 Expr *RetValExp = static_cast<Expr *>(rex);
555 QualType lhsType = CurFunctionDecl->getResultType();
556
557 if (lhsType->isVoidType()) {
558 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
559 Diag(ReturnLoc, diag::ext_return_has_expr,
560 CurFunctionDecl->getIdentifier()->getName(),
561 RetValExp->getSourceRange());
Steve Naroffc32a20d2007-08-31 23:49:30 +0000562 return new ReturnStmt(ReturnLoc, RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000563 } else {
564 if (!RetValExp) {
565 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
566 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
567 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
568 else // C90 6.6.6.4p4
569 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
Steve Naroffc32a20d2007-08-31 23:49:30 +0000570 return new ReturnStmt(ReturnLoc, (Expr*)0);
Chris Lattner4b009652007-07-25 00:24:17 +0000571 }
572 }
573 // we have a non-void function with an expression, continue checking
574 QualType rhsType = RetValExp->getType();
575
576 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
577 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
578 // function return.
579 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
580 RetValExp);
Ted Kremenek235e1ad2007-08-14 18:14:14 +0000581
Chris Lattner4b009652007-07-25 00:24:17 +0000582 // decode the result (notice that extensions still return a type).
583 switch (result) {
584 case Compatible:
585 break;
586 case Incompatible:
587 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
588 lhsType.getAsString(), rhsType.getAsString(),
589 RetValExp->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000590 break;
591 case PointerFromInt:
592 // check for null pointer constant (C99 6.3.2.3p3)
593 if (!RetValExp->isNullPointerConstant(Context)) {
594 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
595 lhsType.getAsString(), rhsType.getAsString(),
596 RetValExp->getSourceRange());
597 }
598 break;
599 case IntFromPointer:
600 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
601 lhsType.getAsString(), rhsType.getAsString(),
602 RetValExp->getSourceRange());
603 break;
604 case IncompatiblePointer:
605 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
606 lhsType.getAsString(), rhsType.getAsString(),
607 RetValExp->getSourceRange());
608 break;
609 case CompatiblePointerDiscardsQualifiers:
610 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
611 lhsType.getAsString(), rhsType.getAsString(),
612 RetValExp->getSourceRange());
613 break;
614 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000615
616 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
617
Steve Naroffc32a20d2007-08-31 23:49:30 +0000618 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000619}
620