blob: dd505f6967310ec184f3e5a5f43d748f66727200 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for statements.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anders Carlsson51fe9962008-11-22 21:04:56 +000015#include "clang/AST/APValue.h"
Chris Lattnerf4021e72007-08-23 05:46:52 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/Expr.h"
Ted Kremenek8189cde2009-02-07 01:47:29 +000019#include "clang/AST/ASTContext.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000020#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Sebastian Redla60528c2008-12-21 12:04:03 +000023Sema::OwningStmtResult Sema::ActOnExprStmt(ExprArg expr) {
24 Expr *E = static_cast<Expr*>(expr.release());
Steve Naroff1b273c42007-09-16 14:56:35 +000025 assert(E && "ActOnExprStmt(): missing expression");
Sebastian Redla60528c2008-12-21 12:04:03 +000026
Chris Lattner834a72a2008-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 Redla60528c2008-12-21 12:04:03 +000030
Chris Lattner834a72a2008-07-25 23:18:17 +000031 // Same thing in for stmt first clause (when expr) and third clause.
Sebastian Redla60528c2008-12-21 12:04:03 +000032 return Owned(static_cast<Stmt*>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +000033}
34
35
Sebastian Redla60528c2008-12-21 12:04:03 +000036Sema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000037 return Owned(new (Context) NullStmt(SemiLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +000038}
39
Sebastian Redla60528c2008-12-21 12:04:03 +000040Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclTy *decl,
41 SourceLocation StartLoc,
42 SourceLocation EndLoc) {
Chris Lattner81c018d2008-03-13 06:29:04 +000043 if (decl == 0)
Sebastian Redla60528c2008-12-21 12:04:03 +000044 return StmtError();
45
Douglas Gregor4afa39d2009-01-20 01:17:11 +000046 Decl *D = static_cast<Decl *>(decl);
Sebastian Redla60528c2008-12-21 12:04:03 +000047
Ted Kremenek8ffb1592008-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 Gregor4afa39d2009-01-20 01:17:11 +000051 while (D) {
52 Decl* d = D;
53 D = D->getNextDeclarator();
Ted Kremenek8ffb1592008-10-07 23:09:49 +000054 d->setNextDeclarator(0);
55 decls.push_back(d);
56 }
57
58 assert (!decls.empty());
Sebastian Redla60528c2008-12-21 12:04:03 +000059
Ted Kremenek8ffb1592008-10-07 23:09:49 +000060 if (decls.size() == 1) {
61 DeclGroupOwningRef DG(*decls.begin());
Ted Kremenek8189cde2009-02-07 01:47:29 +000062 return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
Ted Kremenek8ffb1592008-10-07 23:09:49 +000063 }
64 else {
Chris Lattner08631c52008-11-23 21:45:46 +000065 DeclGroupOwningRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
Ted Kremenek8189cde2009-02-07 01:47:29 +000066 return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
Ted Kremenek8ffb1592008-10-07 23:09:49 +000067 }
Reid Spencer5f016e22007-07-11 17:01:13 +000068}
69
Sebastian Redla60528c2008-12-21 12:04:03 +000070Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +000071Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Sebastian Redla60528c2008-12-21 12:04:03 +000072 MultiStmtArg elts, bool isStmtExpr) {
73 unsigned NumElts = elts.size();
74 Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000075 // If we're in C89 mode, check that we don't have any decls after stmts. If
76 // so, emit an extension diagnostic.
77 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
78 // Note that __extension__ can be around a decl.
79 unsigned i = 0;
80 // Skip over all declarations.
81 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
82 /*empty*/;
83
84 // We found the end of the list or a statement. Scan for another declstmt.
85 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
86 /*empty*/;
87
88 if (i != NumElts) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +000089 Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000090 Diag(D->getLocation(), diag::ext_mixed_decls_code);
91 }
92 }
Chris Lattner98414c12007-08-31 21:49:55 +000093 // Warn about unused expressions in statements.
94 for (unsigned i = 0; i != NumElts; ++i) {
95 Expr *E = dyn_cast<Expr>(Elts[i]);
96 if (!E) continue;
97
98 // Warn about expressions with unused results.
99 if (E->hasLocalSideEffect() || E->getType()->isVoidType())
100 continue;
101
102 // The last expr in a stmt expr really is used.
103 if (isStmtExpr && i == NumElts-1)
104 continue;
105
106 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
107 /// a context where the result is unused. Emit a diagnostic to warn about
108 /// this.
109 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000110 Diag(BO->getOperatorLoc(), diag::warn_unused_expr)
111 << BO->getLHS()->getSourceRange() << BO->getRHS()->getSourceRange();
Chris Lattner98414c12007-08-31 21:49:55 +0000112 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000113 Diag(UO->getOperatorLoc(), diag::warn_unused_expr)
114 << UO->getSubExpr()->getSourceRange();
Sebastian Redla60528c2008-12-21 12:04:03 +0000115 else
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000116 Diag(E->getExprLoc(), diag::warn_unused_expr) << E->getSourceRange();
Chris Lattner98414c12007-08-31 21:49:55 +0000117 }
Sebastian Redla60528c2008-12-21 12:04:03 +0000118
Ted Kremenek8189cde2009-02-07 01:47:29 +0000119 return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
Reid Spencer5f016e22007-07-11 17:01:13 +0000120}
121
Sebastian Redl117054a2008-12-28 16:13:43 +0000122Action::OwningStmtResult
123Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval,
124 SourceLocation DotDotDotLoc, ExprArg rhsval,
125 SourceLocation ColonLoc, StmtArg subStmt) {
126 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
127 assert((lhsval.get() != 0) && "missing expression in case statement");
128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 // C99 6.8.4.2p3: The expression shall be an integer constant.
Anders Carlsson51fe9962008-11-22 21:04:56 +0000130 // However, GCC allows any evaluatable integer expression.
Anders Carlssond3a61d52008-12-01 02:13:02 +0000131
Sebastian Redl117054a2008-12-28 16:13:43 +0000132 Expr *LHSVal = static_cast<Expr*>(lhsval.get());
Anders Carlssond3a61d52008-12-01 02:13:02 +0000133 if (VerifyIntegerConstantExpression(LHSVal))
Sebastian Redl117054a2008-12-28 16:13:43 +0000134 return Owned(SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000135
Chris Lattner6c36be52007-07-18 02:28:47 +0000136 // GCC extension: The expression shall be an integer constant.
Sebastian Redl117054a2008-12-28 16:13:43 +0000137
138 Expr *RHSVal = static_cast<Expr*>(rhsval.get());
139 if (RHSVal && VerifyIntegerConstantExpression(RHSVal)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000140 RHSVal = 0; // Recover by just forgetting about it.
Sebastian Redl117054a2008-12-28 16:13:43 +0000141 rhsval = 0;
142 }
143
Chris Lattner8a87e572007-07-23 17:05:23 +0000144 if (SwitchStack.empty()) {
145 Diag(CaseLoc, diag::err_case_not_in_switch);
Sebastian Redl117054a2008-12-28 16:13:43 +0000146 return Owned(SubStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000147 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000148
Sebastian Redl117054a2008-12-28 16:13:43 +0000149 // Only now release the smart pointers.
150 lhsval.release();
151 rhsval.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000152 CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner8a87e572007-07-23 17:05:23 +0000153 SwitchStack.back()->addSwitchCase(CS);
Sebastian Redl117054a2008-12-28 16:13:43 +0000154 return Owned(CS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000155}
156
Sebastian Redl117054a2008-12-28 16:13:43 +0000157Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000158Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Sebastian Redl117054a2008-12-28 16:13:43 +0000159 StmtArg subStmt, Scope *CurScope) {
160 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
161
Chris Lattner8a87e572007-07-23 17:05:23 +0000162 if (SwitchStack.empty()) {
Chris Lattner0fa152e2007-07-21 03:00:26 +0000163 Diag(DefaultLoc, diag::err_default_not_in_switch);
Sebastian Redl117054a2008-12-28 16:13:43 +0000164 return Owned(SubStmt);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000165 }
Sebastian Redl117054a2008-12-28 16:13:43 +0000166
Ted Kremenek8189cde2009-02-07 01:47:29 +0000167 DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000168 SwitchStack.back()->addSwitchCase(DS);
Sebastian Redl117054a2008-12-28 16:13:43 +0000169 return Owned(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000170}
171
Sebastian Redlde307472009-01-11 00:38:46 +0000172Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000173Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Sebastian Redlde307472009-01-11 00:38:46 +0000174 SourceLocation ColonLoc, StmtArg subStmt) {
175 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 // Look up the record for this label identifier.
177 LabelStmt *&LabelDecl = LabelMap[II];
Sebastian Redlde307472009-01-11 00:38:46 +0000178
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 // If not forward referenced or defined already, just create a new LabelStmt.
180 if (LabelDecl == 0)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000181 return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt));
Sebastian Redlde307472009-01-11 00:38:46 +0000182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 assert(LabelDecl->getID() == II && "Label mismatch!");
Sebastian Redlde307472009-01-11 00:38:46 +0000184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 // Otherwise, this label was either forward reference or multiply defined. If
186 // multiply defined, reject it now.
187 if (LabelDecl->getSubStmt()) {
Chris Lattner08631c52008-11-23 21:45:46 +0000188 Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000189 Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
Sebastian Redlde307472009-01-11 00:38:46 +0000190 return Owned(SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 }
Sebastian Redlde307472009-01-11 00:38:46 +0000192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 // Otherwise, this label was forward declared, and we just found its real
194 // definition. Fill in the forward definition and return it.
195 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000196 LabelDecl->setSubStmt(SubStmt);
Sebastian Redlde307472009-01-11 00:38:46 +0000197 return Owned(LabelDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199
Sebastian Redlde307472009-01-11 00:38:46 +0000200Action::OwningStmtResult
201Sema::ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal,
202 StmtArg ThenVal, SourceLocation ElseLoc,
203 StmtArg ElseVal) {
204 Expr *condExpr = (Expr *)CondVal.release();
205
Steve Naroff1b273c42007-09-16 14:56:35 +0000206 assert(condExpr && "ActOnIfStmt(): missing expression");
Sebastian Redlde307472009-01-11 00:38:46 +0000207
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000208 DefaultFunctionArrayConversion(condExpr);
Sebastian Redlde307472009-01-11 00:38:46 +0000209 // Take ownership again until we're past the error checking.
210 CondVal = condExpr;
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000211 QualType condType = condExpr->getType();
Sebastian Redlde307472009-01-11 00:38:46 +0000212
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000213 if (getLangOptions().CPlusPlus) {
214 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redlde307472009-01-11 00:38:46 +0000215 return StmtError();
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000216 } else if (!condType->isScalarType()) // C99 6.8.4.1p1
Sebastian Redlde307472009-01-11 00:38:46 +0000217 return StmtError(Diag(IfLoc, diag::err_typecheck_statement_requires_scalar)
218 << condType << condExpr->getSourceRange());
219
220 Stmt *thenStmt = (Stmt *)ThenVal.release();
Reid Spencer5f016e22007-07-11 17:01:13 +0000221
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000222 // Warn if the if block has a null body without an else value.
223 // this helps prevent bugs due to typos, such as
224 // if (condition);
225 // do_stuff();
Sebastian Redlde307472009-01-11 00:38:46 +0000226 if (!ElseVal.get()) {
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000227 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
228 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
229 }
230
Sebastian Redlde307472009-01-11 00:38:46 +0000231 CondVal.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000232 return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt,
233 (Stmt*)ElseVal.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000234}
235
Sebastian Redlde307472009-01-11 00:38:46 +0000236Action::OwningStmtResult
237Sema::ActOnStartOfSwitchStmt(ExprArg cond) {
238 Expr *Cond = static_cast<Expr*>(cond.release());
239
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000240 if (getLangOptions().CPlusPlus) {
241 // C++ 6.4.2.p2:
242 // The condition shall be of integral type, enumeration type, or of a class
243 // type for which a single conversion function to integral or enumeration
244 // type exists (12.3). If the condition is of class type, the condition is
245 // converted by calling that conversion function, and the result of the
246 // conversion is used in place of the original condition for the remainder
247 // of this section. Integral promotions are performed.
248
249 QualType Ty = Cond->getType();
250
251 // FIXME: Handle class types.
252
253 // If the type is wrong a diagnostic will be emitted later at
254 // ActOnFinishSwitchStmt.
255 if (Ty->isIntegralType() || Ty->isEnumeralType()) {
256 // Integral promotions are performed.
257 // FIXME: Integral promotions for C++ are not complete.
258 UsualUnaryConversions(Cond);
259 }
260 } else {
261 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
262 UsualUnaryConversions(Cond);
263 }
Sebastian Redlde307472009-01-11 00:38:46 +0000264
Ted Kremenek8189cde2009-02-07 01:47:29 +0000265 SwitchStmt *SS = new (Context) SwitchStmt(Cond);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000266 SwitchStack.push_back(SS);
Sebastian Redlde307472009-01-11 00:38:46 +0000267 return Owned(SS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000268}
Chris Lattner6c36be52007-07-18 02:28:47 +0000269
Chris Lattnerf4021e72007-08-23 05:46:52 +0000270/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
271/// the specified width and sign. If an overflow occurs, detect it and emit
272/// the specified diagnostic.
273void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
274 unsigned NewWidth, bool NewSign,
275 SourceLocation Loc,
276 unsigned DiagID) {
277 // Perform a conversion to the promoted condition type if needed.
278 if (NewWidth > Val.getBitWidth()) {
279 // If this is an extension, just do it.
280 llvm::APSInt OldVal(Val);
281 Val.extend(NewWidth);
282
283 // If the input was signed and negative and the output is unsigned,
284 // warn.
285 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000286 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000287
288 Val.setIsSigned(NewSign);
289 } else if (NewWidth < Val.getBitWidth()) {
290 // If this is a truncation, check for overflow.
291 llvm::APSInt ConvVal(Val);
292 ConvVal.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000293 ConvVal.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000294 ConvVal.extend(Val.getBitWidth());
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000295 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000296 if (ConvVal != Val)
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000297 Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000298
299 // Regardless of whether a diagnostic was emitted, really do the
300 // truncation.
301 Val.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000302 Val.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000303 } else if (NewSign != Val.isSigned()) {
304 // Convert the sign to match the sign of the condition. This can cause
305 // overflow as well: unsigned(INTMIN)
306 llvm::APSInt OldVal(Val);
307 Val.setIsSigned(NewSign);
308
309 if (Val.isNegative()) // Sign bit changes meaning.
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000310 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000311 }
312}
313
Chris Lattner0471f5b2007-08-23 18:29:20 +0000314namespace {
315 struct CaseCompareFunctor {
316 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
317 const llvm::APSInt &RHS) {
318 return LHS.first < RHS;
319 }
Chris Lattner0e85a272007-09-03 18:31:57 +0000320 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
321 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
322 return LHS.first < RHS.first;
323 }
Chris Lattner0471f5b2007-08-23 18:29:20 +0000324 bool operator()(const llvm::APSInt &LHS,
325 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
326 return LHS < RHS.first;
327 }
328 };
329}
330
Chris Lattner764a7ce2007-09-21 18:15:22 +0000331/// CmpCaseVals - Comparison predicate for sorting case values.
332///
333static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
334 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
335 if (lhs.first < rhs.first)
336 return true;
337
338 if (lhs.first == rhs.first &&
339 lhs.second->getCaseLoc().getRawEncoding()
340 < rhs.second->getCaseLoc().getRawEncoding())
341 return true;
342 return false;
343}
344
Sebastian Redlde307472009-01-11 00:38:46 +0000345Action::OwningStmtResult
346Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
347 StmtArg Body) {
348 Stmt *BodyStmt = (Stmt*)Body.release();
349
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000350 SwitchStmt *SS = SwitchStack.back();
Sebastian Redlde307472009-01-11 00:38:46 +0000351 assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!");
352
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000353 SS->setBody(BodyStmt, SwitchLoc);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000354 SwitchStack.pop_back();
355
Chris Lattnerf4021e72007-08-23 05:46:52 +0000356 Expr *CondExpr = SS->getCond();
357 QualType CondType = CondExpr->getType();
Sebastian Redlde307472009-01-11 00:38:46 +0000358
Chris Lattnerf4021e72007-08-23 05:46:52 +0000359 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000360 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000361 << CondType << CondExpr->getSourceRange();
Sebastian Redlde307472009-01-11 00:38:46 +0000362 return StmtError();
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000363 }
Sebastian Redlde307472009-01-11 00:38:46 +0000364
Chris Lattnerf4021e72007-08-23 05:46:52 +0000365 // Get the bitwidth of the switched-on value before promotions. We must
366 // convert the integer case values to this width before comparison.
Chris Lattner98be4942008-03-05 18:54:05 +0000367 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000368 bool CondIsSigned = CondType->isSignedIntegerType();
369
370 // Accumulate all of the case values in a vector so that we can sort them
371 // and detect duplicates. This vector contains the APInt for the case after
372 // it has been converted to the condition type.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000373 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
374 CaseValsTy CaseVals;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000375
376 // Keep track of any GNU case ranges we see. The APSInt is the low value.
377 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
378
379 DefaultStmt *TheDefaultStmt = 0;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000380
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000381 bool CaseListIsErroneous = false;
382
383 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000384 SC = SC->getNextSwitchCase()) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000385
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000386 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000387 if (TheDefaultStmt) {
388 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
Chris Lattner5f4a6822008-11-23 23:12:31 +0000389 Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
Sebastian Redlde307472009-01-11 00:38:46 +0000390
Chris Lattnerf4021e72007-08-23 05:46:52 +0000391 // FIXME: Remove the default statement from the switch block so that
392 // we'll return a valid AST. This requires recursing down the
393 // AST and finding it, not something we are set up to do right now. For
394 // now, just lop the entire switch stmt out of the AST.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000395 CaseListIsErroneous = true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000396 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000397 TheDefaultStmt = DS;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000398
Chris Lattnerf4021e72007-08-23 05:46:52 +0000399 } else {
400 CaseStmt *CS = cast<CaseStmt>(SC);
401
402 // We already verified that the expression has a i-c-e value (C99
403 // 6.8.4.2p3) - get that value now.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000404 Expr *Lo = CS->getLHS();
Anders Carlsson51fe9962008-11-22 21:04:56 +0000405 llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000406
407 // Convert the value to the same width/sign as the condition.
408 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
409 CS->getLHS()->getLocStart(),
410 diag::warn_case_value_overflow);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000411
Chris Lattner1e0a3902008-01-16 19:17:22 +0000412 // If the LHS is not the same type as the condition, insert an implicit
413 // cast.
414 ImpCastExprToType(Lo, CondType);
415 CS->setLHS(Lo);
416
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000417 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerf4021e72007-08-23 05:46:52 +0000418 if (CS->getRHS())
419 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000420 else
421 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000422 }
423 }
424
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000425 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner764a7ce2007-09-21 18:15:22 +0000426 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000427
Chris Lattnerf3348502007-08-23 14:29:07 +0000428 if (!CaseVals.empty()) {
429 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
430 if (CaseVals[i].first == CaseVals[i+1].first) {
431 // If we have a duplicate, report it.
432 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000433 diag::err_duplicate_case) << CaseVals[i].first.toString(10);
Chris Lattnerf3348502007-08-23 14:29:07 +0000434 Diag(CaseVals[i].second->getLHS()->getLocStart(),
Chris Lattner5f4a6822008-11-23 23:12:31 +0000435 diag::note_duplicate_case_prev);
Chris Lattnerf3348502007-08-23 14:29:07 +0000436 // FIXME: We really want to remove the bogus case stmt from the substmt,
437 // but we have no way to do this right now.
438 CaseListIsErroneous = true;
439 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000440 }
441 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000442
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000443 // Detect duplicate case ranges, which usually don't exist at all in the first
444 // place.
445 if (!CaseRanges.empty()) {
446 // Sort all the case ranges by their low value so we can easily detect
447 // overlaps between ranges.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000448 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000449
450 // Scan the ranges, computing the high values and removing empty ranges.
451 std::vector<llvm::APSInt> HiVals;
Chris Lattner6efc4d32007-08-23 17:48:14 +0000452 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000453 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000454 Expr *Hi = CR->getRHS();
Anders Carlsson51fe9962008-11-22 21:04:56 +0000455 llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000456
457 // Convert the value to the same width/sign as the condition.
458 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
459 CR->getRHS()->getLocStart(),
460 diag::warn_case_value_overflow);
461
Chris Lattner1e0a3902008-01-16 19:17:22 +0000462 // If the LHS is not the same type as the condition, insert an implicit
463 // cast.
464 ImpCastExprToType(Hi, CondType);
465 CR->setRHS(Hi);
466
Chris Lattner6efc4d32007-08-23 17:48:14 +0000467 // If the low value is bigger than the high value, the case is empty.
468 if (CaseRanges[i].first > HiVal) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000469 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
470 << SourceRange(CR->getLHS()->getLocStart(),
471 CR->getRHS()->getLocEnd());
Chris Lattner6efc4d32007-08-23 17:48:14 +0000472 CaseRanges.erase(CaseRanges.begin()+i);
473 --i, --e;
474 continue;
475 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000476 HiVals.push_back(HiVal);
477 }
478
479 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0471f5b2007-08-23 18:29:20 +0000480 // ranges. Since the range list is sorted, we only need to compare case
481 // ranges with their neighbors.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000482 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0471f5b2007-08-23 18:29:20 +0000483 llvm::APSInt &CRLo = CaseRanges[i].first;
484 llvm::APSInt &CRHi = HiVals[i];
485 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000486
Chris Lattner0471f5b2007-08-23 18:29:20 +0000487 // Check to see whether the case range overlaps with any singleton cases.
488 CaseStmt *OverlapStmt = 0;
489 llvm::APSInt OverlapVal(32);
490
491 // Find the smallest value >= the lower bound. If I is in the case range,
492 // then we have overlap.
493 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
494 CaseVals.end(), CRLo,
495 CaseCompareFunctor());
496 if (I != CaseVals.end() && I->first < CRHi) {
497 OverlapVal = I->first; // Found overlap with scalar.
498 OverlapStmt = I->second;
499 }
500
501 // Find the smallest value bigger than the upper bound.
502 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
503 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
504 OverlapVal = (I-1)->first; // Found overlap with scalar.
505 OverlapStmt = (I-1)->second;
506 }
507
508 // Check to see if this case stmt overlaps with the subsequent case range.
509 if (i && CRLo <= HiVals[i-1]) {
510 OverlapVal = HiVals[i-1]; // Found overlap with range.
511 OverlapStmt = CaseRanges[i-1].second;
512 }
513
514 if (OverlapStmt) {
515 // If we have a duplicate, report it.
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000516 Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
517 << OverlapVal.toString(10);
Chris Lattner0471f5b2007-08-23 18:29:20 +0000518 Diag(OverlapStmt->getLHS()->getLocStart(),
Chris Lattner5f4a6822008-11-23 23:12:31 +0000519 diag::note_duplicate_case_prev);
Chris Lattner0471f5b2007-08-23 18:29:20 +0000520 // FIXME: We really want to remove the bogus case stmt from the substmt,
521 // but we have no way to do this right now.
522 CaseListIsErroneous = true;
523 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000524 }
525 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000526
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000527 // FIXME: If the case list was broken is some way, we don't have a good system
528 // to patch it up. Instead, just return the whole substmt as broken.
529 if (CaseListIsErroneous)
Sebastian Redlde307472009-01-11 00:38:46 +0000530 return StmtError();
531
532 Switch.release();
533 return Owned(SS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000534}
535
Sebastian Redlf05b1522009-01-16 23:28:06 +0000536Action::OwningStmtResult
537Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, StmtArg Body) {
538 Expr *condExpr = (Expr *)Cond.release();
Steve Naroff1b273c42007-09-16 14:56:35 +0000539 assert(condExpr && "ActOnWhileStmt(): missing expression");
Sebastian Redlf05b1522009-01-16 23:28:06 +0000540
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000541 DefaultFunctionArrayConversion(condExpr);
Sebastian Redlf05b1522009-01-16 23:28:06 +0000542 Cond = condExpr;
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000543 QualType condType = condExpr->getType();
Sebastian Redlf05b1522009-01-16 23:28:06 +0000544
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000545 if (getLangOptions().CPlusPlus) {
546 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redlf05b1522009-01-16 23:28:06 +0000547 return StmtError();
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000548 } else if (!condType->isScalarType()) // C99 6.8.5p2
Sebastian Redlf05b1522009-01-16 23:28:06 +0000549 return StmtError(Diag(WhileLoc,
550 diag::err_typecheck_statement_requires_scalar)
551 << condType << condExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000552
Sebastian Redlf05b1522009-01-16 23:28:06 +0000553 Cond.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000554 return Owned(new (Context) WhileStmt(condExpr, (Stmt*)Body.release(),
555 WhileLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000556}
557
Sebastian Redlf05b1522009-01-16 23:28:06 +0000558Action::OwningStmtResult
559Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body,
560 SourceLocation WhileLoc, ExprArg Cond) {
561 Expr *condExpr = (Expr *)Cond.release();
Steve Naroff1b273c42007-09-16 14:56:35 +0000562 assert(condExpr && "ActOnDoStmt(): missing expression");
Sebastian Redlf05b1522009-01-16 23:28:06 +0000563
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000564 DefaultFunctionArrayConversion(condExpr);
Sebastian Redlf05b1522009-01-16 23:28:06 +0000565 Cond = condExpr;
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000566 QualType condType = condExpr->getType();
Sebastian Redlf05b1522009-01-16 23:28:06 +0000567
Argyrios Kyrtzidis6314ff22008-09-11 05:16:22 +0000568 if (getLangOptions().CPlusPlus) {
569 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redlf05b1522009-01-16 23:28:06 +0000570 return StmtError();
Argyrios Kyrtzidis6314ff22008-09-11 05:16:22 +0000571 } else if (!condType->isScalarType()) // C99 6.8.5p2
Sebastian Redlf05b1522009-01-16 23:28:06 +0000572 return StmtError(Diag(DoLoc, diag::err_typecheck_statement_requires_scalar)
573 << condType << condExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000574
Sebastian Redlf05b1522009-01-16 23:28:06 +0000575 Cond.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000576 return Owned(new (Context) DoStmt((Stmt*)Body.release(), condExpr, DoLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000577}
578
Sebastian Redlf05b1522009-01-16 23:28:06 +0000579Action::OwningStmtResult
580Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
581 StmtArg first, ExprArg second, ExprArg third,
582 SourceLocation RParenLoc, StmtArg body) {
583 Stmt *First = static_cast<Stmt*>(first.get());
584 Expr *Second = static_cast<Expr*>(second.get());
585 Expr *Third = static_cast<Expr*>(third.get());
586 Stmt *Body = static_cast<Stmt*>(body.get());
587
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000588 if (!getLangOptions().CPlusPlus) {
589 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000590 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
591 // declare identifiers for objects having storage class 'auto' or
592 // 'register'.
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000593 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
594 DI!=DE; ++DI) {
595 VarDecl *VD = dyn_cast<VarDecl>(*DI);
596 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
597 VD = 0;
598 if (VD == 0)
599 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
600 // FIXME: mark decl erroneous!
601 }
Chris Lattnerae3b7012007-08-28 05:03:08 +0000602 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 }
604 if (Second) {
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000605 DefaultFunctionArrayConversion(Second);
606 QualType SecondType = Second->getType();
Sebastian Redlf05b1522009-01-16 23:28:06 +0000607
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000608 if (getLangOptions().CPlusPlus) {
609 if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
Sebastian Redlf05b1522009-01-16 23:28:06 +0000610 return StmtError();
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000611 } else if (!SecondType->isScalarType()) // C99 6.8.5p2
Sebastian Redlf05b1522009-01-16 23:28:06 +0000612 return StmtError(Diag(ForLoc,
613 diag::err_typecheck_statement_requires_scalar)
614 << SecondType << Second->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 }
Sebastian Redlf05b1522009-01-16 23:28:06 +0000616 first.release();
617 second.release();
618 third.release();
619 body.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000620 return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000621}
622
Sebastian Redlf05b1522009-01-16 23:28:06 +0000623Action::OwningStmtResult
624Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
625 SourceLocation LParenLoc,
626 StmtArg first, ExprArg second,
627 SourceLocation RParenLoc, StmtArg body) {
628 Stmt *First = static_cast<Stmt*>(first.get());
629 Expr *Second = static_cast<Expr*>(second.get());
630 Stmt *Body = static_cast<Stmt*>(body.get());
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000631 if (First) {
632 QualType FirstType;
633 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
Ted Kremenekf34afee2008-10-06 20:58:11 +0000634 if (!DS->hasSolitaryDecl())
Sebastian Redlf05b1522009-01-16 23:28:06 +0000635 return StmtError(Diag((*DS->decl_begin())->getLocation(),
636 diag::err_toomany_element_decls));
637
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000638 Decl *D = DS->getSolitaryDecl();
Ted Kremenekf34afee2008-10-06 20:58:11 +0000639 FirstType = cast<ValueDecl>(D)->getType();
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000640 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
641 // declare identifiers for objects having storage class 'auto' or
642 // 'register'.
Steve Naroff248a7532008-04-15 22:42:06 +0000643 VarDecl *VD = cast<VarDecl>(D);
644 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
Sebastian Redlf05b1522009-01-16 23:28:06 +0000645 return StmtError(Diag(VD->getLocation(),
646 diag::err_non_variable_decl_in_for));
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000647 } else {
648 Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context);
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000649
Sebastian Redlf05b1522009-01-16 23:28:06 +0000650 if (lval != Expr::LV_Valid)
651 return StmtError(Diag(First->getLocStart(),
652 diag::err_selector_element_not_lvalue)
653 << First->getSourceRange());
654
655 FirstType = static_cast<Expr*>(First)->getType();
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000656 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000657 if (!Context.isObjCObjectPointerType(FirstType))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000658 Diag(ForLoc, diag::err_selector_element_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000659 << FirstType << First->getSourceRange();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000660 }
661 if (Second) {
662 DefaultFunctionArrayConversion(Second);
663 QualType SecondType = Second->getType();
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000664 if (!Context.isObjCObjectPointerType(SecondType))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000665 Diag(ForLoc, diag::err_collection_expr_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000666 << SecondType << Second->getSourceRange();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000667 }
Sebastian Redlf05b1522009-01-16 23:28:06 +0000668 first.release();
669 second.release();
670 body.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000671 return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
672 ForLoc, RParenLoc));
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000673}
Reid Spencer5f016e22007-07-11 17:01:13 +0000674
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000675Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000676Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 IdentifierInfo *LabelII) {
Steve Naroff4eb206b2008-09-03 18:15:37 +0000678 // If we are in a block, reject all gotos for now.
679 if (CurBlock)
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000680 return StmtError(Diag(GotoLoc, diag::err_goto_in_block));
Steve Naroff4eb206b2008-09-03 18:15:37 +0000681
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 // Look up the record for this label identifier.
683 LabelStmt *&LabelDecl = LabelMap[LabelII];
684
685 // If we haven't seen this label yet, create a forward reference.
686 if (LabelDecl == 0)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000687 LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000688
Ted Kremenek8189cde2009-02-07 01:47:29 +0000689 return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000690}
691
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000692Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000693Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000694 ExprArg DestExp) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 // FIXME: Verify that the operand is convertible to void*.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000696
Ted Kremenek8189cde2009-02-07 01:47:29 +0000697 return Owned(new (Context) IndirectGotoStmt((Expr*)DestExp.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000698}
699
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000700Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000701Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 Scope *S = CurScope->getContinueParent();
703 if (!S) {
704 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000705 return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000707
Ted Kremenek8189cde2009-02-07 01:47:29 +0000708 return Owned(new (Context) ContinueStmt(ContinueLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000709}
710
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000711Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000712Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 Scope *S = CurScope->getBreakParent();
714 if (!S) {
715 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000716 return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000718
Ted Kremenek8189cde2009-02-07 01:47:29 +0000719 return Owned(new (Context) BreakStmt(BreakLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000720}
721
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000722/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000723///
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000724Action::OwningStmtResult
Steve Naroff4eb206b2008-09-03 18:15:37 +0000725Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000726
Steve Naroff4eb206b2008-09-03 18:15:37 +0000727 // If this is the first return we've seen in the block, infer the type of
728 // the block from it.
729 if (CurBlock->ReturnType == 0) {
Steve Naroffc50a4a52008-09-16 22:25:10 +0000730 if (RetValExp) {
Steve Naroff16564422008-09-24 22:26:48 +0000731 // Don't call UsualUnaryConversions(), since we don't want to do
732 // integer promotions here.
733 DefaultFunctionArrayConversion(RetValExp);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000734 CurBlock->ReturnType = RetValExp->getType().getTypePtr();
Steve Naroffc50a4a52008-09-16 22:25:10 +0000735 } else
Steve Naroff4eb206b2008-09-03 18:15:37 +0000736 CurBlock->ReturnType = Context.VoidTy.getTypePtr();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000737 }
Mike Stump98eb8a72009-02-04 22:31:32 +0000738 QualType FnRetType = QualType(CurBlock->ReturnType, 0);
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000739
Steve Naroff4eb206b2008-09-03 18:15:37 +0000740 // Otherwise, verify that this result type matches the previous one. We are
741 // pickier with blocks than for normal functions because we don't have GCC
742 // compatibility to worry about here.
743 if (CurBlock->ReturnType->isVoidType()) {
744 if (RetValExp) {
745 Diag(ReturnLoc, diag::err_return_block_has_expr);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000746 RetValExp->Destroy(Context);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000747 RetValExp = 0;
748 }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000749 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Steve Naroff4eb206b2008-09-03 18:15:37 +0000750 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000751
752 if (!RetValExp)
753 return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
754
Mike Stump98eb8a72009-02-04 22:31:32 +0000755 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
756 // we have a non-void block with an expression, continue checking
757 QualType RetValType = RetValExp->getType();
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000758
Mike Stump98eb8a72009-02-04 22:31:32 +0000759 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
760 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
761 // function return.
762
763 // In C++ the return statement is handled via a copy initialization.
764 // the C version of which boils down to CheckSingleAssignmentConstraints.
765 // FIXME: Leaks RetValExp.
766 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
767 return StmtError();
768
769 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000770 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000771
Ted Kremenek8189cde2009-02-07 01:47:29 +0000772 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Steve Naroff4eb206b2008-09-03 18:15:37 +0000773}
Reid Spencer5f016e22007-07-11 17:01:13 +0000774
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000775Action::OwningStmtResult
776Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) {
777 Expr *RetValExp = static_cast<Expr *>(rex.release());
Steve Naroff4eb206b2008-09-03 18:15:37 +0000778 if (CurBlock)
779 return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000780
Chris Lattner371f2582008-12-04 23:50:19 +0000781 QualType FnRetType;
782 if (FunctionDecl *FD = getCurFunctionDecl())
783 FnRetType = FD->getResultType();
784 else
785 FnRetType = getCurMethodDecl()->getResultType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000786
Chris Lattner5cf216b2008-01-04 18:04:52 +0000787 if (FnRetType->isVoidType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000788 if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)
Chris Lattner65ce04b2008-12-18 02:01:17 +0000789 unsigned D = diag::ext_return_has_expr;
790 if (RetValExp->getType()->isVoidType())
791 D = diag::ext_return_has_void_expr;
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000792
Chris Lattnere878eb02008-12-18 02:03:48 +0000793 // return (some void expression); is legal in C++.
794 if (D != diag::ext_return_has_void_expr ||
795 !getLangOptions().CPlusPlus) {
796 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
797 Diag(ReturnLoc, D)
798 << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
799 << RetValExp->getSourceRange();
800 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000802 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000804
Chris Lattner3c73c412008-11-19 08:23:25 +0000805 if (!RetValExp) {
806 unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4
807 // C99 6.8.6.4p1 (ext_ since GCC warns)
808 if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
809
810 if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattner08631c52008-11-23 21:45:46 +0000811 Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
Chris Lattner3c73c412008-11-19 08:23:25 +0000812 else
Chris Lattner08631c52008-11-23 21:45:46 +0000813 Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000814 return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0));
Chris Lattner3c73c412008-11-19 08:23:25 +0000815 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000816
Douglas Gregor898574e2008-12-05 23:32:09 +0000817 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
818 // we have a non-void function with an expression, continue checking
819 QualType RetValType = RetValExp->getType();
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000820
Douglas Gregor898574e2008-12-05 23:32:09 +0000821 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
822 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000823 // function return.
824
Douglas Gregor898574e2008-12-05 23:32:09 +0000825 // In C++ the return statement is handled via a copy initialization.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000826 // the C version of which boils down to CheckSingleAssignmentConstraints.
827 // FIXME: Leaks RetValExp.
Douglas Gregor898574e2008-12-05 23:32:09 +0000828 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000829 return StmtError();
830
Douglas Gregor898574e2008-12-05 23:32:09 +0000831 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
832 }
833
Ted Kremenek8189cde2009-02-07 01:47:29 +0000834 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Reid Spencer5f016e22007-07-11 17:01:13 +0000835}
836
Sebastian Redl3037ed02009-01-18 16:53:17 +0000837Sema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
838 bool IsSimple,
839 bool IsVolatile,
840 unsigned NumOutputs,
841 unsigned NumInputs,
842 std::string *Names,
843 MultiExprArg constraints,
844 MultiExprArg exprs,
845 ExprArg asmString,
846 MultiExprArg clobbers,
847 SourceLocation RParenLoc) {
848 unsigned NumClobbers = clobbers.size();
849 StringLiteral **Constraints =
850 reinterpret_cast<StringLiteral**>(constraints.get());
851 Expr **Exprs = reinterpret_cast<Expr **>(exprs.get());
852 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get());
853 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
854
Anders Carlsson03eb5432009-01-27 20:38:24 +0000855 llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
856
Chris Lattner1708b962008-08-18 19:55:17 +0000857 // The parser verifies that there is a string literal here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000858 if (AsmString->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000859 return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
860 << AsmString->getSourceRange());
861
862
Chris Lattner1708b962008-08-18 19:55:17 +0000863 for (unsigned i = 0; i != NumOutputs; i++) {
864 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000865 if (Literal->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000866 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
867 << Literal->getSourceRange());
868
Anders Carlssond04c6e22007-11-27 04:11:28 +0000869 std::string OutputConstraint(Literal->getStrData(),
870 Literal->getByteLength());
Sebastian Redl3037ed02009-01-18 16:53:17 +0000871
Anders Carlssond04c6e22007-11-27 04:11:28 +0000872 TargetInfo::ConstraintInfo info;
Chris Lattner6bc52112008-07-23 06:46:56 +0000873 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Sebastian Redl3037ed02009-01-18 16:53:17 +0000874 return StmtError(Diag(Literal->getLocStart(),
875 diag::err_asm_invalid_output_constraint) << OutputConstraint);
876
Anders Carlssond04c6e22007-11-27 04:11:28 +0000877 // Check that the output exprs are valid lvalues.
Chris Lattner1708b962008-08-18 19:55:17 +0000878 ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]);
Chris Lattner28be73f2008-07-26 21:30:36 +0000879 Expr::isLvalueResult Result = OutputExpr->isLvalue(Context);
Anders Carlsson04728b72007-11-23 19:43:50 +0000880 if (Result != Expr::LV_Valid) {
Sebastian Redl3037ed02009-01-18 16:53:17 +0000881 return StmtError(Diag(OutputExpr->getSubExpr()->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000882 diag::err_asm_invalid_lvalue_in_output)
Sebastian Redl3037ed02009-01-18 16:53:17 +0000883 << OutputExpr->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000884 }
Anders Carlsson03eb5432009-01-27 20:38:24 +0000885
886 OutputConstraintInfos.push_back(info);
Anders Carlsson04728b72007-11-23 19:43:50 +0000887 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000888
Anders Carlsson04728b72007-11-23 19:43:50 +0000889 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Chris Lattner1708b962008-08-18 19:55:17 +0000890 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000891 if (Literal->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000892 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
893 << Literal->getSourceRange());
894
895 std::string InputConstraint(Literal->getStrData(),
Anders Carlssond04c6e22007-11-27 04:11:28 +0000896 Literal->getByteLength());
Sebastian Redl3037ed02009-01-18 16:53:17 +0000897
Anders Carlssond04c6e22007-11-27 04:11:28 +0000898 TargetInfo::ConstraintInfo info;
899 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
Anders Carlsson45b050e2009-01-17 23:36:15 +0000900 &Names[0],
Anders Carlsson03eb5432009-01-27 20:38:24 +0000901 &Names[0] + NumOutputs,
902 &OutputConstraintInfos[0],
903 info)) {
Sebastian Redl3037ed02009-01-18 16:53:17 +0000904 return StmtError(Diag(Literal->getLocStart(),
905 diag::err_asm_invalid_input_constraint) << InputConstraint);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000906 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000907
Chris Lattner1708b962008-08-18 19:55:17 +0000908 ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]);
Sebastian Redl3037ed02009-01-18 16:53:17 +0000909
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000910 // Only allow void types for memory constraints.
Anders Carlssone6ea2792009-01-21 06:27:20 +0000911 if ((info & TargetInfo::CI_AllowsMemory)
912 && !(info & TargetInfo::CI_AllowsRegister)) {
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000913 if (InputExpr->isLvalue(Context) != Expr::LV_Valid)
914 return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(),
915 diag::err_asm_invalid_lvalue_in_input)
916 << InputConstraint << InputExpr->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000917 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000918
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000919 if (info & TargetInfo::CI_AllowsRegister) {
920 if (InputExpr->getType()->isVoidType()) {
921 return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(),
922 diag::err_asm_invalid_type_in_input)
923 << InputExpr->getType() << InputConstraint
924 << InputExpr->getSubExpr()->getSourceRange());
925 }
926
Anders Carlsson562489e2008-12-31 07:27:38 +0000927 DefaultFunctionArrayConversion(Exprs[i]);
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000928 }
Anders Carlsson04728b72007-11-23 19:43:50 +0000929 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000930
Anders Carlsson6fa90862007-11-25 00:25:21 +0000931 // Check that the clobbers are valid.
Chris Lattner1708b962008-08-18 19:55:17 +0000932 for (unsigned i = 0; i != NumClobbers; i++) {
933 StringLiteral *Literal = Clobbers[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000934 if (Literal->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000935 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
936 << Literal->getSourceRange());
937
938 llvm::SmallString<16> Clobber(Literal->getStrData(),
939 Literal->getStrData() +
Anders Carlsson6fa90862007-11-25 00:25:21 +0000940 Literal->getByteLength());
Sebastian Redl3037ed02009-01-18 16:53:17 +0000941
Chris Lattner6bc52112008-07-23 06:46:56 +0000942 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Sebastian Redl3037ed02009-01-18 16:53:17 +0000943 return StmtError(Diag(Literal->getLocStart(),
944 diag::err_asm_unknown_register_name) << Clobber.c_str());
Anders Carlsson6fa90862007-11-25 00:25:21 +0000945 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000946
947 constraints.release();
948 exprs.release();
949 asmString.release();
950 clobbers.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000951 return Owned(new (Context) AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs,
952 NumInputs, Names, Constraints, Exprs,
953 AsmString, NumClobbers,
954 Clobbers, RParenLoc));
Chris Lattnerfe795952007-10-29 04:04:16 +0000955}
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000956
Sebastian Redl431e90e2009-01-18 17:43:11 +0000957Action::OwningStmtResult
958Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
959 SourceLocation RParen, StmtArg Parm,
960 StmtArg Body, StmtArg catchList) {
961 Stmt *CatchList = static_cast<Stmt*>(catchList.release());
Ted Kremenek8189cde2009-02-07 01:47:29 +0000962 ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen,
Sebastian Redl431e90e2009-01-18 17:43:11 +0000963 static_cast<Stmt*>(Parm.release()), static_cast<Stmt*>(Body.release()),
964 CatchList);
965 return Owned(CatchList ? CatchList : CS);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000966}
967
Sebastian Redl431e90e2009-01-18 17:43:11 +0000968Action::OwningStmtResult
969Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000970 return Owned(new (Context) ObjCAtFinallyStmt(AtLoc,
971 static_cast<Stmt*>(Body.release())));
Fariborz Jahanian161a9c52007-11-02 00:18:53 +0000972}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000973
Sebastian Redl431e90e2009-01-18 17:43:11 +0000974Action::OwningStmtResult
975Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
976 StmtArg Try, StmtArg Catch, StmtArg Finally) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000977 return Owned(new (Context) ObjCAtTryStmt(AtLoc,
978 static_cast<Stmt*>(Try.release()),
Sebastian Redl431e90e2009-01-18 17:43:11 +0000979 static_cast<Stmt*>(Catch.release()),
980 static_cast<Stmt*>(Finally.release())));
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000981}
982
Sebastian Redl431e90e2009-01-18 17:43:11 +0000983Action::OwningStmtResult
Steve Naroff3dcfe102009-02-12 15:54:59 +0000984Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr,Scope *CurScope) {
Steve Naroff7151bbb2009-02-11 17:45:08 +0000985 Expr *ThrowExpr = static_cast<Expr*>(expr.release());
986 if (!ThrowExpr) {
Steve Naroffe21dd6f2009-02-11 20:05:44 +0000987 // @throw without an expression designates a rethrow (which much occur
988 // in the context of an @catch clause).
989 Scope *AtCatchParent = CurScope;
990 while (AtCatchParent && !AtCatchParent->isAtCatchScope())
991 AtCatchParent = AtCatchParent->getParent();
992 if (!AtCatchParent)
Steve Naroff4ab24142009-02-12 18:09:32 +0000993 return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
Steve Naroff7151bbb2009-02-11 17:45:08 +0000994 } else {
995 QualType ThrowType = ThrowExpr->getType();
996 // Make sure the expression type is an ObjC pointer or "void *".
997 if (!Context.isObjCObjectPointerType(ThrowType)) {
998 const PointerType *PT = ThrowType->getAsPointerType();
999 if (!PT || !PT->getPointeeType()->isVoidType())
Steve Naroff4ab24142009-02-12 18:09:32 +00001000 return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
1001 << ThrowExpr->getType() << ThrowExpr->getSourceRange());
Steve Naroff7151bbb2009-02-11 17:45:08 +00001002 }
1003 }
1004 return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowExpr));
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001005}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001006
Sebastian Redl431e90e2009-01-18 17:43:11 +00001007Action::OwningStmtResult
1008Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr,
1009 StmtArg SynchBody) {
Ted Kremenek8189cde2009-02-07 01:47:29 +00001010 return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc,
Sebastian Redl431e90e2009-01-18 17:43:11 +00001011 static_cast<Stmt*>(SynchExpr.release()),
1012 static_cast<Stmt*>(SynchBody.release())));
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001013}
Sebastian Redl4b07b292008-12-22 19:15:10 +00001014
1015/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
1016/// and creates a proper catch handler from them.
1017Action::OwningStmtResult
1018Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclTy *ExDecl,
1019 StmtArg HandlerBlock) {
1020 // There's nothing to test that ActOnExceptionDecl didn't already test.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001021 return Owned(new (Context) CXXCatchStmt(CatchLoc,
1022 static_cast<VarDecl*>(ExDecl),
1023 static_cast<Stmt*>(HandlerBlock.release())));
Sebastian Redl4b07b292008-12-22 19:15:10 +00001024}
Sebastian Redl8351da02008-12-22 21:35:02 +00001025
1026/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
1027/// handlers and creates a try statement from them.
1028Action::OwningStmtResult
1029Sema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock,
1030 MultiStmtArg RawHandlers) {
1031 unsigned NumHandlers = RawHandlers.size();
1032 assert(NumHandlers > 0 &&
1033 "The parser shouldn't call this if there are no handlers.");
1034 Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get());
1035
1036 for(unsigned i = 0; i < NumHandlers - 1; ++i) {
1037 CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
1038 if (!Handler->getExceptionDecl())
1039 return StmtError(Diag(Handler->getLocStart(), diag::err_early_catch_all));
1040 }
1041 // FIXME: We should detect handlers for the same type as an earlier one.
1042 // This one is rather easy.
1043 // FIXME: We should detect handlers that cannot catch anything because an
1044 // earlier handler catches a superclass. Need to find a method that is not
1045 // quadratic for this.
1046 // Neither of these are explicitly forbidden, but every compiler detects them
1047 // and warns.
1048
1049 RawHandlers.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +00001050 return Owned(new (Context) CXXTryStmt(TryLoc,
1051 static_cast<Stmt*>(TryBlock.release()),
1052 Handlers, NumHandlers));
Sebastian Redl8351da02008-12-22 21:35:02 +00001053}