blob: 1c26202c76d7d0b494ec64b3e3c12231447ba379 [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 }
249 bool operator()(const llvm::APSInt &LHS,
250 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
251 return LHS < RHS.first;
252 }
253 };
254}
255
Chris Lattner4b009652007-07-25 00:24:17 +0000256Action::StmtResult
257Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
258 Stmt *BodyStmt = (Stmt*)Body;
259
260 SwitchStmt *SS = SwitchStack.back();
261 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
262
263 SS->setBody(BodyStmt);
264 SwitchStack.pop_back();
265
Chris Lattner3429a812007-08-23 05:46:52 +0000266 Expr *CondExpr = SS->getCond();
267 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000268
Chris Lattner3429a812007-08-23 05:46:52 +0000269 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner4b009652007-07-25 00:24:17 +0000270 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattner3429a812007-08-23 05:46:52 +0000271 CondType.getAsString(), CondExpr->getSourceRange());
272 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000273 }
Chris Lattner3429a812007-08-23 05:46:52 +0000274
275 // Get the bitwidth of the switched-on value before promotions. We must
276 // convert the integer case values to this width before comparison.
277 unsigned CondWidth = Context.getTypeSize(CondType, SwitchLoc);
278 bool CondIsSigned = CondType->isSignedIntegerType();
279
280 // Accumulate all of the case values in a vector so that we can sort them
281 // and detect duplicates. This vector contains the APInt for the case after
282 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000283 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
284 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000285
286 // Keep track of any GNU case ranges we see. The APSInt is the low value.
287 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
288
289 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000290
Chris Lattner1a4066d2007-08-23 06:23:56 +0000291 bool CaseListIsErroneous = false;
292
293 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000294 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000295
Chris Lattner4b009652007-07-25 00:24:17 +0000296 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000297 if (TheDefaultStmt) {
298 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
299 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Chris Lattner4b009652007-07-25 00:24:17 +0000300
Chris Lattner3429a812007-08-23 05:46:52 +0000301 // FIXME: Remove the default statement from the switch block so that
302 // we'll return a valid AST. This requires recursing down the
303 // AST and finding it, not something we are set up to do right now. For
304 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000305 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000306 }
Chris Lattner3429a812007-08-23 05:46:52 +0000307 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000308
Chris Lattner3429a812007-08-23 05:46:52 +0000309 } else {
310 CaseStmt *CS = cast<CaseStmt>(SC);
311
312 // We already verified that the expression has a i-c-e value (C99
313 // 6.8.4.2p3) - get that value now.
314 llvm::APSInt LoVal(32);
315 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
316
317 // Convert the value to the same width/sign as the condition.
318 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
319 CS->getLHS()->getLocStart(),
320 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000321
Chris Lattner1a4066d2007-08-23 06:23:56 +0000322 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000323 if (CS->getRHS())
324 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000325 else
326 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000327 }
328 }
329
Chris Lattner1a4066d2007-08-23 06:23:56 +0000330 // Sort all the scalar case values so we can easily detect duplicates.
331 std::stable_sort(CaseVals.begin(), CaseVals.end());
Chris Lattner3429a812007-08-23 05:46:52 +0000332
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000333 if (!CaseVals.empty()) {
334 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
335 if (CaseVals[i].first == CaseVals[i+1].first) {
336 // If we have a duplicate, report it.
337 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
338 diag::err_duplicate_case, CaseVals[i].first.toString());
339 Diag(CaseVals[i].second->getLHS()->getLocStart(),
340 diag::err_duplicate_case_prev);
341 // FIXME: We really want to remove the bogus case stmt from the substmt,
342 // but we have no way to do this right now.
343 CaseListIsErroneous = true;
344 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000345 }
346 }
Chris Lattner3429a812007-08-23 05:46:52 +0000347
Chris Lattner1a4066d2007-08-23 06:23:56 +0000348 // Detect duplicate case ranges, which usually don't exist at all in the first
349 // place.
350 if (!CaseRanges.empty()) {
351 // Sort all the case ranges by their low value so we can easily detect
352 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000353 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000354
355 // Scan the ranges, computing the high values and removing empty ranges.
356 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000357 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000358 CaseStmt *CR = CaseRanges[i].second;
359 llvm::APSInt HiVal(32);
360 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
361
362 // Convert the value to the same width/sign as the condition.
363 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
364 CR->getRHS()->getLocStart(),
365 diag::warn_case_value_overflow);
366
Chris Lattner7443e0f2007-08-23 17:48:14 +0000367 // If the low value is bigger than the high value, the case is empty.
368 if (CaseRanges[i].first > HiVal) {
369 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
370 SourceRange(CR->getLHS()->getLocStart(),
371 CR->getRHS()->getLocEnd()));
372 CaseRanges.erase(CaseRanges.begin()+i);
373 --i, --e;
374 continue;
375 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000376 HiVals.push_back(HiVal);
377 }
378
379 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000380 // ranges. Since the range list is sorted, we only need to compare case
381 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000382 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000383 llvm::APSInt &CRLo = CaseRanges[i].first;
384 llvm::APSInt &CRHi = HiVals[i];
385 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000386
Chris Lattner0ab833c2007-08-23 18:29:20 +0000387 // Check to see whether the case range overlaps with any singleton cases.
388 CaseStmt *OverlapStmt = 0;
389 llvm::APSInt OverlapVal(32);
390
391 // Find the smallest value >= the lower bound. If I is in the case range,
392 // then we have overlap.
393 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
394 CaseVals.end(), CRLo,
395 CaseCompareFunctor());
396 if (I != CaseVals.end() && I->first < CRHi) {
397 OverlapVal = I->first; // Found overlap with scalar.
398 OverlapStmt = I->second;
399 }
400
401 // Find the smallest value bigger than the upper bound.
402 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
403 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
404 OverlapVal = (I-1)->first; // Found overlap with scalar.
405 OverlapStmt = (I-1)->second;
406 }
407
408 // Check to see if this case stmt overlaps with the subsequent case range.
409 if (i && CRLo <= HiVals[i-1]) {
410 OverlapVal = HiVals[i-1]; // Found overlap with range.
411 OverlapStmt = CaseRanges[i-1].second;
412 }
413
414 if (OverlapStmt) {
415 // If we have a duplicate, report it.
416 Diag(CR->getLHS()->getLocStart(),
417 diag::err_duplicate_case, OverlapVal.toString());
418 Diag(OverlapStmt->getLHS()->getLocStart(),
419 diag::err_duplicate_case_prev);
420 // FIXME: We really want to remove the bogus case stmt from the substmt,
421 // but we have no way to do this right now.
422 CaseListIsErroneous = true;
423 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000424 }
425 }
Chris Lattner3429a812007-08-23 05:46:52 +0000426
Chris Lattner1a4066d2007-08-23 06:23:56 +0000427 // FIXME: If the case list was broken is some way, we don't have a good system
428 // to patch it up. Instead, just return the whole substmt as broken.
429 if (CaseListIsErroneous)
430 return true;
Chris Lattner3429a812007-08-23 05:46:52 +0000431
Chris Lattner4b009652007-07-25 00:24:17 +0000432 return SS;
433}
434
435Action::StmtResult
436Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
437 Expr *condExpr = (Expr *)Cond;
438 assert(condExpr && "ParseWhileStmt(): missing expression");
439
440 DefaultFunctionArrayConversion(condExpr);
441 QualType condType = condExpr->getType();
442
443 if (!condType->isScalarType()) // C99 6.8.5p2
444 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
445 condType.getAsString(), condExpr->getSourceRange());
446
Steve Naroff5d2fff82007-08-31 23:28:33 +0000447 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000448}
449
450Action::StmtResult
451Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
452 SourceLocation WhileLoc, ExprTy *Cond) {
453 Expr *condExpr = (Expr *)Cond;
454 assert(condExpr && "ParseDoStmt(): missing expression");
455
456 DefaultFunctionArrayConversion(condExpr);
457 QualType condType = condExpr->getType();
458
459 if (!condType->isScalarType()) // C99 6.8.5p2
460 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
461 condType.getAsString(), condExpr->getSourceRange());
462
Steve Naroff5d2fff82007-08-31 23:28:33 +0000463 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000464}
465
466Action::StmtResult
467Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000468 StmtTy *first, ExprTy *second, ExprTy *third,
469 SourceLocation RParenLoc, StmtTy *body) {
470 Stmt *First = static_cast<Stmt*>(first);
471 Expr *Second = static_cast<Expr*>(second);
472 Expr *Third = static_cast<Expr*>(third);
473 Stmt *Body = static_cast<Stmt*>(body);
474
Chris Lattner06611052007-08-28 05:03:08 +0000475 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
476 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
477 // identifiers for objects having storage class 'auto' or 'register'.
478 for (Decl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
479 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D);
480 if (BVD && !BVD->hasLocalStorage())
481 BVD = 0;
482 if (BVD == 0)
483 Diag(D->getLocation(), diag::err_non_variable_decl_in_for);
484 // FIXME: mark decl erroneous!
485 }
Chris Lattner4b009652007-07-25 00:24:17 +0000486 }
487 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000488 DefaultFunctionArrayConversion(Second);
489 QualType SecondType = Second->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000490
Chris Lattner3332fbd2007-08-28 04:55:47 +0000491 if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattner4b009652007-07-25 00:24:17 +0000492 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000493 SecondType.getAsString(), Second->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000494 }
Steve Naroff5d2fff82007-08-31 23:28:33 +0000495 return new ForStmt(First, Second, Third, Body, ForLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000496}
497
498
499Action::StmtResult
500Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
501 IdentifierInfo *LabelII) {
502 // Look up the record for this label identifier.
503 LabelStmt *&LabelDecl = LabelMap[LabelII];
504
505 // If we haven't seen this label yet, create a forward reference.
506 if (LabelDecl == 0)
507 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
508
509 return new GotoStmt(LabelDecl);
510}
511
512Action::StmtResult
513Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
514 ExprTy *DestExp) {
515 // FIXME: Verify that the operand is convertible to void*.
516
517 return new IndirectGotoStmt((Expr*)DestExp);
518}
519
520Action::StmtResult
521Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
522 Scope *S = CurScope->getContinueParent();
523 if (!S) {
524 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
525 Diag(ContinueLoc, diag::err_continue_not_in_loop);
526 return true;
527 }
528
529 return new ContinueStmt();
530}
531
532Action::StmtResult
533Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
534 Scope *S = CurScope->getBreakParent();
535 if (!S) {
536 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
537 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
538 return true;
539 }
540
541 return new BreakStmt();
542}
543
544
545Action::StmtResult
546Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
547 Expr *RetValExp = static_cast<Expr *>(rex);
548 QualType lhsType = CurFunctionDecl->getResultType();
549
550 if (lhsType->isVoidType()) {
551 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
552 Diag(ReturnLoc, diag::ext_return_has_expr,
553 CurFunctionDecl->getIdentifier()->getName(),
554 RetValExp->getSourceRange());
555 return new ReturnStmt(RetValExp);
556 } else {
557 if (!RetValExp) {
558 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
559 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
560 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
561 else // C90 6.6.6.4p4
562 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
563 return new ReturnStmt((Expr*)0);
564 }
565 }
566 // we have a non-void function with an expression, continue checking
567 QualType rhsType = RetValExp->getType();
568
569 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
570 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
571 // function return.
572 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
573 RetValExp);
Ted Kremenek235e1ad2007-08-14 18:14:14 +0000574
Chris Lattner4b009652007-07-25 00:24:17 +0000575 // decode the result (notice that extensions still return a type).
576 switch (result) {
577 case Compatible:
578 break;
579 case Incompatible:
580 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
581 lhsType.getAsString(), rhsType.getAsString(),
582 RetValExp->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000583 break;
584 case PointerFromInt:
585 // check for null pointer constant (C99 6.3.2.3p3)
586 if (!RetValExp->isNullPointerConstant(Context)) {
587 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
588 lhsType.getAsString(), rhsType.getAsString(),
589 RetValExp->getSourceRange());
590 }
591 break;
592 case IntFromPointer:
593 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
594 lhsType.getAsString(), rhsType.getAsString(),
595 RetValExp->getSourceRange());
596 break;
597 case IncompatiblePointer:
598 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
599 lhsType.getAsString(), rhsType.getAsString(),
600 RetValExp->getSourceRange());
601 break;
602 case CompatiblePointerDiscardsQualifiers:
603 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
604 lhsType.getAsString(), rhsType.getAsString(),
605 RetValExp->getSourceRange());
606 break;
607 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000608
609 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
610
Chris Lattner4b009652007-07-25 00:24:17 +0000611 return new ReturnStmt((Expr*)RetValExp);
612}
613