blob: f4c54147778761b9d1f19eb1dc67b1cc0192ee47 [file] [log] [blame]
Chris Lattneraf8d5812006-11-10 05:07:45 +00001//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for statements.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattnerfc1c44a2007-08-23 05:46:52 +000015#include "clang/AST/ASTContext.h"
Steve Naroff8eeeb132007-05-08 21:09:37 +000016#include "clang/AST/Expr.h"
Chris Lattnerfc1c44a2007-08-23 05:46:52 +000017#include "clang/AST/Stmt.h"
Chris Lattnereaafe1222006-11-10 05:17:58 +000018#include "clang/Parse/Scope.h"
19#include "clang/Basic/Diagnostic.h"
Steve Naroff6f49f5d2007-05-29 14:23:36 +000020#include "clang/Basic/LangOptions.h"
21#include "clang/Lex/IdentifierTable.h"
Chris Lattneraf8d5812006-11-10 05:07:45 +000022using namespace clang;
23
Chris Lattner1ec5f562007-06-27 05:38:08 +000024Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) {
25 Expr *E = static_cast<Expr*>(expr);
Steve Narofff8fd09e2007-07-27 22:15:19 +000026 assert(E && "ParseExprStmt(): missing expression");
Chris Lattner1ec5f562007-06-27 05:38:08 +000027
28 // Exprs are statements, so there is no need to do a conversion here. However,
29 // diagnose some potentially bad code.
Chris Lattner9decfba2007-08-26 17:32:59 +000030 if (!E->hasLocalSideEffect() && !E->getType()->isVoidType())
Chris Lattner9b3b9a12007-06-27 06:08:24 +000031 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
Chris Lattner1ec5f562007-06-27 05:38:08 +000032
33 return E;
34}
35
36
Chris Lattner0f203a72007-05-28 01:45:28 +000037Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
38 return new NullStmt(SemiLoc);
39}
40
Steve Naroff2a8ad182007-05-29 22:59:26 +000041Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
Steve Naroff56faab22007-05-30 04:20:12 +000042 if (decl)
43 return new DeclStmt(static_cast<Decl *>(decl));
44 else
45 return true; // error
Steve Naroff2a8ad182007-05-29 22:59:26 +000046}
Chris Lattneraf8d5812006-11-10 05:07:45 +000047
48Action::StmtResult
49Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnerd864daf2007-08-27 04:29:41 +000050 StmtTy **elts, unsigned NumElts) {
51 Stmt **Elts = reinterpret_cast<Stmt**>(elts);
52 // If we're in C89 mode, check that we don't have any decls after stmts. If
53 // so, emit an extension diagnostic.
54 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
55 // Note that __extension__ can be around a decl.
56 unsigned i = 0;
57 // Skip over all declarations.
58 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
59 /*empty*/;
60
61 // We found the end of the list or a statement. Scan for another declstmt.
62 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
63 /*empty*/;
64
65 if (i != NumElts) {
66 Decl *D = cast<DeclStmt>(Elts[i])->getDecl();
67 Diag(D->getLocation(), diag::ext_mixed_decls_code);
68 }
69 }
Chris Lattnerdfaf9f82007-08-27 01:01:57 +000070
Chris Lattnerd864daf2007-08-27 04:29:41 +000071 return new CompoundStmt(Elts, NumElts);
Chris Lattneraf8d5812006-11-10 05:07:45 +000072}
73
74Action::StmtResult
Chris Lattner0c46c5d2007-06-02 23:53:17 +000075Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner46eeb222007-07-18 02:28:47 +000076 SourceLocation DotDotDotLoc, ExprTy *rhsval,
Chris Lattner39407372007-07-21 03:00:26 +000077 SourceLocation ColonLoc, StmtTy *subStmt) {
78 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +000079 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
Steve Naroff8eeeb132007-05-08 21:09:37 +000080 assert((LHSVal != 0) && "missing expression in case statement");
Anders Carlsson51873c22007-07-22 07:07:56 +000081
Chris Lattner0c46c5d2007-06-02 23:53:17 +000082 SourceLocation ExpLoc;
Steve Naroff8eeeb132007-05-08 21:09:37 +000083 // C99 6.8.4.2p3: The expression shall be an integer constant.
Chris Lattner39407372007-07-21 03:00:26 +000084 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
85 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
86 LHSVal->getSourceRange());
87 return SubStmt;
88 }
Steve Naroff8eeeb132007-05-08 21:09:37 +000089
Chris Lattner46eeb222007-07-18 02:28:47 +000090 // GCC extension: The expression shall be an integer constant.
Chris Lattner39407372007-07-21 03:00:26 +000091 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
92 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
93 RHSVal->getSourceRange());
Chris Lattnerfc1c44a2007-08-23 05:46:52 +000094 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner46eeb222007-07-18 02:28:47 +000095 }
Chris Lattner54f4d2b2007-07-23 17:05:23 +000096
97 if (SwitchStack.empty()) {
98 Diag(CaseLoc, diag::err_case_not_in_switch);
99 return SubStmt;
100 }
Chris Lattner35e287b2007-06-03 01:44:43 +0000101
Anders Carlsson51873c22007-07-22 07:07:56 +0000102 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000103 SwitchStack.back()->addSwitchCase(CS);
Anders Carlsson51873c22007-07-22 07:07:56 +0000104 return CS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000105}
106
107Action::StmtResult
Chris Lattner46eeb222007-07-18 02:28:47 +0000108Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner39407372007-07-21 03:00:26 +0000109 StmtTy *subStmt, Scope *CurScope) {
110 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner46eeb222007-07-18 02:28:47 +0000111
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000112 if (SwitchStack.empty()) {
Chris Lattner39407372007-07-21 03:00:26 +0000113 Diag(DefaultLoc, diag::err_default_not_in_switch);
114 return SubStmt;
115 }
116
Chris Lattner39407372007-07-21 03:00:26 +0000117 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000118 SwitchStack.back()->addSwitchCase(DS);
Anders Carlsson51873c22007-07-22 07:07:56 +0000119
Chris Lattner46eeb222007-07-18 02:28:47 +0000120 return DS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000121}
122
123Action::StmtResult
124Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner39407372007-07-21 03:00:26 +0000125 SourceLocation ColonLoc, StmtTy *subStmt) {
126 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000127 // Look up the record for this label identifier.
128 LabelStmt *&LabelDecl = LabelMap[II];
129
130 // If not forward referenced or defined already, just create a new LabelStmt.
131 if (LabelDecl == 0)
Chris Lattner39407372007-07-21 03:00:26 +0000132 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000133
Chris Lattnereefa10e2007-05-28 06:56:27 +0000134 assert(LabelDecl->getID() == II && "Label mismatch!");
Chris Lattnere2473062007-05-28 06:28:18 +0000135
136 // Otherwise, this label was either forward reference or multiply defined. If
137 // multiply defined, reject it now.
138 if (LabelDecl->getSubStmt()) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000139 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000140 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
Chris Lattner39407372007-07-21 03:00:26 +0000141 return SubStmt;
Chris Lattnere2473062007-05-28 06:28:18 +0000142 }
143
144 // Otherwise, this label was forward declared, and we just found its real
145 // definition. Fill in the forward definition and return it.
146 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner39407372007-07-21 03:00:26 +0000147 LabelDecl->setSubStmt(SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000148 return LabelDecl;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000149}
150
151Action::StmtResult
152Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
153 StmtTy *ThenVal, SourceLocation ElseLoc,
154 StmtTy *ElseVal) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000155 Expr *condExpr = (Expr *)CondVal;
156 assert(condExpr && "ParseIfStmt(): missing expression");
157
Steve Naroff31090012007-07-16 21:54:35 +0000158 DefaultFunctionArrayConversion(condExpr);
159 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000160
161 if (!condType->isScalarType()) // C99 6.8.4.1p1
162 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
163 condType.getAsString(), condExpr->getSourceRange());
164
165 return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000166}
Steve Naroff86272ea2007-05-29 02:14:17 +0000167
Chris Lattneraf8d5812006-11-10 05:07:45 +0000168Action::StmtResult
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000169Sema::StartSwitchStmt(ExprTy *cond) {
170 Expr *Cond = static_cast<Expr*>(cond);
171
172 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
173 UsualUnaryConversions(Cond);
174
175 SwitchStmt *SS = new SwitchStmt(Cond);
Anders Carlsson51873c22007-07-22 07:07:56 +0000176 SwitchStack.push_back(SS);
177 return SS;
178}
Chris Lattner46eeb222007-07-18 02:28:47 +0000179
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000180/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
181/// the specified width and sign. If an overflow occurs, detect it and emit
182/// the specified diagnostic.
183void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
184 unsigned NewWidth, bool NewSign,
185 SourceLocation Loc,
186 unsigned DiagID) {
187 // Perform a conversion to the promoted condition type if needed.
188 if (NewWidth > Val.getBitWidth()) {
189 // If this is an extension, just do it.
190 llvm::APSInt OldVal(Val);
191 Val.extend(NewWidth);
192
193 // If the input was signed and negative and the output is unsigned,
194 // warn.
195 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
196 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
197
198 Val.setIsSigned(NewSign);
199 } else if (NewWidth < Val.getBitWidth()) {
200 // If this is a truncation, check for overflow.
201 llvm::APSInt ConvVal(Val);
202 ConvVal.trunc(NewWidth);
Chris Lattner247ef952007-08-23 22:08:35 +0000203 ConvVal.setIsSigned(NewSign);
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000204 ConvVal.extend(Val.getBitWidth());
Chris Lattner247ef952007-08-23 22:08:35 +0000205 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000206 if (ConvVal != Val)
207 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
208
209 // Regardless of whether a diagnostic was emitted, really do the
210 // truncation.
211 Val.trunc(NewWidth);
Chris Lattner247ef952007-08-23 22:08:35 +0000212 Val.setIsSigned(NewSign);
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000213 } else if (NewSign != Val.isSigned()) {
214 // Convert the sign to match the sign of the condition. This can cause
215 // overflow as well: unsigned(INTMIN)
216 llvm::APSInt OldVal(Val);
217 Val.setIsSigned(NewSign);
218
219 if (Val.isNegative()) // Sign bit changes meaning.
220 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
221 }
222}
223
Chris Lattner67998452007-08-23 18:29:20 +0000224namespace {
225 struct CaseCompareFunctor {
226 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
227 const llvm::APSInt &RHS) {
228 return LHS.first < RHS;
229 }
230 bool operator()(const llvm::APSInt &LHS,
231 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
232 return LHS < RHS.first;
233 }
234 };
235}
236
Anders Carlsson51873c22007-07-22 07:07:56 +0000237Action::StmtResult
238Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
239 Stmt *BodyStmt = (Stmt*)Body;
240
241 SwitchStmt *SS = SwitchStack.back();
242 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
243
244 SS->setBody(BodyStmt);
245 SwitchStack.pop_back();
246
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000247 Expr *CondExpr = SS->getCond();
248 QualType CondType = CondExpr->getType();
Chris Lattner46eeb222007-07-18 02:28:47 +0000249
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000250 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Anders Carlsson51873c22007-07-22 07:07:56 +0000251 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000252 CondType.getAsString(), CondExpr->getSourceRange());
253 return true;
Anders Carlsson51873c22007-07-22 07:07:56 +0000254 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000255
256 // Get the bitwidth of the switched-on value before promotions. We must
257 // convert the integer case values to this width before comparison.
258 unsigned CondWidth = Context.getTypeSize(CondType, SwitchLoc);
259 bool CondIsSigned = CondType->isSignedIntegerType();
260
261 // Accumulate all of the case values in a vector so that we can sort them
262 // and detect duplicates. This vector contains the APInt for the case after
263 // it has been converted to the condition type.
Chris Lattner67998452007-08-23 18:29:20 +0000264 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
265 CaseValsTy CaseVals;
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000266
267 // Keep track of any GNU case ranges we see. The APSInt is the low value.
268 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
269
270 DefaultStmt *TheDefaultStmt = 0;
Anders Carlsson51873c22007-07-22 07:07:56 +0000271
Chris Lattner10cb5e52007-08-23 06:23:56 +0000272 bool CaseListIsErroneous = false;
273
274 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlsson51873c22007-07-22 07:07:56 +0000275 SC = SC->getNextSwitchCase()) {
Chris Lattner10cb5e52007-08-23 06:23:56 +0000276
Anders Carlsson51873c22007-07-22 07:07:56 +0000277 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000278 if (TheDefaultStmt) {
279 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
280 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Anders Carlsson51873c22007-07-22 07:07:56 +0000281
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000282 // FIXME: Remove the default statement from the switch block so that
283 // we'll return a valid AST. This requires recursing down the
284 // AST and finding it, not something we are set up to do right now. For
285 // now, just lop the entire switch stmt out of the AST.
Chris Lattner10cb5e52007-08-23 06:23:56 +0000286 CaseListIsErroneous = true;
Anders Carlsson51873c22007-07-22 07:07:56 +0000287 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000288 TheDefaultStmt = DS;
Anders Carlsson51873c22007-07-22 07:07:56 +0000289
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000290 } else {
291 CaseStmt *CS = cast<CaseStmt>(SC);
292
293 // We already verified that the expression has a i-c-e value (C99
294 // 6.8.4.2p3) - get that value now.
295 llvm::APSInt LoVal(32);
296 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
297
298 // Convert the value to the same width/sign as the condition.
299 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
300 CS->getLHS()->getLocStart(),
301 diag::warn_case_value_overflow);
Anders Carlsson51873c22007-07-22 07:07:56 +0000302
Chris Lattner10cb5e52007-08-23 06:23:56 +0000303 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000304 if (CS->getRHS())
305 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner10cb5e52007-08-23 06:23:56 +0000306 else
307 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000308 }
309 }
310
Chris Lattner10cb5e52007-08-23 06:23:56 +0000311 // Sort all the scalar case values so we can easily detect duplicates.
312 std::stable_sort(CaseVals.begin(), CaseVals.end());
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000313
Chris Lattnerfcb920d2007-08-23 14:29:07 +0000314 if (!CaseVals.empty()) {
315 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
316 if (CaseVals[i].first == CaseVals[i+1].first) {
317 // If we have a duplicate, report it.
318 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
319 diag::err_duplicate_case, CaseVals[i].first.toString());
320 Diag(CaseVals[i].second->getLHS()->getLocStart(),
321 diag::err_duplicate_case_prev);
322 // FIXME: We really want to remove the bogus case stmt from the substmt,
323 // but we have no way to do this right now.
324 CaseListIsErroneous = true;
325 }
Chris Lattner10cb5e52007-08-23 06:23:56 +0000326 }
327 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000328
Chris Lattner10cb5e52007-08-23 06:23:56 +0000329 // Detect duplicate case ranges, which usually don't exist at all in the first
330 // place.
331 if (!CaseRanges.empty()) {
332 // Sort all the case ranges by their low value so we can easily detect
333 // overlaps between ranges.
Chris Lattner67998452007-08-23 18:29:20 +0000334 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner10cb5e52007-08-23 06:23:56 +0000335
336 // Scan the ranges, computing the high values and removing empty ranges.
337 std::vector<llvm::APSInt> HiVals;
Chris Lattnerf81460f2007-08-23 17:48:14 +0000338 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner10cb5e52007-08-23 06:23:56 +0000339 CaseStmt *CR = CaseRanges[i].second;
340 llvm::APSInt HiVal(32);
341 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
342
343 // Convert the value to the same width/sign as the condition.
344 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
345 CR->getRHS()->getLocStart(),
346 diag::warn_case_value_overflow);
347
Chris Lattnerf81460f2007-08-23 17:48:14 +0000348 // If the low value is bigger than the high value, the case is empty.
349 if (CaseRanges[i].first > HiVal) {
350 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
351 SourceRange(CR->getLHS()->getLocStart(),
352 CR->getRHS()->getLocEnd()));
353 CaseRanges.erase(CaseRanges.begin()+i);
354 --i, --e;
355 continue;
356 }
Chris Lattner10cb5e52007-08-23 06:23:56 +0000357 HiVals.push_back(HiVal);
358 }
359
360 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner67998452007-08-23 18:29:20 +0000361 // ranges. Since the range list is sorted, we only need to compare case
362 // ranges with their neighbors.
Chris Lattner10cb5e52007-08-23 06:23:56 +0000363 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner67998452007-08-23 18:29:20 +0000364 llvm::APSInt &CRLo = CaseRanges[i].first;
365 llvm::APSInt &CRHi = HiVals[i];
366 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner10cb5e52007-08-23 06:23:56 +0000367
Chris Lattner67998452007-08-23 18:29:20 +0000368 // Check to see whether the case range overlaps with any singleton cases.
369 CaseStmt *OverlapStmt = 0;
370 llvm::APSInt OverlapVal(32);
371
372 // Find the smallest value >= the lower bound. If I is in the case range,
373 // then we have overlap.
374 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
375 CaseVals.end(), CRLo,
376 CaseCompareFunctor());
377 if (I != CaseVals.end() && I->first < CRHi) {
378 OverlapVal = I->first; // Found overlap with scalar.
379 OverlapStmt = I->second;
380 }
381
382 // Find the smallest value bigger than the upper bound.
383 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
384 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
385 OverlapVal = (I-1)->first; // Found overlap with scalar.
386 OverlapStmt = (I-1)->second;
387 }
388
389 // Check to see if this case stmt overlaps with the subsequent case range.
390 if (i && CRLo <= HiVals[i-1]) {
391 OverlapVal = HiVals[i-1]; // Found overlap with range.
392 OverlapStmt = CaseRanges[i-1].second;
393 }
394
395 if (OverlapStmt) {
396 // If we have a duplicate, report it.
397 Diag(CR->getLHS()->getLocStart(),
398 diag::err_duplicate_case, OverlapVal.toString());
399 Diag(OverlapStmt->getLHS()->getLocStart(),
400 diag::err_duplicate_case_prev);
401 // FIXME: We really want to remove the bogus case stmt from the substmt,
402 // but we have no way to do this right now.
403 CaseListIsErroneous = true;
404 }
Chris Lattner10cb5e52007-08-23 06:23:56 +0000405 }
406 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000407
Chris Lattner10cb5e52007-08-23 06:23:56 +0000408 // FIXME: If the case list was broken is some way, we don't have a good system
409 // to patch it up. Instead, just return the whole substmt as broken.
410 if (CaseListIsErroneous)
411 return true;
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000412
Anders Carlsson51873c22007-07-22 07:07:56 +0000413 return SS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000414}
415
416Action::StmtResult
Steve Naroff86272ea2007-05-29 02:14:17 +0000417Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
418 Expr *condExpr = (Expr *)Cond;
419 assert(condExpr && "ParseWhileStmt(): missing expression");
420
Steve Naroff31090012007-07-16 21:54:35 +0000421 DefaultFunctionArrayConversion(condExpr);
422 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000423
424 if (!condType->isScalarType()) // C99 6.8.5p2
425 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
426 condType.getAsString(), condExpr->getSourceRange());
427
428 return new WhileStmt(condExpr, (Stmt*)Body);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000429}
430
431Action::StmtResult
432Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
433 SourceLocation WhileLoc, ExprTy *Cond) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000434 Expr *condExpr = (Expr *)Cond;
435 assert(condExpr && "ParseDoStmt(): missing expression");
436
Steve Naroff31090012007-07-16 21:54:35 +0000437 DefaultFunctionArrayConversion(condExpr);
438 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000439
440 if (!condType->isScalarType()) // C99 6.8.5p2
441 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
442 condType.getAsString(), condExpr->getSourceRange());
443
444 return new DoStmt((Stmt*)Body, condExpr);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000445}
446
447Action::StmtResult
448Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
449 StmtTy *First, ExprTy *Second, ExprTy *Third,
450 SourceLocation RParenLoc, StmtTy *Body) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000451 if (First) {
452 // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
453 // declaration support to create a DeclStmt node. Once this is done,
454 // we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
455 }
456 if (Second) {
457 Expr *testExpr = (Expr *)Second;
Steve Naroff31090012007-07-16 21:54:35 +0000458 DefaultFunctionArrayConversion(testExpr);
459 QualType testType = testExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000460
461 if (!testType->isScalarType()) // C99 6.8.5p2
462 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
463 testType.getAsString(), testExpr->getSourceRange());
464 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000465 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
466}
467
468
469Action::StmtResult
470Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
471 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000472 // Look up the record for this label identifier.
473 LabelStmt *&LabelDecl = LabelMap[LabelII];
474
475 // If we haven't seen this label yet, create a forward reference.
476 if (LabelDecl == 0)
477 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
478
479 return new GotoStmt(LabelDecl);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000480}
Chris Lattner1c310502007-05-31 06:00:00 +0000481
Chris Lattneraf8d5812006-11-10 05:07:45 +0000482Action::StmtResult
483Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
484 ExprTy *DestExp) {
Chris Lattner1c310502007-05-31 06:00:00 +0000485 // FIXME: Verify that the operand is convertible to void*.
486
Chris Lattneraf8d5812006-11-10 05:07:45 +0000487 return new IndirectGotoStmt((Expr*)DestExp);
488}
489
490Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000491Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
492 Scope *S = CurScope->getContinueParent();
493 if (!S) {
494 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
495 Diag(ContinueLoc, diag::err_continue_not_in_loop);
496 return true;
497 }
498
Chris Lattneraf8d5812006-11-10 05:07:45 +0000499 return new ContinueStmt();
500}
501
502Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000503Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
504 Scope *S = CurScope->getBreakParent();
505 if (!S) {
506 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
507 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
508 return true;
509 }
510
Chris Lattneraf8d5812006-11-10 05:07:45 +0000511 return new BreakStmt();
512}
513
514
515Action::StmtResult
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000516Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
517 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff9358c712007-05-27 23:58:33 +0000518 QualType lhsType = CurFunctionDecl->getResultType();
519
Steve Naroff9358c712007-05-27 23:58:33 +0000520 if (lhsType->isVoidType()) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000521 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
522 Diag(ReturnLoc, diag::ext_return_has_expr,
523 CurFunctionDecl->getIdentifier()->getName(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000524 RetValExp->getSourceRange());
525 return new ReturnStmt(RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000526 } else {
527 if (!RetValExp) {
528 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
529 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
530 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
531 else // C90 6.6.6.4p4
532 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
533 return new ReturnStmt((Expr*)0);
534 }
Steve Naroff9358c712007-05-27 23:58:33 +0000535 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000536 // we have a non-void function with an expression, continue checking
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000537 QualType rhsType = RetValExp->getType();
Steve Naroff9358c712007-05-27 23:58:33 +0000538
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000539 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
540 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
541 // function return.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000542 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
543 RetValExp);
Ted Kremenekc48affb2007-08-14 18:14:14 +0000544
Steve Naroff9358c712007-05-27 23:58:33 +0000545 // decode the result (notice that extensions still return a type).
546 switch (result) {
547 case Compatible:
548 break;
549 case Incompatible:
550 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
551 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000552 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000553 break;
554 case PointerFromInt:
555 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000556 if (!RetValExp->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000557 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
558 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000559 RetValExp->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000560 }
Steve Naroff9358c712007-05-27 23:58:33 +0000561 break;
562 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000563 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
564 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000565 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000566 break;
567 case IncompatiblePointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000568 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
569 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000570 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000571 break;
572 case CompatiblePointerDiscardsQualifiers:
Steve Naroff56faab22007-05-30 04:20:12 +0000573 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
574 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000575 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000576 break;
577 }
Ted Kremenekcff94fa2007-08-17 16:46:58 +0000578
579 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
580
Chris Lattneraf8d5812006-11-10 05:07:45 +0000581 return new ReturnStmt((Expr*)RetValExp);
582}
583