blob: 0bd05d86aedec8beb1cdee209b36397b7eb8d46a [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
24Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) {
25 Expr *E = static_cast<Expr*>(expr);
Steve Naroff1b8a46c2007-07-27 22:15:19 +000026 assert(E && "ParseExprStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +000027
28 // Exprs are statements, so there is no need to do a conversion here. However,
29 // diagnose some potentially bad code.
Chris Lattner852d1152007-08-26 17:32:59 +000030 if (!E->hasLocalSideEffect() && !E->getType()->isVoidType())
Chris Lattner4b009652007-07-25 00:24:17 +000031 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
32
33 return E;
34}
35
36
37Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
38 return new NullStmt(SemiLoc);
39}
40
41Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
42 if (decl)
43 return new DeclStmt(static_cast<Decl *>(decl));
44 else
45 return true; // error
46}
47
48Action::StmtResult
49Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
50 StmtTy **Elts, unsigned NumElts) {
Chris Lattner81417722007-08-27 01:01:57 +000051 // FIXME: ISO C90 forbids mixed declarations and code
52 // Note that __extension__ can be around a decl.
53
54
Chris Lattner4b009652007-07-25 00:24:17 +000055 return new CompoundStmt((Stmt**)Elts, NumElts);
56}
57
58Action::StmtResult
59Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
60 SourceLocation DotDotDotLoc, ExprTy *rhsval,
61 SourceLocation ColonLoc, StmtTy *subStmt) {
62 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
63 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
64 assert((LHSVal != 0) && "missing expression in case statement");
65
66 SourceLocation ExpLoc;
67 // C99 6.8.4.2p3: The expression shall be an integer constant.
68 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
69 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
70 LHSVal->getSourceRange());
71 return SubStmt;
72 }
73
74 // GCC extension: The expression shall be an integer constant.
75 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
76 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
77 RHSVal->getSourceRange());
Chris Lattner3429a812007-08-23 05:46:52 +000078 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner4b009652007-07-25 00:24:17 +000079 }
80
81 if (SwitchStack.empty()) {
82 Diag(CaseLoc, diag::err_case_not_in_switch);
83 return SubStmt;
84 }
85
86 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
87 SwitchStack.back()->addSwitchCase(CS);
88 return CS;
89}
90
91Action::StmtResult
92Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
93 StmtTy *subStmt, Scope *CurScope) {
94 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
95
96 if (SwitchStack.empty()) {
97 Diag(DefaultLoc, diag::err_default_not_in_switch);
98 return SubStmt;
99 }
100
101 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
102 SwitchStack.back()->addSwitchCase(DS);
103
104 return DS;
105}
106
107Action::StmtResult
108Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
109 SourceLocation ColonLoc, StmtTy *subStmt) {
110 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
111 // Look up the record for this label identifier.
112 LabelStmt *&LabelDecl = LabelMap[II];
113
114 // If not forward referenced or defined already, just create a new LabelStmt.
115 if (LabelDecl == 0)
116 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
117
118 assert(LabelDecl->getID() == II && "Label mismatch!");
119
120 // Otherwise, this label was either forward reference or multiply defined. If
121 // multiply defined, reject it now.
122 if (LabelDecl->getSubStmt()) {
123 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
124 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
125 return SubStmt;
126 }
127
128 // Otherwise, this label was forward declared, and we just found its real
129 // definition. Fill in the forward definition and return it.
130 LabelDecl->setIdentLoc(IdentLoc);
131 LabelDecl->setSubStmt(SubStmt);
132 return LabelDecl;
133}
134
135Action::StmtResult
136Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
137 StmtTy *ThenVal, SourceLocation ElseLoc,
138 StmtTy *ElseVal) {
139 Expr *condExpr = (Expr *)CondVal;
140 assert(condExpr && "ParseIfStmt(): missing expression");
141
142 DefaultFunctionArrayConversion(condExpr);
143 QualType condType = condExpr->getType();
144
145 if (!condType->isScalarType()) // C99 6.8.4.1p1
146 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
147 condType.getAsString(), condExpr->getSourceRange());
148
149 return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
150}
151
152Action::StmtResult
Chris Lattner3429a812007-08-23 05:46:52 +0000153Sema::StartSwitchStmt(ExprTy *cond) {
154 Expr *Cond = static_cast<Expr*>(cond);
155
156 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
157 UsualUnaryConversions(Cond);
158
159 SwitchStmt *SS = new SwitchStmt(Cond);
Chris Lattner4b009652007-07-25 00:24:17 +0000160 SwitchStack.push_back(SS);
161 return SS;
162}
163
Chris Lattner3429a812007-08-23 05:46:52 +0000164/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
165/// the specified width and sign. If an overflow occurs, detect it and emit
166/// the specified diagnostic.
167void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
168 unsigned NewWidth, bool NewSign,
169 SourceLocation Loc,
170 unsigned DiagID) {
171 // Perform a conversion to the promoted condition type if needed.
172 if (NewWidth > Val.getBitWidth()) {
173 // If this is an extension, just do it.
174 llvm::APSInt OldVal(Val);
175 Val.extend(NewWidth);
176
177 // If the input was signed and negative and the output is unsigned,
178 // warn.
179 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
180 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
181
182 Val.setIsSigned(NewSign);
183 } else if (NewWidth < Val.getBitWidth()) {
184 // If this is a truncation, check for overflow.
185 llvm::APSInt ConvVal(Val);
186 ConvVal.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000187 ConvVal.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000188 ConvVal.extend(Val.getBitWidth());
Chris Lattner5c039602007-08-23 22:08:35 +0000189 ConvVal.setIsSigned(Val.isSigned());
Chris Lattner3429a812007-08-23 05:46:52 +0000190 if (ConvVal != Val)
191 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
192
193 // Regardless of whether a diagnostic was emitted, really do the
194 // truncation.
195 Val.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000196 Val.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000197 } else if (NewSign != Val.isSigned()) {
198 // Convert the sign to match the sign of the condition. This can cause
199 // overflow as well: unsigned(INTMIN)
200 llvm::APSInt OldVal(Val);
201 Val.setIsSigned(NewSign);
202
203 if (Val.isNegative()) // Sign bit changes meaning.
204 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
205 }
206}
207
Chris Lattner0ab833c2007-08-23 18:29:20 +0000208namespace {
209 struct CaseCompareFunctor {
210 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
211 const llvm::APSInt &RHS) {
212 return LHS.first < RHS;
213 }
214 bool operator()(const llvm::APSInt &LHS,
215 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
216 return LHS < RHS.first;
217 }
218 };
219}
220
Chris Lattner4b009652007-07-25 00:24:17 +0000221Action::StmtResult
222Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
223 Stmt *BodyStmt = (Stmt*)Body;
224
225 SwitchStmt *SS = SwitchStack.back();
226 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
227
228 SS->setBody(BodyStmt);
229 SwitchStack.pop_back();
230
Chris Lattner3429a812007-08-23 05:46:52 +0000231 Expr *CondExpr = SS->getCond();
232 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000233
Chris Lattner3429a812007-08-23 05:46:52 +0000234 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner4b009652007-07-25 00:24:17 +0000235 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattner3429a812007-08-23 05:46:52 +0000236 CondType.getAsString(), CondExpr->getSourceRange());
237 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000238 }
Chris Lattner3429a812007-08-23 05:46:52 +0000239
240 // Get the bitwidth of the switched-on value before promotions. We must
241 // convert the integer case values to this width before comparison.
242 unsigned CondWidth = Context.getTypeSize(CondType, SwitchLoc);
243 bool CondIsSigned = CondType->isSignedIntegerType();
244
245 // Accumulate all of the case values in a vector so that we can sort them
246 // and detect duplicates. This vector contains the APInt for the case after
247 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000248 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
249 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000250
251 // Keep track of any GNU case ranges we see. The APSInt is the low value.
252 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
253
254 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000255
Chris Lattner1a4066d2007-08-23 06:23:56 +0000256 bool CaseListIsErroneous = false;
257
258 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000259 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000260
Chris Lattner4b009652007-07-25 00:24:17 +0000261 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000262 if (TheDefaultStmt) {
263 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
264 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Chris Lattner4b009652007-07-25 00:24:17 +0000265
Chris Lattner3429a812007-08-23 05:46:52 +0000266 // FIXME: Remove the default statement from the switch block so that
267 // we'll return a valid AST. This requires recursing down the
268 // AST and finding it, not something we are set up to do right now. For
269 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000270 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000271 }
Chris Lattner3429a812007-08-23 05:46:52 +0000272 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000273
Chris Lattner3429a812007-08-23 05:46:52 +0000274 } else {
275 CaseStmt *CS = cast<CaseStmt>(SC);
276
277 // We already verified that the expression has a i-c-e value (C99
278 // 6.8.4.2p3) - get that value now.
279 llvm::APSInt LoVal(32);
280 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
281
282 // Convert the value to the same width/sign as the condition.
283 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
284 CS->getLHS()->getLocStart(),
285 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000286
Chris Lattner1a4066d2007-08-23 06:23:56 +0000287 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000288 if (CS->getRHS())
289 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000290 else
291 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000292 }
293 }
294
Chris Lattner1a4066d2007-08-23 06:23:56 +0000295 // Sort all the scalar case values so we can easily detect duplicates.
296 std::stable_sort(CaseVals.begin(), CaseVals.end());
Chris Lattner3429a812007-08-23 05:46:52 +0000297
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000298 if (!CaseVals.empty()) {
299 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
300 if (CaseVals[i].first == CaseVals[i+1].first) {
301 // If we have a duplicate, report it.
302 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
303 diag::err_duplicate_case, CaseVals[i].first.toString());
304 Diag(CaseVals[i].second->getLHS()->getLocStart(),
305 diag::err_duplicate_case_prev);
306 // FIXME: We really want to remove the bogus case stmt from the substmt,
307 // but we have no way to do this right now.
308 CaseListIsErroneous = true;
309 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000310 }
311 }
Chris Lattner3429a812007-08-23 05:46:52 +0000312
Chris Lattner1a4066d2007-08-23 06:23:56 +0000313 // Detect duplicate case ranges, which usually don't exist at all in the first
314 // place.
315 if (!CaseRanges.empty()) {
316 // Sort all the case ranges by their low value so we can easily detect
317 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000318 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000319
320 // Scan the ranges, computing the high values and removing empty ranges.
321 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000322 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000323 CaseStmt *CR = CaseRanges[i].second;
324 llvm::APSInt HiVal(32);
325 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
326
327 // Convert the value to the same width/sign as the condition.
328 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
329 CR->getRHS()->getLocStart(),
330 diag::warn_case_value_overflow);
331
Chris Lattner7443e0f2007-08-23 17:48:14 +0000332 // If the low value is bigger than the high value, the case is empty.
333 if (CaseRanges[i].first > HiVal) {
334 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
335 SourceRange(CR->getLHS()->getLocStart(),
336 CR->getRHS()->getLocEnd()));
337 CaseRanges.erase(CaseRanges.begin()+i);
338 --i, --e;
339 continue;
340 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000341 HiVals.push_back(HiVal);
342 }
343
344 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000345 // ranges. Since the range list is sorted, we only need to compare case
346 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000347 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000348 llvm::APSInt &CRLo = CaseRanges[i].first;
349 llvm::APSInt &CRHi = HiVals[i];
350 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000351
Chris Lattner0ab833c2007-08-23 18:29:20 +0000352 // Check to see whether the case range overlaps with any singleton cases.
353 CaseStmt *OverlapStmt = 0;
354 llvm::APSInt OverlapVal(32);
355
356 // Find the smallest value >= the lower bound. If I is in the case range,
357 // then we have overlap.
358 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
359 CaseVals.end(), CRLo,
360 CaseCompareFunctor());
361 if (I != CaseVals.end() && I->first < CRHi) {
362 OverlapVal = I->first; // Found overlap with scalar.
363 OverlapStmt = I->second;
364 }
365
366 // Find the smallest value bigger than the upper bound.
367 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
368 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
369 OverlapVal = (I-1)->first; // Found overlap with scalar.
370 OverlapStmt = (I-1)->second;
371 }
372
373 // Check to see if this case stmt overlaps with the subsequent case range.
374 if (i && CRLo <= HiVals[i-1]) {
375 OverlapVal = HiVals[i-1]; // Found overlap with range.
376 OverlapStmt = CaseRanges[i-1].second;
377 }
378
379 if (OverlapStmt) {
380 // If we have a duplicate, report it.
381 Diag(CR->getLHS()->getLocStart(),
382 diag::err_duplicate_case, OverlapVal.toString());
383 Diag(OverlapStmt->getLHS()->getLocStart(),
384 diag::err_duplicate_case_prev);
385 // FIXME: We really want to remove the bogus case stmt from the substmt,
386 // but we have no way to do this right now.
387 CaseListIsErroneous = true;
388 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000389 }
390 }
Chris Lattner3429a812007-08-23 05:46:52 +0000391
Chris Lattner1a4066d2007-08-23 06:23:56 +0000392 // FIXME: If the case list was broken is some way, we don't have a good system
393 // to patch it up. Instead, just return the whole substmt as broken.
394 if (CaseListIsErroneous)
395 return true;
Chris Lattner3429a812007-08-23 05:46:52 +0000396
Chris Lattner4b009652007-07-25 00:24:17 +0000397 return SS;
398}
399
400Action::StmtResult
401Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
402 Expr *condExpr = (Expr *)Cond;
403 assert(condExpr && "ParseWhileStmt(): missing expression");
404
405 DefaultFunctionArrayConversion(condExpr);
406 QualType condType = condExpr->getType();
407
408 if (!condType->isScalarType()) // C99 6.8.5p2
409 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
410 condType.getAsString(), condExpr->getSourceRange());
411
412 return new WhileStmt(condExpr, (Stmt*)Body);
413}
414
415Action::StmtResult
416Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
417 SourceLocation WhileLoc, ExprTy *Cond) {
418 Expr *condExpr = (Expr *)Cond;
419 assert(condExpr && "ParseDoStmt(): missing expression");
420
421 DefaultFunctionArrayConversion(condExpr);
422 QualType condType = condExpr->getType();
423
424 if (!condType->isScalarType()) // C99 6.8.5p2
425 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
426 condType.getAsString(), condExpr->getSourceRange());
427
428 return new DoStmt((Stmt*)Body, condExpr);
429}
430
431Action::StmtResult
432Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
433 StmtTy *First, ExprTy *Second, ExprTy *Third,
434 SourceLocation RParenLoc, StmtTy *Body) {
435 if (First) {
436 // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
437 // declaration support to create a DeclStmt node. Once this is done,
438 // we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
439 }
440 if (Second) {
441 Expr *testExpr = (Expr *)Second;
442 DefaultFunctionArrayConversion(testExpr);
443 QualType testType = testExpr->getType();
444
445 if (!testType->isScalarType()) // C99 6.8.5p2
446 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
447 testType.getAsString(), testExpr->getSourceRange());
448 }
449 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
450}
451
452
453Action::StmtResult
454Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
455 IdentifierInfo *LabelII) {
456 // Look up the record for this label identifier.
457 LabelStmt *&LabelDecl = LabelMap[LabelII];
458
459 // If we haven't seen this label yet, create a forward reference.
460 if (LabelDecl == 0)
461 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
462
463 return new GotoStmt(LabelDecl);
464}
465
466Action::StmtResult
467Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
468 ExprTy *DestExp) {
469 // FIXME: Verify that the operand is convertible to void*.
470
471 return new IndirectGotoStmt((Expr*)DestExp);
472}
473
474Action::StmtResult
475Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
476 Scope *S = CurScope->getContinueParent();
477 if (!S) {
478 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
479 Diag(ContinueLoc, diag::err_continue_not_in_loop);
480 return true;
481 }
482
483 return new ContinueStmt();
484}
485
486Action::StmtResult
487Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
488 Scope *S = CurScope->getBreakParent();
489 if (!S) {
490 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
491 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
492 return true;
493 }
494
495 return new BreakStmt();
496}
497
498
499Action::StmtResult
500Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
501 Expr *RetValExp = static_cast<Expr *>(rex);
502 QualType lhsType = CurFunctionDecl->getResultType();
503
504 if (lhsType->isVoidType()) {
505 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
506 Diag(ReturnLoc, diag::ext_return_has_expr,
507 CurFunctionDecl->getIdentifier()->getName(),
508 RetValExp->getSourceRange());
509 return new ReturnStmt(RetValExp);
510 } else {
511 if (!RetValExp) {
512 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
513 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
514 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
515 else // C90 6.6.6.4p4
516 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
517 return new ReturnStmt((Expr*)0);
518 }
519 }
520 // we have a non-void function with an expression, continue checking
521 QualType rhsType = RetValExp->getType();
522
523 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
524 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
525 // function return.
526 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
527 RetValExp);
Ted Kremenek235e1ad2007-08-14 18:14:14 +0000528
Chris Lattner4b009652007-07-25 00:24:17 +0000529 // decode the result (notice that extensions still return a type).
530 switch (result) {
531 case Compatible:
532 break;
533 case Incompatible:
534 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
535 lhsType.getAsString(), rhsType.getAsString(),
536 RetValExp->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000537 break;
538 case PointerFromInt:
539 // check for null pointer constant (C99 6.3.2.3p3)
540 if (!RetValExp->isNullPointerConstant(Context)) {
541 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
542 lhsType.getAsString(), rhsType.getAsString(),
543 RetValExp->getSourceRange());
544 }
545 break;
546 case IntFromPointer:
547 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
548 lhsType.getAsString(), rhsType.getAsString(),
549 RetValExp->getSourceRange());
550 break;
551 case IncompatiblePointer:
552 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
553 lhsType.getAsString(), rhsType.getAsString(),
554 RetValExp->getSourceRange());
555 break;
556 case CompatiblePointerDiscardsQualifiers:
557 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
558 lhsType.getAsString(), rhsType.getAsString(),
559 RetValExp->getSourceRange());
560 break;
561 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000562
563 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
564
Chris Lattner4b009652007-07-25 00:24:17 +0000565 return new ReturnStmt((Expr*)RetValExp);
566}
567