blob: 4ef0fda36123be9dc784d9a28c86d71db2e48b54 [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"
Ted Kremenek0c97e042009-02-07 01:47:29 +000019#include "clang/AST/ASTContext.h"
Anders Carlsson49dadd62007-11-25 00:25:21 +000020#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000023Sema::OwningStmtResult Sema::ActOnExprStmt(ExprArg expr) {
24 Expr *E = static_cast<Expr*>(expr.release());
Steve Naroff5cbb02f2007-09-16 14:56:35 +000025 assert(E && "ActOnExprStmt(): missing expression");
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000026
Chris Lattnere02e4402008-07-25 23:18:17 +000027 // 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.
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000030
Chris Lattnere02e4402008-07-25 23:18:17 +000031 // Same thing in for stmt first clause (when expr) and third clause.
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000032 return Owned(static_cast<Stmt*>(E));
Chris Lattner4b009652007-07-25 00:24:17 +000033}
34
35
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000036Sema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Ted Kremenek0c97e042009-02-07 01:47:29 +000037 return Owned(new (Context) NullStmt(SemiLoc));
Chris Lattner4b009652007-07-25 00:24:17 +000038}
39
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000040Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclTy *decl,
41 SourceLocation StartLoc,
42 SourceLocation EndLoc) {
Chris Lattnera4ff4272008-03-13 06:29:04 +000043 if (decl == 0)
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000044 return StmtError();
45
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000046 Decl *D = static_cast<Decl *>(decl);
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000047
Ted Kremenek1bc18e62008-10-07 23:09:49 +000048 // This is a temporary hack until we are always passing around
49 // DeclGroupRefs.
50 llvm::SmallVector<Decl*, 10> decls;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000051 while (D) {
52 Decl* d = D;
53 D = D->getNextDeclarator();
Ted Kremenek1bc18e62008-10-07 23:09:49 +000054 d->setNextDeclarator(0);
55 decls.push_back(d);
56 }
57
58 assert (!decls.empty());
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000059
Ted Kremenek1bc18e62008-10-07 23:09:49 +000060 if (decls.size() == 1) {
Douglas Gregorfb0cac22009-02-13 19:06:18 +000061 DeclGroupRef DG(*decls.begin());
Ted Kremenek0c97e042009-02-07 01:47:29 +000062 return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
Ted Kremenek1bc18e62008-10-07 23:09:49 +000063 }
Chris Lattnerdaac6942009-03-04 04:23:07 +000064
65 DeclGroupRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
66 return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
Chris Lattner4b009652007-07-25 00:24:17 +000067}
68
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000069Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +000070Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000071 MultiStmtArg elts, bool isStmtExpr) {
72 unsigned NumElts = elts.size();
73 Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
Chris Lattner3ea3b662007-08-27 04:29:41 +000074 // 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) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000088 Decl *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
Chris Lattnerd2c66552009-02-14 07:37:35 +000097 // Warn about expressions with unused results if they are non-void and if
98 // this not the last stmt in a stmt expr.
99 if (E->getType()->isVoidType() || (isStmtExpr && i == NumElts-1))
Chris Lattnerf2b07572007-08-31 21:49:55 +0000100 continue;
101
Chris Lattnerd2c66552009-02-14 07:37:35 +0000102 SourceLocation Loc;
103 SourceRange R1, R2;
104 if (!E->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerf2b07572007-08-31 21:49:55 +0000105 continue;
Chris Lattnerd2c66552009-02-14 07:37:35 +0000106
107 Diag(Loc, diag::warn_unused_expr) << R1 << R2;
Chris Lattnerf2b07572007-08-31 21:49:55 +0000108 }
Sebastian Redl76b9ddb2008-12-21 12:04:03 +0000109
Ted Kremenek0c97e042009-02-07 01:47:29 +0000110 return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
Chris Lattner4b009652007-07-25 00:24:17 +0000111}
112
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000113Action::OwningStmtResult
114Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval,
115 SourceLocation DotDotDotLoc, ExprArg rhsval,
Chris Lattnerdaac6942009-03-04 04:23:07 +0000116 SourceLocation ColonLoc) {
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000117 assert((lhsval.get() != 0) && "missing expression in case statement");
118
Chris Lattner4b009652007-07-25 00:24:17 +0000119 // C99 6.8.4.2p3: The expression shall be an integer constant.
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000120 // However, GCC allows any evaluatable integer expression.
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000121 Expr *LHSVal = static_cast<Expr*>(lhsval.get());
Anders Carlsson613314d2008-12-01 02:13:02 +0000122 if (VerifyIntegerConstantExpression(LHSVal))
Chris Lattnerdaac6942009-03-04 04:23:07 +0000123 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000124
125 // GCC extension: The expression shall be an integer constant.
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000126
127 Expr *RHSVal = static_cast<Expr*>(rhsval.get());
128 if (RHSVal && VerifyIntegerConstantExpression(RHSVal)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000129 RHSVal = 0; // Recover by just forgetting about it.
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000130 rhsval = 0;
131 }
132
Chris Lattner4b009652007-07-25 00:24:17 +0000133 if (SwitchStack.empty()) {
134 Diag(CaseLoc, diag::err_case_not_in_switch);
Chris Lattnerdaac6942009-03-04 04:23:07 +0000135 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
137
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000138 // Only now release the smart pointers.
139 lhsval.release();
140 rhsval.release();
Chris Lattnerdaac6942009-03-04 04:23:07 +0000141 CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000142 SwitchStack.back()->addSwitchCase(CS);
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000143 return Owned(CS);
Chris Lattner4b009652007-07-25 00:24:17 +0000144}
145
Chris Lattnerdaac6942009-03-04 04:23:07 +0000146/// ActOnCaseStmtBody - This installs a statement as the body of a case.
147void Sema::ActOnCaseStmtBody(StmtTy *caseStmt, StmtArg subStmt) {
148 CaseStmt *CS = static_cast<CaseStmt*>(caseStmt);
149 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
150 CS->setSubStmt(SubStmt);
151}
152
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000153Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000154Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000155 StmtArg subStmt, Scope *CurScope) {
156 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
157
Chris Lattner4b009652007-07-25 00:24:17 +0000158 if (SwitchStack.empty()) {
159 Diag(DefaultLoc, diag::err_default_not_in_switch);
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000160 return Owned(SubStmt);
Chris Lattner4b009652007-07-25 00:24:17 +0000161 }
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000162
Ted Kremenek0c97e042009-02-07 01:47:29 +0000163 DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner4b009652007-07-25 00:24:17 +0000164 SwitchStack.back()->addSwitchCase(DS);
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000165 return Owned(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000166}
167
Sebastian Redl2437ec62009-01-11 00:38:46 +0000168Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000169Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Sebastian Redl2437ec62009-01-11 00:38:46 +0000170 SourceLocation ColonLoc, StmtArg subStmt) {
171 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
Steve Naroff313c4162009-02-28 16:48:43 +0000172 // Look up the record for this label identifier.
Steve Naroffff9ecaf2009-03-13 16:03:38 +0000173 LabelStmt *&LabelDecl = CurBlock ? CurBlock->LabelMap[II] : LabelMap[II];
Steve Naroff313c4162009-02-28 16:48:43 +0000174
Chris Lattner4b009652007-07-25 00:24:17 +0000175 // If not forward referenced or defined already, just create a new LabelStmt.
Steve Naroffb88d81c2009-03-13 15:38:40 +0000176 if (LabelDecl == 0)
177 return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt));
Sebastian Redl2437ec62009-01-11 00:38:46 +0000178
Chris Lattner4b009652007-07-25 00:24:17 +0000179 assert(LabelDecl->getID() == II && "Label mismatch!");
Sebastian Redl2437ec62009-01-11 00:38:46 +0000180
Chris Lattner4b009652007-07-25 00:24:17 +0000181 // Otherwise, this label was either forward reference or multiply defined. If
182 // multiply defined, reject it now.
183 if (LabelDecl->getSubStmt()) {
Chris Lattnerb1753422008-11-23 21:45:46 +0000184 Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
Chris Lattner1336cab2008-11-23 23:12:31 +0000185 Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
Sebastian Redl2437ec62009-01-11 00:38:46 +0000186 return Owned(SubStmt);
Chris Lattner4b009652007-07-25 00:24:17 +0000187 }
Sebastian Redl2437ec62009-01-11 00:38:46 +0000188
Chris Lattner4b009652007-07-25 00:24:17 +0000189 // Otherwise, this label was forward declared, and we just found its real
190 // definition. Fill in the forward definition and return it.
191 LabelDecl->setIdentLoc(IdentLoc);
192 LabelDecl->setSubStmt(SubStmt);
Sebastian Redl2437ec62009-01-11 00:38:46 +0000193 return Owned(LabelDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000194}
195
Sebastian Redl2437ec62009-01-11 00:38:46 +0000196Action::OwningStmtResult
197Sema::ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal,
198 StmtArg ThenVal, SourceLocation ElseLoc,
199 StmtArg ElseVal) {
200 Expr *condExpr = (Expr *)CondVal.release();
201
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000202 assert(condExpr && "ActOnIfStmt(): missing expression");
Sebastian Redl2437ec62009-01-11 00:38:46 +0000203
Chris Lattner4b009652007-07-25 00:24:17 +0000204 DefaultFunctionArrayConversion(condExpr);
Sebastian Redl2437ec62009-01-11 00:38:46 +0000205 // Take ownership again until we're past the error checking.
206 CondVal = condExpr;
Chris Lattner4b009652007-07-25 00:24:17 +0000207 QualType condType = condExpr->getType();
Sebastian Redl2437ec62009-01-11 00:38:46 +0000208
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000209 if (getLangOptions().CPlusPlus) {
210 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redl2437ec62009-01-11 00:38:46 +0000211 return StmtError();
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000212 } else if (!condType->isScalarType()) // C99 6.8.4.1p1
Sebastian Redl2437ec62009-01-11 00:38:46 +0000213 return StmtError(Diag(IfLoc, diag::err_typecheck_statement_requires_scalar)
214 << condType << condExpr->getSourceRange());
215
216 Stmt *thenStmt = (Stmt *)ThenVal.release();
Chris Lattner4b009652007-07-25 00:24:17 +0000217
Anders Carlsson663733e2007-10-10 20:50:11 +0000218 // Warn if the if block has a null body without an else value.
219 // this helps prevent bugs due to typos, such as
220 // if (condition);
221 // do_stuff();
Sebastian Redl2437ec62009-01-11 00:38:46 +0000222 if (!ElseVal.get()) {
Anders Carlsson663733e2007-10-10 20:50:11 +0000223 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
224 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
225 }
226
Sebastian Redl2437ec62009-01-11 00:38:46 +0000227 CondVal.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000228 return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt,
229 (Stmt*)ElseVal.release()));
Chris Lattner4b009652007-07-25 00:24:17 +0000230}
231
Sebastian Redl2437ec62009-01-11 00:38:46 +0000232Action::OwningStmtResult
233Sema::ActOnStartOfSwitchStmt(ExprArg cond) {
234 Expr *Cond = static_cast<Expr*>(cond.release());
235
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000236 if (getLangOptions().CPlusPlus) {
237 // C++ 6.4.2.p2:
238 // The condition shall be of integral type, enumeration type, or of a class
239 // type for which a single conversion function to integral or enumeration
240 // type exists (12.3). If the condition is of class type, the condition is
241 // converted by calling that conversion function, and the result of the
242 // conversion is used in place of the original condition for the remainder
243 // of this section. Integral promotions are performed.
244
245 QualType Ty = Cond->getType();
246
247 // FIXME: Handle class types.
248
249 // If the type is wrong a diagnostic will be emitted later at
250 // ActOnFinishSwitchStmt.
251 if (Ty->isIntegralType() || Ty->isEnumeralType()) {
252 // Integral promotions are performed.
253 // FIXME: Integral promotions for C++ are not complete.
254 UsualUnaryConversions(Cond);
255 }
256 } else {
257 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
258 UsualUnaryConversions(Cond);
259 }
Sebastian Redl2437ec62009-01-11 00:38:46 +0000260
Ted Kremenek0c97e042009-02-07 01:47:29 +0000261 SwitchStmt *SS = new (Context) SwitchStmt(Cond);
Chris Lattner4b009652007-07-25 00:24:17 +0000262 SwitchStack.push_back(SS);
Sebastian Redl2437ec62009-01-11 00:38:46 +0000263 return Owned(SS);
Chris Lattner4b009652007-07-25 00:24:17 +0000264}
265
Chris Lattner3429a812007-08-23 05:46:52 +0000266/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
267/// the specified width and sign. If an overflow occurs, detect it and emit
268/// the specified diagnostic.
269void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
270 unsigned NewWidth, bool NewSign,
271 SourceLocation Loc,
272 unsigned DiagID) {
273 // Perform a conversion to the promoted condition type if needed.
274 if (NewWidth > Val.getBitWidth()) {
275 // If this is an extension, just do it.
276 llvm::APSInt OldVal(Val);
277 Val.extend(NewWidth);
278
279 // If the input was signed and negative and the output is unsigned,
280 // warn.
281 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
Chris Lattner77d52da2008-11-20 06:06:08 +0000282 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000283
284 Val.setIsSigned(NewSign);
285 } else if (NewWidth < Val.getBitWidth()) {
286 // If this is a truncation, check for overflow.
287 llvm::APSInt ConvVal(Val);
288 ConvVal.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000289 ConvVal.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000290 ConvVal.extend(Val.getBitWidth());
Chris Lattner5c039602007-08-23 22:08:35 +0000291 ConvVal.setIsSigned(Val.isSigned());
Chris Lattner3429a812007-08-23 05:46:52 +0000292 if (ConvVal != Val)
Chris Lattner77d52da2008-11-20 06:06:08 +0000293 Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000294
295 // Regardless of whether a diagnostic was emitted, really do the
296 // truncation.
297 Val.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000298 Val.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000299 } else if (NewSign != Val.isSigned()) {
300 // Convert the sign to match the sign of the condition. This can cause
301 // overflow as well: unsigned(INTMIN)
302 llvm::APSInt OldVal(Val);
303 Val.setIsSigned(NewSign);
304
305 if (Val.isNegative()) // Sign bit changes meaning.
Chris Lattner77d52da2008-11-20 06:06:08 +0000306 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000307 }
308}
309
Chris Lattner0ab833c2007-08-23 18:29:20 +0000310namespace {
311 struct CaseCompareFunctor {
312 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
313 const llvm::APSInt &RHS) {
314 return LHS.first < RHS;
315 }
Chris Lattner2157f272007-09-03 18:31:57 +0000316 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
317 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
318 return LHS.first < RHS.first;
319 }
Chris Lattner0ab833c2007-08-23 18:29:20 +0000320 bool operator()(const llvm::APSInt &LHS,
321 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
322 return LHS < RHS.first;
323 }
324 };
325}
326
Chris Lattner766afb82007-09-21 18:15:22 +0000327/// CmpCaseVals - Comparison predicate for sorting case values.
328///
329static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
330 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
331 if (lhs.first < rhs.first)
332 return true;
333
334 if (lhs.first == rhs.first &&
335 lhs.second->getCaseLoc().getRawEncoding()
336 < rhs.second->getCaseLoc().getRawEncoding())
337 return true;
338 return false;
339}
340
Sebastian Redl2437ec62009-01-11 00:38:46 +0000341Action::OwningStmtResult
342Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
343 StmtArg Body) {
344 Stmt *BodyStmt = (Stmt*)Body.release();
345
Chris Lattner4b009652007-07-25 00:24:17 +0000346 SwitchStmt *SS = SwitchStack.back();
Sebastian Redl2437ec62009-01-11 00:38:46 +0000347 assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!");
348
Steve Naroffa610eab2007-09-01 21:08:38 +0000349 SS->setBody(BodyStmt, SwitchLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000350 SwitchStack.pop_back();
351
Chris Lattner3429a812007-08-23 05:46:52 +0000352 Expr *CondExpr = SS->getCond();
353 QualType CondType = CondExpr->getType();
Sebastian Redl2437ec62009-01-11 00:38:46 +0000354
Chris Lattner3429a812007-08-23 05:46:52 +0000355 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner77d52da2008-11-20 06:06:08 +0000356 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000357 << CondType << CondExpr->getSourceRange();
Sebastian Redl2437ec62009-01-11 00:38:46 +0000358 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000359 }
Sebastian Redl2437ec62009-01-11 00:38:46 +0000360
Chris Lattner3429a812007-08-23 05:46:52 +0000361 // Get the bitwidth of the switched-on value before promotions. We must
362 // convert the integer case values to this width before comparison.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000363 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattner3429a812007-08-23 05:46:52 +0000364 bool CondIsSigned = CondType->isSignedIntegerType();
365
366 // Accumulate all of the case values in a vector so that we can sort them
367 // and detect duplicates. This vector contains the APInt for the case after
368 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000369 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
370 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000371
372 // Keep track of any GNU case ranges we see. The APSInt is the low value.
373 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
374
375 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000376
Chris Lattner1a4066d2007-08-23 06:23:56 +0000377 bool CaseListIsErroneous = false;
378
379 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000380 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000381
Chris Lattner4b009652007-07-25 00:24:17 +0000382 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000383 if (TheDefaultStmt) {
384 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
Chris Lattner1336cab2008-11-23 23:12:31 +0000385 Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
Sebastian Redl2437ec62009-01-11 00:38:46 +0000386
Chris Lattner3429a812007-08-23 05:46:52 +0000387 // FIXME: Remove the default statement from the switch block so that
388 // we'll return a valid AST. This requires recursing down the
389 // AST and finding it, not something we are set up to do right now. For
390 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000391 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000392 }
Chris Lattner3429a812007-08-23 05:46:52 +0000393 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000394
Chris Lattner3429a812007-08-23 05:46:52 +0000395 } else {
396 CaseStmt *CS = cast<CaseStmt>(SC);
397
398 // We already verified that the expression has a i-c-e value (C99
399 // 6.8.4.2p3) - get that value now.
Chris Lattnere992d6c2008-01-16 19:17:22 +0000400 Expr *Lo = CS->getLHS();
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000401 llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
Chris Lattner3429a812007-08-23 05:46:52 +0000402
403 // Convert the value to the same width/sign as the condition.
404 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
405 CS->getLHS()->getLocStart(),
406 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000407
Chris Lattnere992d6c2008-01-16 19:17:22 +0000408 // If the LHS is not the same type as the condition, insert an implicit
409 // cast.
410 ImpCastExprToType(Lo, CondType);
411 CS->setLHS(Lo);
412
Chris Lattner1a4066d2007-08-23 06:23:56 +0000413 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000414 if (CS->getRHS())
415 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000416 else
417 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000418 }
419 }
420
Chris Lattner1a4066d2007-08-23 06:23:56 +0000421 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner766afb82007-09-21 18:15:22 +0000422 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattner3429a812007-08-23 05:46:52 +0000423
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000424 if (!CaseVals.empty()) {
425 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
426 if (CaseVals[i].first == CaseVals[i+1].first) {
427 // If we have a duplicate, report it.
428 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000429 diag::err_duplicate_case) << CaseVals[i].first.toString(10);
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000430 Diag(CaseVals[i].second->getLHS()->getLocStart(),
Chris Lattner1336cab2008-11-23 23:12:31 +0000431 diag::note_duplicate_case_prev);
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000432 // FIXME: We really want to remove the bogus case stmt from the substmt,
433 // but we have no way to do this right now.
434 CaseListIsErroneous = true;
435 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000436 }
437 }
Chris Lattner3429a812007-08-23 05:46:52 +0000438
Chris Lattner1a4066d2007-08-23 06:23:56 +0000439 // Detect duplicate case ranges, which usually don't exist at all in the first
440 // place.
441 if (!CaseRanges.empty()) {
442 // Sort all the case ranges by their low value so we can easily detect
443 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000444 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000445
446 // Scan the ranges, computing the high values and removing empty ranges.
447 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000448 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000449 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnere992d6c2008-01-16 19:17:22 +0000450 Expr *Hi = CR->getRHS();
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000451 llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
Chris Lattner1a4066d2007-08-23 06:23:56 +0000452
453 // Convert the value to the same width/sign as the condition.
454 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
455 CR->getRHS()->getLocStart(),
456 diag::warn_case_value_overflow);
457
Chris Lattnere992d6c2008-01-16 19:17:22 +0000458 // If the LHS is not the same type as the condition, insert an implicit
459 // cast.
460 ImpCastExprToType(Hi, CondType);
461 CR->setRHS(Hi);
462
Chris Lattner7443e0f2007-08-23 17:48:14 +0000463 // If the low value is bigger than the high value, the case is empty.
464 if (CaseRanges[i].first > HiVal) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000465 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
466 << SourceRange(CR->getLHS()->getLocStart(),
467 CR->getRHS()->getLocEnd());
Chris Lattner7443e0f2007-08-23 17:48:14 +0000468 CaseRanges.erase(CaseRanges.begin()+i);
469 --i, --e;
470 continue;
471 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000472 HiVals.push_back(HiVal);
473 }
474
475 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000476 // ranges. Since the range list is sorted, we only need to compare case
477 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000478 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000479 llvm::APSInt &CRLo = CaseRanges[i].first;
480 llvm::APSInt &CRHi = HiVals[i];
481 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000482
Chris Lattner0ab833c2007-08-23 18:29:20 +0000483 // Check to see whether the case range overlaps with any singleton cases.
484 CaseStmt *OverlapStmt = 0;
485 llvm::APSInt OverlapVal(32);
486
487 // Find the smallest value >= the lower bound. If I is in the case range,
488 // then we have overlap.
489 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
490 CaseVals.end(), CRLo,
491 CaseCompareFunctor());
492 if (I != CaseVals.end() && I->first < CRHi) {
493 OverlapVal = I->first; // Found overlap with scalar.
494 OverlapStmt = I->second;
495 }
496
497 // Find the smallest value bigger than the upper bound.
498 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
499 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
500 OverlapVal = (I-1)->first; // Found overlap with scalar.
501 OverlapStmt = (I-1)->second;
502 }
503
504 // Check to see if this case stmt overlaps with the subsequent case range.
505 if (i && CRLo <= HiVals[i-1]) {
506 OverlapVal = HiVals[i-1]; // Found overlap with range.
507 OverlapStmt = CaseRanges[i-1].second;
508 }
509
510 if (OverlapStmt) {
511 // If we have a duplicate, report it.
Chris Lattner77d52da2008-11-20 06:06:08 +0000512 Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
513 << OverlapVal.toString(10);
Chris Lattner0ab833c2007-08-23 18:29:20 +0000514 Diag(OverlapStmt->getLHS()->getLocStart(),
Chris Lattner1336cab2008-11-23 23:12:31 +0000515 diag::note_duplicate_case_prev);
Chris Lattner0ab833c2007-08-23 18:29:20 +0000516 // FIXME: We really want to remove the bogus case stmt from the substmt,
517 // but we have no way to do this right now.
518 CaseListIsErroneous = true;
519 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000520 }
521 }
Chris Lattner3429a812007-08-23 05:46:52 +0000522
Chris Lattner1a4066d2007-08-23 06:23:56 +0000523 // FIXME: If the case list was broken is some way, we don't have a good system
524 // to patch it up. Instead, just return the whole substmt as broken.
525 if (CaseListIsErroneous)
Sebastian Redl2437ec62009-01-11 00:38:46 +0000526 return StmtError();
527
528 Switch.release();
529 return Owned(SS);
Chris Lattner4b009652007-07-25 00:24:17 +0000530}
531
Sebastian Redl19c74d32009-01-16 23:28:06 +0000532Action::OwningStmtResult
533Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, StmtArg Body) {
534 Expr *condExpr = (Expr *)Cond.release();
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000535 assert(condExpr && "ActOnWhileStmt(): missing expression");
Sebastian Redl19c74d32009-01-16 23:28:06 +0000536
Chris Lattner4b009652007-07-25 00:24:17 +0000537 DefaultFunctionArrayConversion(condExpr);
Sebastian Redl19c74d32009-01-16 23:28:06 +0000538 Cond = condExpr;
Chris Lattner4b009652007-07-25 00:24:17 +0000539 QualType condType = condExpr->getType();
Sebastian Redl19c74d32009-01-16 23:28:06 +0000540
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000541 if (getLangOptions().CPlusPlus) {
542 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redl19c74d32009-01-16 23:28:06 +0000543 return StmtError();
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000544 } else if (!condType->isScalarType()) // C99 6.8.5p2
Sebastian Redl19c74d32009-01-16 23:28:06 +0000545 return StmtError(Diag(WhileLoc,
546 diag::err_typecheck_statement_requires_scalar)
547 << condType << condExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000548
Sebastian Redl19c74d32009-01-16 23:28:06 +0000549 Cond.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000550 return Owned(new (Context) WhileStmt(condExpr, (Stmt*)Body.release(),
551 WhileLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000552}
553
Sebastian Redl19c74d32009-01-16 23:28:06 +0000554Action::OwningStmtResult
555Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body,
556 SourceLocation WhileLoc, ExprArg Cond) {
557 Expr *condExpr = (Expr *)Cond.release();
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000558 assert(condExpr && "ActOnDoStmt(): missing expression");
Sebastian Redl19c74d32009-01-16 23:28:06 +0000559
Chris Lattner4b009652007-07-25 00:24:17 +0000560 DefaultFunctionArrayConversion(condExpr);
Sebastian Redl19c74d32009-01-16 23:28:06 +0000561 Cond = condExpr;
Chris Lattner4b009652007-07-25 00:24:17 +0000562 QualType condType = condExpr->getType();
Sebastian Redl19c74d32009-01-16 23:28:06 +0000563
Argiris Kirtzidisc362d382008-09-11 05:16:22 +0000564 if (getLangOptions().CPlusPlus) {
565 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redl19c74d32009-01-16 23:28:06 +0000566 return StmtError();
Argiris Kirtzidisc362d382008-09-11 05:16:22 +0000567 } else if (!condType->isScalarType()) // C99 6.8.5p2
Sebastian Redl19c74d32009-01-16 23:28:06 +0000568 return StmtError(Diag(DoLoc, diag::err_typecheck_statement_requires_scalar)
569 << condType << condExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000570
Sebastian Redl19c74d32009-01-16 23:28:06 +0000571 Cond.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000572 return Owned(new (Context) DoStmt((Stmt*)Body.release(), condExpr, DoLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000573}
574
Sebastian Redl19c74d32009-01-16 23:28:06 +0000575Action::OwningStmtResult
576Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
577 StmtArg first, ExprArg second, ExprArg third,
578 SourceLocation RParenLoc, StmtArg body) {
579 Stmt *First = static_cast<Stmt*>(first.get());
580 Expr *Second = static_cast<Expr*>(second.get());
581 Expr *Third = static_cast<Expr*>(third.get());
582 Stmt *Body = static_cast<Stmt*>(body.get());
583
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000584 if (!getLangOptions().CPlusPlus) {
585 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000586 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
587 // declare identifiers for objects having storage class 'auto' or
588 // 'register'.
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000589 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
590 DI!=DE; ++DI) {
591 VarDecl *VD = dyn_cast<VarDecl>(*DI);
592 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
593 VD = 0;
594 if (VD == 0)
595 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
596 // FIXME: mark decl erroneous!
597 }
Chris Lattner06611052007-08-28 05:03:08 +0000598 }
Chris Lattner4b009652007-07-25 00:24:17 +0000599 }
600 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000601 DefaultFunctionArrayConversion(Second);
602 QualType SecondType = Second->getType();
Sebastian Redl19c74d32009-01-16 23:28:06 +0000603
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000604 if (getLangOptions().CPlusPlus) {
605 if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
Sebastian Redl19c74d32009-01-16 23:28:06 +0000606 return StmtError();
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000607 } else if (!SecondType->isScalarType()) // C99 6.8.5p2
Sebastian Redl19c74d32009-01-16 23:28:06 +0000608 return StmtError(Diag(ForLoc,
609 diag::err_typecheck_statement_requires_scalar)
610 << SecondType << Second->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000611 }
Sebastian Redl19c74d32009-01-16 23:28:06 +0000612 first.release();
613 second.release();
614 third.release();
615 body.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000616 return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000617}
618
Sebastian Redl19c74d32009-01-16 23:28:06 +0000619Action::OwningStmtResult
620Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
621 SourceLocation LParenLoc,
622 StmtArg first, ExprArg second,
623 SourceLocation RParenLoc, StmtArg body) {
624 Stmt *First = static_cast<Stmt*>(first.get());
625 Expr *Second = static_cast<Expr*>(second.get());
626 Stmt *Body = static_cast<Stmt*>(body.get());
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +0000627 if (First) {
628 QualType FirstType;
629 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
Ted Kremenek779e1c22008-10-06 20:58:11 +0000630 if (!DS->hasSolitaryDecl())
Sebastian Redl19c74d32009-01-16 23:28:06 +0000631 return StmtError(Diag((*DS->decl_begin())->getLocation(),
632 diag::err_toomany_element_decls));
633
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000634 Decl *D = DS->getSolitaryDecl();
Ted Kremenek779e1c22008-10-06 20:58:11 +0000635 FirstType = cast<ValueDecl>(D)->getType();
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000636 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
637 // declare identifiers for objects having storage class 'auto' or
638 // 'register'.
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000639 VarDecl *VD = cast<VarDecl>(D);
640 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
Sebastian Redl19c74d32009-01-16 23:28:06 +0000641 return StmtError(Diag(VD->getLocation(),
642 diag::err_non_variable_decl_in_for));
Anders Carlsson8d0bcb02008-08-25 18:16:36 +0000643 } else {
Chris Lattner1e3eedb2009-03-13 17:38:01 +0000644 if (cast<Expr>(First)->isLvalue(Context) != Expr::LV_Valid)
Sebastian Redl19c74d32009-01-16 23:28:06 +0000645 return StmtError(Diag(First->getLocStart(),
646 diag::err_selector_element_not_lvalue)
647 << First->getSourceRange());
648
649 FirstType = static_cast<Expr*>(First)->getType();
Anders Carlsson8d0bcb02008-08-25 18:16:36 +0000650 }
Ted Kremenek118930e2008-07-24 23:58:27 +0000651 if (!Context.isObjCObjectPointerType(FirstType))
Chris Lattner9d2cf082008-11-19 05:27:50 +0000652 Diag(ForLoc, diag::err_selector_element_type)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000653 << FirstType << First->getSourceRange();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000654 }
655 if (Second) {
656 DefaultFunctionArrayConversion(Second);
657 QualType SecondType = Second->getType();
Ted Kremenek118930e2008-07-24 23:58:27 +0000658 if (!Context.isObjCObjectPointerType(SecondType))
Chris Lattner9d2cf082008-11-19 05:27:50 +0000659 Diag(ForLoc, diag::err_collection_expr_type)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000660 << SecondType << Second->getSourceRange();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000661 }
Sebastian Redl19c74d32009-01-16 23:28:06 +0000662 first.release();
663 second.release();
664 body.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000665 return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
666 ForLoc, RParenLoc));
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000667}
Chris Lattner4b009652007-07-25 00:24:17 +0000668
Sebastian Redl539eb572009-01-18 13:19:59 +0000669Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000670Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000671 IdentifierInfo *LabelII) {
Steve Naroff52a81c02008-09-03 18:15:37 +0000672 // If we are in a block, reject all gotos for now.
673 if (CurBlock)
Sebastian Redl539eb572009-01-18 13:19:59 +0000674 return StmtError(Diag(GotoLoc, diag::err_goto_in_block));
Steve Naroff52a81c02008-09-03 18:15:37 +0000675
Chris Lattner4b009652007-07-25 00:24:17 +0000676 // Look up the record for this label identifier.
Steve Naroffff9ecaf2009-03-13 16:03:38 +0000677 LabelStmt *&LabelDecl = CurBlock ? CurBlock->LabelMap[LabelII] :
678 LabelMap[LabelII];
Chris Lattner4b009652007-07-25 00:24:17 +0000679
Steve Naroffb88d81c2009-03-13 15:38:40 +0000680 // If we haven't seen this label yet, create a forward reference.
681 if (LabelDecl == 0)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000682 LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
Sebastian Redl539eb572009-01-18 13:19:59 +0000683
Ted Kremenek0c97e042009-02-07 01:47:29 +0000684 return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000685}
686
Sebastian Redl539eb572009-01-18 13:19:59 +0000687Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000688Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Sebastian Redl539eb572009-01-18 13:19:59 +0000689 ExprArg DestExp) {
Chris Lattner4b009652007-07-25 00:24:17 +0000690 // FIXME: Verify that the operand is convertible to void*.
Eli Friedman4b99aba2009-03-26 00:18:06 +0000691 // Convert operand to void*
692 Expr* E = (Expr*)DestExp.release();
693 ImpCastExprToType(E, Context.VoidPtrTy);
694 return Owned(new (Context) IndirectGotoStmt(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000695}
696
Sebastian Redl539eb572009-01-18 13:19:59 +0000697Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000698Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000699 Scope *S = CurScope->getContinueParent();
700 if (!S) {
701 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
Sebastian Redl539eb572009-01-18 13:19:59 +0000702 return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
Chris Lattner4b009652007-07-25 00:24:17 +0000703 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000704
Ted Kremenek0c97e042009-02-07 01:47:29 +0000705 return Owned(new (Context) ContinueStmt(ContinueLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000706}
707
Sebastian Redl539eb572009-01-18 13:19:59 +0000708Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000709Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000710 Scope *S = CurScope->getBreakParent();
711 if (!S) {
712 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
Sebastian Redl539eb572009-01-18 13:19:59 +0000713 return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
Chris Lattner4b009652007-07-25 00:24:17 +0000714 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000715
Ted Kremenek0c97e042009-02-07 01:47:29 +0000716 return Owned(new (Context) BreakStmt(BreakLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000717}
718
Douglas Gregor81c29152008-10-29 00:13:59 +0000719/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
Steve Naroff52a81c02008-09-03 18:15:37 +0000720///
Sebastian Redl539eb572009-01-18 13:19:59 +0000721Action::OwningStmtResult
Steve Naroff52a81c02008-09-03 18:15:37 +0000722Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
Sebastian Redl539eb572009-01-18 13:19:59 +0000723
Steve Naroff52a81c02008-09-03 18:15:37 +0000724 // If this is the first return we've seen in the block, infer the type of
725 // the block from it.
726 if (CurBlock->ReturnType == 0) {
Steve Naroff503996b2008-09-16 22:25:10 +0000727 if (RetValExp) {
Steve Naroffe2b66a82008-09-24 22:26:48 +0000728 // Don't call UsualUnaryConversions(), since we don't want to do
729 // integer promotions here.
730 DefaultFunctionArrayConversion(RetValExp);
Steve Naroff52a81c02008-09-03 18:15:37 +0000731 CurBlock->ReturnType = RetValExp->getType().getTypePtr();
Steve Naroff503996b2008-09-16 22:25:10 +0000732 } else
Steve Naroff52a81c02008-09-03 18:15:37 +0000733 CurBlock->ReturnType = Context.VoidTy.getTypePtr();
Steve Naroff52a81c02008-09-03 18:15:37 +0000734 }
Mike Stumpc1fddff2009-02-04 22:31:32 +0000735 QualType FnRetType = QualType(CurBlock->ReturnType, 0);
Sebastian Redl539eb572009-01-18 13:19:59 +0000736
Steve Naroff52a81c02008-09-03 18:15:37 +0000737 // Otherwise, verify that this result type matches the previous one. We are
738 // pickier with blocks than for normal functions because we don't have GCC
739 // compatibility to worry about here.
740 if (CurBlock->ReturnType->isVoidType()) {
741 if (RetValExp) {
742 Diag(ReturnLoc, diag::err_return_block_has_expr);
Ted Kremenek0c97e042009-02-07 01:47:29 +0000743 RetValExp->Destroy(Context);
Steve Naroff52a81c02008-09-03 18:15:37 +0000744 RetValExp = 0;
745 }
Ted Kremenek0c97e042009-02-07 01:47:29 +0000746 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Steve Naroff52a81c02008-09-03 18:15:37 +0000747 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000748
749 if (!RetValExp)
750 return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
751
Mike Stumpc1fddff2009-02-04 22:31:32 +0000752 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
753 // we have a non-void block with an expression, continue checking
754 QualType RetValType = RetValExp->getType();
Sebastian Redl539eb572009-01-18 13:19:59 +0000755
Mike Stumpc1fddff2009-02-04 22:31:32 +0000756 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
757 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
758 // function return.
759
760 // In C++ the return statement is handled via a copy initialization.
761 // the C version of which boils down to CheckSingleAssignmentConstraints.
762 // FIXME: Leaks RetValExp.
763 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
764 return StmtError();
765
766 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
Steve Naroff52a81c02008-09-03 18:15:37 +0000767 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000768
Ted Kremenek0c97e042009-02-07 01:47:29 +0000769 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Steve Naroff52a81c02008-09-03 18:15:37 +0000770}
Chris Lattner4b009652007-07-25 00:24:17 +0000771
Sebastian Redl539eb572009-01-18 13:19:59 +0000772Action::OwningStmtResult
773Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) {
774 Expr *RetValExp = static_cast<Expr *>(rex.release());
Steve Naroff52a81c02008-09-03 18:15:37 +0000775 if (CurBlock)
776 return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
Sebastian Redl539eb572009-01-18 13:19:59 +0000777
Chris Lattnere5cb5862008-12-04 23:50:19 +0000778 QualType FnRetType;
779 if (FunctionDecl *FD = getCurFunctionDecl())
780 FnRetType = FD->getResultType();
Steve Naroff5fc6a6e2009-03-03 00:45:38 +0000781 else if (ObjCMethodDecl *MD = getCurMethodDecl())
782 FnRetType = MD->getResultType();
783 else // If we don't have a function/method context, bail.
784 return StmtError();
785
Chris Lattner005ed752008-01-04 18:04:52 +0000786 if (FnRetType->isVoidType()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000787 if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)
Chris Lattner6ed167c2008-12-18 02:01:17 +0000788 unsigned D = diag::ext_return_has_expr;
789 if (RetValExp->getType()->isVoidType())
790 D = diag::ext_return_has_void_expr;
Sebastian Redl539eb572009-01-18 13:19:59 +0000791
Chris Lattnerd1a05392008-12-18 02:03:48 +0000792 // return (some void expression); is legal in C++.
793 if (D != diag::ext_return_has_void_expr ||
794 !getLangOptions().CPlusPlus) {
795 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
796 Diag(ReturnLoc, D)
797 << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
798 << RetValExp->getSourceRange();
799 }
Chris Lattner4b009652007-07-25 00:24:17 +0000800 }
Ted Kremenek0c97e042009-02-07 01:47:29 +0000801 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Chris Lattner4b009652007-07-25 00:24:17 +0000802 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000803
Chris Lattner65cae292008-11-19 08:23:25 +0000804 if (!RetValExp) {
805 unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4
806 // C99 6.8.6.4p1 (ext_ since GCC warns)
807 if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
808
809 if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattnerb1753422008-11-23 21:45:46 +0000810 Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
Chris Lattner65cae292008-11-19 08:23:25 +0000811 else
Chris Lattnerb1753422008-11-23 21:45:46 +0000812 Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
Ted Kremenek0c97e042009-02-07 01:47:29 +0000813 return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0));
Chris Lattner65cae292008-11-19 08:23:25 +0000814 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000815
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000816 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
817 // we have a non-void function with an expression, continue checking
818 QualType RetValType = RetValExp->getType();
Sebastian Redl539eb572009-01-18 13:19:59 +0000819
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000820 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
821 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
Sebastian Redl539eb572009-01-18 13:19:59 +0000822 // function return.
823
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000824 // In C++ the return statement is handled via a copy initialization.
Sebastian Redl539eb572009-01-18 13:19:59 +0000825 // the C version of which boils down to CheckSingleAssignmentConstraints.
826 // FIXME: Leaks RetValExp.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000827 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
Sebastian Redl539eb572009-01-18 13:19:59 +0000828 return StmtError();
829
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000830 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
831 }
832
Ted Kremenek0c97e042009-02-07 01:47:29 +0000833 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Chris Lattner4b009652007-07-25 00:24:17 +0000834}
835
Chris Lattner1e3eedb2009-03-13 17:38:01 +0000836/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
837/// ignore "noop" casts in places where an lvalue is required by an inline asm.
838/// We emulate this behavior when -fheinous-gnu-extensions is specified, but
839/// provide a strong guidance to not use it.
840///
841/// This method checks to see if the argument is an acceptable l-value and
842/// returns false if it is a case we can handle.
843static bool CheckAsmLValue(const Expr *E, Sema &S) {
844 if (E->isLvalue(S.Context) == Expr::LV_Valid)
845 return false; // Cool, this is an lvalue.
846
847 // Okay, this is not an lvalue, but perhaps it is the result of a cast that we
848 // are supposed to allow.
849 const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
850 if (E != E2 && E2->isLvalue(S.Context) == Expr::LV_Valid) {
851 if (!S.getLangOptions().HeinousExtensions)
852 S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue)
853 << E->getSourceRange();
854 else
855 S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
856 << E->getSourceRange();
857 // Accept, even if we emitted an error diagnostic.
858 return false;
859 }
860
861 // None of the above, just randomly invalid non-lvalue.
862 return true;
863}
864
865
Sebastian Redlc6b86332009-01-18 16:53:17 +0000866Sema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
867 bool IsSimple,
868 bool IsVolatile,
869 unsigned NumOutputs,
870 unsigned NumInputs,
871 std::string *Names,
872 MultiExprArg constraints,
873 MultiExprArg exprs,
874 ExprArg asmString,
875 MultiExprArg clobbers,
876 SourceLocation RParenLoc) {
877 unsigned NumClobbers = clobbers.size();
878 StringLiteral **Constraints =
879 reinterpret_cast<StringLiteral**>(constraints.get());
880 Expr **Exprs = reinterpret_cast<Expr **>(exprs.get());
881 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get());
882 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
883
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000884 llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
885
Chris Lattnerb052a832008-08-18 19:55:17 +0000886 // The parser verifies that there is a string literal here.
Chris Lattner84418022008-07-23 06:46:56 +0000887 if (AsmString->isWide())
Sebastian Redlc6b86332009-01-18 16:53:17 +0000888 return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
889 << AsmString->getSourceRange());
890
891
Chris Lattnerb052a832008-08-18 19:55:17 +0000892 for (unsigned i = 0; i != NumOutputs; i++) {
893 StringLiteral *Literal = Constraints[i];
Chris Lattner84418022008-07-23 06:46:56 +0000894 if (Literal->isWide())
Sebastian Redlc6b86332009-01-18 16:53:17 +0000895 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
896 << Literal->getSourceRange());
897
Anders Carlsson4ce42302007-11-27 04:11:28 +0000898 std::string OutputConstraint(Literal->getStrData(),
899 Literal->getByteLength());
Sebastian Redlc6b86332009-01-18 16:53:17 +0000900
Anders Carlsson4ce42302007-11-27 04:11:28 +0000901 TargetInfo::ConstraintInfo info;
Chris Lattner84418022008-07-23 06:46:56 +0000902 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Sebastian Redlc6b86332009-01-18 16:53:17 +0000903 return StmtError(Diag(Literal->getLocStart(),
904 diag::err_asm_invalid_output_constraint) << OutputConstraint);
905
Anders Carlsson4ce42302007-11-27 04:11:28 +0000906 // Check that the output exprs are valid lvalues.
Chris Lattnerb052a832008-08-18 19:55:17 +0000907 ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]);
Chris Lattner1e3eedb2009-03-13 17:38:01 +0000908 if (CheckAsmLValue(OutputExpr, *this)) {
Sebastian Redlc6b86332009-01-18 16:53:17 +0000909 return StmtError(Diag(OutputExpr->getSubExpr()->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000910 diag::err_asm_invalid_lvalue_in_output)
Sebastian Redlc6b86332009-01-18 16:53:17 +0000911 << OutputExpr->getSubExpr()->getSourceRange());
Anders Carlssonb4487a82007-11-23 19:43:50 +0000912 }
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000913
914 OutputConstraintInfos.push_back(info);
Anders Carlssonb4487a82007-11-23 19:43:50 +0000915 }
Sebastian Redlc6b86332009-01-18 16:53:17 +0000916
Anders Carlssonb4487a82007-11-23 19:43:50 +0000917 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Chris Lattnerb052a832008-08-18 19:55:17 +0000918 StringLiteral *Literal = Constraints[i];
Chris Lattner84418022008-07-23 06:46:56 +0000919 if (Literal->isWide())
Sebastian Redlc6b86332009-01-18 16:53:17 +0000920 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
921 << Literal->getSourceRange());
922
923 std::string InputConstraint(Literal->getStrData(),
Anders Carlsson4ce42302007-11-27 04:11:28 +0000924 Literal->getByteLength());
Sebastian Redlc6b86332009-01-18 16:53:17 +0000925
Anders Carlsson4ce42302007-11-27 04:11:28 +0000926 TargetInfo::ConstraintInfo info;
927 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
Anders Carlsson7b49cec2009-01-17 23:36:15 +0000928 &Names[0],
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000929 &Names[0] + NumOutputs,
930 &OutputConstraintInfos[0],
931 info)) {
Sebastian Redlc6b86332009-01-18 16:53:17 +0000932 return StmtError(Diag(Literal->getLocStart(),
933 diag::err_asm_invalid_input_constraint) << InputConstraint);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000934 }
Sebastian Redlc6b86332009-01-18 16:53:17 +0000935
Chris Lattnerb052a832008-08-18 19:55:17 +0000936 ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]);
Sebastian Redlc6b86332009-01-18 16:53:17 +0000937
Anders Carlssone7d92702009-01-20 20:49:22 +0000938 // Only allow void types for memory constraints.
Anders Carlssonde93d332009-01-21 06:27:20 +0000939 if ((info & TargetInfo::CI_AllowsMemory)
940 && !(info & TargetInfo::CI_AllowsRegister)) {
Chris Lattner1e3eedb2009-03-13 17:38:01 +0000941 if (CheckAsmLValue(InputExpr, *this))
Anders Carlssone7d92702009-01-20 20:49:22 +0000942 return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(),
943 diag::err_asm_invalid_lvalue_in_input)
944 << InputConstraint << InputExpr->getSubExpr()->getSourceRange());
Anders Carlssonb4487a82007-11-23 19:43:50 +0000945 }
Sebastian Redlc6b86332009-01-18 16:53:17 +0000946
Anders Carlssone7d92702009-01-20 20:49:22 +0000947 if (info & TargetInfo::CI_AllowsRegister) {
948 if (InputExpr->getType()->isVoidType()) {
949 return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(),
950 diag::err_asm_invalid_type_in_input)
951 << InputExpr->getType() << InputConstraint
952 << InputExpr->getSubExpr()->getSourceRange());
953 }
Anders Carlssone7d92702009-01-20 20:49:22 +0000954 }
Anders Carlsson14125b02009-02-22 02:11:23 +0000955
956 DefaultFunctionArrayConversion(Exprs[i]);
Anders Carlssonb4487a82007-11-23 19:43:50 +0000957 }
Sebastian Redlc6b86332009-01-18 16:53:17 +0000958
Anders Carlsson49dadd62007-11-25 00:25:21 +0000959 // Check that the clobbers are valid.
Chris Lattnerb052a832008-08-18 19:55:17 +0000960 for (unsigned i = 0; i != NumClobbers; i++) {
961 StringLiteral *Literal = Clobbers[i];
Chris Lattner84418022008-07-23 06:46:56 +0000962 if (Literal->isWide())
Sebastian Redlc6b86332009-01-18 16:53:17 +0000963 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
964 << Literal->getSourceRange());
965
966 llvm::SmallString<16> Clobber(Literal->getStrData(),
967 Literal->getStrData() +
Anders Carlsson49dadd62007-11-25 00:25:21 +0000968 Literal->getByteLength());
Sebastian Redlc6b86332009-01-18 16:53:17 +0000969
Chris Lattner84418022008-07-23 06:46:56 +0000970 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Sebastian Redlc6b86332009-01-18 16:53:17 +0000971 return StmtError(Diag(Literal->getLocStart(),
972 diag::err_asm_unknown_register_name) << Clobber.c_str());
Anders Carlsson49dadd62007-11-25 00:25:21 +0000973 }
Sebastian Redlc6b86332009-01-18 16:53:17 +0000974
975 constraints.release();
976 exprs.release();
977 asmString.release();
978 clobbers.release();
Chris Lattnerc5164732009-03-10 23:41:04 +0000979 AsmStmt *NS =
980 new (Context) AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
981 Names, Constraints, Exprs, AsmString, NumClobbers,
982 Clobbers, RParenLoc);
983 // Validate the asm string, ensuring it makes sense given the operands we
984 // have.
985 llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces;
986 unsigned DiagOffs;
987 if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
Chris Lattnerc0da38d2009-03-10 23:57:07 +0000988 Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
989 << AsmString->getSourceRange();
Chris Lattnerc5164732009-03-10 23:41:04 +0000990 DeleteStmt(NS);
991 return StmtError();
992 }
993
994
995 return Owned(NS);
Chris Lattner8a40a832007-10-29 04:04:16 +0000996}
Fariborz Jahanian06798362007-11-01 23:59:59 +0000997
Sebastian Redlb3860a72009-01-18 17:43:11 +0000998Action::OwningStmtResult
999Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Steve Naroff0e8b96a2009-03-03 19:52:17 +00001000 SourceLocation RParen, DeclTy *Parm,
Sebastian Redlb3860a72009-01-18 17:43:11 +00001001 StmtArg Body, StmtArg catchList) {
1002 Stmt *CatchList = static_cast<Stmt*>(catchList.release());
Steve Naroff45c237d2009-03-03 20:59:06 +00001003 ParmVarDecl *PVD = static_cast<ParmVarDecl*>(Parm);
1004
1005 // PVD == 0 implies @catch(...).
Steve Naroff30db0042009-03-03 21:16:54 +00001006 if (PVD) {
1007 if (!Context.isObjCObjectPointerType(PVD->getType()))
1008 return StmtError(Diag(PVD->getLocation(),
1009 diag::err_catch_param_not_objc_type));
1010 if (PVD->getType()->isObjCQualifiedIdType())
1011 return StmtError(Diag(PVD->getLocation(),
Steve Naroffe54d4eb2009-03-03 23:13:51 +00001012 diag::err_illegal_qualifiers_on_catch_parm));
Steve Naroff30db0042009-03-03 21:16:54 +00001013 }
Steve Naroff45c237d2009-03-03 20:59:06 +00001014
Ted Kremenek0c97e042009-02-07 01:47:29 +00001015 ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen,
Steve Naroff45c237d2009-03-03 20:59:06 +00001016 PVD, static_cast<Stmt*>(Body.release()), CatchList);
Sebastian Redlb3860a72009-01-18 17:43:11 +00001017 return Owned(CatchList ? CatchList : CS);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001018}
1019
Sebastian Redlb3860a72009-01-18 17:43:11 +00001020Action::OwningStmtResult
1021Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) {
Ted Kremenek0c97e042009-02-07 01:47:29 +00001022 return Owned(new (Context) ObjCAtFinallyStmt(AtLoc,
1023 static_cast<Stmt*>(Body.release())));
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001024}
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001025
Sebastian Redlb3860a72009-01-18 17:43:11 +00001026Action::OwningStmtResult
1027Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
1028 StmtArg Try, StmtArg Catch, StmtArg Finally) {
Ted Kremenek0c97e042009-02-07 01:47:29 +00001029 return Owned(new (Context) ObjCAtTryStmt(AtLoc,
1030 static_cast<Stmt*>(Try.release()),
Sebastian Redlb3860a72009-01-18 17:43:11 +00001031 static_cast<Stmt*>(Catch.release()),
1032 static_cast<Stmt*>(Finally.release())));
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001033}
1034
Sebastian Redlb3860a72009-01-18 17:43:11 +00001035Action::OwningStmtResult
Steve Naroff9a8739a2009-02-12 15:54:59 +00001036Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr,Scope *CurScope) {
Steve Naroff677f4012009-02-11 17:45:08 +00001037 Expr *ThrowExpr = static_cast<Expr*>(expr.release());
1038 if (!ThrowExpr) {
Steve Naroff590fe482009-02-11 20:05:44 +00001039 // @throw without an expression designates a rethrow (which much occur
1040 // in the context of an @catch clause).
1041 Scope *AtCatchParent = CurScope;
1042 while (AtCatchParent && !AtCatchParent->isAtCatchScope())
1043 AtCatchParent = AtCatchParent->getParent();
1044 if (!AtCatchParent)
Steve Naroff644567e2009-02-12 18:09:32 +00001045 return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
Steve Naroff677f4012009-02-11 17:45:08 +00001046 } else {
1047 QualType ThrowType = ThrowExpr->getType();
1048 // Make sure the expression type is an ObjC pointer or "void *".
1049 if (!Context.isObjCObjectPointerType(ThrowType)) {
1050 const PointerType *PT = ThrowType->getAsPointerType();
1051 if (!PT || !PT->getPointeeType()->isVoidType())
Steve Naroff644567e2009-02-12 18:09:32 +00001052 return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
1053 << ThrowExpr->getType() << ThrowExpr->getSourceRange());
Steve Naroff677f4012009-02-11 17:45:08 +00001054 }
1055 }
1056 return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowExpr));
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001057}
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001058
Sebastian Redlb3860a72009-01-18 17:43:11 +00001059Action::OwningStmtResult
1060Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr,
1061 StmtArg SynchBody) {
Ted Kremenek0c97e042009-02-07 01:47:29 +00001062 return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc,
Sebastian Redlb3860a72009-01-18 17:43:11 +00001063 static_cast<Stmt*>(SynchExpr.release()),
1064 static_cast<Stmt*>(SynchBody.release())));
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001065}
Sebastian Redl743c8162008-12-22 19:15:10 +00001066
1067/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
1068/// and creates a proper catch handler from them.
1069Action::OwningStmtResult
1070Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclTy *ExDecl,
1071 StmtArg HandlerBlock) {
1072 // There's nothing to test that ActOnExceptionDecl didn't already test.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001073 return Owned(new (Context) CXXCatchStmt(CatchLoc,
1074 static_cast<VarDecl*>(ExDecl),
1075 static_cast<Stmt*>(HandlerBlock.release())));
Sebastian Redl743c8162008-12-22 19:15:10 +00001076}
Sebastian Redl237116b2008-12-22 21:35:02 +00001077
1078/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
1079/// handlers and creates a try statement from them.
1080Action::OwningStmtResult
1081Sema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock,
1082 MultiStmtArg RawHandlers) {
1083 unsigned NumHandlers = RawHandlers.size();
1084 assert(NumHandlers > 0 &&
1085 "The parser shouldn't call this if there are no handlers.");
1086 Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get());
1087
1088 for(unsigned i = 0; i < NumHandlers - 1; ++i) {
1089 CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
1090 if (!Handler->getExceptionDecl())
1091 return StmtError(Diag(Handler->getLocStart(), diag::err_early_catch_all));
1092 }
1093 // FIXME: We should detect handlers for the same type as an earlier one.
1094 // This one is rather easy.
1095 // FIXME: We should detect handlers that cannot catch anything because an
1096 // earlier handler catches a superclass. Need to find a method that is not
1097 // quadratic for this.
1098 // Neither of these are explicitly forbidden, but every compiler detects them
1099 // and warns.
1100
1101 RawHandlers.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +00001102 return Owned(new (Context) CXXTryStmt(TryLoc,
1103 static_cast<Stmt*>(TryBlock.release()),
1104 Handlers, NumHandlers));
Sebastian Redl237116b2008-12-22 21:35:02 +00001105}