blob: 7e7edbe51ad10861ef6ee6e7a711834c41ec9196 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for statements.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anders Carlssone8bd9f22008-11-22 21:04:56 +000015#include "clang/AST/APValue.h"
Chris Lattner3429a812007-08-23 05:46:52 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/AST/Expr.h"
Ted Kremenek0c97e042009-02-07 01:47:29 +000019#include "clang/AST/ASTContext.h"
Anders Carlsson49dadd62007-11-25 00:25:21 +000020#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000023Sema::OwningStmtResult Sema::ActOnExprStmt(ExprArg expr) {
24 Expr *E = static_cast<Expr*>(expr.release());
Steve Naroff5cbb02f2007-09-16 14:56:35 +000025 assert(E && "ActOnExprStmt(): missing expression");
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000026
Chris Lattnere02e4402008-07-25 23:18:17 +000027 // C99 6.8.3p2: The expression in an expression statement is evaluated as a
28 // void expression for its side effects. Conversion to void allows any
29 // operand, even incomplete types.
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000030
Chris Lattnere02e4402008-07-25 23:18:17 +000031 // Same thing in for stmt first clause (when expr) and third clause.
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000032 return Owned(static_cast<Stmt*>(E));
Chris Lattner4b009652007-07-25 00:24:17 +000033}
34
35
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000036Sema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Ted Kremenek0c97e042009-02-07 01:47:29 +000037 return Owned(new (Context) NullStmt(SemiLoc));
Chris Lattner4b009652007-07-25 00:24:17 +000038}
39
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000040Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclTy *decl,
41 SourceLocation StartLoc,
42 SourceLocation EndLoc) {
Chris Lattnera4ff4272008-03-13 06:29:04 +000043 if (decl == 0)
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000044 return StmtError();
45
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000046 Decl *D = static_cast<Decl *>(decl);
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000047
Ted Kremenek1bc18e62008-10-07 23:09:49 +000048 // This is a temporary hack until we are always passing around
49 // DeclGroupRefs.
50 llvm::SmallVector<Decl*, 10> decls;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000051 while (D) {
52 Decl* d = D;
53 D = D->getNextDeclarator();
Ted Kremenek1bc18e62008-10-07 23:09:49 +000054 d->setNextDeclarator(0);
55 decls.push_back(d);
56 }
57
58 assert (!decls.empty());
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000059
Ted Kremenek1bc18e62008-10-07 23:09:49 +000060 if (decls.size() == 1) {
Douglas Gregorfb0cac22009-02-13 19:06:18 +000061 DeclGroupRef DG(*decls.begin());
Ted Kremenek0c97e042009-02-07 01:47:29 +000062 return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
Ted Kremenek1bc18e62008-10-07 23:09:49 +000063 }
64 else {
Douglas Gregorfb0cac22009-02-13 19:06:18 +000065 DeclGroupRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
Ted Kremenek0c97e042009-02-07 01:47:29 +000066 return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
Ted Kremenek1bc18e62008-10-07 23:09:49 +000067 }
Chris Lattner4b009652007-07-25 00:24:17 +000068}
69
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000070Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +000071Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000072 MultiStmtArg elts, bool isStmtExpr) {
73 unsigned NumElts = elts.size();
74 Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
Chris Lattner3ea3b662007-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 Gregoraf8ad2b2009-01-20 01:17:11 +000089 Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
Chris Lattner3ea3b662007-08-27 04:29:41 +000090 Diag(D->getLocation(), diag::ext_mixed_decls_code);
91 }
92 }
Chris Lattnerf2b07572007-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
Chris Lattnerd2c66552009-02-14 07:37:35 +000098 // Warn about expressions with unused results if they are non-void and if
99 // this not the last stmt in a stmt expr.
100 if (E->getType()->isVoidType() || (isStmtExpr && i == NumElts-1))
Chris Lattnerf2b07572007-08-31 21:49:55 +0000101 continue;
102
Chris Lattnerd2c66552009-02-14 07:37:35 +0000103 SourceLocation Loc;
104 SourceRange R1, R2;
105 if (!E->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerf2b07572007-08-31 21:49:55 +0000106 continue;
Chris Lattnerd2c66552009-02-14 07:37:35 +0000107
108 Diag(Loc, diag::warn_unused_expr) << R1 << R2;
Chris Lattnerf2b07572007-08-31 21:49:55 +0000109 }
Sebastian Redl76b9ddb2008-12-21 12:04:03 +0000110
Ted Kremenek0c97e042009-02-07 01:47:29 +0000111 return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
Chris Lattner4b009652007-07-25 00:24:17 +0000112}
113
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000114Action::OwningStmtResult
115Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval,
116 SourceLocation DotDotDotLoc, ExprArg rhsval,
117 SourceLocation ColonLoc, StmtArg subStmt) {
118 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
119 assert((lhsval.get() != 0) && "missing expression in case statement");
120
Chris Lattner4b009652007-07-25 00:24:17 +0000121 // C99 6.8.4.2p3: The expression shall be an integer constant.
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000122 // However, GCC allows any evaluatable integer expression.
Anders Carlsson613314d2008-12-01 02:13:02 +0000123
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000124 Expr *LHSVal = static_cast<Expr*>(lhsval.get());
Anders Carlsson613314d2008-12-01 02:13:02 +0000125 if (VerifyIntegerConstantExpression(LHSVal))
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000126 return Owned(SubStmt);
Chris Lattner4b009652007-07-25 00:24:17 +0000127
128 // GCC extension: The expression shall be an integer constant.
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000129
130 Expr *RHSVal = static_cast<Expr*>(rhsval.get());
131 if (RHSVal && VerifyIntegerConstantExpression(RHSVal)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000132 RHSVal = 0; // Recover by just forgetting about it.
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000133 rhsval = 0;
134 }
135
Chris Lattner4b009652007-07-25 00:24:17 +0000136 if (SwitchStack.empty()) {
137 Diag(CaseLoc, diag::err_case_not_in_switch);
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000138 return Owned(SubStmt);
Chris Lattner4b009652007-07-25 00:24:17 +0000139 }
140
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000141 // Only now release the smart pointers.
142 lhsval.release();
143 rhsval.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000144 CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000145 SwitchStack.back()->addSwitchCase(CS);
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000146 return Owned(CS);
Chris Lattner4b009652007-07-25 00:24:17 +0000147}
148
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000149Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000150Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000151 StmtArg subStmt, Scope *CurScope) {
152 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
153
Chris Lattner4b009652007-07-25 00:24:17 +0000154 if (SwitchStack.empty()) {
155 Diag(DefaultLoc, diag::err_default_not_in_switch);
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000156 return Owned(SubStmt);
Chris Lattner4b009652007-07-25 00:24:17 +0000157 }
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000158
Ted Kremenek0c97e042009-02-07 01:47:29 +0000159 DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner4b009652007-07-25 00:24:17 +0000160 SwitchStack.back()->addSwitchCase(DS);
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000161 return Owned(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000162}
163
Sebastian Redl2437ec62009-01-11 00:38:46 +0000164Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000165Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Sebastian Redl2437ec62009-01-11 00:38:46 +0000166 SourceLocation ColonLoc, StmtArg subStmt) {
167 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
Sebastian Redl2437ec62009-01-11 00:38:46 +0000168
Steve Naroff313c4162009-02-28 16:48:43 +0000169 // Look up the record for this label identifier.
170 Scope::LabelMapTy::iterator I = ActiveScope->LabelMap.find(II);
171
172 LabelStmt *LabelDecl;
173
Chris Lattner4b009652007-07-25 00:24:17 +0000174 // If not forward referenced or defined already, just create a new LabelStmt.
Steve Naroff313c4162009-02-28 16:48:43 +0000175 if (I == ActiveScope->LabelMap.end()) {
176 LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt);
177 ActiveScope->LabelMap.insert(std::make_pair(II, LabelDecl));
178 return Owned(LabelDecl);
179 } else
180 LabelDecl = static_cast<LabelStmt *>(I->second);
Sebastian Redl2437ec62009-01-11 00:38:46 +0000181
Chris Lattner4b009652007-07-25 00:24:17 +0000182 assert(LabelDecl->getID() == II && "Label mismatch!");
Sebastian Redl2437ec62009-01-11 00:38:46 +0000183
Chris Lattner4b009652007-07-25 00:24:17 +0000184 // Otherwise, this label was either forward reference or multiply defined. If
185 // multiply defined, reject it now.
186 if (LabelDecl->getSubStmt()) {
Chris Lattnerb1753422008-11-23 21:45:46 +0000187 Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
Chris Lattner1336cab2008-11-23 23:12:31 +0000188 Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
Sebastian Redl2437ec62009-01-11 00:38:46 +0000189 return Owned(SubStmt);
Chris Lattner4b009652007-07-25 00:24:17 +0000190 }
Sebastian Redl2437ec62009-01-11 00:38:46 +0000191
Chris Lattner4b009652007-07-25 00:24:17 +0000192 // Otherwise, this label was forward declared, and we just found its real
193 // definition. Fill in the forward definition and return it.
194 LabelDecl->setIdentLoc(IdentLoc);
195 LabelDecl->setSubStmt(SubStmt);
Sebastian Redl2437ec62009-01-11 00:38:46 +0000196 return Owned(LabelDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000197}
198
Sebastian Redl2437ec62009-01-11 00:38:46 +0000199Action::OwningStmtResult
200Sema::ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal,
201 StmtArg ThenVal, SourceLocation ElseLoc,
202 StmtArg ElseVal) {
203 Expr *condExpr = (Expr *)CondVal.release();
204
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000205 assert(condExpr && "ActOnIfStmt(): missing expression");
Sebastian Redl2437ec62009-01-11 00:38:46 +0000206
Chris Lattner4b009652007-07-25 00:24:17 +0000207 DefaultFunctionArrayConversion(condExpr);
Sebastian Redl2437ec62009-01-11 00:38:46 +0000208 // Take ownership again until we're past the error checking.
209 CondVal = condExpr;
Chris Lattner4b009652007-07-25 00:24:17 +0000210 QualType condType = condExpr->getType();
Sebastian Redl2437ec62009-01-11 00:38:46 +0000211
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000212 if (getLangOptions().CPlusPlus) {
213 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redl2437ec62009-01-11 00:38:46 +0000214 return StmtError();
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000215 } else if (!condType->isScalarType()) // C99 6.8.4.1p1
Sebastian Redl2437ec62009-01-11 00:38:46 +0000216 return StmtError(Diag(IfLoc, diag::err_typecheck_statement_requires_scalar)
217 << condType << condExpr->getSourceRange());
218
219 Stmt *thenStmt = (Stmt *)ThenVal.release();
Chris Lattner4b009652007-07-25 00:24:17 +0000220
Anders Carlsson663733e2007-10-10 20:50:11 +0000221 // Warn if the if block has a null body without an else value.
222 // this helps prevent bugs due to typos, such as
223 // if (condition);
224 // do_stuff();
Sebastian Redl2437ec62009-01-11 00:38:46 +0000225 if (!ElseVal.get()) {
Anders Carlsson663733e2007-10-10 20:50:11 +0000226 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
227 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
228 }
229
Sebastian Redl2437ec62009-01-11 00:38:46 +0000230 CondVal.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000231 return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt,
232 (Stmt*)ElseVal.release()));
Chris Lattner4b009652007-07-25 00:24:17 +0000233}
234
Sebastian Redl2437ec62009-01-11 00:38:46 +0000235Action::OwningStmtResult
236Sema::ActOnStartOfSwitchStmt(ExprArg cond) {
237 Expr *Cond = static_cast<Expr*>(cond.release());
238
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000239 if (getLangOptions().CPlusPlus) {
240 // C++ 6.4.2.p2:
241 // The condition shall be of integral type, enumeration type, or of a class
242 // type for which a single conversion function to integral or enumeration
243 // type exists (12.3). If the condition is of class type, the condition is
244 // converted by calling that conversion function, and the result of the
245 // conversion is used in place of the original condition for the remainder
246 // of this section. Integral promotions are performed.
247
248 QualType Ty = Cond->getType();
249
250 // FIXME: Handle class types.
251
252 // If the type is wrong a diagnostic will be emitted later at
253 // ActOnFinishSwitchStmt.
254 if (Ty->isIntegralType() || Ty->isEnumeralType()) {
255 // Integral promotions are performed.
256 // FIXME: Integral promotions for C++ are not complete.
257 UsualUnaryConversions(Cond);
258 }
259 } else {
260 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
261 UsualUnaryConversions(Cond);
262 }
Sebastian Redl2437ec62009-01-11 00:38:46 +0000263
Ted Kremenek0c97e042009-02-07 01:47:29 +0000264 SwitchStmt *SS = new (Context) SwitchStmt(Cond);
Chris Lattner4b009652007-07-25 00:24:17 +0000265 SwitchStack.push_back(SS);
Sebastian Redl2437ec62009-01-11 00:38:46 +0000266 return Owned(SS);
Chris Lattner4b009652007-07-25 00:24:17 +0000267}
268
Chris Lattner3429a812007-08-23 05:46:52 +0000269/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
270/// the specified width and sign. If an overflow occurs, detect it and emit
271/// the specified diagnostic.
272void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
273 unsigned NewWidth, bool NewSign,
274 SourceLocation Loc,
275 unsigned DiagID) {
276 // Perform a conversion to the promoted condition type if needed.
277 if (NewWidth > Val.getBitWidth()) {
278 // If this is an extension, just do it.
279 llvm::APSInt OldVal(Val);
280 Val.extend(NewWidth);
281
282 // If the input was signed and negative and the output is unsigned,
283 // warn.
284 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
Chris Lattner77d52da2008-11-20 06:06:08 +0000285 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000286
287 Val.setIsSigned(NewSign);
288 } else if (NewWidth < Val.getBitWidth()) {
289 // If this is a truncation, check for overflow.
290 llvm::APSInt ConvVal(Val);
291 ConvVal.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000292 ConvVal.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000293 ConvVal.extend(Val.getBitWidth());
Chris Lattner5c039602007-08-23 22:08:35 +0000294 ConvVal.setIsSigned(Val.isSigned());
Chris Lattner3429a812007-08-23 05:46:52 +0000295 if (ConvVal != Val)
Chris Lattner77d52da2008-11-20 06:06:08 +0000296 Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000297
298 // Regardless of whether a diagnostic was emitted, really do the
299 // truncation.
300 Val.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000301 Val.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000302 } else if (NewSign != Val.isSigned()) {
303 // Convert the sign to match the sign of the condition. This can cause
304 // overflow as well: unsigned(INTMIN)
305 llvm::APSInt OldVal(Val);
306 Val.setIsSigned(NewSign);
307
308 if (Val.isNegative()) // Sign bit changes meaning.
Chris Lattner77d52da2008-11-20 06:06:08 +0000309 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000310 }
311}
312
Chris Lattner0ab833c2007-08-23 18:29:20 +0000313namespace {
314 struct CaseCompareFunctor {
315 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
316 const llvm::APSInt &RHS) {
317 return LHS.first < RHS;
318 }
Chris Lattner2157f272007-09-03 18:31:57 +0000319 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
320 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
321 return LHS.first < RHS.first;
322 }
Chris Lattner0ab833c2007-08-23 18:29:20 +0000323 bool operator()(const llvm::APSInt &LHS,
324 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
325 return LHS < RHS.first;
326 }
327 };
328}
329
Chris Lattner766afb82007-09-21 18:15:22 +0000330/// CmpCaseVals - Comparison predicate for sorting case values.
331///
332static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
333 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
334 if (lhs.first < rhs.first)
335 return true;
336
337 if (lhs.first == rhs.first &&
338 lhs.second->getCaseLoc().getRawEncoding()
339 < rhs.second->getCaseLoc().getRawEncoding())
340 return true;
341 return false;
342}
343
Sebastian Redl2437ec62009-01-11 00:38:46 +0000344Action::OwningStmtResult
345Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
346 StmtArg Body) {
347 Stmt *BodyStmt = (Stmt*)Body.release();
348
Chris Lattner4b009652007-07-25 00:24:17 +0000349 SwitchStmt *SS = SwitchStack.back();
Sebastian Redl2437ec62009-01-11 00:38:46 +0000350 assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!");
351
Steve Naroffa610eab2007-09-01 21:08:38 +0000352 SS->setBody(BodyStmt, SwitchLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000353 SwitchStack.pop_back();
354
Chris Lattner3429a812007-08-23 05:46:52 +0000355 Expr *CondExpr = SS->getCond();
356 QualType CondType = CondExpr->getType();
Sebastian Redl2437ec62009-01-11 00:38:46 +0000357
Chris Lattner3429a812007-08-23 05:46:52 +0000358 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner77d52da2008-11-20 06:06:08 +0000359 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000360 << CondType << CondExpr->getSourceRange();
Sebastian Redl2437ec62009-01-11 00:38:46 +0000361 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000362 }
Sebastian Redl2437ec62009-01-11 00:38:46 +0000363
Chris Lattner3429a812007-08-23 05:46:52 +0000364 // Get the bitwidth of the switched-on value before promotions. We must
365 // convert the integer case values to this width before comparison.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000366 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattner3429a812007-08-23 05:46:52 +0000367 bool CondIsSigned = CondType->isSignedIntegerType();
368
369 // Accumulate all of the case values in a vector so that we can sort them
370 // and detect duplicates. This vector contains the APInt for the case after
371 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000372 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
373 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000374
375 // Keep track of any GNU case ranges we see. The APSInt is the low value.
376 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
377
378 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000379
Chris Lattner1a4066d2007-08-23 06:23:56 +0000380 bool CaseListIsErroneous = false;
381
382 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000383 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000384
Chris Lattner4b009652007-07-25 00:24:17 +0000385 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000386 if (TheDefaultStmt) {
387 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
Chris Lattner1336cab2008-11-23 23:12:31 +0000388 Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
Sebastian Redl2437ec62009-01-11 00:38:46 +0000389
Chris Lattner3429a812007-08-23 05:46:52 +0000390 // FIXME: Remove the default statement from the switch block so that
391 // we'll return a valid AST. This requires recursing down the
392 // AST and finding it, not something we are set up to do right now. For
393 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000394 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000395 }
Chris Lattner3429a812007-08-23 05:46:52 +0000396 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000397
Chris Lattner3429a812007-08-23 05:46:52 +0000398 } else {
399 CaseStmt *CS = cast<CaseStmt>(SC);
400
401 // We already verified that the expression has a i-c-e value (C99
402 // 6.8.4.2p3) - get that value now.
Chris Lattnere992d6c2008-01-16 19:17:22 +0000403 Expr *Lo = CS->getLHS();
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000404 llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
Chris Lattner3429a812007-08-23 05:46:52 +0000405
406 // Convert the value to the same width/sign as the condition.
407 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
408 CS->getLHS()->getLocStart(),
409 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000410
Chris Lattnere992d6c2008-01-16 19:17:22 +0000411 // If the LHS is not the same type as the condition, insert an implicit
412 // cast.
413 ImpCastExprToType(Lo, CondType);
414 CS->setLHS(Lo);
415
Chris Lattner1a4066d2007-08-23 06:23:56 +0000416 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000417 if (CS->getRHS())
418 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000419 else
420 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000421 }
422 }
423
Chris Lattner1a4066d2007-08-23 06:23:56 +0000424 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner766afb82007-09-21 18:15:22 +0000425 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattner3429a812007-08-23 05:46:52 +0000426
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000427 if (!CaseVals.empty()) {
428 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
429 if (CaseVals[i].first == CaseVals[i+1].first) {
430 // If we have a duplicate, report it.
431 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000432 diag::err_duplicate_case) << CaseVals[i].first.toString(10);
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000433 Diag(CaseVals[i].second->getLHS()->getLocStart(),
Chris Lattner1336cab2008-11-23 23:12:31 +0000434 diag::note_duplicate_case_prev);
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000435 // FIXME: We really want to remove the bogus case stmt from the substmt,
436 // but we have no way to do this right now.
437 CaseListIsErroneous = true;
438 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000439 }
440 }
Chris Lattner3429a812007-08-23 05:46:52 +0000441
Chris Lattner1a4066d2007-08-23 06:23:56 +0000442 // Detect duplicate case ranges, which usually don't exist at all in the first
443 // place.
444 if (!CaseRanges.empty()) {
445 // Sort all the case ranges by their low value so we can easily detect
446 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000447 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000448
449 // Scan the ranges, computing the high values and removing empty ranges.
450 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000451 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000452 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnere992d6c2008-01-16 19:17:22 +0000453 Expr *Hi = CR->getRHS();
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000454 llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
Chris Lattner1a4066d2007-08-23 06:23:56 +0000455
456 // Convert the value to the same width/sign as the condition.
457 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
458 CR->getRHS()->getLocStart(),
459 diag::warn_case_value_overflow);
460
Chris Lattnere992d6c2008-01-16 19:17:22 +0000461 // If the LHS is not the same type as the condition, insert an implicit
462 // cast.
463 ImpCastExprToType(Hi, CondType);
464 CR->setRHS(Hi);
465
Chris Lattner7443e0f2007-08-23 17:48:14 +0000466 // If the low value is bigger than the high value, the case is empty.
467 if (CaseRanges[i].first > HiVal) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000468 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
469 << SourceRange(CR->getLHS()->getLocStart(),
470 CR->getRHS()->getLocEnd());
Chris Lattner7443e0f2007-08-23 17:48:14 +0000471 CaseRanges.erase(CaseRanges.begin()+i);
472 --i, --e;
473 continue;
474 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000475 HiVals.push_back(HiVal);
476 }
477
478 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000479 // ranges. Since the range list is sorted, we only need to compare case
480 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000481 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000482 llvm::APSInt &CRLo = CaseRanges[i].first;
483 llvm::APSInt &CRHi = HiVals[i];
484 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000485
Chris Lattner0ab833c2007-08-23 18:29:20 +0000486 // Check to see whether the case range overlaps with any singleton cases.
487 CaseStmt *OverlapStmt = 0;
488 llvm::APSInt OverlapVal(32);
489
490 // Find the smallest value >= the lower bound. If I is in the case range,
491 // then we have overlap.
492 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
493 CaseVals.end(), CRLo,
494 CaseCompareFunctor());
495 if (I != CaseVals.end() && I->first < CRHi) {
496 OverlapVal = I->first; // Found overlap with scalar.
497 OverlapStmt = I->second;
498 }
499
500 // Find the smallest value bigger than the upper bound.
501 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
502 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
503 OverlapVal = (I-1)->first; // Found overlap with scalar.
504 OverlapStmt = (I-1)->second;
505 }
506
507 // Check to see if this case stmt overlaps with the subsequent case range.
508 if (i && CRLo <= HiVals[i-1]) {
509 OverlapVal = HiVals[i-1]; // Found overlap with range.
510 OverlapStmt = CaseRanges[i-1].second;
511 }
512
513 if (OverlapStmt) {
514 // If we have a duplicate, report it.
Chris Lattner77d52da2008-11-20 06:06:08 +0000515 Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
516 << OverlapVal.toString(10);
Chris Lattner0ab833c2007-08-23 18:29:20 +0000517 Diag(OverlapStmt->getLHS()->getLocStart(),
Chris Lattner1336cab2008-11-23 23:12:31 +0000518 diag::note_duplicate_case_prev);
Chris Lattner0ab833c2007-08-23 18:29:20 +0000519 // FIXME: We really want to remove the bogus case stmt from the substmt,
520 // but we have no way to do this right now.
521 CaseListIsErroneous = true;
522 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000523 }
524 }
Chris Lattner3429a812007-08-23 05:46:52 +0000525
Chris Lattner1a4066d2007-08-23 06:23:56 +0000526 // FIXME: If the case list was broken is some way, we don't have a good system
527 // to patch it up. Instead, just return the whole substmt as broken.
528 if (CaseListIsErroneous)
Sebastian Redl2437ec62009-01-11 00:38:46 +0000529 return StmtError();
530
531 Switch.release();
532 return Owned(SS);
Chris Lattner4b009652007-07-25 00:24:17 +0000533}
534
Sebastian Redl19c74d32009-01-16 23:28:06 +0000535Action::OwningStmtResult
536Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, StmtArg Body) {
537 Expr *condExpr = (Expr *)Cond.release();
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000538 assert(condExpr && "ActOnWhileStmt(): missing expression");
Sebastian Redl19c74d32009-01-16 23:28:06 +0000539
Chris Lattner4b009652007-07-25 00:24:17 +0000540 DefaultFunctionArrayConversion(condExpr);
Sebastian Redl19c74d32009-01-16 23:28:06 +0000541 Cond = condExpr;
Chris Lattner4b009652007-07-25 00:24:17 +0000542 QualType condType = condExpr->getType();
Sebastian Redl19c74d32009-01-16 23:28:06 +0000543
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000544 if (getLangOptions().CPlusPlus) {
545 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redl19c74d32009-01-16 23:28:06 +0000546 return StmtError();
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000547 } else if (!condType->isScalarType()) // C99 6.8.5p2
Sebastian Redl19c74d32009-01-16 23:28:06 +0000548 return StmtError(Diag(WhileLoc,
549 diag::err_typecheck_statement_requires_scalar)
550 << condType << condExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000551
Sebastian Redl19c74d32009-01-16 23:28:06 +0000552 Cond.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000553 return Owned(new (Context) WhileStmt(condExpr, (Stmt*)Body.release(),
554 WhileLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000555}
556
Sebastian Redl19c74d32009-01-16 23:28:06 +0000557Action::OwningStmtResult
558Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body,
559 SourceLocation WhileLoc, ExprArg Cond) {
560 Expr *condExpr = (Expr *)Cond.release();
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000561 assert(condExpr && "ActOnDoStmt(): missing expression");
Sebastian Redl19c74d32009-01-16 23:28:06 +0000562
Chris Lattner4b009652007-07-25 00:24:17 +0000563 DefaultFunctionArrayConversion(condExpr);
Sebastian Redl19c74d32009-01-16 23:28:06 +0000564 Cond = condExpr;
Chris Lattner4b009652007-07-25 00:24:17 +0000565 QualType condType = condExpr->getType();
Sebastian Redl19c74d32009-01-16 23:28:06 +0000566
Argiris Kirtzidisc362d382008-09-11 05:16:22 +0000567 if (getLangOptions().CPlusPlus) {
568 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redl19c74d32009-01-16 23:28:06 +0000569 return StmtError();
Argiris Kirtzidisc362d382008-09-11 05:16:22 +0000570 } else if (!condType->isScalarType()) // C99 6.8.5p2
Sebastian Redl19c74d32009-01-16 23:28:06 +0000571 return StmtError(Diag(DoLoc, diag::err_typecheck_statement_requires_scalar)
572 << condType << condExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000573
Sebastian Redl19c74d32009-01-16 23:28:06 +0000574 Cond.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000575 return Owned(new (Context) DoStmt((Stmt*)Body.release(), condExpr, DoLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000576}
577
Sebastian Redl19c74d32009-01-16 23:28:06 +0000578Action::OwningStmtResult
579Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
580 StmtArg first, ExprArg second, ExprArg third,
581 SourceLocation RParenLoc, StmtArg body) {
582 Stmt *First = static_cast<Stmt*>(first.get());
583 Expr *Second = static_cast<Expr*>(second.get());
584 Expr *Third = static_cast<Expr*>(third.get());
585 Stmt *Body = static_cast<Stmt*>(body.get());
586
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000587 if (!getLangOptions().CPlusPlus) {
588 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000589 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
590 // declare identifiers for objects having storage class 'auto' or
591 // 'register'.
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000592 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
593 DI!=DE; ++DI) {
594 VarDecl *VD = dyn_cast<VarDecl>(*DI);
595 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
596 VD = 0;
597 if (VD == 0)
598 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
599 // FIXME: mark decl erroneous!
600 }
Chris Lattner06611052007-08-28 05:03:08 +0000601 }
Chris Lattner4b009652007-07-25 00:24:17 +0000602 }
603 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000604 DefaultFunctionArrayConversion(Second);
605 QualType SecondType = Second->getType();
Sebastian Redl19c74d32009-01-16 23:28:06 +0000606
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000607 if (getLangOptions().CPlusPlus) {
608 if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
Sebastian Redl19c74d32009-01-16 23:28:06 +0000609 return StmtError();
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000610 } else if (!SecondType->isScalarType()) // C99 6.8.5p2
Sebastian Redl19c74d32009-01-16 23:28:06 +0000611 return StmtError(Diag(ForLoc,
612 diag::err_typecheck_statement_requires_scalar)
613 << SecondType << Second->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000614 }
Sebastian Redl19c74d32009-01-16 23:28:06 +0000615 first.release();
616 second.release();
617 third.release();
618 body.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000619 return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000620}
621
Sebastian Redl19c74d32009-01-16 23:28:06 +0000622Action::OwningStmtResult
623Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
624 SourceLocation LParenLoc,
625 StmtArg first, ExprArg second,
626 SourceLocation RParenLoc, StmtArg body) {
627 Stmt *First = static_cast<Stmt*>(first.get());
628 Expr *Second = static_cast<Expr*>(second.get());
629 Stmt *Body = static_cast<Stmt*>(body.get());
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +0000630 if (First) {
631 QualType FirstType;
632 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
Ted Kremenek779e1c22008-10-06 20:58:11 +0000633 if (!DS->hasSolitaryDecl())
Sebastian Redl19c74d32009-01-16 23:28:06 +0000634 return StmtError(Diag((*DS->decl_begin())->getLocation(),
635 diag::err_toomany_element_decls));
636
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000637 Decl *D = DS->getSolitaryDecl();
Ted Kremenek779e1c22008-10-06 20:58:11 +0000638 FirstType = cast<ValueDecl>(D)->getType();
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000639 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
640 // declare identifiers for objects having storage class 'auto' or
641 // 'register'.
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000642 VarDecl *VD = cast<VarDecl>(D);
643 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
Sebastian Redl19c74d32009-01-16 23:28:06 +0000644 return StmtError(Diag(VD->getLocation(),
645 diag::err_non_variable_decl_in_for));
Anders Carlsson8d0bcb02008-08-25 18:16:36 +0000646 } else {
647 Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context);
Anders Carlsson8d0bcb02008-08-25 18:16:36 +0000648
Sebastian Redl19c74d32009-01-16 23:28:06 +0000649 if (lval != Expr::LV_Valid)
650 return StmtError(Diag(First->getLocStart(),
651 diag::err_selector_element_not_lvalue)
652 << First->getSourceRange());
653
654 FirstType = static_cast<Expr*>(First)->getType();
Anders Carlsson8d0bcb02008-08-25 18:16:36 +0000655 }
Ted Kremenek118930e2008-07-24 23:58:27 +0000656 if (!Context.isObjCObjectPointerType(FirstType))
Chris Lattner9d2cf082008-11-19 05:27:50 +0000657 Diag(ForLoc, diag::err_selector_element_type)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000658 << FirstType << First->getSourceRange();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000659 }
660 if (Second) {
661 DefaultFunctionArrayConversion(Second);
662 QualType SecondType = Second->getType();
Ted Kremenek118930e2008-07-24 23:58:27 +0000663 if (!Context.isObjCObjectPointerType(SecondType))
Chris Lattner9d2cf082008-11-19 05:27:50 +0000664 Diag(ForLoc, diag::err_collection_expr_type)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000665 << SecondType << Second->getSourceRange();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000666 }
Sebastian Redl19c74d32009-01-16 23:28:06 +0000667 first.release();
668 second.release();
669 body.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000670 return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
671 ForLoc, RParenLoc));
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000672}
Chris Lattner4b009652007-07-25 00:24:17 +0000673
Sebastian Redl539eb572009-01-18 13:19:59 +0000674Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000675Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000676 IdentifierInfo *LabelII) {
Steve Naroff52a81c02008-09-03 18:15:37 +0000677 // If we are in a block, reject all gotos for now.
678 if (CurBlock)
Sebastian Redl539eb572009-01-18 13:19:59 +0000679 return StmtError(Diag(GotoLoc, diag::err_goto_in_block));
Steve Naroff52a81c02008-09-03 18:15:37 +0000680
Chris Lattner4b009652007-07-25 00:24:17 +0000681 // Look up the record for this label identifier.
Steve Naroff313c4162009-02-28 16:48:43 +0000682 Scope::LabelMapTy::iterator I = ActiveScope->LabelMap.find(LabelII);
Chris Lattner4b009652007-07-25 00:24:17 +0000683
Steve Naroff313c4162009-02-28 16:48:43 +0000684 LabelStmt *LabelDecl;
685
686 // If not forward referenced or defined already, just create a new LabelStmt.
687 if (I == ActiveScope->LabelMap.end()) {
Ted Kremenek0c97e042009-02-07 01:47:29 +0000688 LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
Steve Naroff313c4162009-02-28 16:48:43 +0000689 ActiveScope->LabelMap.insert(std::make_pair(LabelII, LabelDecl));
690 } else
691 LabelDecl = static_cast<LabelStmt *>(I->second);
Sebastian Redl539eb572009-01-18 13:19:59 +0000692
Ted Kremenek0c97e042009-02-07 01:47:29 +0000693 return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000694}
695
Sebastian Redl539eb572009-01-18 13:19:59 +0000696Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000697Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Sebastian Redl539eb572009-01-18 13:19:59 +0000698 ExprArg DestExp) {
Chris Lattner4b009652007-07-25 00:24:17 +0000699 // FIXME: Verify that the operand is convertible to void*.
Sebastian Redl539eb572009-01-18 13:19:59 +0000700
Ted Kremenek0c97e042009-02-07 01:47:29 +0000701 return Owned(new (Context) IndirectGotoStmt((Expr*)DestExp.release()));
Chris Lattner4b009652007-07-25 00:24:17 +0000702}
703
Sebastian Redl539eb572009-01-18 13:19:59 +0000704Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000705Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000706 Scope *S = CurScope->getContinueParent();
707 if (!S) {
708 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
Sebastian Redl539eb572009-01-18 13:19:59 +0000709 return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
Chris Lattner4b009652007-07-25 00:24:17 +0000710 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000711
Ted Kremenek0c97e042009-02-07 01:47:29 +0000712 return Owned(new (Context) ContinueStmt(ContinueLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000713}
714
Sebastian Redl539eb572009-01-18 13:19:59 +0000715Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000716Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000717 Scope *S = CurScope->getBreakParent();
718 if (!S) {
719 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
Sebastian Redl539eb572009-01-18 13:19:59 +0000720 return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
Chris Lattner4b009652007-07-25 00:24:17 +0000721 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000722
Ted Kremenek0c97e042009-02-07 01:47:29 +0000723 return Owned(new (Context) BreakStmt(BreakLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000724}
725
Douglas Gregor81c29152008-10-29 00:13:59 +0000726/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
Steve Naroff52a81c02008-09-03 18:15:37 +0000727///
Sebastian Redl539eb572009-01-18 13:19:59 +0000728Action::OwningStmtResult
Steve Naroff52a81c02008-09-03 18:15:37 +0000729Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
Sebastian Redl539eb572009-01-18 13:19:59 +0000730
Steve Naroff52a81c02008-09-03 18:15:37 +0000731 // If this is the first return we've seen in the block, infer the type of
732 // the block from it.
733 if (CurBlock->ReturnType == 0) {
Steve Naroff503996b2008-09-16 22:25:10 +0000734 if (RetValExp) {
Steve Naroffe2b66a82008-09-24 22:26:48 +0000735 // Don't call UsualUnaryConversions(), since we don't want to do
736 // integer promotions here.
737 DefaultFunctionArrayConversion(RetValExp);
Steve Naroff52a81c02008-09-03 18:15:37 +0000738 CurBlock->ReturnType = RetValExp->getType().getTypePtr();
Steve Naroff503996b2008-09-16 22:25:10 +0000739 } else
Steve Naroff52a81c02008-09-03 18:15:37 +0000740 CurBlock->ReturnType = Context.VoidTy.getTypePtr();
Steve Naroff52a81c02008-09-03 18:15:37 +0000741 }
Mike Stumpc1fddff2009-02-04 22:31:32 +0000742 QualType FnRetType = QualType(CurBlock->ReturnType, 0);
Sebastian Redl539eb572009-01-18 13:19:59 +0000743
Steve Naroff52a81c02008-09-03 18:15:37 +0000744 // Otherwise, verify that this result type matches the previous one. We are
745 // pickier with blocks than for normal functions because we don't have GCC
746 // compatibility to worry about here.
747 if (CurBlock->ReturnType->isVoidType()) {
748 if (RetValExp) {
749 Diag(ReturnLoc, diag::err_return_block_has_expr);
Ted Kremenek0c97e042009-02-07 01:47:29 +0000750 RetValExp->Destroy(Context);
Steve Naroff52a81c02008-09-03 18:15:37 +0000751 RetValExp = 0;
752 }
Ted Kremenek0c97e042009-02-07 01:47:29 +0000753 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Steve Naroff52a81c02008-09-03 18:15:37 +0000754 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000755
756 if (!RetValExp)
757 return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
758
Mike Stumpc1fddff2009-02-04 22:31:32 +0000759 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
760 // we have a non-void block with an expression, continue checking
761 QualType RetValType = RetValExp->getType();
Sebastian Redl539eb572009-01-18 13:19:59 +0000762
Mike Stumpc1fddff2009-02-04 22:31:32 +0000763 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
764 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
765 // function return.
766
767 // In C++ the return statement is handled via a copy initialization.
768 // the C version of which boils down to CheckSingleAssignmentConstraints.
769 // FIXME: Leaks RetValExp.
770 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
771 return StmtError();
772
773 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
Steve Naroff52a81c02008-09-03 18:15:37 +0000774 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000775
Ted Kremenek0c97e042009-02-07 01:47:29 +0000776 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Steve Naroff52a81c02008-09-03 18:15:37 +0000777}
Chris Lattner4b009652007-07-25 00:24:17 +0000778
Sebastian Redl539eb572009-01-18 13:19:59 +0000779Action::OwningStmtResult
780Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) {
781 Expr *RetValExp = static_cast<Expr *>(rex.release());
Steve Naroff52a81c02008-09-03 18:15:37 +0000782 if (CurBlock)
783 return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
Sebastian Redl539eb572009-01-18 13:19:59 +0000784
Chris Lattnere5cb5862008-12-04 23:50:19 +0000785 QualType FnRetType;
786 if (FunctionDecl *FD = getCurFunctionDecl())
787 FnRetType = FD->getResultType();
Steve Naroff5fc6a6e2009-03-03 00:45:38 +0000788 else if (ObjCMethodDecl *MD = getCurMethodDecl())
789 FnRetType = MD->getResultType();
790 else // If we don't have a function/method context, bail.
791 return StmtError();
792
Chris Lattner005ed752008-01-04 18:04:52 +0000793 if (FnRetType->isVoidType()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000794 if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)
Chris Lattner6ed167c2008-12-18 02:01:17 +0000795 unsigned D = diag::ext_return_has_expr;
796 if (RetValExp->getType()->isVoidType())
797 D = diag::ext_return_has_void_expr;
Sebastian Redl539eb572009-01-18 13:19:59 +0000798
Chris Lattnerd1a05392008-12-18 02:03:48 +0000799 // return (some void expression); is legal in C++.
800 if (D != diag::ext_return_has_void_expr ||
801 !getLangOptions().CPlusPlus) {
802 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
803 Diag(ReturnLoc, D)
804 << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
805 << RetValExp->getSourceRange();
806 }
Chris Lattner4b009652007-07-25 00:24:17 +0000807 }
Ted Kremenek0c97e042009-02-07 01:47:29 +0000808 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Chris Lattner4b009652007-07-25 00:24:17 +0000809 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000810
Chris Lattner65cae292008-11-19 08:23:25 +0000811 if (!RetValExp) {
812 unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4
813 // C99 6.8.6.4p1 (ext_ since GCC warns)
814 if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
815
816 if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattnerb1753422008-11-23 21:45:46 +0000817 Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
Chris Lattner65cae292008-11-19 08:23:25 +0000818 else
Chris Lattnerb1753422008-11-23 21:45:46 +0000819 Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
Ted Kremenek0c97e042009-02-07 01:47:29 +0000820 return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0));
Chris Lattner65cae292008-11-19 08:23:25 +0000821 }
Sebastian Redl539eb572009-01-18 13:19:59 +0000822
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000823 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
824 // we have a non-void function with an expression, continue checking
825 QualType RetValType = RetValExp->getType();
Sebastian Redl539eb572009-01-18 13:19:59 +0000826
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000827 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
828 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
Sebastian Redl539eb572009-01-18 13:19:59 +0000829 // function return.
830
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000831 // In C++ the return statement is handled via a copy initialization.
Sebastian Redl539eb572009-01-18 13:19:59 +0000832 // the C version of which boils down to CheckSingleAssignmentConstraints.
833 // FIXME: Leaks RetValExp.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000834 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
Sebastian Redl539eb572009-01-18 13:19:59 +0000835 return StmtError();
836
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000837 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
838 }
839
Ted Kremenek0c97e042009-02-07 01:47:29 +0000840 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Chris Lattner4b009652007-07-25 00:24:17 +0000841}
842
Sebastian Redlc6b86332009-01-18 16:53:17 +0000843Sema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
844 bool IsSimple,
845 bool IsVolatile,
846 unsigned NumOutputs,
847 unsigned NumInputs,
848 std::string *Names,
849 MultiExprArg constraints,
850 MultiExprArg exprs,
851 ExprArg asmString,
852 MultiExprArg clobbers,
853 SourceLocation RParenLoc) {
854 unsigned NumClobbers = clobbers.size();
855 StringLiteral **Constraints =
856 reinterpret_cast<StringLiteral**>(constraints.get());
857 Expr **Exprs = reinterpret_cast<Expr **>(exprs.get());
858 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get());
859 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
860
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000861 llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
862
Chris Lattnerb052a832008-08-18 19:55:17 +0000863 // The parser verifies that there is a string literal here.
Chris Lattner84418022008-07-23 06:46:56 +0000864 if (AsmString->isWide())
Sebastian Redlc6b86332009-01-18 16:53:17 +0000865 return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
866 << AsmString->getSourceRange());
867
868
Chris Lattnerb052a832008-08-18 19:55:17 +0000869 for (unsigned i = 0; i != NumOutputs; i++) {
870 StringLiteral *Literal = Constraints[i];
Chris Lattner84418022008-07-23 06:46:56 +0000871 if (Literal->isWide())
Sebastian Redlc6b86332009-01-18 16:53:17 +0000872 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
873 << Literal->getSourceRange());
874
Anders Carlsson4ce42302007-11-27 04:11:28 +0000875 std::string OutputConstraint(Literal->getStrData(),
876 Literal->getByteLength());
Sebastian Redlc6b86332009-01-18 16:53:17 +0000877
Anders Carlsson4ce42302007-11-27 04:11:28 +0000878 TargetInfo::ConstraintInfo info;
Chris Lattner84418022008-07-23 06:46:56 +0000879 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Sebastian Redlc6b86332009-01-18 16:53:17 +0000880 return StmtError(Diag(Literal->getLocStart(),
881 diag::err_asm_invalid_output_constraint) << OutputConstraint);
882
Anders Carlsson4ce42302007-11-27 04:11:28 +0000883 // Check that the output exprs are valid lvalues.
Chris Lattnerb052a832008-08-18 19:55:17 +0000884 ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]);
Chris Lattner25168a52008-07-26 21:30:36 +0000885 Expr::isLvalueResult Result = OutputExpr->isLvalue(Context);
Anders Carlssonb4487a82007-11-23 19:43:50 +0000886 if (Result != Expr::LV_Valid) {
Sebastian Redlc6b86332009-01-18 16:53:17 +0000887 return StmtError(Diag(OutputExpr->getSubExpr()->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000888 diag::err_asm_invalid_lvalue_in_output)
Sebastian Redlc6b86332009-01-18 16:53:17 +0000889 << OutputExpr->getSubExpr()->getSourceRange());
Anders Carlssonb4487a82007-11-23 19:43:50 +0000890 }
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000891
892 OutputConstraintInfos.push_back(info);
Anders Carlssonb4487a82007-11-23 19:43:50 +0000893 }
Sebastian Redlc6b86332009-01-18 16:53:17 +0000894
Anders Carlssonb4487a82007-11-23 19:43:50 +0000895 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Chris Lattnerb052a832008-08-18 19:55:17 +0000896 StringLiteral *Literal = Constraints[i];
Chris Lattner84418022008-07-23 06:46:56 +0000897 if (Literal->isWide())
Sebastian Redlc6b86332009-01-18 16:53:17 +0000898 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
899 << Literal->getSourceRange());
900
901 std::string InputConstraint(Literal->getStrData(),
Anders Carlsson4ce42302007-11-27 04:11:28 +0000902 Literal->getByteLength());
Sebastian Redlc6b86332009-01-18 16:53:17 +0000903
Anders Carlsson4ce42302007-11-27 04:11:28 +0000904 TargetInfo::ConstraintInfo info;
905 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
Anders Carlsson7b49cec2009-01-17 23:36:15 +0000906 &Names[0],
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000907 &Names[0] + NumOutputs,
908 &OutputConstraintInfos[0],
909 info)) {
Sebastian Redlc6b86332009-01-18 16:53:17 +0000910 return StmtError(Diag(Literal->getLocStart(),
911 diag::err_asm_invalid_input_constraint) << InputConstraint);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000912 }
Sebastian Redlc6b86332009-01-18 16:53:17 +0000913
Chris Lattnerb052a832008-08-18 19:55:17 +0000914 ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]);
Sebastian Redlc6b86332009-01-18 16:53:17 +0000915
Anders Carlssone7d92702009-01-20 20:49:22 +0000916 // Only allow void types for memory constraints.
Anders Carlssonde93d332009-01-21 06:27:20 +0000917 if ((info & TargetInfo::CI_AllowsMemory)
918 && !(info & TargetInfo::CI_AllowsRegister)) {
Anders Carlssone7d92702009-01-20 20:49:22 +0000919 if (InputExpr->isLvalue(Context) != Expr::LV_Valid)
920 return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(),
921 diag::err_asm_invalid_lvalue_in_input)
922 << InputConstraint << InputExpr->getSubExpr()->getSourceRange());
Anders Carlssonb4487a82007-11-23 19:43:50 +0000923 }
Sebastian Redlc6b86332009-01-18 16:53:17 +0000924
Anders Carlssone7d92702009-01-20 20:49:22 +0000925 if (info & TargetInfo::CI_AllowsRegister) {
926 if (InputExpr->getType()->isVoidType()) {
927 return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(),
928 diag::err_asm_invalid_type_in_input)
929 << InputExpr->getType() << InputConstraint
930 << InputExpr->getSubExpr()->getSourceRange());
931 }
Anders Carlssone7d92702009-01-20 20:49:22 +0000932 }
Anders Carlsson14125b02009-02-22 02:11:23 +0000933
934 DefaultFunctionArrayConversion(Exprs[i]);
Anders Carlssonb4487a82007-11-23 19:43:50 +0000935 }
Sebastian Redlc6b86332009-01-18 16:53:17 +0000936
Anders Carlsson49dadd62007-11-25 00:25:21 +0000937 // Check that the clobbers are valid.
Chris Lattnerb052a832008-08-18 19:55:17 +0000938 for (unsigned i = 0; i != NumClobbers; i++) {
939 StringLiteral *Literal = Clobbers[i];
Chris Lattner84418022008-07-23 06:46:56 +0000940 if (Literal->isWide())
Sebastian Redlc6b86332009-01-18 16:53:17 +0000941 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
942 << Literal->getSourceRange());
943
944 llvm::SmallString<16> Clobber(Literal->getStrData(),
945 Literal->getStrData() +
Anders Carlsson49dadd62007-11-25 00:25:21 +0000946 Literal->getByteLength());
Sebastian Redlc6b86332009-01-18 16:53:17 +0000947
Chris Lattner84418022008-07-23 06:46:56 +0000948 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Sebastian Redlc6b86332009-01-18 16:53:17 +0000949 return StmtError(Diag(Literal->getLocStart(),
950 diag::err_asm_unknown_register_name) << Clobber.c_str());
Anders Carlsson49dadd62007-11-25 00:25:21 +0000951 }
Sebastian Redlc6b86332009-01-18 16:53:17 +0000952
953 constraints.release();
954 exprs.release();
955 asmString.release();
956 clobbers.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000957 return Owned(new (Context) AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs,
958 NumInputs, Names, Constraints, Exprs,
959 AsmString, NumClobbers,
960 Clobbers, RParenLoc));
Chris Lattner8a40a832007-10-29 04:04:16 +0000961}
Fariborz Jahanian06798362007-11-01 23:59:59 +0000962
Sebastian Redlb3860a72009-01-18 17:43:11 +0000963Action::OwningStmtResult
964Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Steve Naroff0e8b96a2009-03-03 19:52:17 +0000965 SourceLocation RParen, DeclTy *Parm,
Sebastian Redlb3860a72009-01-18 17:43:11 +0000966 StmtArg Body, StmtArg catchList) {
967 Stmt *CatchList = static_cast<Stmt*>(catchList.release());
Steve Naroff45c237d2009-03-03 20:59:06 +0000968 ParmVarDecl *PVD = static_cast<ParmVarDecl*>(Parm);
969
970 // PVD == 0 implies @catch(...).
971 if (PVD && !Context.isObjCObjectPointerType(PVD->getType()))
972 return StmtError(Diag(PVD->getLocation(),
973 diag::err_catch_param_not_objc_type));
974
Ted Kremenek0c97e042009-02-07 01:47:29 +0000975 ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen,
Steve Naroff45c237d2009-03-03 20:59:06 +0000976 PVD, static_cast<Stmt*>(Body.release()), CatchList);
Sebastian Redlb3860a72009-01-18 17:43:11 +0000977 return Owned(CatchList ? CatchList : CS);
Fariborz Jahanian06798362007-11-01 23:59:59 +0000978}
979
Sebastian Redlb3860a72009-01-18 17:43:11 +0000980Action::OwningStmtResult
981Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) {
Ted Kremenek0c97e042009-02-07 01:47:29 +0000982 return Owned(new (Context) ObjCAtFinallyStmt(AtLoc,
983 static_cast<Stmt*>(Body.release())));
Fariborz Jahaniande3abf82007-11-02 00:18:53 +0000984}
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +0000985
Sebastian Redlb3860a72009-01-18 17:43:11 +0000986Action::OwningStmtResult
987Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
988 StmtArg Try, StmtArg Catch, StmtArg Finally) {
Ted Kremenek0c97e042009-02-07 01:47:29 +0000989 return Owned(new (Context) ObjCAtTryStmt(AtLoc,
990 static_cast<Stmt*>(Try.release()),
Sebastian Redlb3860a72009-01-18 17:43:11 +0000991 static_cast<Stmt*>(Catch.release()),
992 static_cast<Stmt*>(Finally.release())));
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +0000993}
994
Sebastian Redlb3860a72009-01-18 17:43:11 +0000995Action::OwningStmtResult
Steve Naroff9a8739a2009-02-12 15:54:59 +0000996Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr,Scope *CurScope) {
Steve Naroff677f4012009-02-11 17:45:08 +0000997 Expr *ThrowExpr = static_cast<Expr*>(expr.release());
998 if (!ThrowExpr) {
Steve Naroff590fe482009-02-11 20:05:44 +0000999 // @throw without an expression designates a rethrow (which much occur
1000 // in the context of an @catch clause).
1001 Scope *AtCatchParent = CurScope;
1002 while (AtCatchParent && !AtCatchParent->isAtCatchScope())
1003 AtCatchParent = AtCatchParent->getParent();
1004 if (!AtCatchParent)
Steve Naroff644567e2009-02-12 18:09:32 +00001005 return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
Steve Naroff677f4012009-02-11 17:45:08 +00001006 } else {
1007 QualType ThrowType = ThrowExpr->getType();
1008 // Make sure the expression type is an ObjC pointer or "void *".
1009 if (!Context.isObjCObjectPointerType(ThrowType)) {
1010 const PointerType *PT = ThrowType->getAsPointerType();
1011 if (!PT || !PT->getPointeeType()->isVoidType())
Steve Naroff644567e2009-02-12 18:09:32 +00001012 return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
1013 << ThrowExpr->getType() << ThrowExpr->getSourceRange());
Steve Naroff677f4012009-02-11 17:45:08 +00001014 }
1015 }
1016 return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowExpr));
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001017}
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001018
Sebastian Redlb3860a72009-01-18 17:43:11 +00001019Action::OwningStmtResult
1020Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr,
1021 StmtArg SynchBody) {
Ted Kremenek0c97e042009-02-07 01:47:29 +00001022 return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc,
Sebastian Redlb3860a72009-01-18 17:43:11 +00001023 static_cast<Stmt*>(SynchExpr.release()),
1024 static_cast<Stmt*>(SynchBody.release())));
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001025}
Sebastian Redl743c8162008-12-22 19:15:10 +00001026
1027/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
1028/// and creates a proper catch handler from them.
1029Action::OwningStmtResult
1030Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclTy *ExDecl,
1031 StmtArg HandlerBlock) {
1032 // There's nothing to test that ActOnExceptionDecl didn't already test.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001033 return Owned(new (Context) CXXCatchStmt(CatchLoc,
1034 static_cast<VarDecl*>(ExDecl),
1035 static_cast<Stmt*>(HandlerBlock.release())));
Sebastian Redl743c8162008-12-22 19:15:10 +00001036}
Sebastian Redl237116b2008-12-22 21:35:02 +00001037
1038/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
1039/// handlers and creates a try statement from them.
1040Action::OwningStmtResult
1041Sema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock,
1042 MultiStmtArg RawHandlers) {
1043 unsigned NumHandlers = RawHandlers.size();
1044 assert(NumHandlers > 0 &&
1045 "The parser shouldn't call this if there are no handlers.");
1046 Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get());
1047
1048 for(unsigned i = 0; i < NumHandlers - 1; ++i) {
1049 CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
1050 if (!Handler->getExceptionDecl())
1051 return StmtError(Diag(Handler->getLocStart(), diag::err_early_catch_all));
1052 }
1053 // FIXME: We should detect handlers for the same type as an earlier one.
1054 // This one is rather easy.
1055 // FIXME: We should detect handlers that cannot catch anything because an
1056 // earlier handler catches a superclass. Need to find a method that is not
1057 // quadratic for this.
1058 // Neither of these are explicitly forbidden, but every compiler detects them
1059 // and warns.
1060
1061 RawHandlers.release();
Ted Kremenek0c97e042009-02-07 01:47:29 +00001062 return Owned(new (Context) CXXTryStmt(TryLoc,
1063 static_cast<Stmt*>(TryBlock.release()),
1064 Handlers, NumHandlers));
Sebastian Redl237116b2008-12-22 21:35:02 +00001065}