blob: 0522996a55e02113bf3da4d45b23e4a566cc1ec7 [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
Steve Naroff66356bd2007-09-16 14:56:35 +000024Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) {
Chris Lattner1ec5f562007-06-27 05:38:08 +000025 Expr *E = static_cast<Expr*>(expr);
Steve Naroff66356bd2007-09-16 14:56:35 +000026 assert(E && "ActOnExprStmt(): missing expression");
Chris Lattner1ec5f562007-06-27 05:38:08 +000027 return E;
28}
29
30
Steve Naroff66356bd2007-09-16 14:56:35 +000031Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Chris Lattner0f203a72007-05-28 01:45:28 +000032 return new NullStmt(SemiLoc);
33}
34
Steve Naroff66356bd2007-09-16 14:56:35 +000035Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl) {
Steve Naroff9def2b12007-09-13 21:41:19 +000036 if (decl) {
37 ScopedDecl *SD = dyn_cast<ScopedDecl>(static_cast<Decl *>(decl));
Steve Naroff66356bd2007-09-16 14:56:35 +000038 assert(SD && "Sema::ActOnDeclStmt(): expected ScopedDecl");
Steve Naroff9def2b12007-09-13 21:41:19 +000039 return new DeclStmt(SD);
40 } else
Steve Naroff56faab22007-05-30 04:20:12 +000041 return true; // error
Steve Naroff2a8ad182007-05-29 22:59:26 +000042}
Chris Lattneraf8d5812006-11-10 05:07:45 +000043
44Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +000045Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnercac27a52007-08-31 21:49:55 +000046 StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
Chris Lattnerd864daf2007-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 Naroff9def2b12007-09-13 21:41:19 +000062 ScopedDecl *D = cast<DeclStmt>(Elts[i])->getDecl();
Chris Lattnerd864daf2007-08-27 04:29:41 +000063 Diag(D->getLocation(), diag::ext_mixed_decls_code);
64 }
65 }
Chris Lattnercac27a52007-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 Naroff33f3d052007-08-31 23:28:33 +000092 return new CompoundStmt(Elts, NumElts, L, R);
Chris Lattneraf8d5812006-11-10 05:07:45 +000093}
94
95Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +000096Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner46eeb222007-07-18 02:28:47 +000097 SourceLocation DotDotDotLoc, ExprTy *rhsval,
Chris Lattner39407372007-07-21 03:00:26 +000098 SourceLocation ColonLoc, StmtTy *subStmt) {
99 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000100 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
Steve Naroff8eeeb132007-05-08 21:09:37 +0000101 assert((LHSVal != 0) && "missing expression in case statement");
Anders Carlsson51873c22007-07-22 07:07:56 +0000102
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000103 SourceLocation ExpLoc;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000104 // C99 6.8.4.2p3: The expression shall be an integer constant.
Chris Lattner39407372007-07-21 03:00:26 +0000105 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
106 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
107 LHSVal->getSourceRange());
108 return SubStmt;
109 }
Steve Naroff8eeeb132007-05-08 21:09:37 +0000110
Chris Lattner46eeb222007-07-18 02:28:47 +0000111 // GCC extension: The expression shall be an integer constant.
Chris Lattner39407372007-07-21 03:00:26 +0000112 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
113 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
114 RHSVal->getSourceRange());
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000115 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner46eeb222007-07-18 02:28:47 +0000116 }
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000117
118 if (SwitchStack.empty()) {
119 Diag(CaseLoc, diag::err_case_not_in_switch);
120 return SubStmt;
121 }
Chris Lattner35e287b2007-06-03 01:44:43 +0000122
Steve Naroff33f3d052007-08-31 23:28:33 +0000123 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000124 SwitchStack.back()->addSwitchCase(CS);
Anders Carlsson51873c22007-07-22 07:07:56 +0000125 return CS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000126}
127
128Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000129Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner39407372007-07-21 03:00:26 +0000130 StmtTy *subStmt, Scope *CurScope) {
131 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner46eeb222007-07-18 02:28:47 +0000132
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000133 if (SwitchStack.empty()) {
Chris Lattner39407372007-07-21 03:00:26 +0000134 Diag(DefaultLoc, diag::err_default_not_in_switch);
135 return SubStmt;
136 }
137
Chris Lattner39407372007-07-21 03:00:26 +0000138 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000139 SwitchStack.back()->addSwitchCase(DS);
Anders Carlsson51873c22007-07-22 07:07:56 +0000140
Chris Lattner46eeb222007-07-18 02:28:47 +0000141 return DS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000142}
143
144Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000145Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner39407372007-07-21 03:00:26 +0000146 SourceLocation ColonLoc, StmtTy *subStmt) {
147 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000148 // 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)
Chris Lattner39407372007-07-21 03:00:26 +0000153 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000154
Chris Lattnereefa10e2007-05-28 06:56:27 +0000155 assert(LabelDecl->getID() == II && "Label mismatch!");
Chris Lattnere2473062007-05-28 06:28:18 +0000156
157 // Otherwise, this label was either forward reference or multiply defined. If
158 // multiply defined, reject it now.
159 if (LabelDecl->getSubStmt()) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000160 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000161 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
Chris Lattner39407372007-07-21 03:00:26 +0000162 return SubStmt;
Chris Lattnere2473062007-05-28 06:28:18 +0000163 }
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);
Chris Lattner39407372007-07-21 03:00:26 +0000168 LabelDecl->setSubStmt(SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000169 return LabelDecl;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000170}
171
172Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000173Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Chris Lattneraf8d5812006-11-10 05:07:45 +0000174 StmtTy *ThenVal, SourceLocation ElseLoc,
175 StmtTy *ElseVal) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000176 Expr *condExpr = (Expr *)CondVal;
Steve Naroff66356bd2007-09-16 14:56:35 +0000177 assert(condExpr && "ActOnIfStmt(): missing expression");
Steve Naroff86272ea2007-05-29 02:14:17 +0000178
Steve Naroff31090012007-07-16 21:54:35 +0000179 DefaultFunctionArrayConversion(condExpr);
180 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000181
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 Naroff33f3d052007-08-31 23:28:33 +0000186 return new IfStmt(IfLoc, condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000187}
Steve Naroff86272ea2007-05-29 02:14:17 +0000188
Chris Lattneraf8d5812006-11-10 05:07:45 +0000189Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000190Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
Chris Lattnerfc1c44a2007-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);
Anders Carlsson51873c22007-07-22 07:07:56 +0000197 SwitchStack.push_back(SS);
198 return SS;
199}
Chris Lattner46eeb222007-07-18 02:28:47 +0000200
Chris Lattnerfc1c44a2007-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 Lattner247ef952007-08-23 22:08:35 +0000224 ConvVal.setIsSigned(NewSign);
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000225 ConvVal.extend(Val.getBitWidth());
Chris Lattner247ef952007-08-23 22:08:35 +0000226 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerfc1c44a2007-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 Lattner247ef952007-08-23 22:08:35 +0000233 Val.setIsSigned(NewSign);
Chris Lattnerfc1c44a2007-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 Lattner67998452007-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 Lattner1463cca2007-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 Lattner67998452007-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
Anders Carlsson51873c22007-07-22 07:07:56 +0000262Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000263Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
Anders Carlsson51873c22007-07-22 07:07:56 +0000264 Stmt *BodyStmt = (Stmt*)Body;
265
266 SwitchStmt *SS = SwitchStack.back();
267 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
268
Steve Naroff42a350a2007-09-01 21:08:38 +0000269 SS->setBody(BodyStmt, SwitchLoc);
Anders Carlsson51873c22007-07-22 07:07:56 +0000270 SwitchStack.pop_back();
271
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000272 Expr *CondExpr = SS->getCond();
273 QualType CondType = CondExpr->getType();
Chris Lattner46eeb222007-07-18 02:28:47 +0000274
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000275 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Anders Carlsson51873c22007-07-22 07:07:56 +0000276 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000277 CondType.getAsString(), CondExpr->getSourceRange());
278 return true;
Anders Carlsson51873c22007-07-22 07:07:56 +0000279 }
Chris Lattnerfc1c44a2007-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 Kaiser0a6b11d2007-09-16 00:04:22 +0000283 unsigned CondWidth =
284 static_cast<unsigned>(Context.getTypeSize(CondType, SwitchLoc));
Chris Lattnerfc1c44a2007-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 Lattner67998452007-08-23 18:29:20 +0000290 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
291 CaseValsTy CaseVals;
Chris Lattnerfc1c44a2007-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;
Anders Carlsson51873c22007-07-22 07:07:56 +0000297
Chris Lattner10cb5e52007-08-23 06:23:56 +0000298 bool CaseListIsErroneous = false;
299
300 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlsson51873c22007-07-22 07:07:56 +0000301 SC = SC->getNextSwitchCase()) {
Chris Lattner10cb5e52007-08-23 06:23:56 +0000302
Anders Carlsson51873c22007-07-22 07:07:56 +0000303 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerfc1c44a2007-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);
Anders Carlsson51873c22007-07-22 07:07:56 +0000307
Chris Lattnerfc1c44a2007-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 Lattner10cb5e52007-08-23 06:23:56 +0000312 CaseListIsErroneous = true;
Anders Carlsson51873c22007-07-22 07:07:56 +0000313 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000314 TheDefaultStmt = DS;
Anders Carlsson51873c22007-07-22 07:07:56 +0000315
Chris Lattnerfc1c44a2007-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);
Anders Carlsson51873c22007-07-22 07:07:56 +0000328
Chris Lattner10cb5e52007-08-23 06:23:56 +0000329 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000330 if (CS->getRHS())
331 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner10cb5e52007-08-23 06:23:56 +0000332 else
333 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000334 }
335 }
336
Chris Lattner10cb5e52007-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 Lattnerfc1c44a2007-08-23 05:46:52 +0000339
Chris Lattnerfcb920d2007-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 Lattner10cb5e52007-08-23 06:23:56 +0000352 }
353 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000354
Chris Lattner10cb5e52007-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 Lattner67998452007-08-23 18:29:20 +0000360 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner10cb5e52007-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 Lattnerf81460f2007-08-23 17:48:14 +0000364 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner10cb5e52007-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 Lattnerf81460f2007-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 Lattner10cb5e52007-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 Lattner67998452007-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 Lattner10cb5e52007-08-23 06:23:56 +0000389 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner67998452007-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 Lattner10cb5e52007-08-23 06:23:56 +0000393
Chris Lattner67998452007-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 Lattner10cb5e52007-08-23 06:23:56 +0000431 }
432 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000433
Chris Lattner10cb5e52007-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 Lattnerfc1c44a2007-08-23 05:46:52 +0000438
Anders Carlsson51873c22007-07-22 07:07:56 +0000439 return SS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000440}
441
442Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000443Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000444 Expr *condExpr = (Expr *)Cond;
Steve Naroff66356bd2007-09-16 14:56:35 +0000445 assert(condExpr && "ActOnWhileStmt(): missing expression");
Steve Naroff86272ea2007-05-29 02:14:17 +0000446
Steve Naroff31090012007-07-16 21:54:35 +0000447 DefaultFunctionArrayConversion(condExpr);
448 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000449
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 Naroff33f3d052007-08-31 23:28:33 +0000454 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000455}
456
457Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000458Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Chris Lattneraf8d5812006-11-10 05:07:45 +0000459 SourceLocation WhileLoc, ExprTy *Cond) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000460 Expr *condExpr = (Expr *)Cond;
Steve Naroff66356bd2007-09-16 14:56:35 +0000461 assert(condExpr && "ActOnDoStmt(): missing expression");
Steve Naroff86272ea2007-05-29 02:14:17 +0000462
Steve Naroff31090012007-07-16 21:54:35 +0000463 DefaultFunctionArrayConversion(condExpr);
464 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000465
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 Naroff33f3d052007-08-31 23:28:33 +0000470 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000471}
472
473Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000474Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner773c0592007-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 Lattner39f920f2007-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 Naroffa23cc792007-09-13 23:52:58 +0000485 for (ScopedDecl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattner39f920f2007-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 Naroff9def2b12007-09-13 21:41:19 +0000490 Diag(dyn_cast<ScopedDecl>(D)->getLocation(),
491 diag::err_non_variable_decl_in_for);
Chris Lattner39f920f2007-08-28 05:03:08 +0000492 // FIXME: mark decl erroneous!
493 }
Steve Naroff86272ea2007-05-29 02:14:17 +0000494 }
495 if (Second) {
Chris Lattner773c0592007-08-28 04:55:47 +0000496 DefaultFunctionArrayConversion(Second);
497 QualType SecondType = Second->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000498
Chris Lattner773c0592007-08-28 04:55:47 +0000499 if (!SecondType->isScalarType()) // C99 6.8.5p2
Steve Naroff86272ea2007-05-29 02:14:17 +0000500 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner773c0592007-08-28 04:55:47 +0000501 SecondType.getAsString(), Second->getSourceRange());
Steve Naroff86272ea2007-05-29 02:14:17 +0000502 }
Steve Naroff33f3d052007-08-31 23:28:33 +0000503 return new ForStmt(First, Second, Third, Body, ForLoc);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000504}
505
506
507Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000508Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Chris Lattneraf8d5812006-11-10 05:07:45 +0000509 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000510 // 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 Kremenek73c18e02007-09-06 17:11:52 +0000517 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000518}
Chris Lattner1c310502007-05-31 06:00:00 +0000519
Chris Lattneraf8d5812006-11-10 05:07:45 +0000520Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000521Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Chris Lattneraf8d5812006-11-10 05:07:45 +0000522 ExprTy *DestExp) {
Chris Lattner1c310502007-05-31 06:00:00 +0000523 // FIXME: Verify that the operand is convertible to void*.
524
Chris Lattneraf8d5812006-11-10 05:07:45 +0000525 return new IndirectGotoStmt((Expr*)DestExp);
526}
527
528Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000529Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Chris Lattnereaafe1222006-11-10 05:17:58 +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 Naroffdc9f36e2007-08-31 23:49:30 +0000537 return new ContinueStmt(ContinueLoc);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000538}
539
540Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000541Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Chris Lattnereaafe1222006-11-10 05:17:58 +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 Naroffdc9f36e2007-08-31 23:49:30 +0000549 return new BreakStmt(BreakLoc);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000550}
551
552
553Action::StmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000554Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000555 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff9358c712007-05-27 23:58:33 +0000556 QualType lhsType = CurFunctionDecl->getResultType();
557
Steve Naroff9358c712007-05-27 23:58:33 +0000558 if (lhsType->isVoidType()) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000559 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
560 Diag(ReturnLoc, diag::ext_return_has_expr,
561 CurFunctionDecl->getIdentifier()->getName(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000562 RetValExp->getSourceRange());
Steve Naroffdc9f36e2007-08-31 23:49:30 +0000563 return new ReturnStmt(ReturnLoc, RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +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 Naroffdc9f36e2007-08-31 23:49:30 +0000571 return new ReturnStmt(ReturnLoc, (Expr*)0);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000572 }
Steve Naroff9358c712007-05-27 23:58:33 +0000573 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000574 // we have a non-void function with an expression, continue checking
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000575 QualType rhsType = RetValExp->getType();
Steve Naroff9358c712007-05-27 23:58:33 +0000576
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000577 // 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.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000580 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
581 RetValExp);
Ted Kremenekc48affb2007-08-14 18:14:14 +0000582
Steve Naroff9358c712007-05-27 23:58:33 +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(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000590 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000591 break;
592 case PointerFromInt:
593 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000594 if (!RetValExp->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000595 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
596 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000597 RetValExp->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000598 }
Steve Naroff9358c712007-05-27 23:58:33 +0000599 break;
600 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000601 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
602 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000603 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000604 break;
605 case IncompatiblePointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000606 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
607 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000608 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000609 break;
610 case CompatiblePointerDiscardsQualifiers:
Steve Naroff56faab22007-05-30 04:20:12 +0000611 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
612 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000613 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000614 break;
615 }
Ted Kremenekcff94fa2007-08-17 16:46:58 +0000616
617 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
618
Steve Naroffdc9f36e2007-08-31 23:49:30 +0000619 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000620}
621