blob: bec7f892dfb5585706ad5295eb0cb2a20209700c [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 Lattner81417722007-08-27 01:01:57 +000064
Chris Lattnerf2b07572007-08-31 21:49:55 +000065 // Warn about unused expressions in statements.
66 for (unsigned i = 0; i != NumElts; ++i) {
67 Expr *E = dyn_cast<Expr>(Elts[i]);
68 if (!E) continue;
69
70 // Warn about expressions with unused results.
71 if (E->hasLocalSideEffect() || E->getType()->isVoidType())
72 continue;
73
74 // The last expr in a stmt expr really is used.
75 if (isStmtExpr && i == NumElts-1)
76 continue;
77
78 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
79 /// a context where the result is unused. Emit a diagnostic to warn about
80 /// this.
81 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
82 Diag(BO->getOperatorLoc(), diag::warn_unused_expr,
83 BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange());
84 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
85 Diag(UO->getOperatorLoc(), diag::warn_unused_expr,
86 UO->getSubExpr()->getSourceRange());
87 else
88 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
89 }
90
Chris Lattner3ea3b662007-08-27 04:29:41 +000091 return new CompoundStmt(Elts, NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +000092}
93
94Action::StmtResult
95Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
96 SourceLocation DotDotDotLoc, ExprTy *rhsval,
97 SourceLocation ColonLoc, StmtTy *subStmt) {
98 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
99 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
100 assert((LHSVal != 0) && "missing expression in case statement");
101
102 SourceLocation ExpLoc;
103 // C99 6.8.4.2p3: The expression shall be an integer constant.
104 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
105 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
106 LHSVal->getSourceRange());
107 return SubStmt;
108 }
109
110 // GCC extension: The expression shall be an integer constant.
111 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
112 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
113 RHSVal->getSourceRange());
Chris Lattner3429a812007-08-23 05:46:52 +0000114 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner4b009652007-07-25 00:24:17 +0000115 }
116
117 if (SwitchStack.empty()) {
118 Diag(CaseLoc, diag::err_case_not_in_switch);
119 return SubStmt;
120 }
121
122 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
123 SwitchStack.back()->addSwitchCase(CS);
124 return CS;
125}
126
127Action::StmtResult
128Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
129 StmtTy *subStmt, Scope *CurScope) {
130 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
131
132 if (SwitchStack.empty()) {
133 Diag(DefaultLoc, diag::err_default_not_in_switch);
134 return SubStmt;
135 }
136
137 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
138 SwitchStack.back()->addSwitchCase(DS);
139
140 return DS;
141}
142
143Action::StmtResult
144Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
145 SourceLocation ColonLoc, StmtTy *subStmt) {
146 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
147 // Look up the record for this label identifier.
148 LabelStmt *&LabelDecl = LabelMap[II];
149
150 // If not forward referenced or defined already, just create a new LabelStmt.
151 if (LabelDecl == 0)
152 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
153
154 assert(LabelDecl->getID() == II && "Label mismatch!");
155
156 // Otherwise, this label was either forward reference or multiply defined. If
157 // multiply defined, reject it now.
158 if (LabelDecl->getSubStmt()) {
159 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
160 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
161 return SubStmt;
162 }
163
164 // Otherwise, this label was forward declared, and we just found its real
165 // definition. Fill in the forward definition and return it.
166 LabelDecl->setIdentLoc(IdentLoc);
167 LabelDecl->setSubStmt(SubStmt);
168 return LabelDecl;
169}
170
171Action::StmtResult
172Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
173 StmtTy *ThenVal, SourceLocation ElseLoc,
174 StmtTy *ElseVal) {
175 Expr *condExpr = (Expr *)CondVal;
176 assert(condExpr && "ParseIfStmt(): missing expression");
177
178 DefaultFunctionArrayConversion(condExpr);
179 QualType condType = condExpr->getType();
180
181 if (!condType->isScalarType()) // C99 6.8.4.1p1
182 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
183 condType.getAsString(), condExpr->getSourceRange());
184
185 return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
186}
187
188Action::StmtResult
Chris Lattner3429a812007-08-23 05:46:52 +0000189Sema::StartSwitchStmt(ExprTy *cond) {
190 Expr *Cond = static_cast<Expr*>(cond);
191
192 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
193 UsualUnaryConversions(Cond);
194
195 SwitchStmt *SS = new SwitchStmt(Cond);
Chris Lattner4b009652007-07-25 00:24:17 +0000196 SwitchStack.push_back(SS);
197 return SS;
198}
199
Chris Lattner3429a812007-08-23 05:46:52 +0000200/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
201/// the specified width and sign. If an overflow occurs, detect it and emit
202/// the specified diagnostic.
203void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
204 unsigned NewWidth, bool NewSign,
205 SourceLocation Loc,
206 unsigned DiagID) {
207 // Perform a conversion to the promoted condition type if needed.
208 if (NewWidth > Val.getBitWidth()) {
209 // If this is an extension, just do it.
210 llvm::APSInt OldVal(Val);
211 Val.extend(NewWidth);
212
213 // If the input was signed and negative and the output is unsigned,
214 // warn.
215 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
216 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
217
218 Val.setIsSigned(NewSign);
219 } else if (NewWidth < Val.getBitWidth()) {
220 // If this is a truncation, check for overflow.
221 llvm::APSInt ConvVal(Val);
222 ConvVal.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000223 ConvVal.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000224 ConvVal.extend(Val.getBitWidth());
Chris Lattner5c039602007-08-23 22:08:35 +0000225 ConvVal.setIsSigned(Val.isSigned());
Chris Lattner3429a812007-08-23 05:46:52 +0000226 if (ConvVal != Val)
227 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
228
229 // Regardless of whether a diagnostic was emitted, really do the
230 // truncation.
231 Val.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000232 Val.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000233 } else if (NewSign != Val.isSigned()) {
234 // Convert the sign to match the sign of the condition. This can cause
235 // overflow as well: unsigned(INTMIN)
236 llvm::APSInt OldVal(Val);
237 Val.setIsSigned(NewSign);
238
239 if (Val.isNegative()) // Sign bit changes meaning.
240 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
241 }
242}
243
Chris Lattner0ab833c2007-08-23 18:29:20 +0000244namespace {
245 struct CaseCompareFunctor {
246 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
247 const llvm::APSInt &RHS) {
248 return LHS.first < RHS;
249 }
250 bool operator()(const llvm::APSInt &LHS,
251 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
252 return LHS < RHS.first;
253 }
254 };
255}
256
Chris Lattner4b009652007-07-25 00:24:17 +0000257Action::StmtResult
258Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
259 Stmt *BodyStmt = (Stmt*)Body;
260
261 SwitchStmt *SS = SwitchStack.back();
262 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
263
264 SS->setBody(BodyStmt);
265 SwitchStack.pop_back();
266
Chris Lattner3429a812007-08-23 05:46:52 +0000267 Expr *CondExpr = SS->getCond();
268 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000269
Chris Lattner3429a812007-08-23 05:46:52 +0000270 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner4b009652007-07-25 00:24:17 +0000271 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattner3429a812007-08-23 05:46:52 +0000272 CondType.getAsString(), CondExpr->getSourceRange());
273 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000274 }
Chris Lattner3429a812007-08-23 05:46:52 +0000275
276 // Get the bitwidth of the switched-on value before promotions. We must
277 // convert the integer case values to this width before comparison.
278 unsigned CondWidth = Context.getTypeSize(CondType, SwitchLoc);
279 bool CondIsSigned = CondType->isSignedIntegerType();
280
281 // Accumulate all of the case values in a vector so that we can sort them
282 // and detect duplicates. This vector contains the APInt for the case after
283 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000284 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
285 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000286
287 // Keep track of any GNU case ranges we see. The APSInt is the low value.
288 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
289
290 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000291
Chris Lattner1a4066d2007-08-23 06:23:56 +0000292 bool CaseListIsErroneous = false;
293
294 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000295 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000296
Chris Lattner4b009652007-07-25 00:24:17 +0000297 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000298 if (TheDefaultStmt) {
299 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
300 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Chris Lattner4b009652007-07-25 00:24:17 +0000301
Chris Lattner3429a812007-08-23 05:46:52 +0000302 // FIXME: Remove the default statement from the switch block so that
303 // we'll return a valid AST. This requires recursing down the
304 // AST and finding it, not something we are set up to do right now. For
305 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000306 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000307 }
Chris Lattner3429a812007-08-23 05:46:52 +0000308 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000309
Chris Lattner3429a812007-08-23 05:46:52 +0000310 } else {
311 CaseStmt *CS = cast<CaseStmt>(SC);
312
313 // We already verified that the expression has a i-c-e value (C99
314 // 6.8.4.2p3) - get that value now.
315 llvm::APSInt LoVal(32);
316 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
317
318 // Convert the value to the same width/sign as the condition.
319 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
320 CS->getLHS()->getLocStart(),
321 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000322
Chris Lattner1a4066d2007-08-23 06:23:56 +0000323 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000324 if (CS->getRHS())
325 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000326 else
327 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000328 }
329 }
330
Chris Lattner1a4066d2007-08-23 06:23:56 +0000331 // Sort all the scalar case values so we can easily detect duplicates.
332 std::stable_sort(CaseVals.begin(), CaseVals.end());
Chris Lattner3429a812007-08-23 05:46:52 +0000333
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000334 if (!CaseVals.empty()) {
335 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
336 if (CaseVals[i].first == CaseVals[i+1].first) {
337 // If we have a duplicate, report it.
338 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
339 diag::err_duplicate_case, CaseVals[i].first.toString());
340 Diag(CaseVals[i].second->getLHS()->getLocStart(),
341 diag::err_duplicate_case_prev);
342 // FIXME: We really want to remove the bogus case stmt from the substmt,
343 // but we have no way to do this right now.
344 CaseListIsErroneous = true;
345 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000346 }
347 }
Chris Lattner3429a812007-08-23 05:46:52 +0000348
Chris Lattner1a4066d2007-08-23 06:23:56 +0000349 // Detect duplicate case ranges, which usually don't exist at all in the first
350 // place.
351 if (!CaseRanges.empty()) {
352 // Sort all the case ranges by their low value so we can easily detect
353 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000354 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000355
356 // Scan the ranges, computing the high values and removing empty ranges.
357 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000358 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000359 CaseStmt *CR = CaseRanges[i].second;
360 llvm::APSInt HiVal(32);
361 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
362
363 // Convert the value to the same width/sign as the condition.
364 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
365 CR->getRHS()->getLocStart(),
366 diag::warn_case_value_overflow);
367
Chris Lattner7443e0f2007-08-23 17:48:14 +0000368 // If the low value is bigger than the high value, the case is empty.
369 if (CaseRanges[i].first > HiVal) {
370 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
371 SourceRange(CR->getLHS()->getLocStart(),
372 CR->getRHS()->getLocEnd()));
373 CaseRanges.erase(CaseRanges.begin()+i);
374 --i, --e;
375 continue;
376 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000377 HiVals.push_back(HiVal);
378 }
379
380 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000381 // ranges. Since the range list is sorted, we only need to compare case
382 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000383 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000384 llvm::APSInt &CRLo = CaseRanges[i].first;
385 llvm::APSInt &CRHi = HiVals[i];
386 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000387
Chris Lattner0ab833c2007-08-23 18:29:20 +0000388 // Check to see whether the case range overlaps with any singleton cases.
389 CaseStmt *OverlapStmt = 0;
390 llvm::APSInt OverlapVal(32);
391
392 // Find the smallest value >= the lower bound. If I is in the case range,
393 // then we have overlap.
394 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
395 CaseVals.end(), CRLo,
396 CaseCompareFunctor());
397 if (I != CaseVals.end() && I->first < CRHi) {
398 OverlapVal = I->first; // Found overlap with scalar.
399 OverlapStmt = I->second;
400 }
401
402 // Find the smallest value bigger than the upper bound.
403 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
404 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
405 OverlapVal = (I-1)->first; // Found overlap with scalar.
406 OverlapStmt = (I-1)->second;
407 }
408
409 // Check to see if this case stmt overlaps with the subsequent case range.
410 if (i && CRLo <= HiVals[i-1]) {
411 OverlapVal = HiVals[i-1]; // Found overlap with range.
412 OverlapStmt = CaseRanges[i-1].second;
413 }
414
415 if (OverlapStmt) {
416 // If we have a duplicate, report it.
417 Diag(CR->getLHS()->getLocStart(),
418 diag::err_duplicate_case, OverlapVal.toString());
419 Diag(OverlapStmt->getLHS()->getLocStart(),
420 diag::err_duplicate_case_prev);
421 // FIXME: We really want to remove the bogus case stmt from the substmt,
422 // but we have no way to do this right now.
423 CaseListIsErroneous = true;
424 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000425 }
426 }
Chris Lattner3429a812007-08-23 05:46:52 +0000427
Chris Lattner1a4066d2007-08-23 06:23:56 +0000428 // FIXME: If the case list was broken is some way, we don't have a good system
429 // to patch it up. Instead, just return the whole substmt as broken.
430 if (CaseListIsErroneous)
431 return true;
Chris Lattner3429a812007-08-23 05:46:52 +0000432
Chris Lattner4b009652007-07-25 00:24:17 +0000433 return SS;
434}
435
436Action::StmtResult
437Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
438 Expr *condExpr = (Expr *)Cond;
439 assert(condExpr && "ParseWhileStmt(): missing expression");
440
441 DefaultFunctionArrayConversion(condExpr);
442 QualType condType = condExpr->getType();
443
444 if (!condType->isScalarType()) // C99 6.8.5p2
445 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
446 condType.getAsString(), condExpr->getSourceRange());
447
448 return new WhileStmt(condExpr, (Stmt*)Body);
449}
450
451Action::StmtResult
452Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
453 SourceLocation WhileLoc, ExprTy *Cond) {
454 Expr *condExpr = (Expr *)Cond;
455 assert(condExpr && "ParseDoStmt(): missing expression");
456
457 DefaultFunctionArrayConversion(condExpr);
458 QualType condType = condExpr->getType();
459
460 if (!condType->isScalarType()) // C99 6.8.5p2
461 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
462 condType.getAsString(), condExpr->getSourceRange());
463
464 return new DoStmt((Stmt*)Body, condExpr);
465}
466
467Action::StmtResult
468Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000469 StmtTy *first, ExprTy *second, ExprTy *third,
470 SourceLocation RParenLoc, StmtTy *body) {
471 Stmt *First = static_cast<Stmt*>(first);
472 Expr *Second = static_cast<Expr*>(second);
473 Expr *Third = static_cast<Expr*>(third);
474 Stmt *Body = static_cast<Stmt*>(body);
475
Chris Lattner06611052007-08-28 05:03:08 +0000476 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
477 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
478 // identifiers for objects having storage class 'auto' or 'register'.
479 for (Decl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
480 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D);
481 if (BVD && !BVD->hasLocalStorage())
482 BVD = 0;
483 if (BVD == 0)
484 Diag(D->getLocation(), diag::err_non_variable_decl_in_for);
485 // FIXME: mark decl erroneous!
486 }
Chris Lattner4b009652007-07-25 00:24:17 +0000487 }
488 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000489 DefaultFunctionArrayConversion(Second);
490 QualType SecondType = Second->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000491
Chris Lattner3332fbd2007-08-28 04:55:47 +0000492 if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattner4b009652007-07-25 00:24:17 +0000493 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000494 SecondType.getAsString(), Second->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000495 }
Chris Lattner3332fbd2007-08-28 04:55:47 +0000496 return new ForStmt(First, Second, Third, Body);
Chris Lattner4b009652007-07-25 00:24:17 +0000497}
498
499
500Action::StmtResult
501Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
502 IdentifierInfo *LabelII) {
503 // Look up the record for this label identifier.
504 LabelStmt *&LabelDecl = LabelMap[LabelII];
505
506 // If we haven't seen this label yet, create a forward reference.
507 if (LabelDecl == 0)
508 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
509
510 return new GotoStmt(LabelDecl);
511}
512
513Action::StmtResult
514Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
515 ExprTy *DestExp) {
516 // FIXME: Verify that the operand is convertible to void*.
517
518 return new IndirectGotoStmt((Expr*)DestExp);
519}
520
521Action::StmtResult
522Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
523 Scope *S = CurScope->getContinueParent();
524 if (!S) {
525 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
526 Diag(ContinueLoc, diag::err_continue_not_in_loop);
527 return true;
528 }
529
530 return new ContinueStmt();
531}
532
533Action::StmtResult
534Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
535 Scope *S = CurScope->getBreakParent();
536 if (!S) {
537 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
538 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
539 return true;
540 }
541
542 return new BreakStmt();
543}
544
545
546Action::StmtResult
547Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
548 Expr *RetValExp = static_cast<Expr *>(rex);
549 QualType lhsType = CurFunctionDecl->getResultType();
550
551 if (lhsType->isVoidType()) {
552 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
553 Diag(ReturnLoc, diag::ext_return_has_expr,
554 CurFunctionDecl->getIdentifier()->getName(),
555 RetValExp->getSourceRange());
556 return new ReturnStmt(RetValExp);
557 } else {
558 if (!RetValExp) {
559 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
560 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
561 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
562 else // C90 6.6.6.4p4
563 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
564 return new ReturnStmt((Expr*)0);
565 }
566 }
567 // we have a non-void function with an expression, continue checking
568 QualType rhsType = RetValExp->getType();
569
570 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
571 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
572 // function return.
573 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
574 RetValExp);
Ted Kremenek235e1ad2007-08-14 18:14:14 +0000575
Chris Lattner4b009652007-07-25 00:24:17 +0000576 // decode the result (notice that extensions still return a type).
577 switch (result) {
578 case Compatible:
579 break;
580 case Incompatible:
581 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
582 lhsType.getAsString(), rhsType.getAsString(),
583 RetValExp->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000584 break;
585 case PointerFromInt:
586 // check for null pointer constant (C99 6.3.2.3p3)
587 if (!RetValExp->isNullPointerConstant(Context)) {
588 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
589 lhsType.getAsString(), rhsType.getAsString(),
590 RetValExp->getSourceRange());
591 }
592 break;
593 case IntFromPointer:
594 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
595 lhsType.getAsString(), rhsType.getAsString(),
596 RetValExp->getSourceRange());
597 break;
598 case IncompatiblePointer:
599 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
600 lhsType.getAsString(), rhsType.getAsString(),
601 RetValExp->getSourceRange());
602 break;
603 case CompatiblePointerDiscardsQualifiers:
604 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
605 lhsType.getAsString(), rhsType.getAsString(),
606 RetValExp->getSourceRange());
607 break;
608 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000609
610 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
611
Chris Lattner4b009652007-07-25 00:24:17 +0000612 return new ReturnStmt((Expr*)RetValExp);
613}
614