blob: 32d295772cd949d7d398d7fc803f805d83f0c3e8 [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) {
Douglas Gregor9653db72009-02-13 19:06:18 +000061 DeclGroupRef 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 }
Chris Lattner24e1e702009-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));
Reid Spencer5f016e22007-07-11 17:01:13 +000067}
68
Sebastian Redla60528c2008-12-21 12:04:03 +000069Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +000070Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Sebastian Redla60528c2008-12-21 12:04:03 +000071 MultiStmtArg elts, bool isStmtExpr) {
72 unsigned NumElts = elts.size();
73 Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
Chris Lattnerc30ebfb2007-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 Gregor4afa39d2009-01-20 01:17:11 +000088 Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000089 Diag(D->getLocation(), diag::ext_mixed_decls_code);
90 }
91 }
Chris Lattner98414c12007-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 Lattner026dc962009-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 Lattner98414c12007-08-31 21:49:55 +0000100 continue;
101
Chris Lattner026dc962009-02-14 07:37:35 +0000102 SourceLocation Loc;
103 SourceRange R1, R2;
104 if (!E->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner98414c12007-08-31 21:49:55 +0000105 continue;
Chris Lattner026dc962009-02-14 07:37:35 +0000106
107 Diag(Loc, diag::warn_unused_expr) << R1 << R2;
Chris Lattner98414c12007-08-31 21:49:55 +0000108 }
Sebastian Redla60528c2008-12-21 12:04:03 +0000109
Ted Kremenek8189cde2009-02-07 01:47:29 +0000110 return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
Reid Spencer5f016e22007-07-11 17:01:13 +0000111}
112
Sebastian Redl117054a2008-12-28 16:13:43 +0000113Action::OwningStmtResult
114Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval,
115 SourceLocation DotDotDotLoc, ExprArg rhsval,
Chris Lattner24e1e702009-03-04 04:23:07 +0000116 SourceLocation ColonLoc) {
Sebastian Redl117054a2008-12-28 16:13:43 +0000117 assert((lhsval.get() != 0) && "missing expression in case statement");
118
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 // C99 6.8.4.2p3: The expression shall be an integer constant.
Anders Carlsson51fe9962008-11-22 21:04:56 +0000120 // However, GCC allows any evaluatable integer expression.
Sebastian Redl117054a2008-12-28 16:13:43 +0000121 Expr *LHSVal = static_cast<Expr*>(lhsval.get());
Anders Carlssond3a61d52008-12-01 02:13:02 +0000122 if (VerifyIntegerConstantExpression(LHSVal))
Chris Lattner24e1e702009-03-04 04:23:07 +0000123 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000124
Chris Lattner6c36be52007-07-18 02:28:47 +0000125 // GCC extension: The expression shall be an integer constant.
Sebastian Redl117054a2008-12-28 16:13:43 +0000126
127 Expr *RHSVal = static_cast<Expr*>(rhsval.get());
128 if (RHSVal && VerifyIntegerConstantExpression(RHSVal)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000129 RHSVal = 0; // Recover by just forgetting about it.
Sebastian Redl117054a2008-12-28 16:13:43 +0000130 rhsval = 0;
131 }
132
Chris Lattner8a87e572007-07-23 17:05:23 +0000133 if (SwitchStack.empty()) {
134 Diag(CaseLoc, diag::err_case_not_in_switch);
Chris Lattner24e1e702009-03-04 04:23:07 +0000135 return StmtError();
Chris Lattner8a87e572007-07-23 17:05:23 +0000136 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000137
Sebastian Redl117054a2008-12-28 16:13:43 +0000138 // Only now release the smart pointers.
139 lhsval.release();
140 rhsval.release();
Chris Lattner24e1e702009-03-04 04:23:07 +0000141 CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc);
Chris Lattner8a87e572007-07-23 17:05:23 +0000142 SwitchStack.back()->addSwitchCase(CS);
Sebastian Redl117054a2008-12-28 16:13:43 +0000143 return Owned(CS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144}
145
Chris Lattner24e1e702009-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 Redl117054a2008-12-28 16:13:43 +0000153Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000154Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Sebastian Redl117054a2008-12-28 16:13:43 +0000155 StmtArg subStmt, Scope *CurScope) {
156 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
157
Chris Lattner8a87e572007-07-23 17:05:23 +0000158 if (SwitchStack.empty()) {
Chris Lattner0fa152e2007-07-21 03:00:26 +0000159 Diag(DefaultLoc, diag::err_default_not_in_switch);
Sebastian Redl117054a2008-12-28 16:13:43 +0000160 return Owned(SubStmt);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000161 }
Sebastian Redl117054a2008-12-28 16:13:43 +0000162
Ted Kremenek8189cde2009-02-07 01:47:29 +0000163 DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000164 SwitchStack.back()->addSwitchCase(DS);
Sebastian Redl117054a2008-12-28 16:13:43 +0000165 return Owned(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166}
167
Sebastian Redlde307472009-01-11 00:38:46 +0000168Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000169Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Sebastian Redlde307472009-01-11 00:38:46 +0000170 SourceLocation ColonLoc, StmtArg subStmt) {
171 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
Sebastian Redlde307472009-01-11 00:38:46 +0000172
Steve Narofff3cf8972009-02-28 16:48:43 +0000173 // Look up the record for this label identifier.
174 Scope::LabelMapTy::iterator I = ActiveScope->LabelMap.find(II);
175
176 LabelStmt *LabelDecl;
177
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 // If not forward referenced or defined already, just create a new LabelStmt.
Steve Narofff3cf8972009-02-28 16:48:43 +0000179 if (I == ActiveScope->LabelMap.end()) {
180 LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt);
181 ActiveScope->LabelMap.insert(std::make_pair(II, LabelDecl));
182 return Owned(LabelDecl);
183 } else
184 LabelDecl = static_cast<LabelStmt *>(I->second);
Sebastian Redlde307472009-01-11 00:38:46 +0000185
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 assert(LabelDecl->getID() == II && "Label mismatch!");
Sebastian Redlde307472009-01-11 00:38:46 +0000187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 // Otherwise, this label was either forward reference or multiply defined. If
189 // multiply defined, reject it now.
190 if (LabelDecl->getSubStmt()) {
Chris Lattner08631c52008-11-23 21:45:46 +0000191 Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000192 Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
Sebastian Redlde307472009-01-11 00:38:46 +0000193 return Owned(SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 }
Sebastian Redlde307472009-01-11 00:38:46 +0000195
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 // Otherwise, this label was forward declared, and we just found its real
197 // definition. Fill in the forward definition and return it.
198 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000199 LabelDecl->setSubStmt(SubStmt);
Sebastian Redlde307472009-01-11 00:38:46 +0000200 return Owned(LabelDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000201}
202
Sebastian Redlde307472009-01-11 00:38:46 +0000203Action::OwningStmtResult
204Sema::ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal,
205 StmtArg ThenVal, SourceLocation ElseLoc,
206 StmtArg ElseVal) {
207 Expr *condExpr = (Expr *)CondVal.release();
208
Steve Naroff1b273c42007-09-16 14:56:35 +0000209 assert(condExpr && "ActOnIfStmt(): missing expression");
Sebastian Redlde307472009-01-11 00:38:46 +0000210
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000211 DefaultFunctionArrayConversion(condExpr);
Sebastian Redlde307472009-01-11 00:38:46 +0000212 // Take ownership again until we're past the error checking.
213 CondVal = condExpr;
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000214 QualType condType = condExpr->getType();
Sebastian Redlde307472009-01-11 00:38:46 +0000215
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000216 if (getLangOptions().CPlusPlus) {
217 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redlde307472009-01-11 00:38:46 +0000218 return StmtError();
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000219 } else if (!condType->isScalarType()) // C99 6.8.4.1p1
Sebastian Redlde307472009-01-11 00:38:46 +0000220 return StmtError(Diag(IfLoc, diag::err_typecheck_statement_requires_scalar)
221 << condType << condExpr->getSourceRange());
222
223 Stmt *thenStmt = (Stmt *)ThenVal.release();
Reid Spencer5f016e22007-07-11 17:01:13 +0000224
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000225 // Warn if the if block has a null body without an else value.
226 // this helps prevent bugs due to typos, such as
227 // if (condition);
228 // do_stuff();
Sebastian Redlde307472009-01-11 00:38:46 +0000229 if (!ElseVal.get()) {
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000230 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
231 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
232 }
233
Sebastian Redlde307472009-01-11 00:38:46 +0000234 CondVal.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000235 return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt,
236 (Stmt*)ElseVal.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000237}
238
Sebastian Redlde307472009-01-11 00:38:46 +0000239Action::OwningStmtResult
240Sema::ActOnStartOfSwitchStmt(ExprArg cond) {
241 Expr *Cond = static_cast<Expr*>(cond.release());
242
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000243 if (getLangOptions().CPlusPlus) {
244 // C++ 6.4.2.p2:
245 // The condition shall be of integral type, enumeration type, or of a class
246 // type for which a single conversion function to integral or enumeration
247 // type exists (12.3). If the condition is of class type, the condition is
248 // converted by calling that conversion function, and the result of the
249 // conversion is used in place of the original condition for the remainder
250 // of this section. Integral promotions are performed.
251
252 QualType Ty = Cond->getType();
253
254 // FIXME: Handle class types.
255
256 // If the type is wrong a diagnostic will be emitted later at
257 // ActOnFinishSwitchStmt.
258 if (Ty->isIntegralType() || Ty->isEnumeralType()) {
259 // Integral promotions are performed.
260 // FIXME: Integral promotions for C++ are not complete.
261 UsualUnaryConversions(Cond);
262 }
263 } else {
264 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
265 UsualUnaryConversions(Cond);
266 }
Sebastian Redlde307472009-01-11 00:38:46 +0000267
Ted Kremenek8189cde2009-02-07 01:47:29 +0000268 SwitchStmt *SS = new (Context) SwitchStmt(Cond);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000269 SwitchStack.push_back(SS);
Sebastian Redlde307472009-01-11 00:38:46 +0000270 return Owned(SS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000271}
Chris Lattner6c36be52007-07-18 02:28:47 +0000272
Chris Lattnerf4021e72007-08-23 05:46:52 +0000273/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
274/// the specified width and sign. If an overflow occurs, detect it and emit
275/// the specified diagnostic.
276void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
277 unsigned NewWidth, bool NewSign,
278 SourceLocation Loc,
279 unsigned DiagID) {
280 // Perform a conversion to the promoted condition type if needed.
281 if (NewWidth > Val.getBitWidth()) {
282 // If this is an extension, just do it.
283 llvm::APSInt OldVal(Val);
284 Val.extend(NewWidth);
285
286 // If the input was signed and negative and the output is unsigned,
287 // warn.
288 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000289 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000290
291 Val.setIsSigned(NewSign);
292 } else if (NewWidth < Val.getBitWidth()) {
293 // If this is a truncation, check for overflow.
294 llvm::APSInt ConvVal(Val);
295 ConvVal.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000296 ConvVal.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000297 ConvVal.extend(Val.getBitWidth());
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000298 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000299 if (ConvVal != Val)
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000300 Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000301
302 // Regardless of whether a diagnostic was emitted, really do the
303 // truncation.
304 Val.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000305 Val.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000306 } else if (NewSign != Val.isSigned()) {
307 // Convert the sign to match the sign of the condition. This can cause
308 // overflow as well: unsigned(INTMIN)
309 llvm::APSInt OldVal(Val);
310 Val.setIsSigned(NewSign);
311
312 if (Val.isNegative()) // Sign bit changes meaning.
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000313 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000314 }
315}
316
Chris Lattner0471f5b2007-08-23 18:29:20 +0000317namespace {
318 struct CaseCompareFunctor {
319 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
320 const llvm::APSInt &RHS) {
321 return LHS.first < RHS;
322 }
Chris Lattner0e85a272007-09-03 18:31:57 +0000323 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
324 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
325 return LHS.first < RHS.first;
326 }
Chris Lattner0471f5b2007-08-23 18:29:20 +0000327 bool operator()(const llvm::APSInt &LHS,
328 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
329 return LHS < RHS.first;
330 }
331 };
332}
333
Chris Lattner764a7ce2007-09-21 18:15:22 +0000334/// CmpCaseVals - Comparison predicate for sorting case values.
335///
336static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
337 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
338 if (lhs.first < rhs.first)
339 return true;
340
341 if (lhs.first == rhs.first &&
342 lhs.second->getCaseLoc().getRawEncoding()
343 < rhs.second->getCaseLoc().getRawEncoding())
344 return true;
345 return false;
346}
347
Sebastian Redlde307472009-01-11 00:38:46 +0000348Action::OwningStmtResult
349Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
350 StmtArg Body) {
351 Stmt *BodyStmt = (Stmt*)Body.release();
352
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000353 SwitchStmt *SS = SwitchStack.back();
Sebastian Redlde307472009-01-11 00:38:46 +0000354 assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!");
355
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000356 SS->setBody(BodyStmt, SwitchLoc);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000357 SwitchStack.pop_back();
358
Chris Lattnerf4021e72007-08-23 05:46:52 +0000359 Expr *CondExpr = SS->getCond();
360 QualType CondType = CondExpr->getType();
Sebastian Redlde307472009-01-11 00:38:46 +0000361
Chris Lattnerf4021e72007-08-23 05:46:52 +0000362 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000363 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000364 << CondType << CondExpr->getSourceRange();
Sebastian Redlde307472009-01-11 00:38:46 +0000365 return StmtError();
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000366 }
Sebastian Redlde307472009-01-11 00:38:46 +0000367
Chris Lattnerf4021e72007-08-23 05:46:52 +0000368 // Get the bitwidth of the switched-on value before promotions. We must
369 // convert the integer case values to this width before comparison.
Chris Lattner98be4942008-03-05 18:54:05 +0000370 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000371 bool CondIsSigned = CondType->isSignedIntegerType();
372
373 // Accumulate all of the case values in a vector so that we can sort them
374 // and detect duplicates. This vector contains the APInt for the case after
375 // it has been converted to the condition type.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000376 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
377 CaseValsTy CaseVals;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000378
379 // Keep track of any GNU case ranges we see. The APSInt is the low value.
380 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
381
382 DefaultStmt *TheDefaultStmt = 0;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000383
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000384 bool CaseListIsErroneous = false;
385
386 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000387 SC = SC->getNextSwitchCase()) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000388
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000389 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000390 if (TheDefaultStmt) {
391 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
Chris Lattner5f4a6822008-11-23 23:12:31 +0000392 Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
Sebastian Redlde307472009-01-11 00:38:46 +0000393
Chris Lattnerf4021e72007-08-23 05:46:52 +0000394 // FIXME: Remove the default statement from the switch block so that
395 // we'll return a valid AST. This requires recursing down the
396 // AST and finding it, not something we are set up to do right now. For
397 // now, just lop the entire switch stmt out of the AST.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000398 CaseListIsErroneous = true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000399 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000400 TheDefaultStmt = DS;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000401
Chris Lattnerf4021e72007-08-23 05:46:52 +0000402 } else {
403 CaseStmt *CS = cast<CaseStmt>(SC);
404
405 // We already verified that the expression has a i-c-e value (C99
406 // 6.8.4.2p3) - get that value now.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000407 Expr *Lo = CS->getLHS();
Anders Carlsson51fe9962008-11-22 21:04:56 +0000408 llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000409
410 // Convert the value to the same width/sign as the condition.
411 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
412 CS->getLHS()->getLocStart(),
413 diag::warn_case_value_overflow);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000414
Chris Lattner1e0a3902008-01-16 19:17:22 +0000415 // If the LHS is not the same type as the condition, insert an implicit
416 // cast.
417 ImpCastExprToType(Lo, CondType);
418 CS->setLHS(Lo);
419
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000420 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerf4021e72007-08-23 05:46:52 +0000421 if (CS->getRHS())
422 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000423 else
424 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000425 }
426 }
427
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000428 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner764a7ce2007-09-21 18:15:22 +0000429 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000430
Chris Lattnerf3348502007-08-23 14:29:07 +0000431 if (!CaseVals.empty()) {
432 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
433 if (CaseVals[i].first == CaseVals[i+1].first) {
434 // If we have a duplicate, report it.
435 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000436 diag::err_duplicate_case) << CaseVals[i].first.toString(10);
Chris Lattnerf3348502007-08-23 14:29:07 +0000437 Diag(CaseVals[i].second->getLHS()->getLocStart(),
Chris Lattner5f4a6822008-11-23 23:12:31 +0000438 diag::note_duplicate_case_prev);
Chris Lattnerf3348502007-08-23 14:29:07 +0000439 // FIXME: We really want to remove the bogus case stmt from the substmt,
440 // but we have no way to do this right now.
441 CaseListIsErroneous = true;
442 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000443 }
444 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000445
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000446 // Detect duplicate case ranges, which usually don't exist at all in the first
447 // place.
448 if (!CaseRanges.empty()) {
449 // Sort all the case ranges by their low value so we can easily detect
450 // overlaps between ranges.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000451 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000452
453 // Scan the ranges, computing the high values and removing empty ranges.
454 std::vector<llvm::APSInt> HiVals;
Chris Lattner6efc4d32007-08-23 17:48:14 +0000455 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000456 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000457 Expr *Hi = CR->getRHS();
Anders Carlsson51fe9962008-11-22 21:04:56 +0000458 llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000459
460 // Convert the value to the same width/sign as the condition.
461 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
462 CR->getRHS()->getLocStart(),
463 diag::warn_case_value_overflow);
464
Chris Lattner1e0a3902008-01-16 19:17:22 +0000465 // If the LHS is not the same type as the condition, insert an implicit
466 // cast.
467 ImpCastExprToType(Hi, CondType);
468 CR->setRHS(Hi);
469
Chris Lattner6efc4d32007-08-23 17:48:14 +0000470 // If the low value is bigger than the high value, the case is empty.
471 if (CaseRanges[i].first > HiVal) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000472 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
473 << SourceRange(CR->getLHS()->getLocStart(),
474 CR->getRHS()->getLocEnd());
Chris Lattner6efc4d32007-08-23 17:48:14 +0000475 CaseRanges.erase(CaseRanges.begin()+i);
476 --i, --e;
477 continue;
478 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000479 HiVals.push_back(HiVal);
480 }
481
482 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0471f5b2007-08-23 18:29:20 +0000483 // ranges. Since the range list is sorted, we only need to compare case
484 // ranges with their neighbors.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000485 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0471f5b2007-08-23 18:29:20 +0000486 llvm::APSInt &CRLo = CaseRanges[i].first;
487 llvm::APSInt &CRHi = HiVals[i];
488 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000489
Chris Lattner0471f5b2007-08-23 18:29:20 +0000490 // Check to see whether the case range overlaps with any singleton cases.
491 CaseStmt *OverlapStmt = 0;
492 llvm::APSInt OverlapVal(32);
493
494 // Find the smallest value >= the lower bound. If I is in the case range,
495 // then we have overlap.
496 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
497 CaseVals.end(), CRLo,
498 CaseCompareFunctor());
499 if (I != CaseVals.end() && I->first < CRHi) {
500 OverlapVal = I->first; // Found overlap with scalar.
501 OverlapStmt = I->second;
502 }
503
504 // Find the smallest value bigger than the upper bound.
505 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
506 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
507 OverlapVal = (I-1)->first; // Found overlap with scalar.
508 OverlapStmt = (I-1)->second;
509 }
510
511 // Check to see if this case stmt overlaps with the subsequent case range.
512 if (i && CRLo <= HiVals[i-1]) {
513 OverlapVal = HiVals[i-1]; // Found overlap with range.
514 OverlapStmt = CaseRanges[i-1].second;
515 }
516
517 if (OverlapStmt) {
518 // If we have a duplicate, report it.
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000519 Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
520 << OverlapVal.toString(10);
Chris Lattner0471f5b2007-08-23 18:29:20 +0000521 Diag(OverlapStmt->getLHS()->getLocStart(),
Chris Lattner5f4a6822008-11-23 23:12:31 +0000522 diag::note_duplicate_case_prev);
Chris Lattner0471f5b2007-08-23 18:29:20 +0000523 // FIXME: We really want to remove the bogus case stmt from the substmt,
524 // but we have no way to do this right now.
525 CaseListIsErroneous = true;
526 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000527 }
528 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000529
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000530 // FIXME: If the case list was broken is some way, we don't have a good system
531 // to patch it up. Instead, just return the whole substmt as broken.
532 if (CaseListIsErroneous)
Sebastian Redlde307472009-01-11 00:38:46 +0000533 return StmtError();
534
535 Switch.release();
536 return Owned(SS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000537}
538
Sebastian Redlf05b1522009-01-16 23:28:06 +0000539Action::OwningStmtResult
540Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, StmtArg Body) {
541 Expr *condExpr = (Expr *)Cond.release();
Steve Naroff1b273c42007-09-16 14:56:35 +0000542 assert(condExpr && "ActOnWhileStmt(): missing expression");
Sebastian Redlf05b1522009-01-16 23:28:06 +0000543
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000544 DefaultFunctionArrayConversion(condExpr);
Sebastian Redlf05b1522009-01-16 23:28:06 +0000545 Cond = condExpr;
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000546 QualType condType = condExpr->getType();
Sebastian Redlf05b1522009-01-16 23:28:06 +0000547
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000548 if (getLangOptions().CPlusPlus) {
549 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redlf05b1522009-01-16 23:28:06 +0000550 return StmtError();
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000551 } else if (!condType->isScalarType()) // C99 6.8.5p2
Sebastian Redlf05b1522009-01-16 23:28:06 +0000552 return StmtError(Diag(WhileLoc,
553 diag::err_typecheck_statement_requires_scalar)
554 << condType << condExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000555
Sebastian Redlf05b1522009-01-16 23:28:06 +0000556 Cond.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000557 return Owned(new (Context) WhileStmt(condExpr, (Stmt*)Body.release(),
558 WhileLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000559}
560
Sebastian Redlf05b1522009-01-16 23:28:06 +0000561Action::OwningStmtResult
562Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body,
563 SourceLocation WhileLoc, ExprArg Cond) {
564 Expr *condExpr = (Expr *)Cond.release();
Steve Naroff1b273c42007-09-16 14:56:35 +0000565 assert(condExpr && "ActOnDoStmt(): missing expression");
Sebastian Redlf05b1522009-01-16 23:28:06 +0000566
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000567 DefaultFunctionArrayConversion(condExpr);
Sebastian Redlf05b1522009-01-16 23:28:06 +0000568 Cond = condExpr;
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000569 QualType condType = condExpr->getType();
Sebastian Redlf05b1522009-01-16 23:28:06 +0000570
Argyrios Kyrtzidis6314ff22008-09-11 05:16:22 +0000571 if (getLangOptions().CPlusPlus) {
572 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redlf05b1522009-01-16 23:28:06 +0000573 return StmtError();
Argyrios Kyrtzidis6314ff22008-09-11 05:16:22 +0000574 } else if (!condType->isScalarType()) // C99 6.8.5p2
Sebastian Redlf05b1522009-01-16 23:28:06 +0000575 return StmtError(Diag(DoLoc, diag::err_typecheck_statement_requires_scalar)
576 << condType << condExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000577
Sebastian Redlf05b1522009-01-16 23:28:06 +0000578 Cond.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000579 return Owned(new (Context) DoStmt((Stmt*)Body.release(), condExpr, DoLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000580}
581
Sebastian Redlf05b1522009-01-16 23:28:06 +0000582Action::OwningStmtResult
583Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
584 StmtArg first, ExprArg second, ExprArg third,
585 SourceLocation RParenLoc, StmtArg body) {
586 Stmt *First = static_cast<Stmt*>(first.get());
587 Expr *Second = static_cast<Expr*>(second.get());
588 Expr *Third = static_cast<Expr*>(third.get());
589 Stmt *Body = static_cast<Stmt*>(body.get());
590
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000591 if (!getLangOptions().CPlusPlus) {
592 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000593 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
594 // declare identifiers for objects having storage class 'auto' or
595 // 'register'.
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000596 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
597 DI!=DE; ++DI) {
598 VarDecl *VD = dyn_cast<VarDecl>(*DI);
599 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
600 VD = 0;
601 if (VD == 0)
602 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
603 // FIXME: mark decl erroneous!
604 }
Chris Lattnerae3b7012007-08-28 05:03:08 +0000605 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 }
607 if (Second) {
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000608 DefaultFunctionArrayConversion(Second);
609 QualType SecondType = Second->getType();
Sebastian Redlf05b1522009-01-16 23:28:06 +0000610
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000611 if (getLangOptions().CPlusPlus) {
612 if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
Sebastian Redlf05b1522009-01-16 23:28:06 +0000613 return StmtError();
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000614 } else if (!SecondType->isScalarType()) // C99 6.8.5p2
Sebastian Redlf05b1522009-01-16 23:28:06 +0000615 return StmtError(Diag(ForLoc,
616 diag::err_typecheck_statement_requires_scalar)
617 << SecondType << Second->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 }
Sebastian Redlf05b1522009-01-16 23:28:06 +0000619 first.release();
620 second.release();
621 third.release();
622 body.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000623 return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000624}
625
Sebastian Redlf05b1522009-01-16 23:28:06 +0000626Action::OwningStmtResult
627Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
628 SourceLocation LParenLoc,
629 StmtArg first, ExprArg second,
630 SourceLocation RParenLoc, StmtArg body) {
631 Stmt *First = static_cast<Stmt*>(first.get());
632 Expr *Second = static_cast<Expr*>(second.get());
633 Stmt *Body = static_cast<Stmt*>(body.get());
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000634 if (First) {
635 QualType FirstType;
636 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
Ted Kremenekf34afee2008-10-06 20:58:11 +0000637 if (!DS->hasSolitaryDecl())
Sebastian Redlf05b1522009-01-16 23:28:06 +0000638 return StmtError(Diag((*DS->decl_begin())->getLocation(),
639 diag::err_toomany_element_decls));
640
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000641 Decl *D = DS->getSolitaryDecl();
Ted Kremenekf34afee2008-10-06 20:58:11 +0000642 FirstType = cast<ValueDecl>(D)->getType();
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000643 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
644 // declare identifiers for objects having storage class 'auto' or
645 // 'register'.
Steve Naroff248a7532008-04-15 22:42:06 +0000646 VarDecl *VD = cast<VarDecl>(D);
647 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
Sebastian Redlf05b1522009-01-16 23:28:06 +0000648 return StmtError(Diag(VD->getLocation(),
649 diag::err_non_variable_decl_in_for));
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000650 } else {
651 Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context);
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000652
Sebastian Redlf05b1522009-01-16 23:28:06 +0000653 if (lval != Expr::LV_Valid)
654 return StmtError(Diag(First->getLocStart(),
655 diag::err_selector_element_not_lvalue)
656 << First->getSourceRange());
657
658 FirstType = static_cast<Expr*>(First)->getType();
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000659 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000660 if (!Context.isObjCObjectPointerType(FirstType))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000661 Diag(ForLoc, diag::err_selector_element_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000662 << FirstType << First->getSourceRange();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000663 }
664 if (Second) {
665 DefaultFunctionArrayConversion(Second);
666 QualType SecondType = Second->getType();
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000667 if (!Context.isObjCObjectPointerType(SecondType))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000668 Diag(ForLoc, diag::err_collection_expr_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000669 << SecondType << Second->getSourceRange();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000670 }
Sebastian Redlf05b1522009-01-16 23:28:06 +0000671 first.release();
672 second.release();
673 body.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000674 return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
675 ForLoc, RParenLoc));
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000676}
Reid Spencer5f016e22007-07-11 17:01:13 +0000677
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000678Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000679Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 IdentifierInfo *LabelII) {
Steve Naroff4eb206b2008-09-03 18:15:37 +0000681 // If we are in a block, reject all gotos for now.
682 if (CurBlock)
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000683 return StmtError(Diag(GotoLoc, diag::err_goto_in_block));
Steve Naroff4eb206b2008-09-03 18:15:37 +0000684
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 // Look up the record for this label identifier.
Steve Narofff3cf8972009-02-28 16:48:43 +0000686 Scope::LabelMapTy::iterator I = ActiveScope->LabelMap.find(LabelII);
Reid Spencer5f016e22007-07-11 17:01:13 +0000687
Steve Narofff3cf8972009-02-28 16:48:43 +0000688 LabelStmt *LabelDecl;
689
690 // If not forward referenced or defined already, just create a new LabelStmt.
691 if (I == ActiveScope->LabelMap.end()) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000692 LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
Steve Narofff3cf8972009-02-28 16:48:43 +0000693 ActiveScope->LabelMap.insert(std::make_pair(LabelII, LabelDecl));
694 } else
695 LabelDecl = static_cast<LabelStmt *>(I->second);
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000696
Ted Kremenek8189cde2009-02-07 01:47:29 +0000697 return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
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::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000702 ExprArg DestExp) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 // FIXME: Verify that the operand is convertible to void*.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000704
Ted Kremenek8189cde2009-02-07 01:47:29 +0000705 return Owned(new (Context) IndirectGotoStmt((Expr*)DestExp.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000706}
707
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000708Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000709Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 Scope *S = CurScope->getContinueParent();
711 if (!S) {
712 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000713 return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000715
Ted Kremenek8189cde2009-02-07 01:47:29 +0000716 return Owned(new (Context) ContinueStmt(ContinueLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000717}
718
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000719Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000720Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 Scope *S = CurScope->getBreakParent();
722 if (!S) {
723 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000724 return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000726
Ted Kremenek8189cde2009-02-07 01:47:29 +0000727 return Owned(new (Context) BreakStmt(BreakLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000728}
729
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000730/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000731///
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000732Action::OwningStmtResult
Steve Naroff4eb206b2008-09-03 18:15:37 +0000733Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000734
Steve Naroff4eb206b2008-09-03 18:15:37 +0000735 // If this is the first return we've seen in the block, infer the type of
736 // the block from it.
737 if (CurBlock->ReturnType == 0) {
Steve Naroffc50a4a52008-09-16 22:25:10 +0000738 if (RetValExp) {
Steve Naroff16564422008-09-24 22:26:48 +0000739 // Don't call UsualUnaryConversions(), since we don't want to do
740 // integer promotions here.
741 DefaultFunctionArrayConversion(RetValExp);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000742 CurBlock->ReturnType = RetValExp->getType().getTypePtr();
Steve Naroffc50a4a52008-09-16 22:25:10 +0000743 } else
Steve Naroff4eb206b2008-09-03 18:15:37 +0000744 CurBlock->ReturnType = Context.VoidTy.getTypePtr();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000745 }
Mike Stump98eb8a72009-02-04 22:31:32 +0000746 QualType FnRetType = QualType(CurBlock->ReturnType, 0);
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000747
Steve Naroff4eb206b2008-09-03 18:15:37 +0000748 // Otherwise, verify that this result type matches the previous one. We are
749 // pickier with blocks than for normal functions because we don't have GCC
750 // compatibility to worry about here.
751 if (CurBlock->ReturnType->isVoidType()) {
752 if (RetValExp) {
753 Diag(ReturnLoc, diag::err_return_block_has_expr);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000754 RetValExp->Destroy(Context);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000755 RetValExp = 0;
756 }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000757 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Steve Naroff4eb206b2008-09-03 18:15:37 +0000758 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000759
760 if (!RetValExp)
761 return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
762
Mike Stump98eb8a72009-02-04 22:31:32 +0000763 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
764 // we have a non-void block with an expression, continue checking
765 QualType RetValType = RetValExp->getType();
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000766
Mike Stump98eb8a72009-02-04 22:31:32 +0000767 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
768 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
769 // function return.
770
771 // In C++ the return statement is handled via a copy initialization.
772 // the C version of which boils down to CheckSingleAssignmentConstraints.
773 // FIXME: Leaks RetValExp.
774 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
775 return StmtError();
776
777 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000778 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000779
Ted Kremenek8189cde2009-02-07 01:47:29 +0000780 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Steve Naroff4eb206b2008-09-03 18:15:37 +0000781}
Reid Spencer5f016e22007-07-11 17:01:13 +0000782
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000783Action::OwningStmtResult
784Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) {
785 Expr *RetValExp = static_cast<Expr *>(rex.release());
Steve Naroff4eb206b2008-09-03 18:15:37 +0000786 if (CurBlock)
787 return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000788
Chris Lattner371f2582008-12-04 23:50:19 +0000789 QualType FnRetType;
790 if (FunctionDecl *FD = getCurFunctionDecl())
791 FnRetType = FD->getResultType();
Steve Naroffc97fb9a2009-03-03 00:45:38 +0000792 else if (ObjCMethodDecl *MD = getCurMethodDecl())
793 FnRetType = MD->getResultType();
794 else // If we don't have a function/method context, bail.
795 return StmtError();
796
Chris Lattner5cf216b2008-01-04 18:04:52 +0000797 if (FnRetType->isVoidType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000798 if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)
Chris Lattner65ce04b2008-12-18 02:01:17 +0000799 unsigned D = diag::ext_return_has_expr;
800 if (RetValExp->getType()->isVoidType())
801 D = diag::ext_return_has_void_expr;
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000802
Chris Lattnere878eb02008-12-18 02:03:48 +0000803 // return (some void expression); is legal in C++.
804 if (D != diag::ext_return_has_void_expr ||
805 !getLangOptions().CPlusPlus) {
806 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
807 Diag(ReturnLoc, D)
808 << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
809 << RetValExp->getSourceRange();
810 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000812 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000814
Chris Lattner3c73c412008-11-19 08:23:25 +0000815 if (!RetValExp) {
816 unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4
817 // C99 6.8.6.4p1 (ext_ since GCC warns)
818 if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
819
820 if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattner08631c52008-11-23 21:45:46 +0000821 Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
Chris Lattner3c73c412008-11-19 08:23:25 +0000822 else
Chris Lattner08631c52008-11-23 21:45:46 +0000823 Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000824 return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0));
Chris Lattner3c73c412008-11-19 08:23:25 +0000825 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000826
Douglas Gregor898574e2008-12-05 23:32:09 +0000827 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
828 // we have a non-void function with an expression, continue checking
829 QualType RetValType = RetValExp->getType();
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000830
Douglas Gregor898574e2008-12-05 23:32:09 +0000831 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
832 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000833 // function return.
834
Douglas Gregor898574e2008-12-05 23:32:09 +0000835 // In C++ the return statement is handled via a copy initialization.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000836 // the C version of which boils down to CheckSingleAssignmentConstraints.
837 // FIXME: Leaks RetValExp.
Douglas Gregor898574e2008-12-05 23:32:09 +0000838 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000839 return StmtError();
840
Douglas Gregor898574e2008-12-05 23:32:09 +0000841 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
842 }
843
Ted Kremenek8189cde2009-02-07 01:47:29 +0000844 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Reid Spencer5f016e22007-07-11 17:01:13 +0000845}
846
Sebastian Redl3037ed02009-01-18 16:53:17 +0000847Sema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
848 bool IsSimple,
849 bool IsVolatile,
850 unsigned NumOutputs,
851 unsigned NumInputs,
852 std::string *Names,
853 MultiExprArg constraints,
854 MultiExprArg exprs,
855 ExprArg asmString,
856 MultiExprArg clobbers,
857 SourceLocation RParenLoc) {
858 unsigned NumClobbers = clobbers.size();
859 StringLiteral **Constraints =
860 reinterpret_cast<StringLiteral**>(constraints.get());
861 Expr **Exprs = reinterpret_cast<Expr **>(exprs.get());
862 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get());
863 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
864
Anders Carlsson03eb5432009-01-27 20:38:24 +0000865 llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
866
Chris Lattner1708b962008-08-18 19:55:17 +0000867 // The parser verifies that there is a string literal here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000868 if (AsmString->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000869 return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
870 << AsmString->getSourceRange());
871
872
Chris Lattner1708b962008-08-18 19:55:17 +0000873 for (unsigned i = 0; i != NumOutputs; i++) {
874 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000875 if (Literal->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000876 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
877 << Literal->getSourceRange());
878
Anders Carlssond04c6e22007-11-27 04:11:28 +0000879 std::string OutputConstraint(Literal->getStrData(),
880 Literal->getByteLength());
Sebastian Redl3037ed02009-01-18 16:53:17 +0000881
Anders Carlssond04c6e22007-11-27 04:11:28 +0000882 TargetInfo::ConstraintInfo info;
Chris Lattner6bc52112008-07-23 06:46:56 +0000883 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Sebastian Redl3037ed02009-01-18 16:53:17 +0000884 return StmtError(Diag(Literal->getLocStart(),
885 diag::err_asm_invalid_output_constraint) << OutputConstraint);
886
Anders Carlssond04c6e22007-11-27 04:11:28 +0000887 // Check that the output exprs are valid lvalues.
Chris Lattner1708b962008-08-18 19:55:17 +0000888 ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]);
Chris Lattner28be73f2008-07-26 21:30:36 +0000889 Expr::isLvalueResult Result = OutputExpr->isLvalue(Context);
Anders Carlsson04728b72007-11-23 19:43:50 +0000890 if (Result != Expr::LV_Valid) {
Sebastian Redl3037ed02009-01-18 16:53:17 +0000891 return StmtError(Diag(OutputExpr->getSubExpr()->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000892 diag::err_asm_invalid_lvalue_in_output)
Sebastian Redl3037ed02009-01-18 16:53:17 +0000893 << OutputExpr->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000894 }
Anders Carlsson03eb5432009-01-27 20:38:24 +0000895
896 OutputConstraintInfos.push_back(info);
Anders Carlsson04728b72007-11-23 19:43:50 +0000897 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000898
Anders Carlsson04728b72007-11-23 19:43:50 +0000899 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Chris Lattner1708b962008-08-18 19:55:17 +0000900 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000901 if (Literal->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000902 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
903 << Literal->getSourceRange());
904
905 std::string InputConstraint(Literal->getStrData(),
Anders Carlssond04c6e22007-11-27 04:11:28 +0000906 Literal->getByteLength());
Sebastian Redl3037ed02009-01-18 16:53:17 +0000907
Anders Carlssond04c6e22007-11-27 04:11:28 +0000908 TargetInfo::ConstraintInfo info;
909 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
Anders Carlsson45b050e2009-01-17 23:36:15 +0000910 &Names[0],
Anders Carlsson03eb5432009-01-27 20:38:24 +0000911 &Names[0] + NumOutputs,
912 &OutputConstraintInfos[0],
913 info)) {
Sebastian Redl3037ed02009-01-18 16:53:17 +0000914 return StmtError(Diag(Literal->getLocStart(),
915 diag::err_asm_invalid_input_constraint) << InputConstraint);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000916 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000917
Chris Lattner1708b962008-08-18 19:55:17 +0000918 ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]);
Sebastian Redl3037ed02009-01-18 16:53:17 +0000919
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000920 // Only allow void types for memory constraints.
Anders Carlssone6ea2792009-01-21 06:27:20 +0000921 if ((info & TargetInfo::CI_AllowsMemory)
922 && !(info & TargetInfo::CI_AllowsRegister)) {
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000923 if (InputExpr->isLvalue(Context) != Expr::LV_Valid)
924 return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(),
925 diag::err_asm_invalid_lvalue_in_input)
926 << InputConstraint << InputExpr->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000927 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000928
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000929 if (info & TargetInfo::CI_AllowsRegister) {
930 if (InputExpr->getType()->isVoidType()) {
931 return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(),
932 diag::err_asm_invalid_type_in_input)
933 << InputExpr->getType() << InputConstraint
934 << InputExpr->getSubExpr()->getSourceRange());
935 }
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000936 }
Anders Carlsson60329792009-02-22 02:11:23 +0000937
938 DefaultFunctionArrayConversion(Exprs[i]);
Anders Carlsson04728b72007-11-23 19:43:50 +0000939 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000940
Anders Carlsson6fa90862007-11-25 00:25:21 +0000941 // Check that the clobbers are valid.
Chris Lattner1708b962008-08-18 19:55:17 +0000942 for (unsigned i = 0; i != NumClobbers; i++) {
943 StringLiteral *Literal = Clobbers[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000944 if (Literal->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000945 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
946 << Literal->getSourceRange());
947
948 llvm::SmallString<16> Clobber(Literal->getStrData(),
949 Literal->getStrData() +
Anders Carlsson6fa90862007-11-25 00:25:21 +0000950 Literal->getByteLength());
Sebastian Redl3037ed02009-01-18 16:53:17 +0000951
Chris Lattner6bc52112008-07-23 06:46:56 +0000952 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Sebastian Redl3037ed02009-01-18 16:53:17 +0000953 return StmtError(Diag(Literal->getLocStart(),
954 diag::err_asm_unknown_register_name) << Clobber.c_str());
Anders Carlsson6fa90862007-11-25 00:25:21 +0000955 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000956
957 constraints.release();
958 exprs.release();
959 asmString.release();
960 clobbers.release();
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000961 AsmStmt *NS =
962 new (Context) AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
963 Names, Constraints, Exprs, AsmString, NumClobbers,
964 Clobbers, RParenLoc);
965 // Validate the asm string, ensuring it makes sense given the operands we
966 // have.
967 llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces;
968 unsigned DiagOffs;
969 if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
Chris Lattner2ff0f422009-03-10 23:57:07 +0000970 Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
971 << AsmString->getSourceRange();
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000972 DeleteStmt(NS);
973 return StmtError();
974 }
975
976
977 return Owned(NS);
Chris Lattnerfe795952007-10-29 04:04:16 +0000978}
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000979
Sebastian Redl431e90e2009-01-18 17:43:11 +0000980Action::OwningStmtResult
981Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000982 SourceLocation RParen, DeclTy *Parm,
Sebastian Redl431e90e2009-01-18 17:43:11 +0000983 StmtArg Body, StmtArg catchList) {
984 Stmt *CatchList = static_cast<Stmt*>(catchList.release());
Steve Narofff50cb362009-03-03 20:59:06 +0000985 ParmVarDecl *PVD = static_cast<ParmVarDecl*>(Parm);
986
987 // PVD == 0 implies @catch(...).
Steve Naroff9d40ee52009-03-03 21:16:54 +0000988 if (PVD) {
989 if (!Context.isObjCObjectPointerType(PVD->getType()))
990 return StmtError(Diag(PVD->getLocation(),
991 diag::err_catch_param_not_objc_type));
992 if (PVD->getType()->isObjCQualifiedIdType())
993 return StmtError(Diag(PVD->getLocation(),
Steve Naroffd198aba2009-03-03 23:13:51 +0000994 diag::err_illegal_qualifiers_on_catch_parm));
Steve Naroff9d40ee52009-03-03 21:16:54 +0000995 }
Steve Narofff50cb362009-03-03 20:59:06 +0000996
Ted Kremenek8189cde2009-02-07 01:47:29 +0000997 ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen,
Steve Narofff50cb362009-03-03 20:59:06 +0000998 PVD, static_cast<Stmt*>(Body.release()), CatchList);
Sebastian Redl431e90e2009-01-18 17:43:11 +0000999 return Owned(CatchList ? CatchList : CS);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001000}
1001
Sebastian Redl431e90e2009-01-18 17:43:11 +00001002Action::OwningStmtResult
1003Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) {
Ted Kremenek8189cde2009-02-07 01:47:29 +00001004 return Owned(new (Context) ObjCAtFinallyStmt(AtLoc,
1005 static_cast<Stmt*>(Body.release())));
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001006}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001007
Sebastian Redl431e90e2009-01-18 17:43:11 +00001008Action::OwningStmtResult
1009Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
1010 StmtArg Try, StmtArg Catch, StmtArg Finally) {
Ted Kremenek8189cde2009-02-07 01:47:29 +00001011 return Owned(new (Context) ObjCAtTryStmt(AtLoc,
1012 static_cast<Stmt*>(Try.release()),
Sebastian Redl431e90e2009-01-18 17:43:11 +00001013 static_cast<Stmt*>(Catch.release()),
1014 static_cast<Stmt*>(Finally.release())));
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001015}
1016
Sebastian Redl431e90e2009-01-18 17:43:11 +00001017Action::OwningStmtResult
Steve Naroff3dcfe102009-02-12 15:54:59 +00001018Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr,Scope *CurScope) {
Steve Naroff7151bbb2009-02-11 17:45:08 +00001019 Expr *ThrowExpr = static_cast<Expr*>(expr.release());
1020 if (!ThrowExpr) {
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001021 // @throw without an expression designates a rethrow (which much occur
1022 // in the context of an @catch clause).
1023 Scope *AtCatchParent = CurScope;
1024 while (AtCatchParent && !AtCatchParent->isAtCatchScope())
1025 AtCatchParent = AtCatchParent->getParent();
1026 if (!AtCatchParent)
Steve Naroff4ab24142009-02-12 18:09:32 +00001027 return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
Steve Naroff7151bbb2009-02-11 17:45:08 +00001028 } else {
1029 QualType ThrowType = ThrowExpr->getType();
1030 // Make sure the expression type is an ObjC pointer or "void *".
1031 if (!Context.isObjCObjectPointerType(ThrowType)) {
1032 const PointerType *PT = ThrowType->getAsPointerType();
1033 if (!PT || !PT->getPointeeType()->isVoidType())
Steve Naroff4ab24142009-02-12 18:09:32 +00001034 return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
1035 << ThrowExpr->getType() << ThrowExpr->getSourceRange());
Steve Naroff7151bbb2009-02-11 17:45:08 +00001036 }
1037 }
1038 return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowExpr));
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001039}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001040
Sebastian Redl431e90e2009-01-18 17:43:11 +00001041Action::OwningStmtResult
1042Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr,
1043 StmtArg SynchBody) {
Ted Kremenek8189cde2009-02-07 01:47:29 +00001044 return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc,
Sebastian Redl431e90e2009-01-18 17:43:11 +00001045 static_cast<Stmt*>(SynchExpr.release()),
1046 static_cast<Stmt*>(SynchBody.release())));
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001047}
Sebastian Redl4b07b292008-12-22 19:15:10 +00001048
1049/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
1050/// and creates a proper catch handler from them.
1051Action::OwningStmtResult
1052Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclTy *ExDecl,
1053 StmtArg HandlerBlock) {
1054 // There's nothing to test that ActOnExceptionDecl didn't already test.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001055 return Owned(new (Context) CXXCatchStmt(CatchLoc,
1056 static_cast<VarDecl*>(ExDecl),
1057 static_cast<Stmt*>(HandlerBlock.release())));
Sebastian Redl4b07b292008-12-22 19:15:10 +00001058}
Sebastian Redl8351da02008-12-22 21:35:02 +00001059
1060/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
1061/// handlers and creates a try statement from them.
1062Action::OwningStmtResult
1063Sema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock,
1064 MultiStmtArg RawHandlers) {
1065 unsigned NumHandlers = RawHandlers.size();
1066 assert(NumHandlers > 0 &&
1067 "The parser shouldn't call this if there are no handlers.");
1068 Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get());
1069
1070 for(unsigned i = 0; i < NumHandlers - 1; ++i) {
1071 CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
1072 if (!Handler->getExceptionDecl())
1073 return StmtError(Diag(Handler->getLocStart(), diag::err_early_catch_all));
1074 }
1075 // FIXME: We should detect handlers for the same type as an earlier one.
1076 // This one is rather easy.
1077 // FIXME: We should detect handlers that cannot catch anything because an
1078 // earlier handler catches a superclass. Need to find a method that is not
1079 // quadratic for this.
1080 // Neither of these are explicitly forbidden, but every compiler detects them
1081 // and warns.
1082
1083 RawHandlers.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +00001084 return Owned(new (Context) CXXTryStmt(TryLoc,
1085 static_cast<Stmt*>(TryBlock.release()),
1086 Handlers, NumHandlers));
Sebastian Redl8351da02008-12-22 21:35:02 +00001087}