blob: bec7f892dfb5585706ad5295eb0cb2a20209700c [file] [log] [blame]
Chris Lattneraf8d5812006-11-10 05:07:45 +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 Lattnerfc1c44a2007-08-23 05:46:52 +000015#include "clang/AST/ASTContext.h"
Steve Naroff8eeeb132007-05-08 21:09:37 +000016#include "clang/AST/Expr.h"
Chris Lattnerfc1c44a2007-08-23 05:46:52 +000017#include "clang/AST/Stmt.h"
Chris Lattnereaafe1222006-11-10 05:17:58 +000018#include "clang/Parse/Scope.h"
19#include "clang/Basic/Diagnostic.h"
Steve Naroff6f49f5d2007-05-29 14:23:36 +000020#include "clang/Basic/LangOptions.h"
21#include "clang/Lex/IdentifierTable.h"
Chris Lattneraf8d5812006-11-10 05:07:45 +000022using namespace clang;
23
Chris Lattner1ec5f562007-06-27 05:38:08 +000024Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) {
25 Expr *E = static_cast<Expr*>(expr);
Steve Narofff8fd09e2007-07-27 22:15:19 +000026 assert(E && "ParseExprStmt(): missing expression");
Chris Lattner1ec5f562007-06-27 05:38:08 +000027 return E;
28}
29
30
Chris Lattner0f203a72007-05-28 01:45:28 +000031Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
32 return new NullStmt(SemiLoc);
33}
34
Steve Naroff2a8ad182007-05-29 22:59:26 +000035Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
Steve Naroff56faab22007-05-30 04:20:12 +000036 if (decl)
37 return new DeclStmt(static_cast<Decl *>(decl));
38 else
39 return true; // error
Steve Naroff2a8ad182007-05-29 22:59:26 +000040}
Chris Lattneraf8d5812006-11-10 05:07:45 +000041
42Action::StmtResult
43Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnercac27a52007-08-31 21:49:55 +000044 StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
Chris Lattnerd864daf2007-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 Lattnerdfaf9f82007-08-27 01:01:57 +000064
Chris Lattnercac27a52007-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 Lattnerd864daf2007-08-27 04:29:41 +000091 return new CompoundStmt(Elts, NumElts);
Chris Lattneraf8d5812006-11-10 05:07:45 +000092}
93
94Action::StmtResult
Chris Lattner0c46c5d2007-06-02 23:53:17 +000095Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner46eeb222007-07-18 02:28:47 +000096 SourceLocation DotDotDotLoc, ExprTy *rhsval,
Chris Lattner39407372007-07-21 03:00:26 +000097 SourceLocation ColonLoc, StmtTy *subStmt) {
98 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +000099 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
Steve Naroff8eeeb132007-05-08 21:09:37 +0000100 assert((LHSVal != 0) && "missing expression in case statement");
Anders Carlsson51873c22007-07-22 07:07:56 +0000101
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000102 SourceLocation ExpLoc;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000103 // C99 6.8.4.2p3: The expression shall be an integer constant.
Chris Lattner39407372007-07-21 03:00:26 +0000104 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
105 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
106 LHSVal->getSourceRange());
107 return SubStmt;
108 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000109
Chris Lattner46eeb222007-07-18 02:28:47 +0000110 // GCC extension: The expression shall be an integer constant.
Chris Lattner39407372007-07-21 03:00:26 +0000111 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
112 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
113 RHSVal->getSourceRange());
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000114 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner46eeb222007-07-18 02:28:47 +0000115 }
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000116
117 if (SwitchStack.empty()) {
118 Diag(CaseLoc, diag::err_case_not_in_switch);
119 return SubStmt;
120 }
Chris Lattner35e287b2007-06-03 01:44:43 +0000121
Anders Carlsson51873c22007-07-22 07:07:56 +0000122 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000123 SwitchStack.back()->addSwitchCase(CS);
Anders Carlsson51873c22007-07-22 07:07:56 +0000124 return CS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000125}
126
127Action::StmtResult
Chris Lattner46eeb222007-07-18 02:28:47 +0000128Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner39407372007-07-21 03:00:26 +0000129 StmtTy *subStmt, Scope *CurScope) {
130 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner46eeb222007-07-18 02:28:47 +0000131
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000132 if (SwitchStack.empty()) {
Chris Lattner39407372007-07-21 03:00:26 +0000133 Diag(DefaultLoc, diag::err_default_not_in_switch);
134 return SubStmt;
135 }
136
Chris Lattner39407372007-07-21 03:00:26 +0000137 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000138 SwitchStack.back()->addSwitchCase(DS);
Anders Carlsson51873c22007-07-22 07:07:56 +0000139
Chris Lattner46eeb222007-07-18 02:28:47 +0000140 return DS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000141}
142
143Action::StmtResult
144Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner39407372007-07-21 03:00:26 +0000145 SourceLocation ColonLoc, StmtTy *subStmt) {
146 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000147 // 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)
Chris Lattner39407372007-07-21 03:00:26 +0000152 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000153
Chris Lattnereefa10e2007-05-28 06:56:27 +0000154 assert(LabelDecl->getID() == II && "Label mismatch!");
Chris Lattnere2473062007-05-28 06:28:18 +0000155
156 // Otherwise, this label was either forward reference or multiply defined. If
157 // multiply defined, reject it now.
158 if (LabelDecl->getSubStmt()) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000159 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000160 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
Chris Lattner39407372007-07-21 03:00:26 +0000161 return SubStmt;
Chris Lattnere2473062007-05-28 06:28:18 +0000162 }
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);
Chris Lattner39407372007-07-21 03:00:26 +0000167 LabelDecl->setSubStmt(SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000168 return LabelDecl;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000169}
170
171Action::StmtResult
172Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
173 StmtTy *ThenVal, SourceLocation ElseLoc,
174 StmtTy *ElseVal) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000175 Expr *condExpr = (Expr *)CondVal;
176 assert(condExpr && "ParseIfStmt(): missing expression");
177
Steve Naroff31090012007-07-16 21:54:35 +0000178 DefaultFunctionArrayConversion(condExpr);
179 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000180
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);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000186}
Steve Naroff86272ea2007-05-29 02:14:17 +0000187
Chris Lattneraf8d5812006-11-10 05:07:45 +0000188Action::StmtResult
Chris Lattnerfc1c44a2007-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);
Anders Carlsson51873c22007-07-22 07:07:56 +0000196 SwitchStack.push_back(SS);
197 return SS;
198}
Chris Lattner46eeb222007-07-18 02:28:47 +0000199
Chris Lattnerfc1c44a2007-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 Lattner247ef952007-08-23 22:08:35 +0000223 ConvVal.setIsSigned(NewSign);
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000224 ConvVal.extend(Val.getBitWidth());
Chris Lattner247ef952007-08-23 22:08:35 +0000225 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerfc1c44a2007-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 Lattner247ef952007-08-23 22:08:35 +0000232 Val.setIsSigned(NewSign);
Chris Lattnerfc1c44a2007-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 Lattner67998452007-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
Anders Carlsson51873c22007-07-22 07:07:56 +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 Lattnerfc1c44a2007-08-23 05:46:52 +0000267 Expr *CondExpr = SS->getCond();
268 QualType CondType = CondExpr->getType();
Chris Lattner46eeb222007-07-18 02:28:47 +0000269
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000270 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Anders Carlsson51873c22007-07-22 07:07:56 +0000271 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000272 CondType.getAsString(), CondExpr->getSourceRange());
273 return true;
Anders Carlsson51873c22007-07-22 07:07:56 +0000274 }
Chris Lattnerfc1c44a2007-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 Lattner67998452007-08-23 18:29:20 +0000284 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
285 CaseValsTy CaseVals;
Chris Lattnerfc1c44a2007-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;
Anders Carlsson51873c22007-07-22 07:07:56 +0000291
Chris Lattner10cb5e52007-08-23 06:23:56 +0000292 bool CaseListIsErroneous = false;
293
294 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlsson51873c22007-07-22 07:07:56 +0000295 SC = SC->getNextSwitchCase()) {
Chris Lattner10cb5e52007-08-23 06:23:56 +0000296
Anders Carlsson51873c22007-07-22 07:07:56 +0000297 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerfc1c44a2007-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);
Anders Carlsson51873c22007-07-22 07:07:56 +0000301
Chris Lattnerfc1c44a2007-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 Lattner10cb5e52007-08-23 06:23:56 +0000306 CaseListIsErroneous = true;
Anders Carlsson51873c22007-07-22 07:07:56 +0000307 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000308 TheDefaultStmt = DS;
Anders Carlsson51873c22007-07-22 07:07:56 +0000309
Chris Lattnerfc1c44a2007-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);
Anders Carlsson51873c22007-07-22 07:07:56 +0000322
Chris Lattner10cb5e52007-08-23 06:23:56 +0000323 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000324 if (CS->getRHS())
325 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner10cb5e52007-08-23 06:23:56 +0000326 else
327 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000328 }
329 }
330
Chris Lattner10cb5e52007-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 Lattnerfc1c44a2007-08-23 05:46:52 +0000333
Chris Lattnerfcb920d2007-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 Lattner10cb5e52007-08-23 06:23:56 +0000346 }
347 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000348
Chris Lattner10cb5e52007-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 Lattner67998452007-08-23 18:29:20 +0000354 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner10cb5e52007-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 Lattnerf81460f2007-08-23 17:48:14 +0000358 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner10cb5e52007-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 Lattnerf81460f2007-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 Lattner10cb5e52007-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 Lattner67998452007-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 Lattner10cb5e52007-08-23 06:23:56 +0000383 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner67998452007-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 Lattner10cb5e52007-08-23 06:23:56 +0000387
Chris Lattner67998452007-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 Lattner10cb5e52007-08-23 06:23:56 +0000425 }
426 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000427
Chris Lattner10cb5e52007-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 Lattnerfc1c44a2007-08-23 05:46:52 +0000432
Anders Carlsson51873c22007-07-22 07:07:56 +0000433 return SS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000434}
435
436Action::StmtResult
Steve Naroff86272ea2007-05-29 02:14:17 +0000437Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
438 Expr *condExpr = (Expr *)Cond;
439 assert(condExpr && "ParseWhileStmt(): missing expression");
440
Steve Naroff31090012007-07-16 21:54:35 +0000441 DefaultFunctionArrayConversion(condExpr);
442 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000443
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);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000449}
450
451Action::StmtResult
452Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
453 SourceLocation WhileLoc, ExprTy *Cond) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000454 Expr *condExpr = (Expr *)Cond;
455 assert(condExpr && "ParseDoStmt(): missing expression");
456
Steve Naroff31090012007-07-16 21:54:35 +0000457 DefaultFunctionArrayConversion(condExpr);
458 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000459
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);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000465}
466
467Action::StmtResult
468Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner773c0592007-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 Lattner39f920f2007-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 }
Steve Naroff86272ea2007-05-29 02:14:17 +0000487 }
488 if (Second) {
Chris Lattner773c0592007-08-28 04:55:47 +0000489 DefaultFunctionArrayConversion(Second);
490 QualType SecondType = Second->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000491
Chris Lattner773c0592007-08-28 04:55:47 +0000492 if (!SecondType->isScalarType()) // C99 6.8.5p2
Steve Naroff86272ea2007-05-29 02:14:17 +0000493 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner773c0592007-08-28 04:55:47 +0000494 SecondType.getAsString(), Second->getSourceRange());
Steve Naroff86272ea2007-05-29 02:14:17 +0000495 }
Chris Lattner773c0592007-08-28 04:55:47 +0000496 return new ForStmt(First, Second, Third, Body);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000497}
498
499
500Action::StmtResult
501Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
502 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000503 // 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);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000511}
Chris Lattner1c310502007-05-31 06:00:00 +0000512
Chris Lattneraf8d5812006-11-10 05:07:45 +0000513Action::StmtResult
514Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
515 ExprTy *DestExp) {
Chris Lattner1c310502007-05-31 06:00:00 +0000516 // FIXME: Verify that the operand is convertible to void*.
517
Chris Lattneraf8d5812006-11-10 05:07:45 +0000518 return new IndirectGotoStmt((Expr*)DestExp);
519}
520
521Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000522Sema::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
Chris Lattneraf8d5812006-11-10 05:07:45 +0000530 return new ContinueStmt();
531}
532
533Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000534Sema::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
Chris Lattneraf8d5812006-11-10 05:07:45 +0000542 return new BreakStmt();
543}
544
545
546Action::StmtResult
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000547Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
548 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff9358c712007-05-27 23:58:33 +0000549 QualType lhsType = CurFunctionDecl->getResultType();
550
Steve Naroff9358c712007-05-27 23:58:33 +0000551 if (lhsType->isVoidType()) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000552 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
553 Diag(ReturnLoc, diag::ext_return_has_expr,
554 CurFunctionDecl->getIdentifier()->getName(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000555 RetValExp->getSourceRange());
556 return new ReturnStmt(RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000557 } 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 }
Steve Naroff9358c712007-05-27 23:58:33 +0000566 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000567 // we have a non-void function with an expression, continue checking
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000568 QualType rhsType = RetValExp->getType();
Steve Naroff9358c712007-05-27 23:58:33 +0000569
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000570 // 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.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000573 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
574 RetValExp);
Ted Kremenekc48affb2007-08-14 18:14:14 +0000575
Steve Naroff9358c712007-05-27 23:58:33 +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(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000583 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000584 break;
585 case PointerFromInt:
586 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000587 if (!RetValExp->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000588 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
589 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000590 RetValExp->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000591 }
Steve Naroff9358c712007-05-27 23:58:33 +0000592 break;
593 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000594 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
595 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000596 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000597 break;
598 case IncompatiblePointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000599 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
600 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000601 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000602 break;
603 case CompatiblePointerDiscardsQualifiers:
Steve Naroff56faab22007-05-30 04:20:12 +0000604 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
605 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000606 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000607 break;
608 }
Ted Kremenekcff94fa2007-08-17 16:46:58 +0000609
610 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
611
Chris Lattneraf8d5812006-11-10 05:07:45 +0000612 return new ReturnStmt((Expr*)RetValExp);
613}
614