blob: 0522996a55e02113bf3da4d45b23e4a566cc1ec7 [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
Steve Naroff5cbb02f2007-09-16 14:56:35 +000024Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) {
Chris Lattner4b009652007-07-25 00:24:17 +000025 Expr *E = static_cast<Expr*>(expr);
Steve Naroff5cbb02f2007-09-16 14:56:35 +000026 assert(E && "ActOnExprStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +000027 return E;
28}
29
30
Steve Naroff5cbb02f2007-09-16 14:56:35 +000031Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000032 return new NullStmt(SemiLoc);
33}
34
Steve Naroff5cbb02f2007-09-16 14:56:35 +000035Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl) {
Steve Naroffcb597472007-09-13 21:41:19 +000036 if (decl) {
37 ScopedDecl *SD = dyn_cast<ScopedDecl>(static_cast<Decl *>(decl));
Steve Naroff5cbb02f2007-09-16 14:56:35 +000038 assert(SD && "Sema::ActOnDeclStmt(): expected ScopedDecl");
Steve Naroffcb597472007-09-13 21:41:19 +000039 return new DeclStmt(SD);
40 } else
Chris Lattner4b009652007-07-25 00:24:17 +000041 return true; // error
42}
43
44Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +000045Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnerf2b07572007-08-31 21:49:55 +000046 StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
Chris Lattner3ea3b662007-08-27 04:29:41 +000047 Stmt **Elts = reinterpret_cast<Stmt**>(elts);
48 // If we're in C89 mode, check that we don't have any decls after stmts. If
49 // so, emit an extension diagnostic.
50 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
51 // Note that __extension__ can be around a decl.
52 unsigned i = 0;
53 // Skip over all declarations.
54 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
55 /*empty*/;
56
57 // We found the end of the list or a statement. Scan for another declstmt.
58 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
59 /*empty*/;
60
61 if (i != NumElts) {
Steve Naroffcb597472007-09-13 21:41:19 +000062 ScopedDecl *D = cast<DeclStmt>(Elts[i])->getDecl();
Chris Lattner3ea3b662007-08-27 04:29:41 +000063 Diag(D->getLocation(), diag::ext_mixed_decls_code);
64 }
65 }
Chris Lattnerf2b07572007-08-31 21:49:55 +000066 // Warn about unused expressions in statements.
67 for (unsigned i = 0; i != NumElts; ++i) {
68 Expr *E = dyn_cast<Expr>(Elts[i]);
69 if (!E) continue;
70
71 // Warn about expressions with unused results.
72 if (E->hasLocalSideEffect() || E->getType()->isVoidType())
73 continue;
74
75 // The last expr in a stmt expr really is used.
76 if (isStmtExpr && i == NumElts-1)
77 continue;
78
79 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
80 /// a context where the result is unused. Emit a diagnostic to warn about
81 /// this.
82 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
83 Diag(BO->getOperatorLoc(), diag::warn_unused_expr,
84 BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange());
85 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
86 Diag(UO->getOperatorLoc(), diag::warn_unused_expr,
87 UO->getSubExpr()->getSourceRange());
88 else
89 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
90 }
91
Steve Naroff5d2fff82007-08-31 23:28:33 +000092 return new CompoundStmt(Elts, NumElts, L, R);
Chris Lattner4b009652007-07-25 00:24:17 +000093}
94
95Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +000096Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner4b009652007-07-25 00:24:17 +000097 SourceLocation DotDotDotLoc, ExprTy *rhsval,
98 SourceLocation ColonLoc, StmtTy *subStmt) {
99 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
100 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
101 assert((LHSVal != 0) && "missing expression in case statement");
102
103 SourceLocation ExpLoc;
104 // C99 6.8.4.2p3: The expression shall be an integer constant.
105 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
106 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
107 LHSVal->getSourceRange());
108 return SubStmt;
109 }
110
111 // GCC extension: The expression shall be an integer constant.
112 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
113 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
114 RHSVal->getSourceRange());
Chris Lattner3429a812007-08-23 05:46:52 +0000115 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner4b009652007-07-25 00:24:17 +0000116 }
117
118 if (SwitchStack.empty()) {
119 Diag(CaseLoc, diag::err_case_not_in_switch);
120 return SubStmt;
121 }
122
Steve Naroff5d2fff82007-08-31 23:28:33 +0000123 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000124 SwitchStack.back()->addSwitchCase(CS);
125 return CS;
126}
127
128Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000129Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000130 StmtTy *subStmt, Scope *CurScope) {
131 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
132
133 if (SwitchStack.empty()) {
134 Diag(DefaultLoc, diag::err_default_not_in_switch);
135 return SubStmt;
136 }
137
138 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
139 SwitchStack.back()->addSwitchCase(DS);
140
141 return DS;
142}
143
144Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000145Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner4b009652007-07-25 00:24:17 +0000146 SourceLocation ColonLoc, StmtTy *subStmt) {
147 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
148 // Look up the record for this label identifier.
149 LabelStmt *&LabelDecl = LabelMap[II];
150
151 // If not forward referenced or defined already, just create a new LabelStmt.
152 if (LabelDecl == 0)
153 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
154
155 assert(LabelDecl->getID() == II && "Label mismatch!");
156
157 // Otherwise, this label was either forward reference or multiply defined. If
158 // multiply defined, reject it now.
159 if (LabelDecl->getSubStmt()) {
160 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
161 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
162 return SubStmt;
163 }
164
165 // Otherwise, this label was forward declared, and we just found its real
166 // definition. Fill in the forward definition and return it.
167 LabelDecl->setIdentLoc(IdentLoc);
168 LabelDecl->setSubStmt(SubStmt);
169 return LabelDecl;
170}
171
172Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000173Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Chris Lattner4b009652007-07-25 00:24:17 +0000174 StmtTy *ThenVal, SourceLocation ElseLoc,
175 StmtTy *ElseVal) {
176 Expr *condExpr = (Expr *)CondVal;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000177 assert(condExpr && "ActOnIfStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000178
179 DefaultFunctionArrayConversion(condExpr);
180 QualType condType = condExpr->getType();
181
182 if (!condType->isScalarType()) // C99 6.8.4.1p1
183 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
184 condType.getAsString(), condExpr->getSourceRange());
185
Steve Naroff5d2fff82007-08-31 23:28:33 +0000186 return new IfStmt(IfLoc, condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattner4b009652007-07-25 00:24:17 +0000187}
188
189Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000190Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
Chris Lattner3429a812007-08-23 05:46:52 +0000191 Expr *Cond = static_cast<Expr*>(cond);
192
193 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
194 UsualUnaryConversions(Cond);
195
196 SwitchStmt *SS = new SwitchStmt(Cond);
Chris Lattner4b009652007-07-25 00:24:17 +0000197 SwitchStack.push_back(SS);
198 return SS;
199}
200
Chris Lattner3429a812007-08-23 05:46:52 +0000201/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
202/// the specified width and sign. If an overflow occurs, detect it and emit
203/// the specified diagnostic.
204void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
205 unsigned NewWidth, bool NewSign,
206 SourceLocation Loc,
207 unsigned DiagID) {
208 // Perform a conversion to the promoted condition type if needed.
209 if (NewWidth > Val.getBitWidth()) {
210 // If this is an extension, just do it.
211 llvm::APSInt OldVal(Val);
212 Val.extend(NewWidth);
213
214 // If the input was signed and negative and the output is unsigned,
215 // warn.
216 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
217 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
218
219 Val.setIsSigned(NewSign);
220 } else if (NewWidth < Val.getBitWidth()) {
221 // If this is a truncation, check for overflow.
222 llvm::APSInt ConvVal(Val);
223 ConvVal.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000224 ConvVal.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000225 ConvVal.extend(Val.getBitWidth());
Chris Lattner5c039602007-08-23 22:08:35 +0000226 ConvVal.setIsSigned(Val.isSigned());
Chris Lattner3429a812007-08-23 05:46:52 +0000227 if (ConvVal != Val)
228 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
229
230 // Regardless of whether a diagnostic was emitted, really do the
231 // truncation.
232 Val.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000233 Val.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000234 } else if (NewSign != Val.isSigned()) {
235 // Convert the sign to match the sign of the condition. This can cause
236 // overflow as well: unsigned(INTMIN)
237 llvm::APSInt OldVal(Val);
238 Val.setIsSigned(NewSign);
239
240 if (Val.isNegative()) // Sign bit changes meaning.
241 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
242 }
243}
244
Chris Lattner0ab833c2007-08-23 18:29:20 +0000245namespace {
246 struct CaseCompareFunctor {
247 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
248 const llvm::APSInt &RHS) {
249 return LHS.first < RHS;
250 }
Chris Lattner2157f272007-09-03 18:31:57 +0000251 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
252 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
253 return LHS.first < RHS.first;
254 }
Chris Lattner0ab833c2007-08-23 18:29:20 +0000255 bool operator()(const llvm::APSInt &LHS,
256 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
257 return LHS < RHS.first;
258 }
259 };
260}
261
Chris Lattner4b009652007-07-25 00:24:17 +0000262Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000263Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
Chris Lattner4b009652007-07-25 00:24:17 +0000264 Stmt *BodyStmt = (Stmt*)Body;
265
266 SwitchStmt *SS = SwitchStack.back();
267 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
268
Steve Naroffa610eab2007-09-01 21:08:38 +0000269 SS->setBody(BodyStmt, SwitchLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000270 SwitchStack.pop_back();
271
Chris Lattner3429a812007-08-23 05:46:52 +0000272 Expr *CondExpr = SS->getCond();
273 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000274
Chris Lattner3429a812007-08-23 05:46:52 +0000275 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner4b009652007-07-25 00:24:17 +0000276 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattner3429a812007-08-23 05:46:52 +0000277 CondType.getAsString(), CondExpr->getSourceRange());
278 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000279 }
Chris Lattner3429a812007-08-23 05:46:52 +0000280
281 // Get the bitwidth of the switched-on value before promotions. We must
282 // convert the integer case values to this width before comparison.
Hartmut Kaiser612f1742007-09-16 00:04:22 +0000283 unsigned CondWidth =
284 static_cast<unsigned>(Context.getTypeSize(CondType, SwitchLoc));
Chris Lattner3429a812007-08-23 05:46:52 +0000285 bool CondIsSigned = CondType->isSignedIntegerType();
286
287 // Accumulate all of the case values in a vector so that we can sort them
288 // and detect duplicates. This vector contains the APInt for the case after
289 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000290 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
291 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000292
293 // Keep track of any GNU case ranges we see. The APSInt is the low value.
294 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
295
296 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000297
Chris Lattner1a4066d2007-08-23 06:23:56 +0000298 bool CaseListIsErroneous = false;
299
300 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000301 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000302
Chris Lattner4b009652007-07-25 00:24:17 +0000303 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000304 if (TheDefaultStmt) {
305 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
306 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Chris Lattner4b009652007-07-25 00:24:17 +0000307
Chris Lattner3429a812007-08-23 05:46:52 +0000308 // FIXME: Remove the default statement from the switch block so that
309 // we'll return a valid AST. This requires recursing down the
310 // AST and finding it, not something we are set up to do right now. For
311 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000312 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000313 }
Chris Lattner3429a812007-08-23 05:46:52 +0000314 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000315
Chris Lattner3429a812007-08-23 05:46:52 +0000316 } else {
317 CaseStmt *CS = cast<CaseStmt>(SC);
318
319 // We already verified that the expression has a i-c-e value (C99
320 // 6.8.4.2p3) - get that value now.
321 llvm::APSInt LoVal(32);
322 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
323
324 // Convert the value to the same width/sign as the condition.
325 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
326 CS->getLHS()->getLocStart(),
327 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000328
Chris Lattner1a4066d2007-08-23 06:23:56 +0000329 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000330 if (CS->getRHS())
331 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000332 else
333 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000334 }
335 }
336
Chris Lattner1a4066d2007-08-23 06:23:56 +0000337 // Sort all the scalar case values so we can easily detect duplicates.
338 std::stable_sort(CaseVals.begin(), CaseVals.end());
Chris Lattner3429a812007-08-23 05:46:52 +0000339
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000340 if (!CaseVals.empty()) {
341 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
342 if (CaseVals[i].first == CaseVals[i+1].first) {
343 // If we have a duplicate, report it.
344 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
345 diag::err_duplicate_case, CaseVals[i].first.toString());
346 Diag(CaseVals[i].second->getLHS()->getLocStart(),
347 diag::err_duplicate_case_prev);
348 // FIXME: We really want to remove the bogus case stmt from the substmt,
349 // but we have no way to do this right now.
350 CaseListIsErroneous = true;
351 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000352 }
353 }
Chris Lattner3429a812007-08-23 05:46:52 +0000354
Chris Lattner1a4066d2007-08-23 06:23:56 +0000355 // Detect duplicate case ranges, which usually don't exist at all in the first
356 // place.
357 if (!CaseRanges.empty()) {
358 // Sort all the case ranges by their low value so we can easily detect
359 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000360 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000361
362 // Scan the ranges, computing the high values and removing empty ranges.
363 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000364 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000365 CaseStmt *CR = CaseRanges[i].second;
366 llvm::APSInt HiVal(32);
367 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
368
369 // Convert the value to the same width/sign as the condition.
370 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
371 CR->getRHS()->getLocStart(),
372 diag::warn_case_value_overflow);
373
Chris Lattner7443e0f2007-08-23 17:48:14 +0000374 // If the low value is bigger than the high value, the case is empty.
375 if (CaseRanges[i].first > HiVal) {
376 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
377 SourceRange(CR->getLHS()->getLocStart(),
378 CR->getRHS()->getLocEnd()));
379 CaseRanges.erase(CaseRanges.begin()+i);
380 --i, --e;
381 continue;
382 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000383 HiVals.push_back(HiVal);
384 }
385
386 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000387 // ranges. Since the range list is sorted, we only need to compare case
388 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000389 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000390 llvm::APSInt &CRLo = CaseRanges[i].first;
391 llvm::APSInt &CRHi = HiVals[i];
392 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000393
Chris Lattner0ab833c2007-08-23 18:29:20 +0000394 // Check to see whether the case range overlaps with any singleton cases.
395 CaseStmt *OverlapStmt = 0;
396 llvm::APSInt OverlapVal(32);
397
398 // Find the smallest value >= the lower bound. If I is in the case range,
399 // then we have overlap.
400 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
401 CaseVals.end(), CRLo,
402 CaseCompareFunctor());
403 if (I != CaseVals.end() && I->first < CRHi) {
404 OverlapVal = I->first; // Found overlap with scalar.
405 OverlapStmt = I->second;
406 }
407
408 // Find the smallest value bigger than the upper bound.
409 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
410 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
411 OverlapVal = (I-1)->first; // Found overlap with scalar.
412 OverlapStmt = (I-1)->second;
413 }
414
415 // Check to see if this case stmt overlaps with the subsequent case range.
416 if (i && CRLo <= HiVals[i-1]) {
417 OverlapVal = HiVals[i-1]; // Found overlap with range.
418 OverlapStmt = CaseRanges[i-1].second;
419 }
420
421 if (OverlapStmt) {
422 // If we have a duplicate, report it.
423 Diag(CR->getLHS()->getLocStart(),
424 diag::err_duplicate_case, OverlapVal.toString());
425 Diag(OverlapStmt->getLHS()->getLocStart(),
426 diag::err_duplicate_case_prev);
427 // FIXME: We really want to remove the bogus case stmt from the substmt,
428 // but we have no way to do this right now.
429 CaseListIsErroneous = true;
430 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000431 }
432 }
Chris Lattner3429a812007-08-23 05:46:52 +0000433
Chris Lattner1a4066d2007-08-23 06:23:56 +0000434 // FIXME: If the case list was broken is some way, we don't have a good system
435 // to patch it up. Instead, just return the whole substmt as broken.
436 if (CaseListIsErroneous)
437 return true;
Chris Lattner3429a812007-08-23 05:46:52 +0000438
Chris Lattner4b009652007-07-25 00:24:17 +0000439 return SS;
440}
441
442Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000443Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Chris Lattner4b009652007-07-25 00:24:17 +0000444 Expr *condExpr = (Expr *)Cond;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000445 assert(condExpr && "ActOnWhileStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000446
447 DefaultFunctionArrayConversion(condExpr);
448 QualType condType = condExpr->getType();
449
450 if (!condType->isScalarType()) // C99 6.8.5p2
451 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
452 condType.getAsString(), condExpr->getSourceRange());
453
Steve Naroff5d2fff82007-08-31 23:28:33 +0000454 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000455}
456
457Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000458Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Chris Lattner4b009652007-07-25 00:24:17 +0000459 SourceLocation WhileLoc, ExprTy *Cond) {
460 Expr *condExpr = (Expr *)Cond;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000461 assert(condExpr && "ActOnDoStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000462
463 DefaultFunctionArrayConversion(condExpr);
464 QualType condType = condExpr->getType();
465
466 if (!condType->isScalarType()) // C99 6.8.5p2
467 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
468 condType.getAsString(), condExpr->getSourceRange());
469
Steve Naroff5d2fff82007-08-31 23:28:33 +0000470 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000471}
472
473Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000474Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000475 StmtTy *first, ExprTy *second, ExprTy *third,
476 SourceLocation RParenLoc, StmtTy *body) {
477 Stmt *First = static_cast<Stmt*>(first);
478 Expr *Second = static_cast<Expr*>(second);
479 Expr *Third = static_cast<Expr*>(third);
480 Stmt *Body = static_cast<Stmt*>(body);
481
Chris Lattner06611052007-08-28 05:03:08 +0000482 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
483 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
484 // identifiers for objects having storage class 'auto' or 'register'.
Steve Naroff2591e1b2007-09-13 23:52:58 +0000485 for (ScopedDecl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattner06611052007-08-28 05:03:08 +0000486 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D);
487 if (BVD && !BVD->hasLocalStorage())
488 BVD = 0;
489 if (BVD == 0)
Steve Naroffcb597472007-09-13 21:41:19 +0000490 Diag(dyn_cast<ScopedDecl>(D)->getLocation(),
491 diag::err_non_variable_decl_in_for);
Chris Lattner06611052007-08-28 05:03:08 +0000492 // FIXME: mark decl erroneous!
493 }
Chris Lattner4b009652007-07-25 00:24:17 +0000494 }
495 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000496 DefaultFunctionArrayConversion(Second);
497 QualType SecondType = Second->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000498
Chris Lattner3332fbd2007-08-28 04:55:47 +0000499 if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattner4b009652007-07-25 00:24:17 +0000500 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000501 SecondType.getAsString(), Second->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000502 }
Steve Naroff5d2fff82007-08-31 23:28:33 +0000503 return new ForStmt(First, Second, Third, Body, ForLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000504}
505
506
507Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000508Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000509 IdentifierInfo *LabelII) {
510 // Look up the record for this label identifier.
511 LabelStmt *&LabelDecl = LabelMap[LabelII];
512
513 // If we haven't seen this label yet, create a forward reference.
514 if (LabelDecl == 0)
515 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
516
Ted Kremeneka65ad462007-09-06 17:11:52 +0000517 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000518}
519
520Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000521Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000522 ExprTy *DestExp) {
523 // FIXME: Verify that the operand is convertible to void*.
524
525 return new IndirectGotoStmt((Expr*)DestExp);
526}
527
528Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000529Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000530 Scope *S = CurScope->getContinueParent();
531 if (!S) {
532 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
533 Diag(ContinueLoc, diag::err_continue_not_in_loop);
534 return true;
535 }
536
Steve Naroffc32a20d2007-08-31 23:49:30 +0000537 return new ContinueStmt(ContinueLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000538}
539
540Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000541Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000542 Scope *S = CurScope->getBreakParent();
543 if (!S) {
544 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
545 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
546 return true;
547 }
548
Steve Naroffc32a20d2007-08-31 23:49:30 +0000549 return new BreakStmt(BreakLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000550}
551
552
553Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000554Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Chris Lattner4b009652007-07-25 00:24:17 +0000555 Expr *RetValExp = static_cast<Expr *>(rex);
556 QualType lhsType = CurFunctionDecl->getResultType();
557
558 if (lhsType->isVoidType()) {
559 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
560 Diag(ReturnLoc, diag::ext_return_has_expr,
561 CurFunctionDecl->getIdentifier()->getName(),
562 RetValExp->getSourceRange());
Steve Naroffc32a20d2007-08-31 23:49:30 +0000563 return new ReturnStmt(ReturnLoc, RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000564 } else {
565 if (!RetValExp) {
566 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
567 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
568 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
569 else // C90 6.6.6.4p4
570 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
Steve Naroffc32a20d2007-08-31 23:49:30 +0000571 return new ReturnStmt(ReturnLoc, (Expr*)0);
Chris Lattner4b009652007-07-25 00:24:17 +0000572 }
573 }
574 // we have a non-void function with an expression, continue checking
575 QualType rhsType = RetValExp->getType();
576
577 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
578 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
579 // function return.
580 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
581 RetValExp);
Ted Kremenek235e1ad2007-08-14 18:14:14 +0000582
Chris Lattner4b009652007-07-25 00:24:17 +0000583 // decode the result (notice that extensions still return a type).
584 switch (result) {
585 case Compatible:
586 break;
587 case Incompatible:
588 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
589 lhsType.getAsString(), rhsType.getAsString(),
590 RetValExp->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000591 break;
592 case PointerFromInt:
593 // check for null pointer constant (C99 6.3.2.3p3)
594 if (!RetValExp->isNullPointerConstant(Context)) {
595 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
596 lhsType.getAsString(), rhsType.getAsString(),
597 RetValExp->getSourceRange());
598 }
599 break;
600 case IntFromPointer:
601 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
602 lhsType.getAsString(), rhsType.getAsString(),
603 RetValExp->getSourceRange());
604 break;
605 case IncompatiblePointer:
606 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
607 lhsType.getAsString(), rhsType.getAsString(),
608 RetValExp->getSourceRange());
609 break;
610 case CompatiblePointerDiscardsQualifiers:
611 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
612 lhsType.getAsString(), rhsType.getAsString(),
613 RetValExp->getSourceRange());
614 break;
615 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000616
617 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
618
Steve Naroffc32a20d2007-08-31 23:49:30 +0000619 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000620}
621