blob: 6839310e0eca77ce6423be51b9fa34fbd28c6106 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for statements.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anders Carlssone8bd9f22008-11-22 21:04:56 +000015#include "clang/AST/APValue.h"
Chris Lattner3429a812007-08-23 05:46:52 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/AST/Expr.h"
Anders Carlsson49dadd62007-11-25 00:25:21 +000019#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Basic/Diagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
Steve Naroff5cbb02f2007-09-16 14:56:35 +000023Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) {
Chris Lattner4b009652007-07-25 00:24:17 +000024 Expr *E = static_cast<Expr*>(expr);
Steve Naroff5cbb02f2007-09-16 14:56:35 +000025 assert(E && "ActOnExprStmt(): missing expression");
Chris Lattnere02e4402008-07-25 23:18:17 +000026
27 // C99 6.8.3p2: The expression in an expression statement is evaluated as a
28 // void expression for its side effects. Conversion to void allows any
29 // operand, even incomplete types.
30
31 // Same thing in for stmt first clause (when expr) and third clause.
Chris Lattner4b009652007-07-25 00:24:17 +000032 return E;
33}
34
35
Steve Naroff5cbb02f2007-09-16 14:56:35 +000036Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000037 return new NullStmt(SemiLoc);
38}
39
Chris Lattnera4ff4272008-03-13 06:29:04 +000040Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl, SourceLocation StartLoc,
41 SourceLocation EndLoc) {
42 if (decl == 0)
43 return true;
44
45 ScopedDecl *SD = cast<ScopedDecl>(static_cast<Decl *>(decl));
Ted Kremenek1bc18e62008-10-07 23:09:49 +000046
47
48 // This is a temporary hack until we are always passing around
49 // DeclGroupRefs.
50 llvm::SmallVector<Decl*, 10> decls;
51 while (SD) {
52 ScopedDecl* d = SD;
53 SD = SD->getNextDeclarator();
54 d->setNextDeclarator(0);
55 decls.push_back(d);
56 }
57
58 assert (!decls.empty());
59
60 if (decls.size() == 1) {
61 DeclGroupOwningRef DG(*decls.begin());
62 return new DeclStmt(DG, StartLoc, EndLoc);
63 }
64 else {
65 DeclGroupOwningRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
66 return new DeclStmt(DG, StartLoc, EndLoc);
67 }
Chris Lattner4b009652007-07-25 00:24:17 +000068}
69
70Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +000071Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnerf2b07572007-08-31 21:49:55 +000072 StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
Chris Lattner3ea3b662007-08-27 04:29:41 +000073 Stmt **Elts = reinterpret_cast<Stmt**>(elts);
74 // If we're in C89 mode, check that we don't have any decls after stmts. If
75 // so, emit an extension diagnostic.
76 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
77 // Note that __extension__ can be around a decl.
78 unsigned i = 0;
79 // Skip over all declarations.
80 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
81 /*empty*/;
82
83 // We found the end of the list or a statement. Scan for another declstmt.
84 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
85 /*empty*/;
86
87 if (i != NumElts) {
Ted Kremenekc7350432008-10-06 18:48:35 +000088 ScopedDecl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
Chris Lattner3ea3b662007-08-27 04:29:41 +000089 Diag(D->getLocation(), diag::ext_mixed_decls_code);
90 }
91 }
Chris Lattnerf2b07572007-08-31 21:49:55 +000092 // Warn about unused expressions in statements.
93 for (unsigned i = 0; i != NumElts; ++i) {
94 Expr *E = dyn_cast<Expr>(Elts[i]);
95 if (!E) continue;
96
97 // Warn about expressions with unused results.
98 if (E->hasLocalSideEffect() || E->getType()->isVoidType())
99 continue;
100
101 // The last expr in a stmt expr really is used.
102 if (isStmtExpr && i == NumElts-1)
103 continue;
104
105 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
106 /// a context where the result is unused. Emit a diagnostic to warn about
107 /// this.
108 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000109 Diag(BO->getOperatorLoc(), diag::warn_unused_expr)
110 << BO->getLHS()->getSourceRange() << BO->getRHS()->getSourceRange();
Chris Lattnerf2b07572007-08-31 21:49:55 +0000111 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000112 Diag(UO->getOperatorLoc(), diag::warn_unused_expr)
113 << UO->getSubExpr()->getSourceRange();
Chris Lattnerf2b07572007-08-31 21:49:55 +0000114 else
Chris Lattner8ba580c2008-11-19 05:08:23 +0000115 Diag(E->getExprLoc(), diag::warn_unused_expr) << E->getSourceRange();
Chris Lattnerf2b07572007-08-31 21:49:55 +0000116 }
117
Steve Naroff5d2fff82007-08-31 23:28:33 +0000118 return new CompoundStmt(Elts, NumElts, L, R);
Chris Lattner4b009652007-07-25 00:24:17 +0000119}
120
121Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000122Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner4b009652007-07-25 00:24:17 +0000123 SourceLocation DotDotDotLoc, ExprTy *rhsval,
124 SourceLocation ColonLoc, StmtTy *subStmt) {
125 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
126 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
127 assert((LHSVal != 0) && "missing expression in case statement");
128
129 SourceLocation ExpLoc;
130 // C99 6.8.4.2p3: The expression shall be an integer constant.
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000131 // However, GCC allows any evaluatable integer expression.
132 // FIXME: Should we warn if this is evaluatable but not an I-C-E?
133 APValue Result;
134
135 if (!LHSVal->Evaluate(Result, Context) || !Result.isInt()) {
136 // FIXME: Evaluate doesn't return the SourceLocation that it failed to
137 // evaluate.
138 ExpLoc = LHSVal->getExprLoc();
Chris Lattner9d2cf082008-11-19 05:27:50 +0000139 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr)
140 << LHSVal->getSourceRange();
Chris Lattner4b009652007-07-25 00:24:17 +0000141 return SubStmt;
142 }
143
144 // GCC extension: The expression shall be an integer constant.
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000145 if (RHSVal && !RHSVal->Evaluate(Result, Context) || !Result.isInt()) {
146 ExpLoc = RHSVal->getExprLoc();
Chris Lattner9d2cf082008-11-19 05:27:50 +0000147 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr)
148 << RHSVal->getSourceRange();
Chris Lattner3429a812007-08-23 05:46:52 +0000149 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner4b009652007-07-25 00:24:17 +0000150 }
151
152 if (SwitchStack.empty()) {
153 Diag(CaseLoc, diag::err_case_not_in_switch);
154 return SubStmt;
155 }
156
Steve Naroff5d2fff82007-08-31 23:28:33 +0000157 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000158 SwitchStack.back()->addSwitchCase(CS);
159 return CS;
160}
161
162Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000163Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000164 StmtTy *subStmt, Scope *CurScope) {
165 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
166
167 if (SwitchStack.empty()) {
168 Diag(DefaultLoc, diag::err_default_not_in_switch);
169 return SubStmt;
170 }
171
172 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
173 SwitchStack.back()->addSwitchCase(DS);
174
175 return DS;
176}
177
178Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000179Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner4b009652007-07-25 00:24:17 +0000180 SourceLocation ColonLoc, StmtTy *subStmt) {
181 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
182 // Look up the record for this label identifier.
183 LabelStmt *&LabelDecl = LabelMap[II];
184
185 // If not forward referenced or defined already, just create a new LabelStmt.
186 if (LabelDecl == 0)
187 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
188
189 assert(LabelDecl->getID() == II && "Label mismatch!");
190
191 // Otherwise, this label was either forward reference or multiply defined. If
192 // multiply defined, reject it now.
193 if (LabelDecl->getSubStmt()) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000194 Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getName();
Chris Lattner4b009652007-07-25 00:24:17 +0000195 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
196 return SubStmt;
197 }
198
199 // Otherwise, this label was forward declared, and we just found its real
200 // definition. Fill in the forward definition and return it.
201 LabelDecl->setIdentLoc(IdentLoc);
202 LabelDecl->setSubStmt(SubStmt);
203 return LabelDecl;
204}
205
206Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000207Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Chris Lattner4b009652007-07-25 00:24:17 +0000208 StmtTy *ThenVal, SourceLocation ElseLoc,
209 StmtTy *ElseVal) {
210 Expr *condExpr = (Expr *)CondVal;
Anders Carlsson663733e2007-10-10 20:50:11 +0000211 Stmt *thenStmt = (Stmt *)ThenVal;
212
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000213 assert(condExpr && "ActOnIfStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000214
215 DefaultFunctionArrayConversion(condExpr);
216 QualType condType = condExpr->getType();
217
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000218 if (getLangOptions().CPlusPlus) {
219 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
220 return true;
221 } else if (!condType->isScalarType()) // C99 6.8.4.1p1
Chris Lattner77d52da2008-11-20 06:06:08 +0000222 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar)
223 << condType.getAsString() << condExpr->getSourceRange();
Chris Lattner4b009652007-07-25 00:24:17 +0000224
Anders Carlsson663733e2007-10-10 20:50:11 +0000225 // Warn if the if block has a null body without an else value.
226 // this helps prevent bugs due to typos, such as
227 // if (condition);
228 // do_stuff();
229 if (!ElseVal) {
230 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
231 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
232 }
233
234 return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal);
Chris Lattner4b009652007-07-25 00:24:17 +0000235}
236
237Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000238Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
Chris Lattner3429a812007-08-23 05:46:52 +0000239 Expr *Cond = static_cast<Expr*>(cond);
240
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000241 if (getLangOptions().CPlusPlus) {
242 // C++ 6.4.2.p2:
243 // The condition shall be of integral type, enumeration type, or of a class
244 // type for which a single conversion function to integral or enumeration
245 // type exists (12.3). If the condition is of class type, the condition is
246 // converted by calling that conversion function, and the result of the
247 // conversion is used in place of the original condition for the remainder
248 // of this section. Integral promotions are performed.
249
250 QualType Ty = Cond->getType();
251
252 // FIXME: Handle class types.
253
254 // If the type is wrong a diagnostic will be emitted later at
255 // ActOnFinishSwitchStmt.
256 if (Ty->isIntegralType() || Ty->isEnumeralType()) {
257 // Integral promotions are performed.
258 // FIXME: Integral promotions for C++ are not complete.
259 UsualUnaryConversions(Cond);
260 }
261 } else {
262 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
263 UsualUnaryConversions(Cond);
264 }
Chris Lattner3429a812007-08-23 05:46:52 +0000265
266 SwitchStmt *SS = new SwitchStmt(Cond);
Chris Lattner4b009652007-07-25 00:24:17 +0000267 SwitchStack.push_back(SS);
268 return SS;
269}
270
Chris Lattner3429a812007-08-23 05:46:52 +0000271/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
272/// the specified width and sign. If an overflow occurs, detect it and emit
273/// the specified diagnostic.
274void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
275 unsigned NewWidth, bool NewSign,
276 SourceLocation Loc,
277 unsigned DiagID) {
278 // Perform a conversion to the promoted condition type if needed.
279 if (NewWidth > Val.getBitWidth()) {
280 // If this is an extension, just do it.
281 llvm::APSInt OldVal(Val);
282 Val.extend(NewWidth);
283
284 // If the input was signed and negative and the output is unsigned,
285 // warn.
286 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
Chris Lattner77d52da2008-11-20 06:06:08 +0000287 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000288
289 Val.setIsSigned(NewSign);
290 } else if (NewWidth < Val.getBitWidth()) {
291 // If this is a truncation, check for overflow.
292 llvm::APSInt ConvVal(Val);
293 ConvVal.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000294 ConvVal.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000295 ConvVal.extend(Val.getBitWidth());
Chris Lattner5c039602007-08-23 22:08:35 +0000296 ConvVal.setIsSigned(Val.isSigned());
Chris Lattner3429a812007-08-23 05:46:52 +0000297 if (ConvVal != Val)
Chris Lattner77d52da2008-11-20 06:06:08 +0000298 Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000299
300 // Regardless of whether a diagnostic was emitted, really do the
301 // truncation.
302 Val.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000303 Val.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000304 } else if (NewSign != Val.isSigned()) {
305 // Convert the sign to match the sign of the condition. This can cause
306 // overflow as well: unsigned(INTMIN)
307 llvm::APSInt OldVal(Val);
308 Val.setIsSigned(NewSign);
309
310 if (Val.isNegative()) // Sign bit changes meaning.
Chris Lattner77d52da2008-11-20 06:06:08 +0000311 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000312 }
313}
314
Chris Lattner0ab833c2007-08-23 18:29:20 +0000315namespace {
316 struct CaseCompareFunctor {
317 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
318 const llvm::APSInt &RHS) {
319 return LHS.first < RHS;
320 }
Chris Lattner2157f272007-09-03 18:31:57 +0000321 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
322 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
323 return LHS.first < RHS.first;
324 }
Chris Lattner0ab833c2007-08-23 18:29:20 +0000325 bool operator()(const llvm::APSInt &LHS,
326 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
327 return LHS < RHS.first;
328 }
329 };
330}
331
Chris Lattner766afb82007-09-21 18:15:22 +0000332/// CmpCaseVals - Comparison predicate for sorting case values.
333///
334static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
335 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
336 if (lhs.first < rhs.first)
337 return true;
338
339 if (lhs.first == rhs.first &&
340 lhs.second->getCaseLoc().getRawEncoding()
341 < rhs.second->getCaseLoc().getRawEncoding())
342 return true;
343 return false;
344}
345
Chris Lattner4b009652007-07-25 00:24:17 +0000346Action::StmtResult
Chris Lattner766afb82007-09-21 18:15:22 +0000347Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
348 ExprTy *Body) {
Chris Lattner4b009652007-07-25 00:24:17 +0000349 Stmt *BodyStmt = (Stmt*)Body;
350
351 SwitchStmt *SS = SwitchStack.back();
352 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
353
Steve Naroffa610eab2007-09-01 21:08:38 +0000354 SS->setBody(BodyStmt, SwitchLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000355 SwitchStack.pop_back();
356
Chris Lattner3429a812007-08-23 05:46:52 +0000357 Expr *CondExpr = SS->getCond();
358 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000359
Chris Lattner3429a812007-08-23 05:46:52 +0000360 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner77d52da2008-11-20 06:06:08 +0000361 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
362 << CondType.getAsString() << CondExpr->getSourceRange();
Chris Lattner3429a812007-08-23 05:46:52 +0000363 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000364 }
Chris Lattner3429a812007-08-23 05:46:52 +0000365
366 // Get the bitwidth of the switched-on value before promotions. We must
367 // convert the integer case values to this width before comparison.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000368 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattner3429a812007-08-23 05:46:52 +0000369 bool CondIsSigned = CondType->isSignedIntegerType();
370
371 // Accumulate all of the case values in a vector so that we can sort them
372 // and detect duplicates. This vector contains the APInt for the case after
373 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000374 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
375 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000376
377 // Keep track of any GNU case ranges we see. The APSInt is the low value.
378 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
379
380 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000381
Chris Lattner1a4066d2007-08-23 06:23:56 +0000382 bool CaseListIsErroneous = false;
383
384 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000385 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000386
Chris Lattner4b009652007-07-25 00:24:17 +0000387 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000388 if (TheDefaultStmt) {
389 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
390 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Chris Lattner4b009652007-07-25 00:24:17 +0000391
Chris Lattner3429a812007-08-23 05:46:52 +0000392 // FIXME: Remove the default statement from the switch block so that
393 // we'll return a valid AST. This requires recursing down the
394 // AST and finding it, not something we are set up to do right now. For
395 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000396 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000397 }
Chris Lattner3429a812007-08-23 05:46:52 +0000398 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000399
Chris Lattner3429a812007-08-23 05:46:52 +0000400 } else {
401 CaseStmt *CS = cast<CaseStmt>(SC);
402
403 // We already verified that the expression has a i-c-e value (C99
404 // 6.8.4.2p3) - get that value now.
Chris Lattnere992d6c2008-01-16 19:17:22 +0000405 Expr *Lo = CS->getLHS();
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000406 llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
Chris Lattner3429a812007-08-23 05:46:52 +0000407
408 // Convert the value to the same width/sign as the condition.
409 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
410 CS->getLHS()->getLocStart(),
411 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000412
Chris Lattnere992d6c2008-01-16 19:17:22 +0000413 // If the LHS is not the same type as the condition, insert an implicit
414 // cast.
415 ImpCastExprToType(Lo, CondType);
416 CS->setLHS(Lo);
417
Chris Lattner1a4066d2007-08-23 06:23:56 +0000418 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000419 if (CS->getRHS())
420 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000421 else
422 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000423 }
424 }
425
Chris Lattner1a4066d2007-08-23 06:23:56 +0000426 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner766afb82007-09-21 18:15:22 +0000427 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattner3429a812007-08-23 05:46:52 +0000428
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000429 if (!CaseVals.empty()) {
430 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
431 if (CaseVals[i].first == CaseVals[i+1].first) {
432 // If we have a duplicate, report it.
433 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000434 diag::err_duplicate_case) << CaseVals[i].first.toString(10);
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000435 Diag(CaseVals[i].second->getLHS()->getLocStart(),
436 diag::err_duplicate_case_prev);
437 // FIXME: We really want to remove the bogus case stmt from the substmt,
438 // but we have no way to do this right now.
439 CaseListIsErroneous = true;
440 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000441 }
442 }
Chris Lattner3429a812007-08-23 05:46:52 +0000443
Chris Lattner1a4066d2007-08-23 06:23:56 +0000444 // Detect duplicate case ranges, which usually don't exist at all in the first
445 // place.
446 if (!CaseRanges.empty()) {
447 // Sort all the case ranges by their low value so we can easily detect
448 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000449 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000450
451 // Scan the ranges, computing the high values and removing empty ranges.
452 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000453 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000454 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnere992d6c2008-01-16 19:17:22 +0000455 Expr *Hi = CR->getRHS();
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000456 llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
Chris Lattner1a4066d2007-08-23 06:23:56 +0000457
458 // Convert the value to the same width/sign as the condition.
459 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
460 CR->getRHS()->getLocStart(),
461 diag::warn_case_value_overflow);
462
Chris Lattnere992d6c2008-01-16 19:17:22 +0000463 // If the LHS is not the same type as the condition, insert an implicit
464 // cast.
465 ImpCastExprToType(Hi, CondType);
466 CR->setRHS(Hi);
467
Chris Lattner7443e0f2007-08-23 17:48:14 +0000468 // If the low value is bigger than the high value, the case is empty.
469 if (CaseRanges[i].first > HiVal) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000470 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
471 << SourceRange(CR->getLHS()->getLocStart(),
472 CR->getRHS()->getLocEnd());
Chris Lattner7443e0f2007-08-23 17:48:14 +0000473 CaseRanges.erase(CaseRanges.begin()+i);
474 --i, --e;
475 continue;
476 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000477 HiVals.push_back(HiVal);
478 }
479
480 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000481 // ranges. Since the range list is sorted, we only need to compare case
482 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000483 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000484 llvm::APSInt &CRLo = CaseRanges[i].first;
485 llvm::APSInt &CRHi = HiVals[i];
486 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000487
Chris Lattner0ab833c2007-08-23 18:29:20 +0000488 // Check to see whether the case range overlaps with any singleton cases.
489 CaseStmt *OverlapStmt = 0;
490 llvm::APSInt OverlapVal(32);
491
492 // Find the smallest value >= the lower bound. If I is in the case range,
493 // then we have overlap.
494 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
495 CaseVals.end(), CRLo,
496 CaseCompareFunctor());
497 if (I != CaseVals.end() && I->first < CRHi) {
498 OverlapVal = I->first; // Found overlap with scalar.
499 OverlapStmt = I->second;
500 }
501
502 // Find the smallest value bigger than the upper bound.
503 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
504 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
505 OverlapVal = (I-1)->first; // Found overlap with scalar.
506 OverlapStmt = (I-1)->second;
507 }
508
509 // Check to see if this case stmt overlaps with the subsequent case range.
510 if (i && CRLo <= HiVals[i-1]) {
511 OverlapVal = HiVals[i-1]; // Found overlap with range.
512 OverlapStmt = CaseRanges[i-1].second;
513 }
514
515 if (OverlapStmt) {
516 // If we have a duplicate, report it.
Chris Lattner77d52da2008-11-20 06:06:08 +0000517 Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
518 << OverlapVal.toString(10);
Chris Lattner0ab833c2007-08-23 18:29:20 +0000519 Diag(OverlapStmt->getLHS()->getLocStart(),
520 diag::err_duplicate_case_prev);
521 // FIXME: We really want to remove the bogus case stmt from the substmt,
522 // but we have no way to do this right now.
523 CaseListIsErroneous = true;
524 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000525 }
526 }
Chris Lattner3429a812007-08-23 05:46:52 +0000527
Chris Lattner1a4066d2007-08-23 06:23:56 +0000528 // FIXME: If the case list was broken is some way, we don't have a good system
529 // to patch it up. Instead, just return the whole substmt as broken.
530 if (CaseListIsErroneous)
531 return true;
Chris Lattner3429a812007-08-23 05:46:52 +0000532
Chris Lattner4b009652007-07-25 00:24:17 +0000533 return SS;
534}
535
536Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000537Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Chris Lattner4b009652007-07-25 00:24:17 +0000538 Expr *condExpr = (Expr *)Cond;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000539 assert(condExpr && "ActOnWhileStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000540
541 DefaultFunctionArrayConversion(condExpr);
542 QualType condType = condExpr->getType();
543
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000544 if (getLangOptions().CPlusPlus) {
545 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
546 return true;
547 } else if (!condType->isScalarType()) // C99 6.8.5p2
Chris Lattner77d52da2008-11-20 06:06:08 +0000548 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar)
549 << condType.getAsString() << condExpr->getSourceRange();
Chris Lattner4b009652007-07-25 00:24:17 +0000550
Steve Naroff5d2fff82007-08-31 23:28:33 +0000551 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000552}
553
554Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000555Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Chris Lattner4b009652007-07-25 00:24:17 +0000556 SourceLocation WhileLoc, ExprTy *Cond) {
557 Expr *condExpr = (Expr *)Cond;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000558 assert(condExpr && "ActOnDoStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000559
560 DefaultFunctionArrayConversion(condExpr);
561 QualType condType = condExpr->getType();
562
Argiris Kirtzidisc362d382008-09-11 05:16:22 +0000563 if (getLangOptions().CPlusPlus) {
564 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
565 return true;
566 } else if (!condType->isScalarType()) // C99 6.8.5p2
Chris Lattner77d52da2008-11-20 06:06:08 +0000567 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar)
568 << condType.getAsString() << condExpr->getSourceRange();
Chris Lattner4b009652007-07-25 00:24:17 +0000569
Steve Naroff5d2fff82007-08-31 23:28:33 +0000570 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000571}
572
573Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000574Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000575 StmtTy *first, ExprTy *second, ExprTy *third,
576 SourceLocation RParenLoc, StmtTy *body) {
577 Stmt *First = static_cast<Stmt*>(first);
578 Expr *Second = static_cast<Expr*>(second);
579 Expr *Third = static_cast<Expr*>(third);
580 Stmt *Body = static_cast<Stmt*>(body);
581
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000582 if (!getLangOptions().CPlusPlus) {
583 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000584 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
585 // declare identifiers for objects having storage class 'auto' or
586 // 'register'.
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000587 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
588 DI!=DE; ++DI) {
589 VarDecl *VD = dyn_cast<VarDecl>(*DI);
590 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
591 VD = 0;
592 if (VD == 0)
593 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
594 // FIXME: mark decl erroneous!
595 }
Chris Lattner06611052007-08-28 05:03:08 +0000596 }
Chris Lattner4b009652007-07-25 00:24:17 +0000597 }
598 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000599 DefaultFunctionArrayConversion(Second);
600 QualType SecondType = Second->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000601
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000602 if (getLangOptions().CPlusPlus) {
603 if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
604 return true;
605 } else if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattner77d52da2008-11-20 06:06:08 +0000606 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar)
607 << SecondType.getAsString() << Second->getSourceRange();
Chris Lattner4b009652007-07-25 00:24:17 +0000608 }
Steve Naroff5d2fff82007-08-31 23:28:33 +0000609 return new ForStmt(First, Second, Third, Body, ForLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000610}
611
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000612Action::StmtResult
Fariborz Jahaniandf2b0952008-01-10 00:24:29 +0000613Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000614 SourceLocation LParenLoc,
615 StmtTy *first, ExprTy *second,
616 SourceLocation RParenLoc, StmtTy *body) {
617 Stmt *First = static_cast<Stmt*>(first);
618 Expr *Second = static_cast<Expr*>(second);
619 Stmt *Body = static_cast<Stmt*>(body);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +0000620 if (First) {
621 QualType FirstType;
622 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
Ted Kremenek779e1c22008-10-06 20:58:11 +0000623 if (!DS->hasSolitaryDecl())
624 return Diag((*DS->decl_begin())->getLocation(),
625 diag::err_toomany_element_decls);
626
627 ScopedDecl *D = DS->getSolitaryDecl();
628 FirstType = cast<ValueDecl>(D)->getType();
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000629 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
630 // declare identifiers for objects having storage class 'auto' or
631 // 'register'.
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000632 VarDecl *VD = cast<VarDecl>(D);
633 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
634 return Diag(VD->getLocation(), diag::err_non_variable_decl_in_for);
Anders Carlsson8d0bcb02008-08-25 18:16:36 +0000635 } else {
636 Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context);
637
638 if (lval != Expr::LV_Valid)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000639 return Diag(First->getLocStart(), diag::err_selector_element_not_lvalue)
640 << First->getSourceRange();
Anders Carlsson8d0bcb02008-08-25 18:16:36 +0000641
642 FirstType = static_cast<Expr*>(first)->getType();
643 }
Ted Kremenek118930e2008-07-24 23:58:27 +0000644 if (!Context.isObjCObjectPointerType(FirstType))
Chris Lattner9d2cf082008-11-19 05:27:50 +0000645 Diag(ForLoc, diag::err_selector_element_type)
646 << FirstType.getAsString() << First->getSourceRange();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000647 }
648 if (Second) {
649 DefaultFunctionArrayConversion(Second);
650 QualType SecondType = Second->getType();
Ted Kremenek118930e2008-07-24 23:58:27 +0000651 if (!Context.isObjCObjectPointerType(SecondType))
Chris Lattner9d2cf082008-11-19 05:27:50 +0000652 Diag(ForLoc, diag::err_collection_expr_type)
653 << SecondType.getAsString() << Second->getSourceRange();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000654 }
Fariborz Jahaniandf2b0952008-01-10 00:24:29 +0000655 return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc);
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000656}
Chris Lattner4b009652007-07-25 00:24:17 +0000657
658Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000659Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000660 IdentifierInfo *LabelII) {
Steve Naroff52a81c02008-09-03 18:15:37 +0000661 // If we are in a block, reject all gotos for now.
662 if (CurBlock)
663 return Diag(GotoLoc, diag::err_goto_in_block);
664
Chris Lattner4b009652007-07-25 00:24:17 +0000665 // Look up the record for this label identifier.
666 LabelStmt *&LabelDecl = LabelMap[LabelII];
667
668 // If we haven't seen this label yet, create a forward reference.
669 if (LabelDecl == 0)
670 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
671
Ted Kremeneka65ad462007-09-06 17:11:52 +0000672 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000673}
674
675Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000676Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000677 ExprTy *DestExp) {
678 // FIXME: Verify that the operand is convertible to void*.
679
680 return new IndirectGotoStmt((Expr*)DestExp);
681}
682
683Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000684Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000685 Scope *S = CurScope->getContinueParent();
686 if (!S) {
687 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
688 Diag(ContinueLoc, diag::err_continue_not_in_loop);
689 return true;
690 }
691
Steve Naroffc32a20d2007-08-31 23:49:30 +0000692 return new ContinueStmt(ContinueLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000693}
694
695Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000696Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000697 Scope *S = CurScope->getBreakParent();
698 if (!S) {
699 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
700 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
701 return true;
702 }
703
Steve Naroffc32a20d2007-08-31 23:49:30 +0000704 return new BreakStmt(BreakLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000705}
706
Douglas Gregor81c29152008-10-29 00:13:59 +0000707/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
Steve Naroff52a81c02008-09-03 18:15:37 +0000708///
709Action::StmtResult
710Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
711
712 // If this is the first return we've seen in the block, infer the type of
713 // the block from it.
714 if (CurBlock->ReturnType == 0) {
Steve Naroff503996b2008-09-16 22:25:10 +0000715 if (RetValExp) {
Steve Naroffe2b66a82008-09-24 22:26:48 +0000716 // Don't call UsualUnaryConversions(), since we don't want to do
717 // integer promotions here.
718 DefaultFunctionArrayConversion(RetValExp);
Steve Naroff52a81c02008-09-03 18:15:37 +0000719 CurBlock->ReturnType = RetValExp->getType().getTypePtr();
Steve Naroff503996b2008-09-16 22:25:10 +0000720 } else
Steve Naroff52a81c02008-09-03 18:15:37 +0000721 CurBlock->ReturnType = Context.VoidTy.getTypePtr();
722 return new ReturnStmt(ReturnLoc, RetValExp);
723 }
724
725 // Otherwise, verify that this result type matches the previous one. We are
726 // pickier with blocks than for normal functions because we don't have GCC
727 // compatibility to worry about here.
728 if (CurBlock->ReturnType->isVoidType()) {
729 if (RetValExp) {
730 Diag(ReturnLoc, diag::err_return_block_has_expr);
731 delete RetValExp;
732 RetValExp = 0;
733 }
734 return new ReturnStmt(ReturnLoc, RetValExp);
735 }
736
737 if (!RetValExp) {
738 Diag(ReturnLoc, diag::err_block_return_missing_expr);
739 return true;
740 }
741
742 // we have a non-void block with an expression, continue checking
743 QualType RetValType = RetValExp->getType();
744
745 // For now, restrict multiple return statements in a block to have
746 // strict compatible types only.
747 QualType BlockQT = QualType(CurBlock->ReturnType, 0);
748 if (Context.getCanonicalType(BlockQT).getTypePtr()
749 != Context.getCanonicalType(RetValType).getTypePtr()) {
750 DiagnoseAssignmentResult(Incompatible, ReturnLoc, BlockQT,
751 RetValType, RetValExp, "returning");
752 return true;
753 }
754
755 if (RetValExp) CheckReturnStackAddr(RetValExp, BlockQT, ReturnLoc);
756
757 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
758}
Chris Lattner4b009652007-07-25 00:24:17 +0000759
760Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000761Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Chris Lattner4b009652007-07-25 00:24:17 +0000762 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff52a81c02008-09-03 18:15:37 +0000763 if (CurBlock)
764 return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000765 QualType FnRetType =
766 getCurFunctionDecl() ? getCurFunctionDecl()->getResultType() :
767 getCurMethodDecl()->getResultType();
Chris Lattner4b009652007-07-25 00:24:17 +0000768
Chris Lattner005ed752008-01-04 18:04:52 +0000769 if (FnRetType->isVoidType()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000770 if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)
771 if (FunctionDecl *FD = getCurFunctionDecl())
772 Diag(ReturnLoc, diag::ext_return_has_expr)
773 << FD->getIdentifier() << RetValExp->getSourceRange();
774 else
775 Diag(ReturnLoc, diag::ext_return_has_expr)
776 << getCurMethodDecl()->getSelector().getName()
777 << RetValExp->getSourceRange();
Chris Lattner4b009652007-07-25 00:24:17 +0000778 }
Chris Lattner65cae292008-11-19 08:23:25 +0000779 return new ReturnStmt(ReturnLoc, RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000780 }
Chris Lattner65cae292008-11-19 08:23:25 +0000781
782 if (!RetValExp) {
783 unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4
784 // C99 6.8.6.4p1 (ext_ since GCC warns)
785 if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
786
787 if (FunctionDecl *FD = getCurFunctionDecl())
788 Diag(ReturnLoc, DiagID) << FD->getIdentifier();
789 else
790 Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getSelector().getName();
791 return new ReturnStmt(ReturnLoc, (Expr*)0);
792 }
793
Chris Lattner4b009652007-07-25 00:24:17 +0000794 // we have a non-void function with an expression, continue checking
Chris Lattner005ed752008-01-04 18:04:52 +0000795 QualType RetValType = RetValExp->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000796
797 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
798 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
799 // function return.
Douglas Gregor81c29152008-10-29 00:13:59 +0000800
801 // In C++ the return statement is handled via a copy initialization.
802 // the C version of which boils down to
803 // CheckSingleAssignmentConstraints.
804 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
Chris Lattner005ed752008-01-04 18:04:52 +0000805 return true;
Ted Kremenek45925ab2007-08-17 16:46:58 +0000806
Chris Lattner005ed752008-01-04 18:04:52 +0000807 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
Ted Kremenek45925ab2007-08-17 16:46:58 +0000808
Steve Naroffc32a20d2007-08-31 23:49:30 +0000809 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000810}
811
Anders Carlsson076c1112007-11-20 19:21:03 +0000812Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000813 bool IsSimple,
Anders Carlsson759f45d2007-11-23 23:12:25 +0000814 bool IsVolatile,
Anders Carlsson965d5202007-11-22 01:36:19 +0000815 unsigned NumOutputs,
816 unsigned NumInputs,
817 std::string *Names,
Chris Lattnerb052a832008-08-18 19:55:17 +0000818 ExprTy **constraints,
819 ExprTy **exprs,
Chris Lattner84418022008-07-23 06:46:56 +0000820 ExprTy *asmString,
Anders Carlsson965d5202007-11-22 01:36:19 +0000821 unsigned NumClobbers,
Chris Lattnerb052a832008-08-18 19:55:17 +0000822 ExprTy **clobbers,
Chris Lattner8a40a832007-10-29 04:04:16 +0000823 SourceLocation RParenLoc) {
Chris Lattnerb052a832008-08-18 19:55:17 +0000824 StringLiteral **Constraints = reinterpret_cast<StringLiteral**>(constraints);
825 Expr **Exprs = reinterpret_cast<Expr **>(exprs);
Chris Lattner84418022008-07-23 06:46:56 +0000826 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString);
Chris Lattnerb052a832008-08-18 19:55:17 +0000827 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers);
828
829 // The parser verifies that there is a string literal here.
Chris Lattner84418022008-07-23 06:46:56 +0000830 if (AsmString->isWide())
831 // FIXME: We currently leak memory here.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000832 return Diag(AsmString->getLocStart(), diag::err_asm_wide_character)
833 << AsmString->getSourceRange();
Chris Lattner84418022008-07-23 06:46:56 +0000834
835
Chris Lattnerb052a832008-08-18 19:55:17 +0000836 for (unsigned i = 0; i != NumOutputs; i++) {
837 StringLiteral *Literal = Constraints[i];
Chris Lattner84418022008-07-23 06:46:56 +0000838 if (Literal->isWide())
839 // FIXME: We currently leak memory here.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000840 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
841 << Literal->getSourceRange();
Chris Lattner84418022008-07-23 06:46:56 +0000842
Anders Carlsson4ce42302007-11-27 04:11:28 +0000843 std::string OutputConstraint(Literal->getStrData(),
844 Literal->getByteLength());
845
846 TargetInfo::ConstraintInfo info;
Chris Lattner84418022008-07-23 06:46:56 +0000847 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Anders Carlsson4ce42302007-11-27 04:11:28 +0000848 // FIXME: We currently leak memory here.
Chris Lattner84418022008-07-23 06:46:56 +0000849 return Diag(Literal->getLocStart(),
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000850 diag::err_asm_invalid_output_constraint) << OutputConstraint;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000851
852 // Check that the output exprs are valid lvalues.
Chris Lattnerb052a832008-08-18 19:55:17 +0000853 ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]);
Chris Lattner25168a52008-07-26 21:30:36 +0000854 Expr::isLvalueResult Result = OutputExpr->isLvalue(Context);
Anders Carlssonb4487a82007-11-23 19:43:50 +0000855 if (Result != Expr::LV_Valid) {
Anders Carlssonb4487a82007-11-23 19:43:50 +0000856 // FIXME: We currently leak memory here.
Chris Lattnerb052a832008-08-18 19:55:17 +0000857 return Diag(OutputExpr->getSubExpr()->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000858 diag::err_asm_invalid_lvalue_in_output)
859 << OutputExpr->getSubExpr()->getSourceRange();
Anders Carlssonb4487a82007-11-23 19:43:50 +0000860 }
861 }
862
Anders Carlssonb4487a82007-11-23 19:43:50 +0000863 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Chris Lattnerb052a832008-08-18 19:55:17 +0000864 StringLiteral *Literal = Constraints[i];
Chris Lattner84418022008-07-23 06:46:56 +0000865 if (Literal->isWide())
866 // FIXME: We currently leak memory here.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000867 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
868 << Literal->getSourceRange();
Anders Carlssonb4487a82007-11-23 19:43:50 +0000869
Anders Carlsson4ce42302007-11-27 04:11:28 +0000870 std::string InputConstraint(Literal->getStrData(),
871 Literal->getByteLength());
872
873 TargetInfo::ConstraintInfo info;
874 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
Chris Lattnerb052a832008-08-18 19:55:17 +0000875 NumOutputs, info)) {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000876 // FIXME: We currently leak memory here.
Chris Lattner84418022008-07-23 06:46:56 +0000877 return Diag(Literal->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000878 diag::err_asm_invalid_input_constraint) << InputConstraint;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000879 }
880
881 // Check that the input exprs aren't of type void.
Chris Lattnerb052a832008-08-18 19:55:17 +0000882 ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]);
Anders Carlssonb4487a82007-11-23 19:43:50 +0000883 if (InputExpr->getType()->isVoidType()) {
Anders Carlssonb4487a82007-11-23 19:43:50 +0000884
Anders Carlssonb4487a82007-11-23 19:43:50 +0000885 // FIXME: We currently leak memory here.
Chris Lattnerb052a832008-08-18 19:55:17 +0000886 return Diag(InputExpr->getSubExpr()->getLocStart(),
Chris Lattner8ba580c2008-11-19 05:08:23 +0000887 diag::err_asm_invalid_type_in_input)
888 << InputExpr->getType().getAsString() << InputConstraint
889 << InputExpr->getSubExpr()->getSourceRange();
Anders Carlssonb4487a82007-11-23 19:43:50 +0000890 }
891 }
Anders Carlsson965d5202007-11-22 01:36:19 +0000892
Anders Carlsson49dadd62007-11-25 00:25:21 +0000893 // Check that the clobbers are valid.
Chris Lattnerb052a832008-08-18 19:55:17 +0000894 for (unsigned i = 0; i != NumClobbers; i++) {
895 StringLiteral *Literal = Clobbers[i];
Chris Lattner84418022008-07-23 06:46:56 +0000896 if (Literal->isWide())
897 // FIXME: We currently leak memory here.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000898 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
899 << Literal->getSourceRange();
Anders Carlsson49dadd62007-11-25 00:25:21 +0000900
901 llvm::SmallString<16> Clobber(Literal->getStrData(),
902 Literal->getStrData() +
903 Literal->getByteLength());
904
Chris Lattner84418022008-07-23 06:46:56 +0000905 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Anders Carlsson49dadd62007-11-25 00:25:21 +0000906 // FIXME: We currently leak memory here.
Chris Lattner84418022008-07-23 06:46:56 +0000907 return Diag(Literal->getLocStart(),
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000908 diag::err_asm_unknown_register_name) << Clobber.c_str();
Anders Carlsson49dadd62007-11-25 00:25:21 +0000909 }
910
Chris Lattnerb052a832008-08-18 19:55:17 +0000911 return new AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
912 Names, Constraints, Exprs, AsmString, NumClobbers,
913 Clobbers, RParenLoc);
Chris Lattner8a40a832007-10-29 04:04:16 +0000914}
Fariborz Jahanian06798362007-11-01 23:59:59 +0000915
916Action::StmtResult
Ted Kremenek42730c52008-01-07 19:49:32 +0000917Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +0000918 SourceLocation RParen, StmtTy *Parm,
919 StmtTy *Body, StmtTy *CatchList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000920 ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen,
Fariborz Jahanian06798362007-11-01 23:59:59 +0000921 static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body),
922 static_cast<Stmt*>(CatchList));
923 return CatchList ? CatchList : CS;
924}
925
Fariborz Jahaniande3abf82007-11-02 00:18:53 +0000926Action::StmtResult
Ted Kremenek42730c52008-01-07 19:49:32 +0000927Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) {
928 ObjCAtFinallyStmt *FS = new ObjCAtFinallyStmt(AtLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +0000929 static_cast<Stmt*>(Body));
930 return FS;
931}
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +0000932
933Action::StmtResult
Ted Kremenek42730c52008-01-07 19:49:32 +0000934Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +0000935 StmtTy *Try, StmtTy *Catch, StmtTy *Finally) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000936 ObjCAtTryStmt *TS = new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try),
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +0000937 static_cast<Stmt*>(Catch),
938 static_cast<Stmt*>(Finally));
939 return TS;
940}
941
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000942Action::StmtResult
Ted Kremenek42730c52008-01-07 19:49:32 +0000943Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) {
944 ObjCAtThrowStmt *TS = new ObjCAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw));
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000945 return TS;
946}
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +0000947
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000948Action::StmtResult
949Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprTy *SynchExpr,
950 StmtTy *SynchBody) {
951 ObjCAtSynchronizedStmt *SS = new ObjCAtSynchronizedStmt(AtLoc,
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000952 static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody));
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000953 return SS;
954}