blob: da2d12bfaf16383b8ed2c6004d042f2583fa33b6 [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,
50 StmtTy **Elts, unsigned NumElts) {
Chris Lattner3bbe7be2007-05-20 22:47:04 +000051 return new CompoundStmt((Stmt**)Elts, NumElts);
Chris Lattneraf8d5812006-11-10 05:07:45 +000052}
53
54Action::StmtResult
Chris Lattner0c46c5d2007-06-02 23:53:17 +000055Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner46eeb222007-07-18 02:28:47 +000056 SourceLocation DotDotDotLoc, ExprTy *rhsval,
Chris Lattner39407372007-07-21 03:00:26 +000057 SourceLocation ColonLoc, StmtTy *subStmt) {
58 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +000059 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
Steve Naroff8eeeb132007-05-08 21:09:37 +000060 assert((LHSVal != 0) && "missing expression in case statement");
Anders Carlsson51873c22007-07-22 07:07:56 +000061
Chris Lattner0c46c5d2007-06-02 23:53:17 +000062 SourceLocation ExpLoc;
Steve Naroff8eeeb132007-05-08 21:09:37 +000063 // C99 6.8.4.2p3: The expression shall be an integer constant.
Chris Lattner39407372007-07-21 03:00:26 +000064 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
65 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
66 LHSVal->getSourceRange());
67 return SubStmt;
68 }
Steve Naroff8eeeb132007-05-08 21:09:37 +000069
Chris Lattner46eeb222007-07-18 02:28:47 +000070 // GCC extension: The expression shall be an integer constant.
Chris Lattner39407372007-07-21 03:00:26 +000071 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
72 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
73 RHSVal->getSourceRange());
Chris Lattnerfc1c44a2007-08-23 05:46:52 +000074 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner46eeb222007-07-18 02:28:47 +000075 }
Chris Lattner54f4d2b2007-07-23 17:05:23 +000076
77 if (SwitchStack.empty()) {
78 Diag(CaseLoc, diag::err_case_not_in_switch);
79 return SubStmt;
80 }
Chris Lattner35e287b2007-06-03 01:44:43 +000081
Anders Carlsson51873c22007-07-22 07:07:56 +000082 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +000083 SwitchStack.back()->addSwitchCase(CS);
Anders Carlsson51873c22007-07-22 07:07:56 +000084 return CS;
Chris Lattneraf8d5812006-11-10 05:07:45 +000085}
86
87Action::StmtResult
Chris Lattner46eeb222007-07-18 02:28:47 +000088Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner39407372007-07-21 03:00:26 +000089 StmtTy *subStmt, Scope *CurScope) {
90 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner46eeb222007-07-18 02:28:47 +000091
Chris Lattner54f4d2b2007-07-23 17:05:23 +000092 if (SwitchStack.empty()) {
Chris Lattner39407372007-07-21 03:00:26 +000093 Diag(DefaultLoc, diag::err_default_not_in_switch);
94 return SubStmt;
95 }
96
Chris Lattner39407372007-07-21 03:00:26 +000097 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +000098 SwitchStack.back()->addSwitchCase(DS);
Anders Carlsson51873c22007-07-22 07:07:56 +000099
Chris Lattner46eeb222007-07-18 02:28:47 +0000100 return DS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000101}
102
103Action::StmtResult
104Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner39407372007-07-21 03:00:26 +0000105 SourceLocation ColonLoc, StmtTy *subStmt) {
106 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000107 // Look up the record for this label identifier.
108 LabelStmt *&LabelDecl = LabelMap[II];
109
110 // If not forward referenced or defined already, just create a new LabelStmt.
111 if (LabelDecl == 0)
Chris Lattner39407372007-07-21 03:00:26 +0000112 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000113
Chris Lattnereefa10e2007-05-28 06:56:27 +0000114 assert(LabelDecl->getID() == II && "Label mismatch!");
Chris Lattnere2473062007-05-28 06:28:18 +0000115
116 // Otherwise, this label was either forward reference or multiply defined. If
117 // multiply defined, reject it now.
118 if (LabelDecl->getSubStmt()) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000119 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000120 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
Chris Lattner39407372007-07-21 03:00:26 +0000121 return SubStmt;
Chris Lattnere2473062007-05-28 06:28:18 +0000122 }
123
124 // Otherwise, this label was forward declared, and we just found its real
125 // definition. Fill in the forward definition and return it.
126 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner39407372007-07-21 03:00:26 +0000127 LabelDecl->setSubStmt(SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000128 return LabelDecl;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000129}
130
131Action::StmtResult
132Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
133 StmtTy *ThenVal, SourceLocation ElseLoc,
134 StmtTy *ElseVal) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000135 Expr *condExpr = (Expr *)CondVal;
136 assert(condExpr && "ParseIfStmt(): missing expression");
137
Steve Naroff31090012007-07-16 21:54:35 +0000138 DefaultFunctionArrayConversion(condExpr);
139 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000140
141 if (!condType->isScalarType()) // C99 6.8.4.1p1
142 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
143 condType.getAsString(), condExpr->getSourceRange());
144
145 return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000146}
Steve Naroff86272ea2007-05-29 02:14:17 +0000147
Chris Lattneraf8d5812006-11-10 05:07:45 +0000148Action::StmtResult
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000149Sema::StartSwitchStmt(ExprTy *cond) {
150 Expr *Cond = static_cast<Expr*>(cond);
151
152 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
153 UsualUnaryConversions(Cond);
154
155 SwitchStmt *SS = new SwitchStmt(Cond);
Anders Carlsson51873c22007-07-22 07:07:56 +0000156 SwitchStack.push_back(SS);
157 return SS;
158}
Chris Lattner46eeb222007-07-18 02:28:47 +0000159
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000160/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
161/// the specified width and sign. If an overflow occurs, detect it and emit
162/// the specified diagnostic.
163void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
164 unsigned NewWidth, bool NewSign,
165 SourceLocation Loc,
166 unsigned DiagID) {
167 // Perform a conversion to the promoted condition type if needed.
168 if (NewWidth > Val.getBitWidth()) {
169 // If this is an extension, just do it.
170 llvm::APSInt OldVal(Val);
171 Val.extend(NewWidth);
172
173 // If the input was signed and negative and the output is unsigned,
174 // warn.
175 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
176 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
177
178 Val.setIsSigned(NewSign);
179 } else if (NewWidth < Val.getBitWidth()) {
180 // If this is a truncation, check for overflow.
181 llvm::APSInt ConvVal(Val);
182 ConvVal.trunc(NewWidth);
Chris Lattner247ef952007-08-23 22:08:35 +0000183 ConvVal.setIsSigned(NewSign);
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000184 ConvVal.extend(Val.getBitWidth());
Chris Lattner247ef952007-08-23 22:08:35 +0000185 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000186 if (ConvVal != Val)
187 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
188
189 // Regardless of whether a diagnostic was emitted, really do the
190 // truncation.
191 Val.trunc(NewWidth);
Chris Lattner247ef952007-08-23 22:08:35 +0000192 Val.setIsSigned(NewSign);
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000193 } else if (NewSign != Val.isSigned()) {
194 // Convert the sign to match the sign of the condition. This can cause
195 // overflow as well: unsigned(INTMIN)
196 llvm::APSInt OldVal(Val);
197 Val.setIsSigned(NewSign);
198
199 if (Val.isNegative()) // Sign bit changes meaning.
200 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
201 }
202}
203
Chris Lattner67998452007-08-23 18:29:20 +0000204namespace {
205 struct CaseCompareFunctor {
206 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
207 const llvm::APSInt &RHS) {
208 return LHS.first < RHS;
209 }
210 bool operator()(const llvm::APSInt &LHS,
211 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
212 return LHS < RHS.first;
213 }
214 };
215}
216
Anders Carlsson51873c22007-07-22 07:07:56 +0000217Action::StmtResult
218Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
219 Stmt *BodyStmt = (Stmt*)Body;
220
221 SwitchStmt *SS = SwitchStack.back();
222 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
223
224 SS->setBody(BodyStmt);
225 SwitchStack.pop_back();
226
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000227 Expr *CondExpr = SS->getCond();
228 QualType CondType = CondExpr->getType();
Chris Lattner46eeb222007-07-18 02:28:47 +0000229
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000230 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Anders Carlsson51873c22007-07-22 07:07:56 +0000231 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000232 CondType.getAsString(), CondExpr->getSourceRange());
233 return true;
Anders Carlsson51873c22007-07-22 07:07:56 +0000234 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000235
236 // Get the bitwidth of the switched-on value before promotions. We must
237 // convert the integer case values to this width before comparison.
238 unsigned CondWidth = Context.getTypeSize(CondType, SwitchLoc);
239 bool CondIsSigned = CondType->isSignedIntegerType();
240
241 // Accumulate all of the case values in a vector so that we can sort them
242 // and detect duplicates. This vector contains the APInt for the case after
243 // it has been converted to the condition type.
Chris Lattner67998452007-08-23 18:29:20 +0000244 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
245 CaseValsTy CaseVals;
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000246
247 // Keep track of any GNU case ranges we see. The APSInt is the low value.
248 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
249
250 DefaultStmt *TheDefaultStmt = 0;
Anders Carlsson51873c22007-07-22 07:07:56 +0000251
Chris Lattner10cb5e52007-08-23 06:23:56 +0000252 bool CaseListIsErroneous = false;
253
254 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlsson51873c22007-07-22 07:07:56 +0000255 SC = SC->getNextSwitchCase()) {
Chris Lattner10cb5e52007-08-23 06:23:56 +0000256
Anders Carlsson51873c22007-07-22 07:07:56 +0000257 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000258 if (TheDefaultStmt) {
259 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
260 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Anders Carlsson51873c22007-07-22 07:07:56 +0000261
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000262 // FIXME: Remove the default statement from the switch block so that
263 // we'll return a valid AST. This requires recursing down the
264 // AST and finding it, not something we are set up to do right now. For
265 // now, just lop the entire switch stmt out of the AST.
Chris Lattner10cb5e52007-08-23 06:23:56 +0000266 CaseListIsErroneous = true;
Anders Carlsson51873c22007-07-22 07:07:56 +0000267 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000268 TheDefaultStmt = DS;
Anders Carlsson51873c22007-07-22 07:07:56 +0000269
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000270 } else {
271 CaseStmt *CS = cast<CaseStmt>(SC);
272
273 // We already verified that the expression has a i-c-e value (C99
274 // 6.8.4.2p3) - get that value now.
275 llvm::APSInt LoVal(32);
276 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
277
278 // Convert the value to the same width/sign as the condition.
279 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
280 CS->getLHS()->getLocStart(),
281 diag::warn_case_value_overflow);
Anders Carlsson51873c22007-07-22 07:07:56 +0000282
Chris Lattner10cb5e52007-08-23 06:23:56 +0000283 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000284 if (CS->getRHS())
285 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner10cb5e52007-08-23 06:23:56 +0000286 else
287 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000288 }
289 }
290
Chris Lattner10cb5e52007-08-23 06:23:56 +0000291 // Sort all the scalar case values so we can easily detect duplicates.
292 std::stable_sort(CaseVals.begin(), CaseVals.end());
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000293
Chris Lattnerfcb920d2007-08-23 14:29:07 +0000294 if (!CaseVals.empty()) {
295 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
296 if (CaseVals[i].first == CaseVals[i+1].first) {
297 // If we have a duplicate, report it.
298 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
299 diag::err_duplicate_case, CaseVals[i].first.toString());
300 Diag(CaseVals[i].second->getLHS()->getLocStart(),
301 diag::err_duplicate_case_prev);
302 // FIXME: We really want to remove the bogus case stmt from the substmt,
303 // but we have no way to do this right now.
304 CaseListIsErroneous = true;
305 }
Chris Lattner10cb5e52007-08-23 06:23:56 +0000306 }
307 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000308
Chris Lattner10cb5e52007-08-23 06:23:56 +0000309 // Detect duplicate case ranges, which usually don't exist at all in the first
310 // place.
311 if (!CaseRanges.empty()) {
312 // Sort all the case ranges by their low value so we can easily detect
313 // overlaps between ranges.
Chris Lattner67998452007-08-23 18:29:20 +0000314 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner10cb5e52007-08-23 06:23:56 +0000315
316 // Scan the ranges, computing the high values and removing empty ranges.
317 std::vector<llvm::APSInt> HiVals;
Chris Lattnerf81460f2007-08-23 17:48:14 +0000318 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner10cb5e52007-08-23 06:23:56 +0000319 CaseStmt *CR = CaseRanges[i].second;
320 llvm::APSInt HiVal(32);
321 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
322
323 // Convert the value to the same width/sign as the condition.
324 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
325 CR->getRHS()->getLocStart(),
326 diag::warn_case_value_overflow);
327
Chris Lattnerf81460f2007-08-23 17:48:14 +0000328 // If the low value is bigger than the high value, the case is empty.
329 if (CaseRanges[i].first > HiVal) {
330 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
331 SourceRange(CR->getLHS()->getLocStart(),
332 CR->getRHS()->getLocEnd()));
333 CaseRanges.erase(CaseRanges.begin()+i);
334 --i, --e;
335 continue;
336 }
Chris Lattner10cb5e52007-08-23 06:23:56 +0000337 HiVals.push_back(HiVal);
338 }
339
340 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner67998452007-08-23 18:29:20 +0000341 // ranges. Since the range list is sorted, we only need to compare case
342 // ranges with their neighbors.
Chris Lattner10cb5e52007-08-23 06:23:56 +0000343 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner67998452007-08-23 18:29:20 +0000344 llvm::APSInt &CRLo = CaseRanges[i].first;
345 llvm::APSInt &CRHi = HiVals[i];
346 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner10cb5e52007-08-23 06:23:56 +0000347
Chris Lattner67998452007-08-23 18:29:20 +0000348 // Check to see whether the case range overlaps with any singleton cases.
349 CaseStmt *OverlapStmt = 0;
350 llvm::APSInt OverlapVal(32);
351
352 // Find the smallest value >= the lower bound. If I is in the case range,
353 // then we have overlap.
354 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
355 CaseVals.end(), CRLo,
356 CaseCompareFunctor());
357 if (I != CaseVals.end() && I->first < CRHi) {
358 OverlapVal = I->first; // Found overlap with scalar.
359 OverlapStmt = I->second;
360 }
361
362 // Find the smallest value bigger than the upper bound.
363 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
364 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
365 OverlapVal = (I-1)->first; // Found overlap with scalar.
366 OverlapStmt = (I-1)->second;
367 }
368
369 // Check to see if this case stmt overlaps with the subsequent case range.
370 if (i && CRLo <= HiVals[i-1]) {
371 OverlapVal = HiVals[i-1]; // Found overlap with range.
372 OverlapStmt = CaseRanges[i-1].second;
373 }
374
375 if (OverlapStmt) {
376 // If we have a duplicate, report it.
377 Diag(CR->getLHS()->getLocStart(),
378 diag::err_duplicate_case, OverlapVal.toString());
379 Diag(OverlapStmt->getLHS()->getLocStart(),
380 diag::err_duplicate_case_prev);
381 // FIXME: We really want to remove the bogus case stmt from the substmt,
382 // but we have no way to do this right now.
383 CaseListIsErroneous = true;
384 }
Chris Lattner10cb5e52007-08-23 06:23:56 +0000385 }
386 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000387
Chris Lattner10cb5e52007-08-23 06:23:56 +0000388 // FIXME: If the case list was broken is some way, we don't have a good system
389 // to patch it up. Instead, just return the whole substmt as broken.
390 if (CaseListIsErroneous)
391 return true;
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000392
Anders Carlsson51873c22007-07-22 07:07:56 +0000393 return SS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000394}
395
396Action::StmtResult
Steve Naroff86272ea2007-05-29 02:14:17 +0000397Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
398 Expr *condExpr = (Expr *)Cond;
399 assert(condExpr && "ParseWhileStmt(): missing expression");
400
Steve Naroff31090012007-07-16 21:54:35 +0000401 DefaultFunctionArrayConversion(condExpr);
402 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000403
404 if (!condType->isScalarType()) // C99 6.8.5p2
405 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
406 condType.getAsString(), condExpr->getSourceRange());
407
408 return new WhileStmt(condExpr, (Stmt*)Body);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000409}
410
411Action::StmtResult
412Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
413 SourceLocation WhileLoc, ExprTy *Cond) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000414 Expr *condExpr = (Expr *)Cond;
415 assert(condExpr && "ParseDoStmt(): missing expression");
416
Steve Naroff31090012007-07-16 21:54:35 +0000417 DefaultFunctionArrayConversion(condExpr);
418 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000419
420 if (!condType->isScalarType()) // C99 6.8.5p2
421 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
422 condType.getAsString(), condExpr->getSourceRange());
423
424 return new DoStmt((Stmt*)Body, condExpr);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000425}
426
427Action::StmtResult
428Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
429 StmtTy *First, ExprTy *Second, ExprTy *Third,
430 SourceLocation RParenLoc, StmtTy *Body) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000431 if (First) {
432 // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
433 // declaration support to create a DeclStmt node. Once this is done,
434 // we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
435 }
436 if (Second) {
437 Expr *testExpr = (Expr *)Second;
Steve Naroff31090012007-07-16 21:54:35 +0000438 DefaultFunctionArrayConversion(testExpr);
439 QualType testType = testExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000440
441 if (!testType->isScalarType()) // C99 6.8.5p2
442 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
443 testType.getAsString(), testExpr->getSourceRange());
444 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000445 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
446}
447
448
449Action::StmtResult
450Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
451 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000452 // Look up the record for this label identifier.
453 LabelStmt *&LabelDecl = LabelMap[LabelII];
454
455 // If we haven't seen this label yet, create a forward reference.
456 if (LabelDecl == 0)
457 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
458
459 return new GotoStmt(LabelDecl);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000460}
Chris Lattner1c310502007-05-31 06:00:00 +0000461
Chris Lattneraf8d5812006-11-10 05:07:45 +0000462Action::StmtResult
463Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
464 ExprTy *DestExp) {
Chris Lattner1c310502007-05-31 06:00:00 +0000465 // FIXME: Verify that the operand is convertible to void*.
466
Chris Lattneraf8d5812006-11-10 05:07:45 +0000467 return new IndirectGotoStmt((Expr*)DestExp);
468}
469
470Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000471Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
472 Scope *S = CurScope->getContinueParent();
473 if (!S) {
474 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
475 Diag(ContinueLoc, diag::err_continue_not_in_loop);
476 return true;
477 }
478
Chris Lattneraf8d5812006-11-10 05:07:45 +0000479 return new ContinueStmt();
480}
481
482Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000483Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
484 Scope *S = CurScope->getBreakParent();
485 if (!S) {
486 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
487 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
488 return true;
489 }
490
Chris Lattneraf8d5812006-11-10 05:07:45 +0000491 return new BreakStmt();
492}
493
494
495Action::StmtResult
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000496Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
497 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff9358c712007-05-27 23:58:33 +0000498 QualType lhsType = CurFunctionDecl->getResultType();
499
Steve Naroff9358c712007-05-27 23:58:33 +0000500 if (lhsType->isVoidType()) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000501 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
502 Diag(ReturnLoc, diag::ext_return_has_expr,
503 CurFunctionDecl->getIdentifier()->getName(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000504 RetValExp->getSourceRange());
505 return new ReturnStmt(RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000506 } else {
507 if (!RetValExp) {
508 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
509 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
510 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
511 else // C90 6.6.6.4p4
512 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
513 return new ReturnStmt((Expr*)0);
514 }
Steve Naroff9358c712007-05-27 23:58:33 +0000515 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000516 // we have a non-void function with an expression, continue checking
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000517 QualType rhsType = RetValExp->getType();
Steve Naroff9358c712007-05-27 23:58:33 +0000518
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000519 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
520 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
521 // function return.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000522 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
523 RetValExp);
Ted Kremenekc48affb2007-08-14 18:14:14 +0000524
Steve Naroff9358c712007-05-27 23:58:33 +0000525 // decode the result (notice that extensions still return a type).
526 switch (result) {
527 case Compatible:
528 break;
529 case Incompatible:
530 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
531 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000532 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000533 break;
534 case PointerFromInt:
535 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000536 if (!RetValExp->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000537 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
538 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000539 RetValExp->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000540 }
Steve Naroff9358c712007-05-27 23:58:33 +0000541 break;
542 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000543 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
544 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000545 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000546 break;
547 case IncompatiblePointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000548 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
549 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000550 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000551 break;
552 case CompatiblePointerDiscardsQualifiers:
Steve Naroff56faab22007-05-30 04:20:12 +0000553 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
554 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000555 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000556 break;
557 }
Ted Kremenekcff94fa2007-08-17 16:46:58 +0000558
559 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
560
Chris Lattneraf8d5812006-11-10 05:07:45 +0000561 return new ReturnStmt((Expr*)RetValExp);
562}
563