blob: 300388cf33ae13b876acce95ad6211ec87689461 [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
Chris Lattner820dfbd2007-08-28 18:40:30 +000024/// DiagnoseDeadExpr - The specified expression is side-effect free and
25/// evaluated in a context where the result is unused. Emit a diagnostic to
26/// warn about this if appropriate.
27static void DiagnoseDeadExpr(Expr *E, Sema &S) {
28 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
29 S.Diag(BO->getOperatorLoc(), diag::warn_unused_expr,
30 BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange());
31 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
32 S.Diag(UO->getOperatorLoc(), diag::warn_unused_expr,
33 UO->getSubExpr()->getSourceRange());
34 else
35 S.Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
36}
37
Chris Lattner4b009652007-07-25 00:24:17 +000038Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) {
39 Expr *E = static_cast<Expr*>(expr);
Steve Naroff1b8a46c2007-07-27 22:15:19 +000040 assert(E && "ParseExprStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +000041
42 // Exprs are statements, so there is no need to do a conversion here. However,
43 // diagnose some potentially bad code.
Chris Lattner852d1152007-08-26 17:32:59 +000044 if (!E->hasLocalSideEffect() && !E->getType()->isVoidType())
Chris Lattner820dfbd2007-08-28 18:40:30 +000045 DiagnoseDeadExpr(E, *this);
Chris Lattner4b009652007-07-25 00:24:17 +000046
47 return E;
48}
49
50
51Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
52 return new NullStmt(SemiLoc);
53}
54
55Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
56 if (decl)
57 return new DeclStmt(static_cast<Decl *>(decl));
58 else
59 return true; // error
60}
61
62Action::StmtResult
63Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattner3ea3b662007-08-27 04:29:41 +000064 StmtTy **elts, unsigned NumElts) {
65 Stmt **Elts = reinterpret_cast<Stmt**>(elts);
66 // If we're in C89 mode, check that we don't have any decls after stmts. If
67 // so, emit an extension diagnostic.
68 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
69 // Note that __extension__ can be around a decl.
70 unsigned i = 0;
71 // Skip over all declarations.
72 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
73 /*empty*/;
74
75 // We found the end of the list or a statement. Scan for another declstmt.
76 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
77 /*empty*/;
78
79 if (i != NumElts) {
80 Decl *D = cast<DeclStmt>(Elts[i])->getDecl();
81 Diag(D->getLocation(), diag::ext_mixed_decls_code);
82 }
83 }
Chris Lattner81417722007-08-27 01:01:57 +000084
Chris Lattner3ea3b662007-08-27 04:29:41 +000085 return new CompoundStmt(Elts, NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +000086}
87
88Action::StmtResult
89Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
90 SourceLocation DotDotDotLoc, ExprTy *rhsval,
91 SourceLocation ColonLoc, StmtTy *subStmt) {
92 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
93 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
94 assert((LHSVal != 0) && "missing expression in case statement");
95
96 SourceLocation ExpLoc;
97 // C99 6.8.4.2p3: The expression shall be an integer constant.
98 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
99 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
100 LHSVal->getSourceRange());
101 return SubStmt;
102 }
103
104 // GCC extension: The expression shall be an integer constant.
105 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
106 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
107 RHSVal->getSourceRange());
Chris Lattner3429a812007-08-23 05:46:52 +0000108 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner4b009652007-07-25 00:24:17 +0000109 }
110
111 if (SwitchStack.empty()) {
112 Diag(CaseLoc, diag::err_case_not_in_switch);
113 return SubStmt;
114 }
115
116 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
117 SwitchStack.back()->addSwitchCase(CS);
118 return CS;
119}
120
121Action::StmtResult
122Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
123 StmtTy *subStmt, Scope *CurScope) {
124 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
125
126 if (SwitchStack.empty()) {
127 Diag(DefaultLoc, diag::err_default_not_in_switch);
128 return SubStmt;
129 }
130
131 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
132 SwitchStack.back()->addSwitchCase(DS);
133
134 return DS;
135}
136
137Action::StmtResult
138Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
139 SourceLocation ColonLoc, StmtTy *subStmt) {
140 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
141 // Look up the record for this label identifier.
142 LabelStmt *&LabelDecl = LabelMap[II];
143
144 // If not forward referenced or defined already, just create a new LabelStmt.
145 if (LabelDecl == 0)
146 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
147
148 assert(LabelDecl->getID() == II && "Label mismatch!");
149
150 // Otherwise, this label was either forward reference or multiply defined. If
151 // multiply defined, reject it now.
152 if (LabelDecl->getSubStmt()) {
153 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
154 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
155 return SubStmt;
156 }
157
158 // Otherwise, this label was forward declared, and we just found its real
159 // definition. Fill in the forward definition and return it.
160 LabelDecl->setIdentLoc(IdentLoc);
161 LabelDecl->setSubStmt(SubStmt);
162 return LabelDecl;
163}
164
165Action::StmtResult
166Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
167 StmtTy *ThenVal, SourceLocation ElseLoc,
168 StmtTy *ElseVal) {
169 Expr *condExpr = (Expr *)CondVal;
170 assert(condExpr && "ParseIfStmt(): missing expression");
171
172 DefaultFunctionArrayConversion(condExpr);
173 QualType condType = condExpr->getType();
174
175 if (!condType->isScalarType()) // C99 6.8.4.1p1
176 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
177 condType.getAsString(), condExpr->getSourceRange());
178
179 return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
180}
181
182Action::StmtResult
Chris Lattner3429a812007-08-23 05:46:52 +0000183Sema::StartSwitchStmt(ExprTy *cond) {
184 Expr *Cond = static_cast<Expr*>(cond);
185
186 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
187 UsualUnaryConversions(Cond);
188
189 SwitchStmt *SS = new SwitchStmt(Cond);
Chris Lattner4b009652007-07-25 00:24:17 +0000190 SwitchStack.push_back(SS);
191 return SS;
192}
193
Chris Lattner3429a812007-08-23 05:46:52 +0000194/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
195/// the specified width and sign. If an overflow occurs, detect it and emit
196/// the specified diagnostic.
197void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
198 unsigned NewWidth, bool NewSign,
199 SourceLocation Loc,
200 unsigned DiagID) {
201 // Perform a conversion to the promoted condition type if needed.
202 if (NewWidth > Val.getBitWidth()) {
203 // If this is an extension, just do it.
204 llvm::APSInt OldVal(Val);
205 Val.extend(NewWidth);
206
207 // If the input was signed and negative and the output is unsigned,
208 // warn.
209 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
210 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
211
212 Val.setIsSigned(NewSign);
213 } else if (NewWidth < Val.getBitWidth()) {
214 // If this is a truncation, check for overflow.
215 llvm::APSInt ConvVal(Val);
216 ConvVal.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000217 ConvVal.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000218 ConvVal.extend(Val.getBitWidth());
Chris Lattner5c039602007-08-23 22:08:35 +0000219 ConvVal.setIsSigned(Val.isSigned());
Chris Lattner3429a812007-08-23 05:46:52 +0000220 if (ConvVal != Val)
221 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
222
223 // Regardless of whether a diagnostic was emitted, really do the
224 // truncation.
225 Val.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000226 Val.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000227 } else if (NewSign != Val.isSigned()) {
228 // Convert the sign to match the sign of the condition. This can cause
229 // overflow as well: unsigned(INTMIN)
230 llvm::APSInt OldVal(Val);
231 Val.setIsSigned(NewSign);
232
233 if (Val.isNegative()) // Sign bit changes meaning.
234 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
235 }
236}
237
Chris Lattner0ab833c2007-08-23 18:29:20 +0000238namespace {
239 struct CaseCompareFunctor {
240 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
241 const llvm::APSInt &RHS) {
242 return LHS.first < RHS;
243 }
244 bool operator()(const llvm::APSInt &LHS,
245 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
246 return LHS < RHS.first;
247 }
248 };
249}
250
Chris Lattner4b009652007-07-25 00:24:17 +0000251Action::StmtResult
252Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
253 Stmt *BodyStmt = (Stmt*)Body;
254
255 SwitchStmt *SS = SwitchStack.back();
256 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
257
258 SS->setBody(BodyStmt);
259 SwitchStack.pop_back();
260
Chris Lattner3429a812007-08-23 05:46:52 +0000261 Expr *CondExpr = SS->getCond();
262 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000263
Chris Lattner3429a812007-08-23 05:46:52 +0000264 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner4b009652007-07-25 00:24:17 +0000265 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattner3429a812007-08-23 05:46:52 +0000266 CondType.getAsString(), CondExpr->getSourceRange());
267 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000268 }
Chris Lattner3429a812007-08-23 05:46:52 +0000269
270 // Get the bitwidth of the switched-on value before promotions. We must
271 // convert the integer case values to this width before comparison.
272 unsigned CondWidth = Context.getTypeSize(CondType, SwitchLoc);
273 bool CondIsSigned = CondType->isSignedIntegerType();
274
275 // Accumulate all of the case values in a vector so that we can sort them
276 // and detect duplicates. This vector contains the APInt for the case after
277 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000278 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
279 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000280
281 // Keep track of any GNU case ranges we see. The APSInt is the low value.
282 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
283
284 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000285
Chris Lattner1a4066d2007-08-23 06:23:56 +0000286 bool CaseListIsErroneous = false;
287
288 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000289 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000290
Chris Lattner4b009652007-07-25 00:24:17 +0000291 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000292 if (TheDefaultStmt) {
293 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
294 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Chris Lattner4b009652007-07-25 00:24:17 +0000295
Chris Lattner3429a812007-08-23 05:46:52 +0000296 // FIXME: Remove the default statement from the switch block so that
297 // we'll return a valid AST. This requires recursing down the
298 // AST and finding it, not something we are set up to do right now. For
299 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000300 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000301 }
Chris Lattner3429a812007-08-23 05:46:52 +0000302 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000303
Chris Lattner3429a812007-08-23 05:46:52 +0000304 } else {
305 CaseStmt *CS = cast<CaseStmt>(SC);
306
307 // We already verified that the expression has a i-c-e value (C99
308 // 6.8.4.2p3) - get that value now.
309 llvm::APSInt LoVal(32);
310 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
311
312 // Convert the value to the same width/sign as the condition.
313 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
314 CS->getLHS()->getLocStart(),
315 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000316
Chris Lattner1a4066d2007-08-23 06:23:56 +0000317 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000318 if (CS->getRHS())
319 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000320 else
321 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000322 }
323 }
324
Chris Lattner1a4066d2007-08-23 06:23:56 +0000325 // Sort all the scalar case values so we can easily detect duplicates.
326 std::stable_sort(CaseVals.begin(), CaseVals.end());
Chris Lattner3429a812007-08-23 05:46:52 +0000327
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000328 if (!CaseVals.empty()) {
329 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
330 if (CaseVals[i].first == CaseVals[i+1].first) {
331 // If we have a duplicate, report it.
332 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
333 diag::err_duplicate_case, CaseVals[i].first.toString());
334 Diag(CaseVals[i].second->getLHS()->getLocStart(),
335 diag::err_duplicate_case_prev);
336 // FIXME: We really want to remove the bogus case stmt from the substmt,
337 // but we have no way to do this right now.
338 CaseListIsErroneous = true;
339 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000340 }
341 }
Chris Lattner3429a812007-08-23 05:46:52 +0000342
Chris Lattner1a4066d2007-08-23 06:23:56 +0000343 // Detect duplicate case ranges, which usually don't exist at all in the first
344 // place.
345 if (!CaseRanges.empty()) {
346 // Sort all the case ranges by their low value so we can easily detect
347 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000348 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000349
350 // Scan the ranges, computing the high values and removing empty ranges.
351 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000352 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000353 CaseStmt *CR = CaseRanges[i].second;
354 llvm::APSInt HiVal(32);
355 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
356
357 // Convert the value to the same width/sign as the condition.
358 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
359 CR->getRHS()->getLocStart(),
360 diag::warn_case_value_overflow);
361
Chris Lattner7443e0f2007-08-23 17:48:14 +0000362 // If the low value is bigger than the high value, the case is empty.
363 if (CaseRanges[i].first > HiVal) {
364 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
365 SourceRange(CR->getLHS()->getLocStart(),
366 CR->getRHS()->getLocEnd()));
367 CaseRanges.erase(CaseRanges.begin()+i);
368 --i, --e;
369 continue;
370 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000371 HiVals.push_back(HiVal);
372 }
373
374 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000375 // ranges. Since the range list is sorted, we only need to compare case
376 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000377 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000378 llvm::APSInt &CRLo = CaseRanges[i].first;
379 llvm::APSInt &CRHi = HiVals[i];
380 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000381
Chris Lattner0ab833c2007-08-23 18:29:20 +0000382 // Check to see whether the case range overlaps with any singleton cases.
383 CaseStmt *OverlapStmt = 0;
384 llvm::APSInt OverlapVal(32);
385
386 // Find the smallest value >= the lower bound. If I is in the case range,
387 // then we have overlap.
388 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
389 CaseVals.end(), CRLo,
390 CaseCompareFunctor());
391 if (I != CaseVals.end() && I->first < CRHi) {
392 OverlapVal = I->first; // Found overlap with scalar.
393 OverlapStmt = I->second;
394 }
395
396 // Find the smallest value bigger than the upper bound.
397 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
398 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
399 OverlapVal = (I-1)->first; // Found overlap with scalar.
400 OverlapStmt = (I-1)->second;
401 }
402
403 // Check to see if this case stmt overlaps with the subsequent case range.
404 if (i && CRLo <= HiVals[i-1]) {
405 OverlapVal = HiVals[i-1]; // Found overlap with range.
406 OverlapStmt = CaseRanges[i-1].second;
407 }
408
409 if (OverlapStmt) {
410 // If we have a duplicate, report it.
411 Diag(CR->getLHS()->getLocStart(),
412 diag::err_duplicate_case, OverlapVal.toString());
413 Diag(OverlapStmt->getLHS()->getLocStart(),
414 diag::err_duplicate_case_prev);
415 // FIXME: We really want to remove the bogus case stmt from the substmt,
416 // but we have no way to do this right now.
417 CaseListIsErroneous = true;
418 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000419 }
420 }
Chris Lattner3429a812007-08-23 05:46:52 +0000421
Chris Lattner1a4066d2007-08-23 06:23:56 +0000422 // FIXME: If the case list was broken is some way, we don't have a good system
423 // to patch it up. Instead, just return the whole substmt as broken.
424 if (CaseListIsErroneous)
425 return true;
Chris Lattner3429a812007-08-23 05:46:52 +0000426
Chris Lattner4b009652007-07-25 00:24:17 +0000427 return SS;
428}
429
430Action::StmtResult
431Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
432 Expr *condExpr = (Expr *)Cond;
433 assert(condExpr && "ParseWhileStmt(): missing expression");
434
435 DefaultFunctionArrayConversion(condExpr);
436 QualType condType = condExpr->getType();
437
438 if (!condType->isScalarType()) // C99 6.8.5p2
439 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
440 condType.getAsString(), condExpr->getSourceRange());
441
442 return new WhileStmt(condExpr, (Stmt*)Body);
443}
444
445Action::StmtResult
446Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
447 SourceLocation WhileLoc, ExprTy *Cond) {
448 Expr *condExpr = (Expr *)Cond;
449 assert(condExpr && "ParseDoStmt(): missing expression");
450
451 DefaultFunctionArrayConversion(condExpr);
452 QualType condType = condExpr->getType();
453
454 if (!condType->isScalarType()) // C99 6.8.5p2
455 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
456 condType.getAsString(), condExpr->getSourceRange());
457
458 return new DoStmt((Stmt*)Body, condExpr);
459}
460
461Action::StmtResult
462Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000463 StmtTy *first, ExprTy *second, ExprTy *third,
464 SourceLocation RParenLoc, StmtTy *body) {
465 Stmt *First = static_cast<Stmt*>(first);
466 Expr *Second = static_cast<Expr*>(second);
467 Expr *Third = static_cast<Expr*>(third);
468 Stmt *Body = static_cast<Stmt*>(body);
469
Chris Lattner06611052007-08-28 05:03:08 +0000470 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
471 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
472 // identifiers for objects having storage class 'auto' or 'register'.
473 for (Decl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
474 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D);
475 if (BVD && !BVD->hasLocalStorage())
476 BVD = 0;
477 if (BVD == 0)
478 Diag(D->getLocation(), diag::err_non_variable_decl_in_for);
479 // FIXME: mark decl erroneous!
480 }
Chris Lattner4b009652007-07-25 00:24:17 +0000481 }
482 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000483 DefaultFunctionArrayConversion(Second);
484 QualType SecondType = Second->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000485
Chris Lattner3332fbd2007-08-28 04:55:47 +0000486 if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattner4b009652007-07-25 00:24:17 +0000487 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000488 SecondType.getAsString(), Second->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000489 }
Chris Lattner3332fbd2007-08-28 04:55:47 +0000490 return new ForStmt(First, Second, Third, Body);
Chris Lattner4b009652007-07-25 00:24:17 +0000491}
492
493
494Action::StmtResult
495Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
496 IdentifierInfo *LabelII) {
497 // Look up the record for this label identifier.
498 LabelStmt *&LabelDecl = LabelMap[LabelII];
499
500 // If we haven't seen this label yet, create a forward reference.
501 if (LabelDecl == 0)
502 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
503
504 return new GotoStmt(LabelDecl);
505}
506
507Action::StmtResult
508Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
509 ExprTy *DestExp) {
510 // FIXME: Verify that the operand is convertible to void*.
511
512 return new IndirectGotoStmt((Expr*)DestExp);
513}
514
515Action::StmtResult
516Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
517 Scope *S = CurScope->getContinueParent();
518 if (!S) {
519 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
520 Diag(ContinueLoc, diag::err_continue_not_in_loop);
521 return true;
522 }
523
524 return new ContinueStmt();
525}
526
527Action::StmtResult
528Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
529 Scope *S = CurScope->getBreakParent();
530 if (!S) {
531 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
532 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
533 return true;
534 }
535
536 return new BreakStmt();
537}
538
539
540Action::StmtResult
541Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
542 Expr *RetValExp = static_cast<Expr *>(rex);
543 QualType lhsType = CurFunctionDecl->getResultType();
544
545 if (lhsType->isVoidType()) {
546 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
547 Diag(ReturnLoc, diag::ext_return_has_expr,
548 CurFunctionDecl->getIdentifier()->getName(),
549 RetValExp->getSourceRange());
550 return new ReturnStmt(RetValExp);
551 } else {
552 if (!RetValExp) {
553 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
554 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
555 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
556 else // C90 6.6.6.4p4
557 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
558 return new ReturnStmt((Expr*)0);
559 }
560 }
561 // we have a non-void function with an expression, continue checking
562 QualType rhsType = RetValExp->getType();
563
564 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
565 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
566 // function return.
567 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
568 RetValExp);
Ted Kremenek235e1ad2007-08-14 18:14:14 +0000569
Chris Lattner4b009652007-07-25 00:24:17 +0000570 // decode the result (notice that extensions still return a type).
571 switch (result) {
572 case Compatible:
573 break;
574 case Incompatible:
575 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
576 lhsType.getAsString(), rhsType.getAsString(),
577 RetValExp->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000578 break;
579 case PointerFromInt:
580 // check for null pointer constant (C99 6.3.2.3p3)
581 if (!RetValExp->isNullPointerConstant(Context)) {
582 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
583 lhsType.getAsString(), rhsType.getAsString(),
584 RetValExp->getSourceRange());
585 }
586 break;
587 case IntFromPointer:
588 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
589 lhsType.getAsString(), rhsType.getAsString(),
590 RetValExp->getSourceRange());
591 break;
592 case IncompatiblePointer:
593 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
594 lhsType.getAsString(), rhsType.getAsString(),
595 RetValExp->getSourceRange());
596 break;
597 case CompatiblePointerDiscardsQualifiers:
598 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
599 lhsType.getAsString(), rhsType.getAsString(),
600 RetValExp->getSourceRange());
601 break;
602 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000603
604 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
605
Chris Lattner4b009652007-07-25 00:24:17 +0000606 return new ReturnStmt((Expr*)RetValExp);
607}
608