blob: 6247215f0ed4884dc4348125f3bb7694ebe20e8e [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,
Chris Lattner3ea3b662007-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 Lattner81417722007-08-27 01:01:57 +000070
Chris Lattner3ea3b662007-08-27 04:29:41 +000071 return new CompoundStmt(Elts, NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +000072}
73
74Action::StmtResult
75Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
76 SourceLocation DotDotDotLoc, ExprTy *rhsval,
77 SourceLocation ColonLoc, StmtTy *subStmt) {
78 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
79 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
80 assert((LHSVal != 0) && "missing expression in case statement");
81
82 SourceLocation ExpLoc;
83 // C99 6.8.4.2p3: The expression shall be an integer constant.
84 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
85 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
86 LHSVal->getSourceRange());
87 return SubStmt;
88 }
89
90 // GCC extension: The expression shall be an integer constant.
91 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
92 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
93 RHSVal->getSourceRange());
Chris Lattner3429a812007-08-23 05:46:52 +000094 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner4b009652007-07-25 00:24:17 +000095 }
96
97 if (SwitchStack.empty()) {
98 Diag(CaseLoc, diag::err_case_not_in_switch);
99 return SubStmt;
100 }
101
102 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
103 SwitchStack.back()->addSwitchCase(CS);
104 return CS;
105}
106
107Action::StmtResult
108Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
109 StmtTy *subStmt, Scope *CurScope) {
110 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
111
112 if (SwitchStack.empty()) {
113 Diag(DefaultLoc, diag::err_default_not_in_switch);
114 return SubStmt;
115 }
116
117 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
118 SwitchStack.back()->addSwitchCase(DS);
119
120 return DS;
121}
122
123Action::StmtResult
124Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
125 SourceLocation ColonLoc, StmtTy *subStmt) {
126 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
127 // 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)
132 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
133
134 assert(LabelDecl->getID() == II && "Label mismatch!");
135
136 // Otherwise, this label was either forward reference or multiply defined. If
137 // multiply defined, reject it now.
138 if (LabelDecl->getSubStmt()) {
139 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
140 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
141 return SubStmt;
142 }
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);
147 LabelDecl->setSubStmt(SubStmt);
148 return LabelDecl;
149}
150
151Action::StmtResult
152Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
153 StmtTy *ThenVal, SourceLocation ElseLoc,
154 StmtTy *ElseVal) {
155 Expr *condExpr = (Expr *)CondVal;
156 assert(condExpr && "ParseIfStmt(): missing expression");
157
158 DefaultFunctionArrayConversion(condExpr);
159 QualType condType = condExpr->getType();
160
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);
166}
167
168Action::StmtResult
Chris Lattner3429a812007-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);
Chris Lattner4b009652007-07-25 00:24:17 +0000176 SwitchStack.push_back(SS);
177 return SS;
178}
179
Chris Lattner3429a812007-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 Lattner5c039602007-08-23 22:08:35 +0000203 ConvVal.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000204 ConvVal.extend(Val.getBitWidth());
Chris Lattner5c039602007-08-23 22:08:35 +0000205 ConvVal.setIsSigned(Val.isSigned());
Chris Lattner3429a812007-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 Lattner5c039602007-08-23 22:08:35 +0000212 Val.setIsSigned(NewSign);
Chris Lattner3429a812007-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 Lattner0ab833c2007-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
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner3429a812007-08-23 05:46:52 +0000247 Expr *CondExpr = SS->getCond();
248 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000249
Chris Lattner3429a812007-08-23 05:46:52 +0000250 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner4b009652007-07-25 00:24:17 +0000251 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattner3429a812007-08-23 05:46:52 +0000252 CondType.getAsString(), CondExpr->getSourceRange());
253 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000254 }
Chris Lattner3429a812007-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 Lattner0ab833c2007-08-23 18:29:20 +0000264 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
265 CaseValsTy CaseVals;
Chris Lattner3429a812007-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000271
Chris Lattner1a4066d2007-08-23 06:23:56 +0000272 bool CaseListIsErroneous = false;
273
274 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000275 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000276
Chris Lattner4b009652007-07-25 00:24:17 +0000277 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-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);
Chris Lattner4b009652007-07-25 00:24:17 +0000281
Chris Lattner3429a812007-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 Lattner1a4066d2007-08-23 06:23:56 +0000286 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000287 }
Chris Lattner3429a812007-08-23 05:46:52 +0000288 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000289
Chris Lattner3429a812007-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);
Chris Lattner4b009652007-07-25 00:24:17 +0000302
Chris Lattner1a4066d2007-08-23 06:23:56 +0000303 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000304 if (CS->getRHS())
305 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000306 else
307 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000308 }
309 }
310
Chris Lattner1a4066d2007-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 Lattner3429a812007-08-23 05:46:52 +0000313
Chris Lattner2b1b9a82007-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 Lattner1a4066d2007-08-23 06:23:56 +0000326 }
327 }
Chris Lattner3429a812007-08-23 05:46:52 +0000328
Chris Lattner1a4066d2007-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 Lattner0ab833c2007-08-23 18:29:20 +0000334 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-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 Lattner7443e0f2007-08-23 17:48:14 +0000338 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-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 Lattner7443e0f2007-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 Lattner1a4066d2007-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 Lattner0ab833c2007-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 Lattner1a4066d2007-08-23 06:23:56 +0000363 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-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 Lattner1a4066d2007-08-23 06:23:56 +0000367
Chris Lattner0ab833c2007-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 Lattner1a4066d2007-08-23 06:23:56 +0000405 }
406 }
Chris Lattner3429a812007-08-23 05:46:52 +0000407
Chris Lattner1a4066d2007-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 Lattner3429a812007-08-23 05:46:52 +0000412
Chris Lattner4b009652007-07-25 00:24:17 +0000413 return SS;
414}
415
416Action::StmtResult
417Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
418 Expr *condExpr = (Expr *)Cond;
419 assert(condExpr && "ParseWhileStmt(): missing expression");
420
421 DefaultFunctionArrayConversion(condExpr);
422 QualType condType = condExpr->getType();
423
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);
429}
430
431Action::StmtResult
432Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
433 SourceLocation WhileLoc, ExprTy *Cond) {
434 Expr *condExpr = (Expr *)Cond;
435 assert(condExpr && "ParseDoStmt(): missing expression");
436
437 DefaultFunctionArrayConversion(condExpr);
438 QualType condType = condExpr->getType();
439
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);
445}
446
447Action::StmtResult
448Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000449 StmtTy *first, ExprTy *second, ExprTy *third,
450 SourceLocation RParenLoc, StmtTy *body) {
451 Stmt *First = static_cast<Stmt*>(first);
452 Expr *Second = static_cast<Expr*>(second);
453 Expr *Third = static_cast<Expr*>(third);
454 Stmt *Body = static_cast<Stmt*>(body);
455
Chris Lattner06611052007-08-28 05:03:08 +0000456 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
457 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
458 // identifiers for objects having storage class 'auto' or 'register'.
459 for (Decl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
460 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D);
461 if (BVD && !BVD->hasLocalStorage())
462 BVD = 0;
463 if (BVD == 0)
464 Diag(D->getLocation(), diag::err_non_variable_decl_in_for);
465 // FIXME: mark decl erroneous!
466 }
Chris Lattner4b009652007-07-25 00:24:17 +0000467 }
468 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000469 DefaultFunctionArrayConversion(Second);
470 QualType SecondType = Second->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000471
Chris Lattner3332fbd2007-08-28 04:55:47 +0000472 if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattner4b009652007-07-25 00:24:17 +0000473 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000474 SecondType.getAsString(), Second->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000475 }
Chris Lattner3332fbd2007-08-28 04:55:47 +0000476 return new ForStmt(First, Second, Third, Body);
Chris Lattner4b009652007-07-25 00:24:17 +0000477}
478
479
480Action::StmtResult
481Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
482 IdentifierInfo *LabelII) {
483 // Look up the record for this label identifier.
484 LabelStmt *&LabelDecl = LabelMap[LabelII];
485
486 // If we haven't seen this label yet, create a forward reference.
487 if (LabelDecl == 0)
488 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
489
490 return new GotoStmt(LabelDecl);
491}
492
493Action::StmtResult
494Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
495 ExprTy *DestExp) {
496 // FIXME: Verify that the operand is convertible to void*.
497
498 return new IndirectGotoStmt((Expr*)DestExp);
499}
500
501Action::StmtResult
502Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
503 Scope *S = CurScope->getContinueParent();
504 if (!S) {
505 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
506 Diag(ContinueLoc, diag::err_continue_not_in_loop);
507 return true;
508 }
509
510 return new ContinueStmt();
511}
512
513Action::StmtResult
514Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
515 Scope *S = CurScope->getBreakParent();
516 if (!S) {
517 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
518 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
519 return true;
520 }
521
522 return new BreakStmt();
523}
524
525
526Action::StmtResult
527Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
528 Expr *RetValExp = static_cast<Expr *>(rex);
529 QualType lhsType = CurFunctionDecl->getResultType();
530
531 if (lhsType->isVoidType()) {
532 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
533 Diag(ReturnLoc, diag::ext_return_has_expr,
534 CurFunctionDecl->getIdentifier()->getName(),
535 RetValExp->getSourceRange());
536 return new ReturnStmt(RetValExp);
537 } else {
538 if (!RetValExp) {
539 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
540 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
541 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
542 else // C90 6.6.6.4p4
543 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
544 return new ReturnStmt((Expr*)0);
545 }
546 }
547 // we have a non-void function with an expression, continue checking
548 QualType rhsType = RetValExp->getType();
549
550 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
551 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
552 // function return.
553 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
554 RetValExp);
Ted Kremenek235e1ad2007-08-14 18:14:14 +0000555
Chris Lattner4b009652007-07-25 00:24:17 +0000556 // decode the result (notice that extensions still return a type).
557 switch (result) {
558 case Compatible:
559 break;
560 case Incompatible:
561 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
562 lhsType.getAsString(), rhsType.getAsString(),
563 RetValExp->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000564 break;
565 case PointerFromInt:
566 // check for null pointer constant (C99 6.3.2.3p3)
567 if (!RetValExp->isNullPointerConstant(Context)) {
568 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
569 lhsType.getAsString(), rhsType.getAsString(),
570 RetValExp->getSourceRange());
571 }
572 break;
573 case IntFromPointer:
574 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
575 lhsType.getAsString(), rhsType.getAsString(),
576 RetValExp->getSourceRange());
577 break;
578 case IncompatiblePointer:
579 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
580 lhsType.getAsString(), rhsType.getAsString(),
581 RetValExp->getSourceRange());
582 break;
583 case CompatiblePointerDiscardsQualifiers:
584 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
585 lhsType.getAsString(), rhsType.getAsString(),
586 RetValExp->getSourceRange());
587 break;
588 }
Ted Kremenek45925ab2007-08-17 16:46:58 +0000589
590 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
591
Chris Lattner4b009652007-07-25 00:24:17 +0000592 return new ReturnStmt((Expr*)RetValExp);
593}
594