blob: b6d4c5580d46f88f597b6e8c2de9004db271ee4a [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"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
Steve Naroff5cbb02f2007-09-16 14:56:35 +000023Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) {
Chris Lattner4b009652007-07-25 00:24:17 +000024 Expr *E = static_cast<Expr*>(expr);
Steve Naroff5cbb02f2007-09-16 14:56:35 +000025 assert(E && "ActOnExprStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +000026 return E;
27}
28
29
Steve Naroff5cbb02f2007-09-16 14:56:35 +000030Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000031 return new NullStmt(SemiLoc);
32}
33
Steve Naroff5cbb02f2007-09-16 14:56:35 +000034Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl) {
Steve Naroffcb597472007-09-13 21:41:19 +000035 if (decl) {
36 ScopedDecl *SD = dyn_cast<ScopedDecl>(static_cast<Decl *>(decl));
Steve Naroff5cbb02f2007-09-16 14:56:35 +000037 assert(SD && "Sema::ActOnDeclStmt(): expected ScopedDecl");
Steve Naroffcb597472007-09-13 21:41:19 +000038 return new DeclStmt(SD);
39 } else
Chris Lattner4b009652007-07-25 00:24:17 +000040 return true; // error
41}
42
43Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +000044Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnerf2b07572007-08-31 21:49:55 +000045 StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
Chris Lattner3ea3b662007-08-27 04:29:41 +000046 Stmt **Elts = reinterpret_cast<Stmt**>(elts);
47 // If we're in C89 mode, check that we don't have any decls after stmts. If
48 // so, emit an extension diagnostic.
49 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
50 // Note that __extension__ can be around a decl.
51 unsigned i = 0;
52 // Skip over all declarations.
53 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
54 /*empty*/;
55
56 // We found the end of the list or a statement. Scan for another declstmt.
57 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
58 /*empty*/;
59
60 if (i != NumElts) {
Steve Naroffcb597472007-09-13 21:41:19 +000061 ScopedDecl *D = cast<DeclStmt>(Elts[i])->getDecl();
Chris Lattner3ea3b662007-08-27 04:29:41 +000062 Diag(D->getLocation(), diag::ext_mixed_decls_code);
63 }
64 }
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
Steve Naroff5d2fff82007-08-31 23:28:33 +000091 return new CompoundStmt(Elts, NumElts, L, R);
Chris Lattner4b009652007-07-25 00:24:17 +000092}
93
94Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +000095Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner4b009652007-07-25 00:24:17 +000096 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
Steve Naroff5d2fff82007-08-31 23:28:33 +0000122 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000123 SwitchStack.back()->addSwitchCase(CS);
124 return CS;
125}
126
127Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000128Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000129 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
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000144Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner4b009652007-07-25 00:24:17 +0000145 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
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000172Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Chris Lattner4b009652007-07-25 00:24:17 +0000173 StmtTy *ThenVal, SourceLocation ElseLoc,
174 StmtTy *ElseVal) {
175 Expr *condExpr = (Expr *)CondVal;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000176 assert(condExpr && "ActOnIfStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000177
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
Steve Naroff5d2fff82007-08-31 23:28:33 +0000185 return new IfStmt(IfLoc, condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattner4b009652007-07-25 00:24:17 +0000186}
187
188Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000189Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
Chris Lattner3429a812007-08-23 05:46:52 +0000190 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 }
Chris Lattner2157f272007-09-03 18:31:57 +0000250 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
251 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
252 return LHS.first < RHS.first;
253 }
Chris Lattner0ab833c2007-08-23 18:29:20 +0000254 bool operator()(const llvm::APSInt &LHS,
255 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
256 return LHS < RHS.first;
257 }
258 };
259}
260
Chris Lattner766afb82007-09-21 18:15:22 +0000261/// CmpCaseVals - Comparison predicate for sorting case values.
262///
263static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
264 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
265 if (lhs.first < rhs.first)
266 return true;
267
268 if (lhs.first == rhs.first &&
269 lhs.second->getCaseLoc().getRawEncoding()
270 < rhs.second->getCaseLoc().getRawEncoding())
271 return true;
272 return false;
273}
274
Chris Lattner4b009652007-07-25 00:24:17 +0000275Action::StmtResult
Chris Lattner766afb82007-09-21 18:15:22 +0000276Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
277 ExprTy *Body) {
Chris Lattner4b009652007-07-25 00:24:17 +0000278 Stmt *BodyStmt = (Stmt*)Body;
279
280 SwitchStmt *SS = SwitchStack.back();
281 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
282
Steve Naroffa610eab2007-09-01 21:08:38 +0000283 SS->setBody(BodyStmt, SwitchLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000284 SwitchStack.pop_back();
285
Chris Lattner3429a812007-08-23 05:46:52 +0000286 Expr *CondExpr = SS->getCond();
287 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000288
Chris Lattner3429a812007-08-23 05:46:52 +0000289 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner4b009652007-07-25 00:24:17 +0000290 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattner3429a812007-08-23 05:46:52 +0000291 CondType.getAsString(), CondExpr->getSourceRange());
292 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000293 }
Chris Lattner3429a812007-08-23 05:46:52 +0000294
295 // Get the bitwidth of the switched-on value before promotions. We must
296 // convert the integer case values to this width before comparison.
Hartmut Kaiser612f1742007-09-16 00:04:22 +0000297 unsigned CondWidth =
298 static_cast<unsigned>(Context.getTypeSize(CondType, SwitchLoc));
Chris Lattner3429a812007-08-23 05:46:52 +0000299 bool CondIsSigned = CondType->isSignedIntegerType();
300
301 // Accumulate all of the case values in a vector so that we can sort them
302 // and detect duplicates. This vector contains the APInt for the case after
303 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000304 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
305 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000306
307 // Keep track of any GNU case ranges we see. The APSInt is the low value.
308 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
309
310 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000311
Chris Lattner1a4066d2007-08-23 06:23:56 +0000312 bool CaseListIsErroneous = false;
313
314 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000315 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000316
Chris Lattner4b009652007-07-25 00:24:17 +0000317 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000318 if (TheDefaultStmt) {
319 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
320 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Chris Lattner4b009652007-07-25 00:24:17 +0000321
Chris Lattner3429a812007-08-23 05:46:52 +0000322 // FIXME: Remove the default statement from the switch block so that
323 // we'll return a valid AST. This requires recursing down the
324 // AST and finding it, not something we are set up to do right now. For
325 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000326 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000327 }
Chris Lattner3429a812007-08-23 05:46:52 +0000328 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000329
Chris Lattner3429a812007-08-23 05:46:52 +0000330 } else {
331 CaseStmt *CS = cast<CaseStmt>(SC);
332
333 // We already verified that the expression has a i-c-e value (C99
334 // 6.8.4.2p3) - get that value now.
335 llvm::APSInt LoVal(32);
336 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
337
338 // Convert the value to the same width/sign as the condition.
339 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
340 CS->getLHS()->getLocStart(),
341 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000342
Chris Lattner1a4066d2007-08-23 06:23:56 +0000343 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000344 if (CS->getRHS())
345 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000346 else
347 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000348 }
349 }
350
Chris Lattner1a4066d2007-08-23 06:23:56 +0000351 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner766afb82007-09-21 18:15:22 +0000352 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattner3429a812007-08-23 05:46:52 +0000353
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000354 if (!CaseVals.empty()) {
355 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
356 if (CaseVals[i].first == CaseVals[i+1].first) {
357 // If we have a duplicate, report it.
358 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
359 diag::err_duplicate_case, CaseVals[i].first.toString());
360 Diag(CaseVals[i].second->getLHS()->getLocStart(),
361 diag::err_duplicate_case_prev);
362 // FIXME: We really want to remove the bogus case stmt from the substmt,
363 // but we have no way to do this right now.
364 CaseListIsErroneous = true;
365 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000366 }
367 }
Chris Lattner3429a812007-08-23 05:46:52 +0000368
Chris Lattner1a4066d2007-08-23 06:23:56 +0000369 // Detect duplicate case ranges, which usually don't exist at all in the first
370 // place.
371 if (!CaseRanges.empty()) {
372 // Sort all the case ranges by their low value so we can easily detect
373 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000374 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000375
376 // Scan the ranges, computing the high values and removing empty ranges.
377 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000378 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000379 CaseStmt *CR = CaseRanges[i].second;
380 llvm::APSInt HiVal(32);
381 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
382
383 // Convert the value to the same width/sign as the condition.
384 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
385 CR->getRHS()->getLocStart(),
386 diag::warn_case_value_overflow);
387
Chris Lattner7443e0f2007-08-23 17:48:14 +0000388 // If the low value is bigger than the high value, the case is empty.
389 if (CaseRanges[i].first > HiVal) {
390 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
391 SourceRange(CR->getLHS()->getLocStart(),
392 CR->getRHS()->getLocEnd()));
393 CaseRanges.erase(CaseRanges.begin()+i);
394 --i, --e;
395 continue;
396 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000397 HiVals.push_back(HiVal);
398 }
399
400 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000401 // ranges. Since the range list is sorted, we only need to compare case
402 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000403 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000404 llvm::APSInt &CRLo = CaseRanges[i].first;
405 llvm::APSInt &CRHi = HiVals[i];
406 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000407
Chris Lattner0ab833c2007-08-23 18:29:20 +0000408 // Check to see whether the case range overlaps with any singleton cases.
409 CaseStmt *OverlapStmt = 0;
410 llvm::APSInt OverlapVal(32);
411
412 // Find the smallest value >= the lower bound. If I is in the case range,
413 // then we have overlap.
414 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
415 CaseVals.end(), CRLo,
416 CaseCompareFunctor());
417 if (I != CaseVals.end() && I->first < CRHi) {
418 OverlapVal = I->first; // Found overlap with scalar.
419 OverlapStmt = I->second;
420 }
421
422 // Find the smallest value bigger than the upper bound.
423 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
424 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
425 OverlapVal = (I-1)->first; // Found overlap with scalar.
426 OverlapStmt = (I-1)->second;
427 }
428
429 // Check to see if this case stmt overlaps with the subsequent case range.
430 if (i && CRLo <= HiVals[i-1]) {
431 OverlapVal = HiVals[i-1]; // Found overlap with range.
432 OverlapStmt = CaseRanges[i-1].second;
433 }
434
435 if (OverlapStmt) {
436 // If we have a duplicate, report it.
437 Diag(CR->getLHS()->getLocStart(),
438 diag::err_duplicate_case, OverlapVal.toString());
439 Diag(OverlapStmt->getLHS()->getLocStart(),
440 diag::err_duplicate_case_prev);
441 // FIXME: We really want to remove the bogus case stmt from the substmt,
442 // but we have no way to do this right now.
443 CaseListIsErroneous = true;
444 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000445 }
446 }
Chris Lattner3429a812007-08-23 05:46:52 +0000447
Chris Lattner1a4066d2007-08-23 06:23:56 +0000448 // FIXME: If the case list was broken is some way, we don't have a good system
449 // to patch it up. Instead, just return the whole substmt as broken.
450 if (CaseListIsErroneous)
451 return true;
Chris Lattner3429a812007-08-23 05:46:52 +0000452
Chris Lattner4b009652007-07-25 00:24:17 +0000453 return SS;
454}
455
456Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000457Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Chris Lattner4b009652007-07-25 00:24:17 +0000458 Expr *condExpr = (Expr *)Cond;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000459 assert(condExpr && "ActOnWhileStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000460
461 DefaultFunctionArrayConversion(condExpr);
462 QualType condType = condExpr->getType();
463
464 if (!condType->isScalarType()) // C99 6.8.5p2
465 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
466 condType.getAsString(), condExpr->getSourceRange());
467
Steve Naroff5d2fff82007-08-31 23:28:33 +0000468 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000469}
470
471Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000472Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Chris Lattner4b009652007-07-25 00:24:17 +0000473 SourceLocation WhileLoc, ExprTy *Cond) {
474 Expr *condExpr = (Expr *)Cond;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000475 assert(condExpr && "ActOnDoStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000476
477 DefaultFunctionArrayConversion(condExpr);
478 QualType condType = condExpr->getType();
479
480 if (!condType->isScalarType()) // C99 6.8.5p2
481 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
482 condType.getAsString(), condExpr->getSourceRange());
483
Steve Naroff5d2fff82007-08-31 23:28:33 +0000484 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000485}
486
487Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000488Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000489 StmtTy *first, ExprTy *second, ExprTy *third,
490 SourceLocation RParenLoc, StmtTy *body) {
491 Stmt *First = static_cast<Stmt*>(first);
492 Expr *Second = static_cast<Expr*>(second);
493 Expr *Third = static_cast<Expr*>(third);
494 Stmt *Body = static_cast<Stmt*>(body);
495
Chris Lattner06611052007-08-28 05:03:08 +0000496 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
497 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
498 // identifiers for objects having storage class 'auto' or 'register'.
Steve Naroff2591e1b2007-09-13 23:52:58 +0000499 for (ScopedDecl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattner06611052007-08-28 05:03:08 +0000500 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D);
501 if (BVD && !BVD->hasLocalStorage())
502 BVD = 0;
503 if (BVD == 0)
Steve Naroffcb597472007-09-13 21:41:19 +0000504 Diag(dyn_cast<ScopedDecl>(D)->getLocation(),
505 diag::err_non_variable_decl_in_for);
Chris Lattner06611052007-08-28 05:03:08 +0000506 // FIXME: mark decl erroneous!
507 }
Chris Lattner4b009652007-07-25 00:24:17 +0000508 }
509 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000510 DefaultFunctionArrayConversion(Second);
511 QualType SecondType = Second->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000512
Chris Lattner3332fbd2007-08-28 04:55:47 +0000513 if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattner4b009652007-07-25 00:24:17 +0000514 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000515 SecondType.getAsString(), Second->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000516 }
Steve Naroff5d2fff82007-08-31 23:28:33 +0000517 return new ForStmt(First, Second, Third, Body, ForLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000518}
519
520
521Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000522Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000523 IdentifierInfo *LabelII) {
524 // Look up the record for this label identifier.
525 LabelStmt *&LabelDecl = LabelMap[LabelII];
526
527 // If we haven't seen this label yet, create a forward reference.
528 if (LabelDecl == 0)
529 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
530
Ted Kremeneka65ad462007-09-06 17:11:52 +0000531 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000532}
533
534Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000535Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000536 ExprTy *DestExp) {
537 // FIXME: Verify that the operand is convertible to void*.
538
539 return new IndirectGotoStmt((Expr*)DestExp);
540}
541
542Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000543Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000544 Scope *S = CurScope->getContinueParent();
545 if (!S) {
546 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
547 Diag(ContinueLoc, diag::err_continue_not_in_loop);
548 return true;
549 }
550
Steve Naroffc32a20d2007-08-31 23:49:30 +0000551 return new ContinueStmt(ContinueLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000552}
553
554Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000555Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000556 Scope *S = CurScope->getBreakParent();
557 if (!S) {
558 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
559 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
560 return true;
561 }
562
Steve Naroffc32a20d2007-08-31 23:49:30 +0000563 return new BreakStmt(BreakLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000564}
565
566
567Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000568Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Chris Lattner4b009652007-07-25 00:24:17 +0000569 Expr *RetValExp = static_cast<Expr *>(rex);
570 QualType lhsType = CurFunctionDecl->getResultType();
571
572 if (lhsType->isVoidType()) {
573 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
574 Diag(ReturnLoc, diag::ext_return_has_expr,
575 CurFunctionDecl->getIdentifier()->getName(),
576 RetValExp->getSourceRange());
Steve Naroffc32a20d2007-08-31 23:49:30 +0000577 return new ReturnStmt(ReturnLoc, RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000578 } else {
579 if (!RetValExp) {
580 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
581 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
582 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
583 else // C90 6.6.6.4p4
584 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
Steve Naroffc32a20d2007-08-31 23:49:30 +0000585 return new ReturnStmt(ReturnLoc, (Expr*)0);
Chris Lattner4b009652007-07-25 00:24:17 +0000586 }
587 }
588 // we have a non-void function with an expression, continue checking
589 QualType rhsType = RetValExp->getType();
590
591 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
592 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
593 // function return.
594 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
595 RetValExp);
Ted Kremenek235e1ad2007-08-14 18:14:14 +0000596
Chris Lattner4b009652007-07-25 00:24:17 +0000597 // decode the result (notice that extensions still return a type).
598 switch (result) {
599 case Compatible:
600 break;
601 case Incompatible:
602 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
603 lhsType.getAsString(), rhsType.getAsString(),
604 RetValExp->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000605 break;
606 case PointerFromInt:
607 // check for null pointer constant (C99 6.3.2.3p3)
608 if (!RetValExp->isNullPointerConstant(Context)) {
609 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
610 lhsType.getAsString(), rhsType.getAsString(),
611 RetValExp->getSourceRange());
612 }
613 break;
614 case IntFromPointer:
615 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
616 lhsType.getAsString(), rhsType.getAsString(),
617 RetValExp->getSourceRange());
618 break;
619 case IncompatiblePointer:
620 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
621 lhsType.getAsString(), rhsType.getAsString(),
622 RetValExp->getSourceRange());
623 break;
624 case CompatiblePointerDiscardsQualifiers:
625 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
626 lhsType.getAsString(), rhsType.getAsString(),
627 RetValExp->getSourceRange());
628 break;
629 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000630
631 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
632
Steve Naroffc32a20d2007-08-31 23:49:30 +0000633 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000634}
635