blob: 653a0ab5b9e3389830922edb6ebe538d827b9589 [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 Lattner766afb82007-09-21 18:15:22 +0000262/// CmpCaseVals - Comparison predicate for sorting case values.
263///
264static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
265 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
266 if (lhs.first < rhs.first)
267 return true;
268
269 if (lhs.first == rhs.first &&
270 lhs.second->getCaseLoc().getRawEncoding()
271 < rhs.second->getCaseLoc().getRawEncoding())
272 return true;
273 return false;
274}
275
Chris Lattner4b009652007-07-25 00:24:17 +0000276Action::StmtResult
Chris Lattner766afb82007-09-21 18:15:22 +0000277Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
278 ExprTy *Body) {
Chris Lattner4b009652007-07-25 00:24:17 +0000279 Stmt *BodyStmt = (Stmt*)Body;
280
281 SwitchStmt *SS = SwitchStack.back();
282 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
283
Steve Naroffa610eab2007-09-01 21:08:38 +0000284 SS->setBody(BodyStmt, SwitchLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000285 SwitchStack.pop_back();
286
Chris Lattner3429a812007-08-23 05:46:52 +0000287 Expr *CondExpr = SS->getCond();
288 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000289
Chris Lattner3429a812007-08-23 05:46:52 +0000290 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner4b009652007-07-25 00:24:17 +0000291 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattner3429a812007-08-23 05:46:52 +0000292 CondType.getAsString(), CondExpr->getSourceRange());
293 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000294 }
Chris Lattner3429a812007-08-23 05:46:52 +0000295
296 // Get the bitwidth of the switched-on value before promotions. We must
297 // convert the integer case values to this width before comparison.
Hartmut Kaiser612f1742007-09-16 00:04:22 +0000298 unsigned CondWidth =
299 static_cast<unsigned>(Context.getTypeSize(CondType, SwitchLoc));
Chris Lattner3429a812007-08-23 05:46:52 +0000300 bool CondIsSigned = CondType->isSignedIntegerType();
301
302 // Accumulate all of the case values in a vector so that we can sort them
303 // and detect duplicates. This vector contains the APInt for the case after
304 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000305 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
306 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000307
308 // Keep track of any GNU case ranges we see. The APSInt is the low value.
309 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
310
311 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000312
Chris Lattner1a4066d2007-08-23 06:23:56 +0000313 bool CaseListIsErroneous = false;
314
315 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000316 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000317
Chris Lattner4b009652007-07-25 00:24:17 +0000318 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000319 if (TheDefaultStmt) {
320 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
321 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Chris Lattner4b009652007-07-25 00:24:17 +0000322
Chris Lattner3429a812007-08-23 05:46:52 +0000323 // FIXME: Remove the default statement from the switch block so that
324 // we'll return a valid AST. This requires recursing down the
325 // AST and finding it, not something we are set up to do right now. For
326 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000327 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000328 }
Chris Lattner3429a812007-08-23 05:46:52 +0000329 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000330
Chris Lattner3429a812007-08-23 05:46:52 +0000331 } else {
332 CaseStmt *CS = cast<CaseStmt>(SC);
333
334 // We already verified that the expression has a i-c-e value (C99
335 // 6.8.4.2p3) - get that value now.
336 llvm::APSInt LoVal(32);
337 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
338
339 // Convert the value to the same width/sign as the condition.
340 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
341 CS->getLHS()->getLocStart(),
342 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000343
Chris Lattner1a4066d2007-08-23 06:23:56 +0000344 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000345 if (CS->getRHS())
346 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000347 else
348 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000349 }
350 }
351
Chris Lattner1a4066d2007-08-23 06:23:56 +0000352 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner766afb82007-09-21 18:15:22 +0000353 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattner3429a812007-08-23 05:46:52 +0000354
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000355 if (!CaseVals.empty()) {
356 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
357 if (CaseVals[i].first == CaseVals[i+1].first) {
358 // If we have a duplicate, report it.
359 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
360 diag::err_duplicate_case, CaseVals[i].first.toString());
361 Diag(CaseVals[i].second->getLHS()->getLocStart(),
362 diag::err_duplicate_case_prev);
363 // FIXME: We really want to remove the bogus case stmt from the substmt,
364 // but we have no way to do this right now.
365 CaseListIsErroneous = true;
366 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000367 }
368 }
Chris Lattner3429a812007-08-23 05:46:52 +0000369
Chris Lattner1a4066d2007-08-23 06:23:56 +0000370 // Detect duplicate case ranges, which usually don't exist at all in the first
371 // place.
372 if (!CaseRanges.empty()) {
373 // Sort all the case ranges by their low value so we can easily detect
374 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000375 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000376
377 // Scan the ranges, computing the high values and removing empty ranges.
378 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000379 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000380 CaseStmt *CR = CaseRanges[i].second;
381 llvm::APSInt HiVal(32);
382 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
383
384 // Convert the value to the same width/sign as the condition.
385 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
386 CR->getRHS()->getLocStart(),
387 diag::warn_case_value_overflow);
388
Chris Lattner7443e0f2007-08-23 17:48:14 +0000389 // If the low value is bigger than the high value, the case is empty.
390 if (CaseRanges[i].first > HiVal) {
391 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
392 SourceRange(CR->getLHS()->getLocStart(),
393 CR->getRHS()->getLocEnd()));
394 CaseRanges.erase(CaseRanges.begin()+i);
395 --i, --e;
396 continue;
397 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000398 HiVals.push_back(HiVal);
399 }
400
401 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000402 // ranges. Since the range list is sorted, we only need to compare case
403 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000404 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000405 llvm::APSInt &CRLo = CaseRanges[i].first;
406 llvm::APSInt &CRHi = HiVals[i];
407 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000408
Chris Lattner0ab833c2007-08-23 18:29:20 +0000409 // Check to see whether the case range overlaps with any singleton cases.
410 CaseStmt *OverlapStmt = 0;
411 llvm::APSInt OverlapVal(32);
412
413 // Find the smallest value >= the lower bound. If I is in the case range,
414 // then we have overlap.
415 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
416 CaseVals.end(), CRLo,
417 CaseCompareFunctor());
418 if (I != CaseVals.end() && I->first < CRHi) {
419 OverlapVal = I->first; // Found overlap with scalar.
420 OverlapStmt = I->second;
421 }
422
423 // Find the smallest value bigger than the upper bound.
424 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
425 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
426 OverlapVal = (I-1)->first; // Found overlap with scalar.
427 OverlapStmt = (I-1)->second;
428 }
429
430 // Check to see if this case stmt overlaps with the subsequent case range.
431 if (i && CRLo <= HiVals[i-1]) {
432 OverlapVal = HiVals[i-1]; // Found overlap with range.
433 OverlapStmt = CaseRanges[i-1].second;
434 }
435
436 if (OverlapStmt) {
437 // If we have a duplicate, report it.
438 Diag(CR->getLHS()->getLocStart(),
439 diag::err_duplicate_case, OverlapVal.toString());
440 Diag(OverlapStmt->getLHS()->getLocStart(),
441 diag::err_duplicate_case_prev);
442 // FIXME: We really want to remove the bogus case stmt from the substmt,
443 // but we have no way to do this right now.
444 CaseListIsErroneous = true;
445 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000446 }
447 }
Chris Lattner3429a812007-08-23 05:46:52 +0000448
Chris Lattner1a4066d2007-08-23 06:23:56 +0000449 // FIXME: If the case list was broken is some way, we don't have a good system
450 // to patch it up. Instead, just return the whole substmt as broken.
451 if (CaseListIsErroneous)
452 return true;
Chris Lattner3429a812007-08-23 05:46:52 +0000453
Chris Lattner4b009652007-07-25 00:24:17 +0000454 return SS;
455}
456
457Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000458Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Chris Lattner4b009652007-07-25 00:24:17 +0000459 Expr *condExpr = (Expr *)Cond;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000460 assert(condExpr && "ActOnWhileStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000461
462 DefaultFunctionArrayConversion(condExpr);
463 QualType condType = condExpr->getType();
464
465 if (!condType->isScalarType()) // C99 6.8.5p2
466 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
467 condType.getAsString(), condExpr->getSourceRange());
468
Steve Naroff5d2fff82007-08-31 23:28:33 +0000469 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000470}
471
472Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000473Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Chris Lattner4b009652007-07-25 00:24:17 +0000474 SourceLocation WhileLoc, ExprTy *Cond) {
475 Expr *condExpr = (Expr *)Cond;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000476 assert(condExpr && "ActOnDoStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000477
478 DefaultFunctionArrayConversion(condExpr);
479 QualType condType = condExpr->getType();
480
481 if (!condType->isScalarType()) // C99 6.8.5p2
482 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
483 condType.getAsString(), condExpr->getSourceRange());
484
Steve Naroff5d2fff82007-08-31 23:28:33 +0000485 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000486}
487
488Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000489Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000490 StmtTy *first, ExprTy *second, ExprTy *third,
491 SourceLocation RParenLoc, StmtTy *body) {
492 Stmt *First = static_cast<Stmt*>(first);
493 Expr *Second = static_cast<Expr*>(second);
494 Expr *Third = static_cast<Expr*>(third);
495 Stmt *Body = static_cast<Stmt*>(body);
496
Chris Lattner06611052007-08-28 05:03:08 +0000497 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
498 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
499 // identifiers for objects having storage class 'auto' or 'register'.
Steve Naroff2591e1b2007-09-13 23:52:58 +0000500 for (ScopedDecl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattner06611052007-08-28 05:03:08 +0000501 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D);
502 if (BVD && !BVD->hasLocalStorage())
503 BVD = 0;
504 if (BVD == 0)
Steve Naroffcb597472007-09-13 21:41:19 +0000505 Diag(dyn_cast<ScopedDecl>(D)->getLocation(),
506 diag::err_non_variable_decl_in_for);
Chris Lattner06611052007-08-28 05:03:08 +0000507 // FIXME: mark decl erroneous!
508 }
Chris Lattner4b009652007-07-25 00:24:17 +0000509 }
510 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000511 DefaultFunctionArrayConversion(Second);
512 QualType SecondType = Second->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000513
Chris Lattner3332fbd2007-08-28 04:55:47 +0000514 if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattner4b009652007-07-25 00:24:17 +0000515 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000516 SecondType.getAsString(), Second->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000517 }
Steve Naroff5d2fff82007-08-31 23:28:33 +0000518 return new ForStmt(First, Second, Third, Body, ForLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000519}
520
521
522Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000523Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000524 IdentifierInfo *LabelII) {
525 // Look up the record for this label identifier.
526 LabelStmt *&LabelDecl = LabelMap[LabelII];
527
528 // If we haven't seen this label yet, create a forward reference.
529 if (LabelDecl == 0)
530 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
531
Ted Kremeneka65ad462007-09-06 17:11:52 +0000532 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000533}
534
535Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000536Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000537 ExprTy *DestExp) {
538 // FIXME: Verify that the operand is convertible to void*.
539
540 return new IndirectGotoStmt((Expr*)DestExp);
541}
542
543Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000544Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000545 Scope *S = CurScope->getContinueParent();
546 if (!S) {
547 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
548 Diag(ContinueLoc, diag::err_continue_not_in_loop);
549 return true;
550 }
551
Steve Naroffc32a20d2007-08-31 23:49:30 +0000552 return new ContinueStmt(ContinueLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000553}
554
555Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000556Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000557 Scope *S = CurScope->getBreakParent();
558 if (!S) {
559 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
560 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
561 return true;
562 }
563
Steve Naroffc32a20d2007-08-31 23:49:30 +0000564 return new BreakStmt(BreakLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000565}
566
567
568Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000569Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Chris Lattner4b009652007-07-25 00:24:17 +0000570 Expr *RetValExp = static_cast<Expr *>(rex);
571 QualType lhsType = CurFunctionDecl->getResultType();
572
573 if (lhsType->isVoidType()) {
574 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
575 Diag(ReturnLoc, diag::ext_return_has_expr,
576 CurFunctionDecl->getIdentifier()->getName(),
577 RetValExp->getSourceRange());
Steve Naroffc32a20d2007-08-31 23:49:30 +0000578 return new ReturnStmt(ReturnLoc, RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000579 } else {
580 if (!RetValExp) {
581 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
582 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
583 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
584 else // C90 6.6.6.4p4
585 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
Steve Naroffc32a20d2007-08-31 23:49:30 +0000586 return new ReturnStmt(ReturnLoc, (Expr*)0);
Chris Lattner4b009652007-07-25 00:24:17 +0000587 }
588 }
589 // we have a non-void function with an expression, continue checking
590 QualType rhsType = RetValExp->getType();
591
592 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
593 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
594 // function return.
595 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
596 RetValExp);
Ted Kremenek235e1ad2007-08-14 18:14:14 +0000597
Chris Lattner4b009652007-07-25 00:24:17 +0000598 // decode the result (notice that extensions still return a type).
599 switch (result) {
600 case Compatible:
601 break;
602 case Incompatible:
603 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
604 lhsType.getAsString(), rhsType.getAsString(),
605 RetValExp->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000606 break;
607 case PointerFromInt:
608 // check for null pointer constant (C99 6.3.2.3p3)
609 if (!RetValExp->isNullPointerConstant(Context)) {
610 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
611 lhsType.getAsString(), rhsType.getAsString(),
612 RetValExp->getSourceRange());
613 }
614 break;
615 case IntFromPointer:
616 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
617 lhsType.getAsString(), rhsType.getAsString(),
618 RetValExp->getSourceRange());
619 break;
620 case IncompatiblePointer:
621 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
622 lhsType.getAsString(), rhsType.getAsString(),
623 RetValExp->getSourceRange());
624 break;
625 case CompatiblePointerDiscardsQualifiers:
626 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
627 lhsType.getAsString(), rhsType.getAsString(),
628 RetValExp->getSourceRange());
629 break;
630 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000631
632 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
633
Steve Naroffc32a20d2007-08-31 23:49:30 +0000634 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000635}
636