blob: 58131daf8301d24dc340c8a028a24551ca17751c [file] [log] [blame]
Chris Lattneraf8d5812006-11-10 05:07:45 +00001//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattneraf8d5812006-11-10 05:07:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for statements.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Douglas Gregore1314a62009-12-18 05:02:21 +000015#include "SemaInit.h"
Anders Carlsson59689ed2008-11-22 21:04:56 +000016#include "clang/AST/APValue.h"
Chris Lattnerfc1c44a2007-08-23 05:46:52 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregord0c22e02009-11-23 13:46:08 +000019#include "clang/AST/ExprCXX.h"
Chris Lattner2ba5ca92009-08-16 16:57:27 +000020#include "clang/AST/ExprObjC.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000021#include "clang/AST/StmtObjC.h"
22#include "clang/AST/StmtCXX.h"
John McCall2351cb92010-04-06 22:24:14 +000023#include "clang/AST/TypeLoc.h"
Douglas Gregord0c22e02009-11-23 13:46:08 +000024#include "clang/Lex/Preprocessor.h"
Anders Carlsson290aa852007-11-25 00:25:21 +000025#include "clang/Basic/TargetInfo.h"
Sebastian Redl63c4da02009-07-29 17:15:45 +000026#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/SmallVector.h"
Chris Lattneraf8d5812006-11-10 05:07:45 +000028using namespace clang;
29
Anders Carlsson24824e52009-05-17 21:11:30 +000030Sema::OwningStmtResult Sema::ActOnExprStmt(FullExprArg expr) {
31 Expr *E = expr->takeAs<Expr>();
Steve Naroff66356bd2007-09-16 14:56:35 +000032 assert(E && "ActOnExprStmt(): missing expression");
John McCall8b07ec22010-05-15 11:32:37 +000033 if (E->getType()->isObjCObjectType()) {
Fariborz Jahanianf15d4b62009-09-03 00:43:07 +000034 if (LangOpts.ObjCNonFragileABI)
35 Diag(E->getLocEnd(), diag::err_indirection_requires_nonfragile_object)
36 << E->getType();
37 else
38 Diag(E->getLocEnd(), diag::err_direct_interface_unsupported)
39 << E->getType();
40 return StmtError();
41 }
Chris Lattner903eb512008-07-25 23:18:17 +000042 // C99 6.8.3p2: The expression in an expression statement is evaluated as a
43 // void expression for its side effects. Conversion to void allows any
44 // operand, even incomplete types.
Sebastian Redl52f03ba2008-12-21 12:04:03 +000045
Chris Lattner903eb512008-07-25 23:18:17 +000046 // Same thing in for stmt first clause (when expr) and third clause.
Sebastian Redl52f03ba2008-12-21 12:04:03 +000047 return Owned(static_cast<Stmt*>(E));
Chris Lattner1ec5f562007-06-27 05:38:08 +000048}
49
50
Sebastian Redl52f03ba2008-12-21 12:04:03 +000051Sema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Ted Kremenek5a201952009-02-07 01:47:29 +000052 return Owned(new (Context) NullStmt(SemiLoc));
Chris Lattner0f203a72007-05-28 01:45:28 +000053}
54
Chris Lattner5bbb3c82009-03-29 16:50:03 +000055Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg,
Sebastian Redl52f03ba2008-12-21 12:04:03 +000056 SourceLocation StartLoc,
57 SourceLocation EndLoc) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +000058 DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
Mike Stump11289f42009-09-09 15:08:12 +000059
Chris Lattnercbafe8d2009-04-12 20:13:14 +000060 // If we have an invalid decl, just return an error.
61 if (DG.isNull()) return StmtError();
Mike Stump11289f42009-09-09 15:08:12 +000062
Chris Lattner34a22092009-03-04 04:23:07 +000063 return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
Steve Naroff2a8ad182007-05-29 22:59:26 +000064}
Chris Lattneraf8d5812006-11-10 05:07:45 +000065
Fariborz Jahaniane774fa62009-11-19 22:12:37 +000066void Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) {
67 DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
68
69 // If we have an invalid decl, just return.
70 if (DG.isNull() || !DG.isSingleDecl()) return;
71 // suppress any potential 'unused variable' warning.
72 DG.getSingleDecl()->setUsed();
73}
74
Anders Carlsson59a2ab92009-07-30 22:17:18 +000075void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
Anders Carlsson5c5f1602009-07-30 22:39:03 +000076 const Expr *E = dyn_cast_or_null<Expr>(S);
Anders Carlsson59a2ab92009-07-30 22:17:18 +000077 if (!E)
78 return;
79
Anders Carlsson59a2ab92009-07-30 22:17:18 +000080 SourceLocation Loc;
81 SourceRange R1, R2;
Mike Stump53f9ded2009-11-03 23:25:48 +000082 if (!E->isUnusedResultAWarning(Loc, R1, R2, Context))
Anders Carlsson59a2ab92009-07-30 22:17:18 +000083 return;
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattner2ba5ca92009-08-16 16:57:27 +000085 // Okay, we have an unused result. Depending on what the base expression is,
86 // we might want to make a more specific diagnostic. Check for one of these
87 // cases now.
88 unsigned DiagID = diag::warn_unused_expr;
89 E = E->IgnoreParens();
Fariborz Jahanian9a846652009-08-20 17:02:02 +000090 if (isa<ObjCImplicitSetterGetterRefExpr>(E))
Chris Lattner2ba5ca92009-08-16 16:57:27 +000091 DiagID = diag::warn_unused_property_expr;
Chris Lattner1a6babf2009-10-13 04:53:48 +000092
Douglas Gregor50dc2192010-02-11 22:55:30 +000093 if (const CXXExprWithTemporaries *Temps = dyn_cast<CXXExprWithTemporaries>(E))
94 E = Temps->getSubExpr();
Douglas Gregor50dc2192010-02-11 22:55:30 +000095
Chris Lattner1a6babf2009-10-13 04:53:48 +000096 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
John McCallc493a732010-03-12 07:11:26 +000097 if (E->getType()->isVoidType())
98 return;
99
Chris Lattner1a6babf2009-10-13 04:53:48 +0000100 // If the callee has attribute pure, const, or warn_unused_result, warn with
101 // a more specific message to make it clear what is happening.
Nuno Lopes518e3702009-12-20 23:11:08 +0000102 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner1a6babf2009-10-13 04:53:48 +0000103 if (FD->getAttr<WarnUnusedResultAttr>()) {
104 Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
105 return;
106 }
107 if (FD->getAttr<PureAttr>()) {
108 Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure";
109 return;
110 }
111 if (FD->getAttr<ConstAttr>()) {
112 Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const";
113 return;
114 }
115 }
116 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000117 else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) {
118 const ObjCMethodDecl *MD = ME->getMethodDecl();
119 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
120 Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
121 return;
122 }
Douglas Gregorb33eed02010-04-16 22:09:46 +0000123 } else if (const CXXFunctionalCastExpr *FC
124 = dyn_cast<CXXFunctionalCastExpr>(E)) {
125 if (isa<CXXConstructExpr>(FC->getSubExpr()) ||
126 isa<CXXTemporaryObjectExpr>(FC->getSubExpr()))
127 return;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000128 }
John McCall2351cb92010-04-06 22:24:14 +0000129 // Diagnose "(void*) blah" as a typo for "(void) blah".
130 else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) {
131 TypeSourceInfo *TI = CE->getTypeInfoAsWritten();
132 QualType T = TI->getType();
133
134 // We really do want to use the non-canonical type here.
135 if (T == Context.VoidPtrTy) {
136 PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc());
137
138 Diag(Loc, diag::warn_unused_voidptr)
139 << FixItHint::CreateRemoval(TL.getStarLoc());
140 return;
141 }
142 }
143
Douglas Gregorcb2b6622010-07-15 18:47:04 +0000144 DiagRuntimeBehavior(Loc, PDiag(DiagID) << R1 << R2);
Anders Carlsson59a2ab92009-07-30 22:17:18 +0000145}
146
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000147Action::OwningStmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000148Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000149 MultiStmtArg elts, bool isStmtExpr) {
150 unsigned NumElts = elts.size();
151 Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
Chris Lattnerd864daf2007-08-27 04:29:41 +0000152 // If we're in C89 mode, check that we don't have any decls after stmts. If
153 // so, emit an extension diagnostic.
154 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
155 // Note that __extension__ can be around a decl.
156 unsigned i = 0;
157 // Skip over all declarations.
158 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
159 /*empty*/;
160
161 // We found the end of the list or a statement. Scan for another declstmt.
162 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
163 /*empty*/;
Mike Stump11289f42009-09-09 15:08:12 +0000164
Chris Lattnerd864daf2007-08-27 04:29:41 +0000165 if (i != NumElts) {
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000166 Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
Chris Lattnerd864daf2007-08-27 04:29:41 +0000167 Diag(D->getLocation(), diag::ext_mixed_decls_code);
168 }
169 }
Chris Lattnercac27a52007-08-31 21:49:55 +0000170 // Warn about unused expressions in statements.
171 for (unsigned i = 0; i != NumElts; ++i) {
Anders Carlsson59a2ab92009-07-30 22:17:18 +0000172 // Ignore statements that are last in a statement expression.
173 if (isStmtExpr && i == NumElts - 1)
Chris Lattnercac27a52007-08-31 21:49:55 +0000174 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000175
Anders Carlsson59a2ab92009-07-30 22:17:18 +0000176 DiagnoseUnusedExprResult(Elts[i]);
Chris Lattnercac27a52007-08-31 21:49:55 +0000177 }
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000178
Ted Kremenek5a201952009-02-07 01:47:29 +0000179 return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
Chris Lattneraf8d5812006-11-10 05:07:45 +0000180}
181
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000182Action::OwningStmtResult
183Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval,
184 SourceLocation DotDotDotLoc, ExprArg rhsval,
Chris Lattner34a22092009-03-04 04:23:07 +0000185 SourceLocation ColonLoc) {
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000186 assert((lhsval.get() != 0) && "missing expression in case statement");
187
Steve Naroff8eeeb132007-05-08 21:09:37 +0000188 // C99 6.8.4.2p3: The expression shall be an integer constant.
Mike Stump11289f42009-09-09 15:08:12 +0000189 // However, GCC allows any evaluatable integer expression.
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000190 Expr *LHSVal = static_cast<Expr*>(lhsval.get());
Mike Stump11289f42009-09-09 15:08:12 +0000191 if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent() &&
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000192 VerifyIntegerConstantExpression(LHSVal))
Chris Lattner34a22092009-03-04 04:23:07 +0000193 return StmtError();
Steve Naroff8eeeb132007-05-08 21:09:37 +0000194
Chris Lattner46eeb222007-07-18 02:28:47 +0000195 // GCC extension: The expression shall be an integer constant.
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000196
197 Expr *RHSVal = static_cast<Expr*>(rhsval.get());
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000198 if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent() &&
199 VerifyIntegerConstantExpression(RHSVal)) {
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000200 RHSVal = 0; // Recover by just forgetting about it.
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000201 rhsval = 0;
202 }
203
Chris Lattner96b31392009-04-18 20:10:59 +0000204 if (getSwitchStack().empty()) {
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000205 Diag(CaseLoc, diag::err_case_not_in_switch);
Chris Lattner34a22092009-03-04 04:23:07 +0000206 return StmtError();
Chris Lattner54f4d2b2007-07-23 17:05:23 +0000207 }
Chris Lattner35e287b2007-06-03 01:44:43 +0000208
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000209 // Only now release the smart pointers.
210 lhsval.release();
211 rhsval.release();
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000212 CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc,
213 ColonLoc);
Chris Lattner96b31392009-04-18 20:10:59 +0000214 getSwitchStack().back()->addSwitchCase(CS);
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000215 return Owned(CS);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000216}
217
Chris Lattner34a22092009-03-04 04:23:07 +0000218/// ActOnCaseStmtBody - This installs a statement as the body of a case.
219void Sema::ActOnCaseStmtBody(StmtTy *caseStmt, StmtArg subStmt) {
220 CaseStmt *CS = static_cast<CaseStmt*>(caseStmt);
Anders Carlsson3cbc8592009-05-01 19:30:39 +0000221 Stmt *SubStmt = subStmt.takeAs<Stmt>();
Chris Lattner34a22092009-03-04 04:23:07 +0000222 CS->setSubStmt(SubStmt);
223}
224
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000225Action::OwningStmtResult
Mike Stump11289f42009-09-09 15:08:12 +0000226Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000227 StmtArg subStmt, Scope *CurScope) {
Anders Carlsson3cbc8592009-05-01 19:30:39 +0000228 Stmt *SubStmt = subStmt.takeAs<Stmt>();
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000229
Chris Lattner96b31392009-04-18 20:10:59 +0000230 if (getSwitchStack().empty()) {
Chris Lattner39407372007-07-21 03:00:26 +0000231 Diag(DefaultLoc, diag::err_default_not_in_switch);
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000232 return Owned(SubStmt);
Chris Lattner39407372007-07-21 03:00:26 +0000233 }
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000234
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000235 DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt);
Chris Lattner96b31392009-04-18 20:10:59 +0000236 getSwitchStack().back()->addSwitchCase(DS);
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000237 return Owned(DS);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000238}
239
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000240Action::OwningStmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000241Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000242 SourceLocation ColonLoc, StmtArg subStmt) {
Anders Carlsson3cbc8592009-05-01 19:30:39 +0000243 Stmt *SubStmt = subStmt.takeAs<Stmt>();
Steve Naroffd123bd02009-02-28 16:48:43 +0000244 // Look up the record for this label identifier.
Chris Lattner3318e862009-04-18 20:01:55 +0000245 LabelStmt *&LabelDecl = getLabelMap()[II];
Steve Naroffd123bd02009-02-28 16:48:43 +0000246
Chris Lattnere2473062007-05-28 06:28:18 +0000247 // If not forward referenced or defined already, just create a new LabelStmt.
Steve Naroff846b1ec2009-03-13 15:38:40 +0000248 if (LabelDecl == 0)
249 return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt));
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000250
Chris Lattnereefa10e2007-05-28 06:56:27 +0000251 assert(LabelDecl->getID() == II && "Label mismatch!");
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000252
Chris Lattnere2473062007-05-28 06:28:18 +0000253 // Otherwise, this label was either forward reference or multiply defined. If
254 // multiply defined, reject it now.
255 if (LabelDecl->getSubStmt()) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000256 Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
Chris Lattner0369c572008-11-23 23:12:31 +0000257 Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000258 return Owned(SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000259 }
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000260
Chris Lattnere2473062007-05-28 06:28:18 +0000261 // Otherwise, this label was forward declared, and we just found its real
262 // definition. Fill in the forward definition and return it.
263 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner39407372007-07-21 03:00:26 +0000264 LabelDecl->setSubStmt(SubStmt);
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000265 return Owned(LabelDecl);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000266}
267
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000268Action::OwningStmtResult
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000269Sema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, DeclPtrTy CondVar,
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000270 StmtArg ThenVal, SourceLocation ElseLoc,
271 StmtArg ElseVal) {
Anders Carlsson52627462009-05-17 18:26:53 +0000272 OwningExprResult CondResult(CondVal.release());
Mike Stump11289f42009-09-09 15:08:12 +0000273
Douglas Gregor633caca2009-11-23 23:44:04 +0000274 VarDecl *ConditionVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000275 if (CondVar.get()) {
276 ConditionVar = CondVar.getAs<VarDecl>();
Douglas Gregore60e41a2010-05-06 17:25:47 +0000277 CondResult = CheckConditionVariable(ConditionVar, IfLoc, true);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000278 if (CondResult.isInvalid())
279 return StmtError();
Douglas Gregor633caca2009-11-23 23:44:04 +0000280 }
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000281 Expr *ConditionExpr = CondResult.takeAs<Expr>();
282 if (!ConditionExpr)
283 return StmtError();
Douglas Gregor633caca2009-11-23 23:44:04 +0000284
Anders Carlssonb781bcd2009-05-01 19:49:17 +0000285 Stmt *thenStmt = ThenVal.takeAs<Stmt>();
Anders Carlsson5c5f1602009-07-30 22:39:03 +0000286 DiagnoseUnusedExprResult(thenStmt);
Steve Naroff86272ea2007-05-29 02:14:17 +0000287
Anders Carlssondb83d772007-10-10 20:50:11 +0000288 // Warn if the if block has a null body without an else value.
289 // this helps prevent bugs due to typos, such as
290 // if (condition);
291 // do_stuff();
Mike Stump11289f42009-09-09 15:08:12 +0000292 if (!ElseVal.get()) {
Anders Carlssondb83d772007-10-10 20:50:11 +0000293 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
294 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
295 }
296
Anders Carlsson5c5f1602009-07-30 22:39:03 +0000297 Stmt *elseStmt = ElseVal.takeAs<Stmt>();
298 DiagnoseUnusedExprResult(elseStmt);
Mike Stump11289f42009-09-09 15:08:12 +0000299
Anders Carlsson52627462009-05-17 18:26:53 +0000300 CondResult.release();
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000301 return Owned(new (Context) IfStmt(Context, IfLoc, ConditionVar, ConditionExpr,
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000302 thenStmt, ElseLoc, elseStmt));
Chris Lattneraf8d5812006-11-10 05:07:45 +0000303}
Steve Naroff86272ea2007-05-29 02:14:17 +0000304
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000305/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
306/// the specified width and sign. If an overflow occurs, detect it and emit
307/// the specified diagnostic.
308void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
309 unsigned NewWidth, bool NewSign,
Mike Stump11289f42009-09-09 15:08:12 +0000310 SourceLocation Loc,
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000311 unsigned DiagID) {
312 // Perform a conversion to the promoted condition type if needed.
313 if (NewWidth > Val.getBitWidth()) {
314 // If this is an extension, just do it.
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000315 Val.extend(NewWidth);
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000316 Val.setIsSigned(NewSign);
Douglas Gregora070ffa2010-03-01 01:04:55 +0000317
318 // If the input was signed and negative and the output is
319 // unsigned, don't bother to warn: this is implementation-defined
320 // behavior.
321 // FIXME: Introduce a second, default-ignored warning for this case?
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000322 } else if (NewWidth < Val.getBitWidth()) {
323 // If this is a truncation, check for overflow.
324 llvm::APSInt ConvVal(Val);
325 ConvVal.trunc(NewWidth);
Chris Lattner247ef952007-08-23 22:08:35 +0000326 ConvVal.setIsSigned(NewSign);
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000327 ConvVal.extend(Val.getBitWidth());
Chris Lattner247ef952007-08-23 22:08:35 +0000328 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000329 if (ConvVal != Val)
Chris Lattner29e812b2008-11-20 06:06:08 +0000330 Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
Mike Stump11289f42009-09-09 15:08:12 +0000331
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000332 // Regardless of whether a diagnostic was emitted, really do the
333 // truncation.
334 Val.trunc(NewWidth);
Chris Lattner247ef952007-08-23 22:08:35 +0000335 Val.setIsSigned(NewSign);
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000336 } else if (NewSign != Val.isSigned()) {
337 // Convert the sign to match the sign of the condition. This can cause
338 // overflow as well: unsigned(INTMIN)
Douglas Gregore5ad57a2010-02-18 00:56:01 +0000339 // We don't diagnose this overflow, because it is implementation-defined
340 // behavior.
341 // FIXME: Introduce a second, default-ignored warning for this case?
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000342 llvm::APSInt OldVal(Val);
343 Val.setIsSigned(NewSign);
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000344 }
345}
346
Chris Lattner67998452007-08-23 18:29:20 +0000347namespace {
348 struct CaseCompareFunctor {
349 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
350 const llvm::APSInt &RHS) {
351 return LHS.first < RHS;
352 }
Chris Lattner1463cca2007-09-03 18:31:57 +0000353 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
354 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
355 return LHS.first < RHS.first;
356 }
Chris Lattner67998452007-08-23 18:29:20 +0000357 bool operator()(const llvm::APSInt &LHS,
358 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
359 return LHS < RHS.first;
360 }
361 };
362}
363
Chris Lattner4b2ff022007-09-21 18:15:22 +0000364/// CmpCaseVals - Comparison predicate for sorting case values.
365///
366static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
367 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
368 if (lhs.first < rhs.first)
369 return true;
370
371 if (lhs.first == rhs.first &&
372 lhs.second->getCaseLoc().getRawEncoding()
373 < rhs.second->getCaseLoc().getRawEncoding())
374 return true;
375 return false;
376}
377
Douglas Gregorbd6839732010-02-08 22:24:16 +0000378/// CmpEnumVals - Comparison predicate for sorting enumeration values.
379///
380static bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
381 const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
382{
383 return lhs.first < rhs.first;
384}
385
386/// EqEnumVals - Comparison preficate for uniqing enumeration values.
387///
388static bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
389 const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
390{
391 return lhs.first == rhs.first;
392}
393
Chris Lattnera96d4272009-10-16 16:45:22 +0000394/// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of
395/// potentially integral-promoted expression @p expr.
396static QualType GetTypeBeforeIntegralPromotion(const Expr* expr) {
John McCall45d30c32010-06-12 01:56:02 +0000397 if (const CastExpr *ImplicitCast = dyn_cast<ImplicitCastExpr>(expr)) {
Chris Lattnera96d4272009-10-16 16:45:22 +0000398 const Expr *ExprBeforePromotion = ImplicitCast->getSubExpr();
399 QualType TypeBeforePromotion = ExprBeforePromotion->getType();
Douglas Gregorb90df602010-06-16 00:17:44 +0000400 if (TypeBeforePromotion->isIntegralOrEnumerationType()) {
Chris Lattnera96d4272009-10-16 16:45:22 +0000401 return TypeBeforePromotion;
402 }
403 }
404 return expr->getType();
405}
406
Douglas Gregore60e41a2010-05-06 17:25:47 +0000407Action::OwningStmtResult
408Sema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, ExprArg Cond,
409 DeclPtrTy CondVar) {
410 VarDecl *ConditionVar = 0;
411 if (CondVar.get()) {
412 ConditionVar = CondVar.getAs<VarDecl>();
Douglas Gregor12cc7ee2010-05-06 21:39:56 +0000413 OwningExprResult CondE = CheckConditionVariable(ConditionVar, SourceLocation(), false);
414 if (CondE.isInvalid())
Douglas Gregore60e41a2010-05-06 17:25:47 +0000415 return StmtError();
Douglas Gregor12cc7ee2010-05-06 21:39:56 +0000416
417 Cond = move(CondE);
Douglas Gregore60e41a2010-05-06 17:25:47 +0000418 }
419
Douglas Gregorf4ea7252010-06-29 23:17:37 +0000420 if (!Cond.get())
Douglas Gregore60e41a2010-05-06 17:25:47 +0000421 return StmtError();
422
Douglas Gregorf4ea7252010-06-29 23:17:37 +0000423 Expr *CondExpr = static_cast<Expr *>(Cond.get());
424 OwningExprResult ConvertedCond
425 = ConvertToIntegralOrEnumerationType(SwitchLoc, move(Cond),
426 PDiag(diag::err_typecheck_statement_requires_integer),
427 PDiag(diag::err_switch_incomplete_class_type)
428 << CondExpr->getSourceRange(),
429 PDiag(diag::err_switch_explicit_conversion),
430 PDiag(diag::note_switch_conversion),
431 PDiag(diag::err_switch_multiple_conversions),
Douglas Gregor4799d032010-06-30 00:20:43 +0000432 PDiag(diag::note_switch_conversion),
433 PDiag(0));
Douglas Gregorf4ea7252010-06-29 23:17:37 +0000434 if (ConvertedCond.isInvalid())
435 return StmtError();
436
437 CondExpr = ConvertedCond.takeAs<Expr>();
438
Douglas Gregore60e41a2010-05-06 17:25:47 +0000439 if (!CondVar.get()) {
440 CondExpr = MaybeCreateCXXExprWithTemporaries(CondExpr);
441 if (!CondExpr)
442 return StmtError();
443 }
444
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000445 SwitchStmt *SS = new (Context) SwitchStmt(Context, ConditionVar, CondExpr);
Douglas Gregore60e41a2010-05-06 17:25:47 +0000446 getSwitchStack().push_back(SS);
447 return Owned(SS);
Chris Lattner8fd2d012010-01-24 01:50:29 +0000448}
449
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000450Action::OwningStmtResult
451Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
452 StmtArg Body) {
Anders Carlssonb781bcd2009-05-01 19:49:17 +0000453 Stmt *BodyStmt = Body.takeAs<Stmt>();
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000454
Chris Lattner96b31392009-04-18 20:10:59 +0000455 SwitchStmt *SS = getSwitchStack().back();
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000456 assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!");
457
Steve Naroff42a350a2007-09-01 21:08:38 +0000458 SS->setBody(BodyStmt, SwitchLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000459 getSwitchStack().pop_back();
Anders Carlsson51873c22007-07-22 07:07:56 +0000460
Douglas Gregorb412e172010-07-25 18:17:45 +0000461 if (SS->getCond() == 0)
Douglas Gregor3ff3af42009-11-25 06:20:02 +0000462 return StmtError();
Douglas Gregor3ff3af42009-11-25 06:20:02 +0000463
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000464 Expr *CondExpr = SS->getCond();
John McCalld3dfbd62010-05-18 03:19:21 +0000465 Expr *CondExprBeforePromotion = CondExpr;
Douglas Gregord0c22e02009-11-23 13:46:08 +0000466 QualType CondTypeBeforePromotion =
467 GetTypeBeforeIntegralPromotion(CondExpr);
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000468
Douglas Gregor7fdcbaf22009-11-25 15:17:36 +0000469 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
470 UsualUnaryConversions(CondExpr);
Douglas Gregor11075552009-11-25 05:02:21 +0000471 QualType CondType = CondExpr->getType();
Douglas Gregord0c22e02009-11-23 13:46:08 +0000472 SS->setCond(CondExpr);
473
Chris Lattnera96d4272009-10-16 16:45:22 +0000474 // C++ 6.4.2.p2:
475 // Integral promotions are performed (on the switch condition).
476 //
477 // A case value unrepresentable by the original switch condition
478 // type (before the promotion) doesn't make sense, even when it can
479 // be represented by the promoted type. Therefore we need to find
480 // the pre-promotion type of the switch condition.
Edward O'Callaghan93135aa2009-10-17 19:32:54 +0000481 if (!CondExpr->isTypeDependent()) {
Douglas Gregor5823da32010-06-29 23:25:20 +0000482 // We have already converted the expression to an integral or enumeration
483 // type, when we started the switch statement. If we don't have an
484 // appropriate type now, just return an error.
485 if (!CondType->isIntegralOrEnumerationType())
Edward O'Callaghan93135aa2009-10-17 19:32:54 +0000486 return StmtError();
Edward O'Callaghan93135aa2009-10-17 19:32:54 +0000487
Chris Lattner4ebae652010-04-16 23:34:13 +0000488 if (CondExpr->isKnownToHaveBooleanValue()) {
Edward O'Callaghan93135aa2009-10-17 19:32:54 +0000489 // switch(bool_expr) {...} is often a programmer error, e.g.
490 // switch(n && mask) { ... } // Doh - should be "n & mask".
491 // One can always use an if statement instead of switch(bool_expr).
492 Diag(SwitchLoc, diag::warn_bool_switch_condition)
493 << CondExpr->getSourceRange();
494 }
Anders Carlsson51873c22007-07-22 07:07:56 +0000495 }
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000496
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000497 // Get the bitwidth of the switched-on value before promotions. We must
498 // convert the integer case values to this width before comparison.
Mike Stump11289f42009-09-09 15:08:12 +0000499 bool HasDependentValue
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000500 = CondExpr->isTypeDependent() || CondExpr->isValueDependent();
Mike Stump11289f42009-09-09 15:08:12 +0000501 unsigned CondWidth
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000502 = HasDependentValue? 0
Chris Lattnera96d4272009-10-16 16:45:22 +0000503 : static_cast<unsigned>(Context.getTypeSize(CondTypeBeforePromotion));
504 bool CondIsSigned = CondTypeBeforePromotion->isSignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +0000505
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000506 // Accumulate all of the case values in a vector so that we can sort them
507 // and detect duplicates. This vector contains the APInt for the case after
508 // it has been converted to the condition type.
Chris Lattner67998452007-08-23 18:29:20 +0000509 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
510 CaseValsTy CaseVals;
Mike Stump11289f42009-09-09 15:08:12 +0000511
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000512 // Keep track of any GNU case ranges we see. The APSInt is the low value.
Douglas Gregorbd6839732010-02-08 22:24:16 +0000513 typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy;
514 CaseRangesTy CaseRanges;
Mike Stump11289f42009-09-09 15:08:12 +0000515
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000516 DefaultStmt *TheDefaultStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000517
Chris Lattner10cb5e52007-08-23 06:23:56 +0000518 bool CaseListIsErroneous = false;
Mike Stump11289f42009-09-09 15:08:12 +0000519
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000520 for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue;
Anders Carlsson51873c22007-07-22 07:07:56 +0000521 SC = SC->getNextSwitchCase()) {
Mike Stump11289f42009-09-09 15:08:12 +0000522
Anders Carlsson51873c22007-07-22 07:07:56 +0000523 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000524 if (TheDefaultStmt) {
525 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
Chris Lattner0369c572008-11-23 23:12:31 +0000526 Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000527
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000528 // FIXME: Remove the default statement from the switch block so that
Mike Stump87c57ac2009-05-16 07:39:55 +0000529 // we'll return a valid AST. This requires recursing down the AST and
530 // finding it, not something we are set up to do right now. For now,
531 // just lop the entire switch stmt out of the AST.
Chris Lattner10cb5e52007-08-23 06:23:56 +0000532 CaseListIsErroneous = true;
Anders Carlsson51873c22007-07-22 07:07:56 +0000533 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000534 TheDefaultStmt = DS;
Mike Stump11289f42009-09-09 15:08:12 +0000535
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000536 } else {
537 CaseStmt *CS = cast<CaseStmt>(SC);
Mike Stump11289f42009-09-09 15:08:12 +0000538
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000539 // We already verified that the expression has a i-c-e value (C99
540 // 6.8.4.2p3) - get that value now.
Chris Lattnera65e1f32008-01-16 19:17:22 +0000541 Expr *Lo = CS->getLHS();
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000542
543 if (Lo->isTypeDependent() || Lo->isValueDependent()) {
544 HasDependentValue = true;
545 break;
546 }
Mike Stump11289f42009-09-09 15:08:12 +0000547
Anders Carlsson59689ed2008-11-22 21:04:56 +0000548 llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000549
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000550 // Convert the value to the same width/sign as the condition.
551 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
552 CS->getLHS()->getLocStart(),
553 diag::warn_case_value_overflow);
Anders Carlsson51873c22007-07-22 07:07:56 +0000554
Chris Lattnera65e1f32008-01-16 19:17:22 +0000555 // If the LHS is not the same type as the condition, insert an implicit
556 // cast.
Eli Friedman06ed2a52009-10-20 08:27:19 +0000557 ImpCastExprToType(Lo, CondType, CastExpr::CK_IntegralCast);
Chris Lattnera65e1f32008-01-16 19:17:22 +0000558 CS->setLHS(Lo);
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattner10cb5e52007-08-23 06:23:56 +0000560 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000561 if (CS->getRHS()) {
Mike Stump11289f42009-09-09 15:08:12 +0000562 if (CS->getRHS()->isTypeDependent() ||
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000563 CS->getRHS()->isValueDependent()) {
564 HasDependentValue = true;
565 break;
566 }
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000567 CaseRanges.push_back(std::make_pair(LoVal, CS));
Mike Stump11289f42009-09-09 15:08:12 +0000568 } else
Chris Lattner10cb5e52007-08-23 06:23:56 +0000569 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000570 }
571 }
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000572
573 if (!HasDependentValue) {
John McCalld3dfbd62010-05-18 03:19:21 +0000574 // If we don't have a default statement, check whether the
575 // condition is constant.
576 llvm::APSInt ConstantCondValue;
577 bool HasConstantCond = false;
578 bool ShouldCheckConstantCond = false;
579 if (!HasDependentValue && !TheDefaultStmt) {
580 Expr::EvalResult Result;
581 HasConstantCond = CondExprBeforePromotion->Evaluate(Result, Context);
582 if (HasConstantCond) {
583 assert(Result.Val.isInt() && "switch condition evaluated to non-int");
584 ConstantCondValue = Result.Val.getInt();
585 ShouldCheckConstantCond = true;
586
587 assert(ConstantCondValue.getBitWidth() == CondWidth &&
588 ConstantCondValue.isSigned() == CondIsSigned);
589 }
590 }
591
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000592 // Sort all the scalar case values so we can easily detect duplicates.
593 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
594
595 if (!CaseVals.empty()) {
John McCalld3dfbd62010-05-18 03:19:21 +0000596 for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) {
597 if (ShouldCheckConstantCond &&
598 CaseVals[i].first == ConstantCondValue)
599 ShouldCheckConstantCond = false;
600
601 if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) {
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000602 // If we have a duplicate, report it.
Mike Stump11289f42009-09-09 15:08:12 +0000603 Diag(CaseVals[i].second->getLHS()->getLocStart(),
John McCalld3dfbd62010-05-18 03:19:21 +0000604 diag::err_duplicate_case) << CaseVals[i].first.toString(10);
605 Diag(CaseVals[i-1].second->getLHS()->getLocStart(),
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000606 diag::note_duplicate_case_prev);
Mike Stump87c57ac2009-05-16 07:39:55 +0000607 // FIXME: We really want to remove the bogus case stmt from the
608 // substmt, but we have no way to do this right now.
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000609 CaseListIsErroneous = true;
610 }
611 }
612 }
Mike Stump11289f42009-09-09 15:08:12 +0000613
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000614 // Detect duplicate case ranges, which usually don't exist at all in
615 // the first place.
616 if (!CaseRanges.empty()) {
617 // Sort all the case ranges by their low value so we can easily detect
618 // overlaps between ranges.
619 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Mike Stump11289f42009-09-09 15:08:12 +0000620
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000621 // Scan the ranges, computing the high values and removing empty ranges.
622 std::vector<llvm::APSInt> HiVals;
623 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
John McCalld3dfbd62010-05-18 03:19:21 +0000624 llvm::APSInt &LoVal = CaseRanges[i].first;
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000625 CaseStmt *CR = CaseRanges[i].second;
626 Expr *Hi = CR->getRHS();
627 llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000628
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000629 // Convert the value to the same width/sign as the condition.
630 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
631 CR->getRHS()->getLocStart(),
632 diag::warn_case_value_overflow);
Mike Stump11289f42009-09-09 15:08:12 +0000633
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000634 // If the LHS is not the same type as the condition, insert an implicit
635 // cast.
Eli Friedman06ed2a52009-10-20 08:27:19 +0000636 ImpCastExprToType(Hi, CondType, CastExpr::CK_IntegralCast);
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000637 CR->setRHS(Hi);
Mike Stump11289f42009-09-09 15:08:12 +0000638
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000639 // If the low value is bigger than the high value, the case is empty.
John McCalld3dfbd62010-05-18 03:19:21 +0000640 if (LoVal > HiVal) {
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000641 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
642 << SourceRange(CR->getLHS()->getLocStart(),
643 CR->getRHS()->getLocEnd());
644 CaseRanges.erase(CaseRanges.begin()+i);
645 --i, --e;
646 continue;
647 }
John McCalld3dfbd62010-05-18 03:19:21 +0000648
649 if (ShouldCheckConstantCond &&
650 LoVal <= ConstantCondValue &&
651 ConstantCondValue <= HiVal)
652 ShouldCheckConstantCond = false;
653
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000654 HiVals.push_back(HiVal);
655 }
Mike Stump11289f42009-09-09 15:08:12 +0000656
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000657 // Rescan the ranges, looking for overlap with singleton values and other
658 // ranges. Since the range list is sorted, we only need to compare case
659 // ranges with their neighbors.
660 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
661 llvm::APSInt &CRLo = CaseRanges[i].first;
662 llvm::APSInt &CRHi = HiVals[i];
663 CaseStmt *CR = CaseRanges[i].second;
Mike Stump11289f42009-09-09 15:08:12 +0000664
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000665 // Check to see whether the case range overlaps with any
666 // singleton cases.
667 CaseStmt *OverlapStmt = 0;
668 llvm::APSInt OverlapVal(32);
Mike Stump11289f42009-09-09 15:08:12 +0000669
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000670 // Find the smallest value >= the lower bound. If I is in the
671 // case range, then we have overlap.
672 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
673 CaseVals.end(), CRLo,
674 CaseCompareFunctor());
675 if (I != CaseVals.end() && I->first < CRHi) {
676 OverlapVal = I->first; // Found overlap with scalar.
677 OverlapStmt = I->second;
678 }
Mike Stump11289f42009-09-09 15:08:12 +0000679
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000680 // Find the smallest value bigger than the upper bound.
681 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
682 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
683 OverlapVal = (I-1)->first; // Found overlap with scalar.
684 OverlapStmt = (I-1)->second;
685 }
Mike Stump11289f42009-09-09 15:08:12 +0000686
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000687 // Check to see if this case stmt overlaps with the subsequent
688 // case range.
689 if (i && CRLo <= HiVals[i-1]) {
690 OverlapVal = HiVals[i-1]; // Found overlap with range.
691 OverlapStmt = CaseRanges[i-1].second;
692 }
Mike Stump11289f42009-09-09 15:08:12 +0000693
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000694 if (OverlapStmt) {
695 // If we have a duplicate, report it.
696 Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
697 << OverlapVal.toString(10);
Mike Stump11289f42009-09-09 15:08:12 +0000698 Diag(OverlapStmt->getLHS()->getLocStart(),
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000699 diag::note_duplicate_case_prev);
Mike Stump87c57ac2009-05-16 07:39:55 +0000700 // FIXME: We really want to remove the bogus case stmt from the
701 // substmt, but we have no way to do this right now.
Douglas Gregor2a2d00f2009-05-15 23:57:33 +0000702 CaseListIsErroneous = true;
703 }
Chris Lattnerfcb920d2007-08-23 14:29:07 +0000704 }
Chris Lattner10cb5e52007-08-23 06:23:56 +0000705 }
Douglas Gregorbd6839732010-02-08 22:24:16 +0000706
John McCalld3dfbd62010-05-18 03:19:21 +0000707 // Complain if we have a constant condition and we didn't find a match.
708 if (!CaseListIsErroneous && ShouldCheckConstantCond) {
709 // TODO: it would be nice if we printed enums as enums, chars as
710 // chars, etc.
711 Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition)
712 << ConstantCondValue.toString(10)
713 << CondExpr->getSourceRange();
714 }
715
716 // Check to see if switch is over an Enum and handles all of its
717 // values. We don't need to do this if there's a default
718 // statement or if we have a constant condition.
719 //
720 // TODO: we might want to check whether case values are out of the
721 // enum even if we don't want to check whether all cases are handled.
Douglas Gregor12ea0f42010-02-17 23:29:11 +0000722 const EnumType* ET = CondTypeBeforePromotion->getAs<EnumType>();
Douglas Gregorbd6839732010-02-08 22:24:16 +0000723 // If switch has default case, then ignore it.
John McCalld3dfbd62010-05-18 03:19:21 +0000724 if (!CaseListIsErroneous && !TheDefaultStmt && !HasConstantCond && ET) {
Douglas Gregorbd6839732010-02-08 22:24:16 +0000725 const EnumDecl *ED = ET->getDecl();
726 typedef llvm::SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> EnumValsTy;
727 EnumValsTy EnumVals;
728
John McCalld3dfbd62010-05-18 03:19:21 +0000729 // Gather all enum values, set their type and sort them,
730 // allowing easier comparison with CaseVals.
731 for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin();
732 EDI != ED->enumerator_end(); EDI++) {
Douglas Gregorbd6839732010-02-08 22:24:16 +0000733 llvm::APSInt Val = (*EDI)->getInitVal();
734 if(Val.getBitWidth() < CondWidth)
735 Val.extend(CondWidth);
Douglas Gregor56980d62010-06-29 17:12:35 +0000736 else if (Val.getBitWidth() > CondWidth)
737 Val.trunc(CondWidth);
Douglas Gregorbd6839732010-02-08 22:24:16 +0000738 Val.setIsSigned(CondIsSigned);
739 EnumVals.push_back(std::make_pair(Val, (*EDI)));
740 }
741 std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals);
John McCalld3dfbd62010-05-18 03:19:21 +0000742 EnumValsTy::iterator EIend =
743 std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals);
Douglas Gregorbd6839732010-02-08 22:24:16 +0000744 // See which case values aren't in enum
745 EnumValsTy::const_iterator EI = EnumVals.begin();
John McCalld3dfbd62010-05-18 03:19:21 +0000746 for (CaseValsTy::const_iterator CI = CaseVals.begin();
747 CI != CaseVals.end(); CI++) {
Douglas Gregorbd6839732010-02-08 22:24:16 +0000748 while (EI != EIend && EI->first < CI->first)
749 EI++;
750 if (EI == EIend || EI->first > CI->first)
John McCalld3dfbd62010-05-18 03:19:21 +0000751 Diag(CI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum)
752 << ED->getDeclName();
Douglas Gregorbd6839732010-02-08 22:24:16 +0000753 }
754 // See which of case ranges aren't in enum
755 EI = EnumVals.begin();
John McCalld3dfbd62010-05-18 03:19:21 +0000756 for (CaseRangesTy::const_iterator RI = CaseRanges.begin();
757 RI != CaseRanges.end() && EI != EIend; RI++) {
Douglas Gregorbd6839732010-02-08 22:24:16 +0000758 while (EI != EIend && EI->first < RI->first)
759 EI++;
760
761 if (EI == EIend || EI->first != RI->first) {
John McCalld3dfbd62010-05-18 03:19:21 +0000762 Diag(RI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum)
763 << ED->getDeclName();
Douglas Gregorbd6839732010-02-08 22:24:16 +0000764 }
765
766 llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context);
767 while (EI != EIend && EI->first < Hi)
768 EI++;
769 if (EI == EIend || EI->first != Hi)
John McCalld3dfbd62010-05-18 03:19:21 +0000770 Diag(RI->second->getRHS()->getExprLoc(), diag::warn_not_in_enum)
771 << ED->getDeclName();
Douglas Gregorbd6839732010-02-08 22:24:16 +0000772 }
773 //Check which enum vals aren't in switch
774 CaseValsTy::const_iterator CI = CaseVals.begin();
775 CaseRangesTy::const_iterator RI = CaseRanges.begin();
776 EI = EnumVals.begin();
777 for (; EI != EIend; EI++) {
778 //Drop unneeded case values
779 llvm::APSInt CIVal;
780 while (CI != CaseVals.end() && CI->first < EI->first)
781 CI++;
782
783 if (CI != CaseVals.end() && CI->first == EI->first)
784 continue;
785
786 //Drop unneeded case ranges
787 for (; RI != CaseRanges.end(); RI++) {
788 llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context);
789 if (EI->first <= Hi)
790 break;
791 }
792
793 if (RI == CaseRanges.end() || EI->first < RI->first)
John McCalld3dfbd62010-05-18 03:19:21 +0000794 Diag(CondExpr->getExprLoc(), diag::warn_missing_cases)
795 << EI->second->getDeclName();
Douglas Gregorbd6839732010-02-08 22:24:16 +0000796 }
797 }
Chris Lattner10cb5e52007-08-23 06:23:56 +0000798 }
Chris Lattner10cb5e52007-08-23 06:23:56 +0000799
Mike Stump87c57ac2009-05-16 07:39:55 +0000800 // FIXME: If the case list was broken is some way, we don't have a good system
801 // to patch it up. Instead, just return the whole substmt as broken.
Chris Lattner10cb5e52007-08-23 06:23:56 +0000802 if (CaseListIsErroneous)
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000803 return StmtError();
804
805 Switch.release();
806 return Owned(SS);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000807}
808
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000809Action::OwningStmtResult
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000810Sema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond,
811 DeclPtrTy CondVar, StmtArg Body) {
812 OwningExprResult CondResult(Cond.release());
813
Douglas Gregor680f8612009-11-24 21:15:44 +0000814 VarDecl *ConditionVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000815 if (CondVar.get()) {
816 ConditionVar = CondVar.getAs<VarDecl>();
Douglas Gregore60e41a2010-05-06 17:25:47 +0000817 CondResult = CheckConditionVariable(ConditionVar, WhileLoc, true);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000818 if (CondResult.isInvalid())
819 return StmtError();
Douglas Gregor680f8612009-11-24 21:15:44 +0000820 }
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000821 Expr *ConditionExpr = CondResult.takeAs<Expr>();
822 if (!ConditionExpr)
823 return StmtError();
824
Anders Carlsson5c5f1602009-07-30 22:39:03 +0000825 Stmt *bodyStmt = Body.takeAs<Stmt>();
826 DiagnoseUnusedExprResult(bodyStmt);
Mike Stump11289f42009-09-09 15:08:12 +0000827
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000828 CondResult.release();
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000829 return Owned(new (Context) WhileStmt(Context, ConditionVar, ConditionExpr,
830 bodyStmt, WhileLoc));
Chris Lattneraf8d5812006-11-10 05:07:45 +0000831}
832
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000833Action::OwningStmtResult
834Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body,
Chris Lattner815b70e2009-06-12 23:04:47 +0000835 SourceLocation WhileLoc, SourceLocation CondLParen,
836 ExprArg Cond, SourceLocation CondRParen) {
Anders Carlssonb781bcd2009-05-01 19:49:17 +0000837 Expr *condExpr = Cond.takeAs<Expr>();
Steve Naroff66356bd2007-09-16 14:56:35 +0000838 assert(condExpr && "ActOnDoStmt(): missing expression");
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000839
John McCalld5707ab2009-10-12 21:59:07 +0000840 if (CheckBooleanCondition(condExpr, DoLoc)) {
Douglas Gregor3daa82d2009-05-15 21:56:04 +0000841 Cond = condExpr;
John McCalld5707ab2009-10-12 21:59:07 +0000842 return StmtError();
Douglas Gregor3daa82d2009-05-15 21:56:04 +0000843 }
Steve Naroff86272ea2007-05-29 02:14:17 +0000844
Douglas Gregore60e41a2010-05-06 17:25:47 +0000845 condExpr = MaybeCreateCXXExprWithTemporaries(condExpr);
846 if (!condExpr)
847 return StmtError();
848
Anders Carlsson5c5f1602009-07-30 22:39:03 +0000849 Stmt *bodyStmt = Body.takeAs<Stmt>();
850 DiagnoseUnusedExprResult(bodyStmt);
851
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000852 Cond.release();
Anders Carlsson5c5f1602009-07-30 22:39:03 +0000853 return Owned(new (Context) DoStmt(bodyStmt, condExpr, DoLoc,
Chris Lattner815b70e2009-06-12 23:04:47 +0000854 WhileLoc, CondRParen));
Chris Lattneraf8d5812006-11-10 05:07:45 +0000855}
856
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000857Action::OwningStmtResult
858Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000859 StmtArg first, FullExprArg second, DeclPtrTy secondVar,
860 FullExprArg third,
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000861 SourceLocation RParenLoc, StmtArg body) {
862 Stmt *First = static_cast<Stmt*>(first.get());
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000863
Argyrios Kyrtzidis7620ee42008-09-10 02:17:11 +0000864 if (!getLangOptions().CPlusPlus) {
865 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
Chris Lattner651d42d2008-11-20 06:38:18 +0000866 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
867 // declare identifiers for objects having storage class 'auto' or
868 // 'register'.
Argyrios Kyrtzidis7620ee42008-09-10 02:17:11 +0000869 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
870 DI!=DE; ++DI) {
871 VarDecl *VD = dyn_cast<VarDecl>(*DI);
872 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
873 VD = 0;
874 if (VD == 0)
875 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
876 // FIXME: mark decl erroneous!
877 }
Chris Lattner39f920f2007-08-28 05:03:08 +0000878 }
Steve Naroff86272ea2007-05-29 02:14:17 +0000879 }
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000880
881 OwningExprResult SecondResult(second.release());
882 VarDecl *ConditionVar = 0;
883 if (secondVar.get()) {
884 ConditionVar = secondVar.getAs<VarDecl>();
Douglas Gregore60e41a2010-05-06 17:25:47 +0000885 SecondResult = CheckConditionVariable(ConditionVar, ForLoc, true);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000886 if (SecondResult.isInvalid())
887 return StmtError();
888 }
889
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000890 Expr *Third = third.release().takeAs<Expr>();
891 Stmt *Body = static_cast<Stmt*>(body.get());
892
Anders Carlsson1682af52009-08-01 01:39:59 +0000893 DiagnoseUnusedExprResult(First);
894 DiagnoseUnusedExprResult(Third);
Anders Carlsson5c5f1602009-07-30 22:39:03 +0000895 DiagnoseUnusedExprResult(Body);
896
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000897 first.release();
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000898 body.release();
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000899 return Owned(new (Context) ForStmt(Context, First,
900 SecondResult.takeAs<Expr>(), ConditionVar,
901 Third, Body, ForLoc, LParenLoc,
902 RParenLoc));
Chris Lattneraf8d5812006-11-10 05:07:45 +0000903}
904
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000905Action::OwningStmtResult
906Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
907 SourceLocation LParenLoc,
908 StmtArg first, ExprArg second,
909 SourceLocation RParenLoc, StmtArg body) {
910 Stmt *First = static_cast<Stmt*>(first.get());
911 Expr *Second = static_cast<Expr*>(second.get());
912 Stmt *Body = static_cast<Stmt*>(body.get());
Fariborz Jahanian93977672008-01-10 20:33:58 +0000913 if (First) {
914 QualType FirstType;
915 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
Chris Lattner529efc72009-03-28 06:33:19 +0000916 if (!DS->isSingleDecl())
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000917 return StmtError(Diag((*DS->decl_begin())->getLocation(),
918 diag::err_toomany_element_decls));
919
Chris Lattner529efc72009-03-28 06:33:19 +0000920 Decl *D = DS->getSingleDecl();
Ted Kremenek11b00422008-10-06 20:58:11 +0000921 FirstType = cast<ValueDecl>(D)->getType();
Chris Lattner651d42d2008-11-20 06:38:18 +0000922 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
923 // declare identifiers for objects having storage class 'auto' or
924 // 'register'.
Steve Naroff08899ff2008-04-15 22:42:06 +0000925 VarDecl *VD = cast<VarDecl>(D);
926 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000927 return StmtError(Diag(VD->getLocation(),
928 diag::err_non_variable_decl_in_for));
Anders Carlsson1ec2ccd2008-08-25 18:16:36 +0000929 } else {
Douglas Gregorf68a5082010-04-22 23:10:45 +0000930 Expr *FirstE = cast<Expr>(First);
931 if (!FirstE->isTypeDependent() &&
932 FirstE->isLvalue(Context) != Expr::LV_Valid)
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000933 return StmtError(Diag(First->getLocStart(),
934 diag::err_selector_element_not_lvalue)
935 << First->getSourceRange());
936
Mike Stump11289f42009-09-09 15:08:12 +0000937 FirstType = static_cast<Expr*>(First)->getType();
Anders Carlsson1ec2ccd2008-08-25 18:16:36 +0000938 }
Douglas Gregorf68a5082010-04-22 23:10:45 +0000939 if (!FirstType->isDependentType() &&
940 !FirstType->isObjCObjectPointerType() &&
Fariborz Jahanian2e4a46b2009-08-14 21:53:27 +0000941 !FirstType->isBlockPointerType())
Chris Lattnerf490e152008-11-19 05:27:50 +0000942 Diag(ForLoc, diag::err_selector_element_type)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000943 << FirstType << First->getSourceRange();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000944 }
Douglas Gregorf68a5082010-04-22 23:10:45 +0000945 if (Second && !Second->isTypeDependent()) {
Douglas Gregorb92a1562010-02-03 00:27:59 +0000946 DefaultFunctionArrayLvalueConversion(Second);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000947 QualType SecondType = Second->getType();
Steve Naroff79d12152009-07-16 15:41:00 +0000948 if (!SecondType->isObjCObjectPointerType())
Chris Lattnerf490e152008-11-19 05:27:50 +0000949 Diag(ForLoc, diag::err_collection_expr_type)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000950 << SecondType << Second->getSourceRange();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000951 }
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000952 first.release();
953 second.release();
954 body.release();
Ted Kremenek5a201952009-02-07 01:47:29 +0000955 return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
956 ForLoc, RParenLoc));
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000957}
Chris Lattneraf8d5812006-11-10 05:07:45 +0000958
Sebastian Redl573feed2009-01-18 13:19:59 +0000959Action::OwningStmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000960Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Chris Lattneraf8d5812006-11-10 05:07:45 +0000961 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000962 // Look up the record for this label identifier.
Chris Lattner3318e862009-04-18 20:01:55 +0000963 LabelStmt *&LabelDecl = getLabelMap()[LabelII];
Chris Lattnere2473062007-05-28 06:28:18 +0000964
Steve Naroff846b1ec2009-03-13 15:38:40 +0000965 // If we haven't seen this label yet, create a forward reference.
966 if (LabelDecl == 0)
Ted Kremenek5a201952009-02-07 01:47:29 +0000967 LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
Sebastian Redl573feed2009-01-18 13:19:59 +0000968
Ted Kremenek5a201952009-02-07 01:47:29 +0000969 return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
Chris Lattneraf8d5812006-11-10 05:07:45 +0000970}
Chris Lattner1c310502007-05-31 06:00:00 +0000971
Sebastian Redl573feed2009-01-18 13:19:59 +0000972Action::OwningStmtResult
Chris Lattner34d9a512009-04-19 01:04:21 +0000973Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc,
Sebastian Redl573feed2009-01-18 13:19:59 +0000974 ExprArg DestExp) {
Eli Friedman8d7ff402009-03-26 00:18:06 +0000975 // Convert operand to void*
Eli Friedman6568eef2009-03-26 07:32:37 +0000976 Expr* E = DestExp.takeAs<Expr>();
Douglas Gregor30776d42009-05-16 00:20:29 +0000977 if (!E->isTypeDependent()) {
978 QualType ETy = E->getType();
Chandler Carruth00216982010-01-31 10:26:25 +0000979 QualType DestTy = Context.getPointerType(Context.VoidTy.withConst());
Douglas Gregor30776d42009-05-16 00:20:29 +0000980 AssignConvertType ConvTy =
Chandler Carruth00216982010-01-31 10:26:25 +0000981 CheckSingleAssignmentConstraints(DestTy, E);
982 if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing))
Douglas Gregor30776d42009-05-16 00:20:29 +0000983 return StmtError();
984 }
985 return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E));
Chris Lattneraf8d5812006-11-10 05:07:45 +0000986}
987
Sebastian Redl573feed2009-01-18 13:19:59 +0000988Action::OwningStmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +0000989Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Chris Lattnereaafe1222006-11-10 05:17:58 +0000990 Scope *S = CurScope->getContinueParent();
991 if (!S) {
992 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
Sebastian Redl573feed2009-01-18 13:19:59 +0000993 return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
Chris Lattnereaafe1222006-11-10 05:17:58 +0000994 }
Sebastian Redl573feed2009-01-18 13:19:59 +0000995
Ted Kremenek5a201952009-02-07 01:47:29 +0000996 return Owned(new (Context) ContinueStmt(ContinueLoc));
Chris Lattneraf8d5812006-11-10 05:07:45 +0000997}
998
Sebastian Redl573feed2009-01-18 13:19:59 +0000999Action::OwningStmtResult
Steve Naroff66356bd2007-09-16 14:56:35 +00001000Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Chris Lattnereaafe1222006-11-10 05:17:58 +00001001 Scope *S = CurScope->getBreakParent();
1002 if (!S) {
1003 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
Sebastian Redl573feed2009-01-18 13:19:59 +00001004 return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
Chris Lattnereaafe1222006-11-10 05:17:58 +00001005 }
Sebastian Redl573feed2009-01-18 13:19:59 +00001006
Ted Kremenek5a201952009-02-07 01:47:29 +00001007 return Owned(new (Context) BreakStmt(BreakLoc));
Chris Lattneraf8d5812006-11-10 05:07:45 +00001008}
1009
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001010/// \brief Determine whether a return statement is a candidate for the named
1011/// return value optimization (C++0x 12.8p34, bullet 1).
1012///
1013/// \param Ctx The context in which the return expression and type occur.
1014///
1015/// \param RetType The return type of the function or block.
1016///
1017/// \param RetExpr The expression being returned from the function or block.
1018///
1019/// \returns The NRVO candidate variable, if the return statement may use the
1020/// NRVO, or NULL if there is no such candidate.
1021static const VarDecl *getNRVOCandidate(ASTContext &Ctx, QualType RetType,
1022 Expr *RetExpr) {
Douglas Gregor222cf0e2010-05-15 00:13:29 +00001023 QualType ExprType = RetExpr->getType();
1024 // - in a return statement in a function with ...
1025 // ... a class return type ...
1026 if (!RetType->isRecordType())
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001027 return 0;
Douglas Gregor222cf0e2010-05-15 00:13:29 +00001028 // ... the same cv-unqualified type as the function return type ...
1029 if (!Ctx.hasSameUnqualifiedType(RetType, ExprType))
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001030 return 0;
Douglas Gregor222cf0e2010-05-15 00:13:29 +00001031 // ... the expression is the name of a non-volatile automatic object ...
1032 // We ignore parentheses here.
1033 // FIXME: Is this compliant? (Everyone else does it)
1034 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetExpr->IgnoreParens());
1035 if (!DR)
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001036 return 0;
Douglas Gregor222cf0e2010-05-15 00:13:29 +00001037 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
1038 if (!VD)
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001039 return 0;
1040
1041 if (VD->getKind() == Decl::Var && VD->hasLocalStorage() &&
Douglas Gregor290c93e2010-05-15 06:46:45 +00001042 !VD->getType()->isReferenceType() && !VD->hasAttr<BlocksAttr>() &&
1043 !VD->getType().isVolatileQualified())
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001044 return VD;
1045
1046 return 0;
Douglas Gregor222cf0e2010-05-15 00:13:29 +00001047}
1048
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001049/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
Steve Naroffc540d662008-09-03 18:15:37 +00001050///
Sebastian Redl573feed2009-01-18 13:19:59 +00001051Action::OwningStmtResult
Steve Naroffc540d662008-09-03 18:15:37 +00001052Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
Steve Naroffc540d662008-09-03 18:15:37 +00001053 // If this is the first return we've seen in the block, infer the type of
1054 // the block from it.
Douglas Gregor9a28e842010-03-01 23:15:13 +00001055 BlockScopeInfo *CurBlock = getCurBlock();
Fariborz Jahanian3fd73102009-06-19 23:37:08 +00001056 if (CurBlock->ReturnType.isNull()) {
Steve Naroff3b1e1722008-09-16 22:25:10 +00001057 if (RetValExp) {
Steve Naroffc60873c2008-09-24 22:26:48 +00001058 // Don't call UsualUnaryConversions(), since we don't want to do
1059 // integer promotions here.
Douglas Gregorb92a1562010-02-03 00:27:59 +00001060 DefaultFunctionArrayLvalueConversion(RetValExp);
Fariborz Jahanian3fd73102009-06-19 23:37:08 +00001061 CurBlock->ReturnType = RetValExp->getType();
1062 if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(RetValExp)) {
1063 // We have to remove a 'const' added to copied-in variable which was
1064 // part of the implementation spec. and not the actual qualifier for
1065 // the variable.
1066 if (CDRE->isConstQualAdded())
1067 CurBlock->ReturnType.removeConst();
1068 }
Steve Naroff3b1e1722008-09-16 22:25:10 +00001069 } else
Fariborz Jahanian3fd73102009-06-19 23:37:08 +00001070 CurBlock->ReturnType = Context.VoidTy;
Steve Naroffc540d662008-09-03 18:15:37 +00001071 }
Fariborz Jahanian3fd73102009-06-19 23:37:08 +00001072 QualType FnRetType = CurBlock->ReturnType;
Sebastian Redl573feed2009-01-18 13:19:59 +00001073
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001074 if (CurBlock->TheDecl->hasAttr<NoReturnAttr>()) {
Mike Stump56ed2ea2009-04-29 21:40:37 +00001075 Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr)
1076 << getCurFunctionOrMethodDecl()->getDeclName();
1077 return StmtError();
1078 }
1079
Steve Naroffc540d662008-09-03 18:15:37 +00001080 // Otherwise, verify that this result type matches the previous one. We are
1081 // pickier with blocks than for normal functions because we don't have GCC
1082 // compatibility to worry about here.
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001083 ReturnStmt *Result = 0;
Steve Naroffc540d662008-09-03 18:15:37 +00001084 if (CurBlock->ReturnType->isVoidType()) {
1085 if (RetValExp) {
1086 Diag(ReturnLoc, diag::err_return_block_has_expr);
Steve Naroffc540d662008-09-03 18:15:37 +00001087 RetValExp = 0;
1088 }
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001089 Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0);
1090 } else if (!RetValExp) {
Sebastian Redl573feed2009-01-18 13:19:59 +00001091 return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001092 } else {
1093 const VarDecl *NRVOCandidate = 0;
1094
1095 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
1096 // we have a non-void block with an expression, continue checking
Sebastian Redl573feed2009-01-18 13:19:59 +00001097
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001098 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
1099 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
1100 // function return.
Sebastian Redl573feed2009-01-18 13:19:59 +00001101
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001102 // In C++ the return statement is handled via a copy initialization.
1103 // the C version of which boils down to CheckSingleAssignmentConstraints.
1104 NRVOCandidate = getNRVOCandidate(Context, FnRetType, RetValExp);
1105 OwningExprResult Res = PerformCopyInitialization(
1106 InitializedEntity::InitializeResult(ReturnLoc,
1107 FnRetType,
1108 NRVOCandidate != 0),
1109 SourceLocation(),
1110 Owned(RetValExp));
1111 if (Res.isInvalid()) {
1112 // FIXME: Cleanup temporaries here, anyway?
1113 return StmtError();
1114 }
1115
1116 if (RetValExp)
1117 RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp);
Mike Stump82f071f2009-02-04 22:31:32 +00001118
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001119 RetValExp = Res.takeAs<Expr>();
1120 if (RetValExp)
1121 CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
Anders Carlsson6f923f82010-01-29 18:30:20 +00001122 }
1123
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001124 Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate);
Steve Naroffc540d662008-09-03 18:15:37 +00001125 }
Sebastian Redl573feed2009-01-18 13:19:59 +00001126
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001127 // If we need to check for the named return value optimization, save the
1128 // return statement in our scope for later processing.
1129 if (getLangOptions().CPlusPlus && FnRetType->isRecordType() &&
1130 !CurContext->isDependentContext())
1131 FunctionScopes.back()->Returns.push_back(Result);
1132
1133 return Owned(Result);
Steve Naroffc540d662008-09-03 18:15:37 +00001134}
Chris Lattneraf8d5812006-11-10 05:07:45 +00001135
Sebastian Redl573feed2009-01-18 13:19:59 +00001136Action::OwningStmtResult
Anders Carlssona1929472009-08-18 16:11:00 +00001137Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) {
1138 Expr *RetValExp = rex.takeAs<Expr>();
Douglas Gregor9a28e842010-03-01 23:15:13 +00001139 if (getCurBlock())
Steve Naroffc540d662008-09-03 18:15:37 +00001140 return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
Sebastian Redl573feed2009-01-18 13:19:59 +00001141
Chris Lattner79413952008-12-04 23:50:19 +00001142 QualType FnRetType;
Mike Stumpd00bc1a2009-04-29 00:43:21 +00001143 if (const FunctionDecl *FD = getCurFunctionDecl()) {
Chris Lattner79413952008-12-04 23:50:19 +00001144 FnRetType = FD->getResultType();
John McCallab26cfa2010-02-05 21:31:56 +00001145 if (FD->hasAttr<NoReturnAttr>() ||
1146 FD->getType()->getAs<FunctionType>()->getNoReturnAttr())
Chris Lattner6e127a62009-05-31 19:32:13 +00001147 Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr)
Mike Stumpd00bc1a2009-04-29 00:43:21 +00001148 << getCurFunctionOrMethodDecl()->getDeclName();
Mike Stumpd00bc1a2009-04-29 00:43:21 +00001149 } else if (ObjCMethodDecl *MD = getCurMethodDecl())
Steve Narofff3833d72009-03-03 00:45:38 +00001150 FnRetType = MD->getResultType();
1151 else // If we don't have a function/method context, bail.
1152 return StmtError();
Mike Stump11289f42009-09-09 15:08:12 +00001153
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001154 ReturnStmt *Result = 0;
Chris Lattner9bad62c2008-01-04 18:04:52 +00001155 if (FnRetType->isVoidType()) {
Douglas Gregor78b691a2009-10-01 23:25:31 +00001156 if (RetValExp && !RetValExp->isTypeDependent()) {
1157 // C99 6.8.6.4p1 (ext_ since GCC warns)
Chris Lattner27e5bef2008-12-18 02:01:17 +00001158 unsigned D = diag::ext_return_has_expr;
1159 if (RetValExp->getType()->isVoidType())
1160 D = diag::ext_return_has_void_expr;
Sebastian Redl573feed2009-01-18 13:19:59 +00001161
Chris Lattner0cb00d62008-12-18 02:03:48 +00001162 // return (some void expression); is legal in C++.
1163 if (D != diag::ext_return_has_void_expr ||
1164 !getLangOptions().CPlusPlus) {
1165 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
1166 Diag(ReturnLoc, D)
1167 << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
1168 << RetValExp->getSourceRange();
1169 }
Mike Stump11289f42009-09-09 15:08:12 +00001170
Anders Carlsson6e997b22009-12-15 20:51:39 +00001171 RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001172 }
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001173
1174 Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0);
1175 } else if (!RetValExp && !FnRetType->isDependentType()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001176 unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4
1177 // C99 6.8.6.4p1 (ext_ since GCC warns)
1178 if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
1179
1180 if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattnere3d20d92008-11-23 21:45:46 +00001181 Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001182 else
Chris Lattnere3d20d92008-11-23 21:45:46 +00001183 Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001184 Result = new (Context) ReturnStmt(ReturnLoc);
1185 } else {
1186 const VarDecl *NRVOCandidate = 0;
1187 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
1188 // we have a non-void function with an expression, continue checking
Sebastian Redl573feed2009-01-18 13:19:59 +00001189
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001190 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
1191 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
1192 // function return.
Sebastian Redl573feed2009-01-18 13:19:59 +00001193
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001194 // In C++ the return statement is handled via a copy initialization.
1195 // the C version of which boils down to CheckSingleAssignmentConstraints.
1196 NRVOCandidate = getNRVOCandidate(Context, FnRetType, RetValExp);
1197 OwningExprResult Res = PerformCopyInitialization(
1198 InitializedEntity::InitializeResult(ReturnLoc,
1199 FnRetType,
1200 NRVOCandidate != 0),
1201 SourceLocation(),
1202 Owned(RetValExp));
1203 if (Res.isInvalid()) {
1204 // FIXME: Cleanup temporaries here, anyway?
1205 return StmtError();
1206 }
Sebastian Redl573feed2009-01-18 13:19:59 +00001207
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001208 RetValExp = Res.takeAs<Expr>();
1209 if (RetValExp)
1210 CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
Douglas Gregorffe14e32009-11-14 01:20:54 +00001211 }
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001212
1213 if (RetValExp)
1214 RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp);
1215 Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate);
Douglas Gregor4619e432008-12-05 23:32:09 +00001216 }
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001217
1218 // If we need to check for the named return value optimization, save the
1219 // return statement in our scope for later processing.
1220 if (getLangOptions().CPlusPlus && FnRetType->isRecordType() &&
1221 !CurContext->isDependentContext())
1222 FunctionScopes.back()->Returns.push_back(Result);
1223
1224 return Owned(Result);
Chris Lattneraf8d5812006-11-10 05:07:45 +00001225}
1226
Chris Lattnercda4d7e2009-03-13 17:38:01 +00001227/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
1228/// ignore "noop" casts in places where an lvalue is required by an inline asm.
1229/// We emulate this behavior when -fheinous-gnu-extensions is specified, but
1230/// provide a strong guidance to not use it.
1231///
1232/// This method checks to see if the argument is an acceptable l-value and
1233/// returns false if it is a case we can handle.
1234static bool CheckAsmLValue(const Expr *E, Sema &S) {
Anders Carlssonaaeef072010-01-24 05:50:09 +00001235 // Type dependent expressions will be checked during instantiation.
1236 if (E->isTypeDependent())
1237 return false;
1238
Chris Lattnercda4d7e2009-03-13 17:38:01 +00001239 if (E->isLvalue(S.Context) == Expr::LV_Valid)
1240 return false; // Cool, this is an lvalue.
1241
1242 // Okay, this is not an lvalue, but perhaps it is the result of a cast that we
1243 // are supposed to allow.
1244 const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
1245 if (E != E2 && E2->isLvalue(S.Context) == Expr::LV_Valid) {
1246 if (!S.getLangOptions().HeinousExtensions)
1247 S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue)
1248 << E->getSourceRange();
1249 else
1250 S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
1251 << E->getSourceRange();
1252 // Accept, even if we emitted an error diagnostic.
1253 return false;
1254 }
1255
1256 // None of the above, just randomly invalid non-lvalue.
1257 return true;
1258}
1259
1260
Sebastian Redl24b8e152009-01-18 16:53:17 +00001261Sema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
1262 bool IsSimple,
1263 bool IsVolatile,
1264 unsigned NumOutputs,
1265 unsigned NumInputs,
Anders Carlsson9a020f92010-01-30 22:25:16 +00001266 IdentifierInfo **Names,
Sebastian Redl24b8e152009-01-18 16:53:17 +00001267 MultiExprArg constraints,
1268 MultiExprArg exprs,
1269 ExprArg asmString,
1270 MultiExprArg clobbers,
Mike Stump90be58a2010-01-04 22:37:17 +00001271 SourceLocation RParenLoc,
1272 bool MSAsm) {
Sebastian Redl24b8e152009-01-18 16:53:17 +00001273 unsigned NumClobbers = clobbers.size();
1274 StringLiteral **Constraints =
1275 reinterpret_cast<StringLiteral**>(constraints.get());
1276 Expr **Exprs = reinterpret_cast<Expr **>(exprs.get());
1277 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get());
1278 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
1279
Anders Carlsson570c3572009-01-27 20:38:24 +00001280 llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
Mike Stump11289f42009-09-09 15:08:12 +00001281
Chris Lattner496acc12008-08-18 19:55:17 +00001282 // The parser verifies that there is a string literal here.
Chris Lattner07096892008-07-23 06:46:56 +00001283 if (AsmString->isWide())
Sebastian Redl24b8e152009-01-18 16:53:17 +00001284 return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
1285 << AsmString->getSourceRange());
1286
Chris Lattner496acc12008-08-18 19:55:17 +00001287 for (unsigned i = 0; i != NumOutputs; i++) {
1288 StringLiteral *Literal = Constraints[i];
Chris Lattner07096892008-07-23 06:46:56 +00001289 if (Literal->isWide())
Sebastian Redl24b8e152009-01-18 16:53:17 +00001290 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
1291 << Literal->getSourceRange());
1292
Anders Carlsson9a020f92010-01-30 22:25:16 +00001293 llvm::StringRef OutputName;
1294 if (Names[i])
1295 OutputName = Names[i]->getName();
1296
1297 TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName);
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +00001298 if (!Context.Target.validateOutputConstraint(Info))
Sebastian Redl24b8e152009-01-18 16:53:17 +00001299 return StmtError(Diag(Literal->getLocStart(),
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +00001300 diag::err_asm_invalid_output_constraint)
1301 << Info.getConstraintStr());
Sebastian Redl24b8e152009-01-18 16:53:17 +00001302
Anders Carlssonf511f642007-11-27 04:11:28 +00001303 // Check that the output exprs are valid lvalues.
Eli Friedman47e78572009-05-03 07:49:42 +00001304 Expr *OutputExpr = Exprs[i];
Chris Lattnercda4d7e2009-03-13 17:38:01 +00001305 if (CheckAsmLValue(OutputExpr, *this)) {
Eli Friedman47e78572009-05-03 07:49:42 +00001306 return StmtError(Diag(OutputExpr->getLocStart(),
Chris Lattnerf490e152008-11-19 05:27:50 +00001307 diag::err_asm_invalid_lvalue_in_output)
Eli Friedman47e78572009-05-03 07:49:42 +00001308 << OutputExpr->getSourceRange());
Anders Carlsson80a5ea32007-11-23 19:43:50 +00001309 }
Mike Stump11289f42009-09-09 15:08:12 +00001310
Chris Lattnerd9725f72009-04-26 07:16:29 +00001311 OutputConstraintInfos.push_back(Info);
Anders Carlsson80a5ea32007-11-23 19:43:50 +00001312 }
Sebastian Redl24b8e152009-01-18 16:53:17 +00001313
Chris Lattner34b51e82009-05-03 05:55:43 +00001314 llvm::SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1315
Anders Carlsson80a5ea32007-11-23 19:43:50 +00001316 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Chris Lattner496acc12008-08-18 19:55:17 +00001317 StringLiteral *Literal = Constraints[i];
Chris Lattner07096892008-07-23 06:46:56 +00001318 if (Literal->isWide())
Sebastian Redl24b8e152009-01-18 16:53:17 +00001319 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
1320 << Literal->getSourceRange());
1321
Anders Carlsson9a020f92010-01-30 22:25:16 +00001322 llvm::StringRef InputName;
1323 if (Names[i])
1324 InputName = Names[i]->getName();
1325
1326 TargetInfo::ConstraintInfo Info(Literal->getString(), InputName);
Jay Foad7d0479f2009-05-21 09:52:38 +00001327 if (!Context.Target.validateInputConstraint(OutputConstraintInfos.data(),
Chris Lattnerc16d4762009-04-26 17:57:12 +00001328 NumOutputs, Info)) {
Sebastian Redl24b8e152009-01-18 16:53:17 +00001329 return StmtError(Diag(Literal->getLocStart(),
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +00001330 diag::err_asm_invalid_input_constraint)
1331 << Info.getConstraintStr());
Anders Carlssonf511f642007-11-27 04:11:28 +00001332 }
Sebastian Redl24b8e152009-01-18 16:53:17 +00001333
Eli Friedman47e78572009-05-03 07:49:42 +00001334 Expr *InputExpr = Exprs[i];
Sebastian Redl24b8e152009-01-18 16:53:17 +00001335
Anders Carlsson224fca82009-01-20 20:49:22 +00001336 // Only allow void types for memory constraints.
Chris Lattnerd9725f72009-04-26 07:16:29 +00001337 if (Info.allowsMemory() && !Info.allowsRegister()) {
Chris Lattnercda4d7e2009-03-13 17:38:01 +00001338 if (CheckAsmLValue(InputExpr, *this))
Eli Friedman47e78572009-05-03 07:49:42 +00001339 return StmtError(Diag(InputExpr->getLocStart(),
Anders Carlsson224fca82009-01-20 20:49:22 +00001340 diag::err_asm_invalid_lvalue_in_input)
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +00001341 << Info.getConstraintStr()
Eli Friedman47e78572009-05-03 07:49:42 +00001342 << InputExpr->getSourceRange());
Anders Carlsson80a5ea32007-11-23 19:43:50 +00001343 }
Sebastian Redl24b8e152009-01-18 16:53:17 +00001344
Chris Lattnerd9725f72009-04-26 07:16:29 +00001345 if (Info.allowsRegister()) {
Anders Carlsson224fca82009-01-20 20:49:22 +00001346 if (InputExpr->getType()->isVoidType()) {
Eli Friedman47e78572009-05-03 07:49:42 +00001347 return StmtError(Diag(InputExpr->getLocStart(),
Anders Carlsson224fca82009-01-20 20:49:22 +00001348 diag::err_asm_invalid_type_in_input)
Mike Stump11289f42009-09-09 15:08:12 +00001349 << InputExpr->getType() << Info.getConstraintStr()
Eli Friedman47e78572009-05-03 07:49:42 +00001350 << InputExpr->getSourceRange());
Anders Carlsson224fca82009-01-20 20:49:22 +00001351 }
Anders Carlsson224fca82009-01-20 20:49:22 +00001352 }
Mike Stump11289f42009-09-09 15:08:12 +00001353
Douglas Gregorb92a1562010-02-03 00:27:59 +00001354 DefaultFunctionArrayLvalueConversion(Exprs[i]);
Mike Stump11289f42009-09-09 15:08:12 +00001355
Chris Lattner34b51e82009-05-03 05:55:43 +00001356 InputConstraintInfos.push_back(Info);
Anders Carlsson80a5ea32007-11-23 19:43:50 +00001357 }
Sebastian Redl24b8e152009-01-18 16:53:17 +00001358
Anders Carlsson290aa852007-11-25 00:25:21 +00001359 // Check that the clobbers are valid.
Chris Lattner496acc12008-08-18 19:55:17 +00001360 for (unsigned i = 0; i != NumClobbers; i++) {
1361 StringLiteral *Literal = Clobbers[i];
Chris Lattner07096892008-07-23 06:46:56 +00001362 if (Literal->isWide())
Sebastian Redl24b8e152009-01-18 16:53:17 +00001363 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
1364 << Literal->getSourceRange());
1365
Anders Carlsson96fe0b52010-01-30 19:34:25 +00001366 llvm::StringRef Clobber = Literal->getString();
Sebastian Redl24b8e152009-01-18 16:53:17 +00001367
Anders Carlsson96fe0b52010-01-30 19:34:25 +00001368 if (!Context.Target.isValidGCCRegisterName(Clobber))
Sebastian Redl24b8e152009-01-18 16:53:17 +00001369 return StmtError(Diag(Literal->getLocStart(),
Daniel Dunbar58bc48c2009-08-19 20:04:03 +00001370 diag::err_asm_unknown_register_name) << Clobber);
Anders Carlsson290aa852007-11-25 00:25:21 +00001371 }
Sebastian Redl24b8e152009-01-18 16:53:17 +00001372
1373 constraints.release();
1374 exprs.release();
1375 asmString.release();
1376 clobbers.release();
Chris Lattnerd8c7ba22009-03-10 23:41:04 +00001377 AsmStmt *NS =
Anders Carlsson98323d22010-01-30 23:19:41 +00001378 new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, MSAsm,
1379 NumOutputs, NumInputs, Names, Constraints, Exprs,
1380 AsmString, NumClobbers, Clobbers, RParenLoc);
Chris Lattnerd8c7ba22009-03-10 23:41:04 +00001381 // Validate the asm string, ensuring it makes sense given the operands we
1382 // have.
1383 llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces;
1384 unsigned DiagOffs;
1385 if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
Chris Lattner0cdaa2e2009-03-10 23:57:07 +00001386 Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
1387 << AsmString->getSourceRange();
Chris Lattnerd8c7ba22009-03-10 23:41:04 +00001388 DeleteStmt(NS);
1389 return StmtError();
1390 }
Mike Stump11289f42009-09-09 15:08:12 +00001391
Chris Lattner34b51e82009-05-03 05:55:43 +00001392 // Validate tied input operands for type mismatches.
1393 for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) {
1394 TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
Mike Stump11289f42009-09-09 15:08:12 +00001395
Chris Lattner34b51e82009-05-03 05:55:43 +00001396 // If this is a tied constraint, verify that the output and input have
1397 // either exactly the same type, or that they are int/ptr operands with the
1398 // same size (int/long, int*/long, are ok etc).
1399 if (!Info.hasTiedOperand()) continue;
Mike Stump11289f42009-09-09 15:08:12 +00001400
Chris Lattner34b51e82009-05-03 05:55:43 +00001401 unsigned TiedTo = Info.getTiedOperand();
Chris Lattnercb66c732009-05-03 07:04:21 +00001402 Expr *OutputExpr = Exprs[TiedTo];
Chris Lattner28b05c82009-05-03 06:50:40 +00001403 Expr *InputExpr = Exprs[i+NumOutputs];
Chris Lattner2c295cf2009-05-03 05:59:17 +00001404 QualType InTy = InputExpr->getType();
1405 QualType OutTy = OutputExpr->getType();
1406 if (Context.hasSameType(InTy, OutTy))
Chris Lattner34b51e82009-05-03 05:55:43 +00001407 continue; // All types can be tied to themselves.
Mike Stump11289f42009-09-09 15:08:12 +00001408
Chris Lattnerdb6d5cb2010-04-23 17:27:29 +00001409 // Decide if the input and output are in the same domain (integer/ptr or
1410 // floating point.
1411 enum AsmDomain {
1412 AD_Int, AD_FP, AD_Other
1413 } InputDomain, OutputDomain;
1414
1415 if (InTy->isIntegerType() || InTy->isPointerType())
1416 InputDomain = AD_Int;
Douglas Gregor49b4d732010-06-22 23:07:26 +00001417 else if (InTy->isRealFloatingType())
Chris Lattnerdb6d5cb2010-04-23 17:27:29 +00001418 InputDomain = AD_FP;
1419 else
1420 InputDomain = AD_Other;
Mike Stump11289f42009-09-09 15:08:12 +00001421
Chris Lattnerdb6d5cb2010-04-23 17:27:29 +00001422 if (OutTy->isIntegerType() || OutTy->isPointerType())
1423 OutputDomain = AD_Int;
Douglas Gregor49b4d732010-06-22 23:07:26 +00001424 else if (OutTy->isRealFloatingType())
Chris Lattnerdb6d5cb2010-04-23 17:27:29 +00001425 OutputDomain = AD_FP;
1426 else
1427 OutputDomain = AD_Other;
1428
1429 // They are ok if they are the same size and in the same domain. This
1430 // allows tying things like:
1431 // void* to int*
1432 // void* to int if they are the same size.
1433 // double to long double if they are the same size.
1434 //
1435 uint64_t OutSize = Context.getTypeSize(OutTy);
1436 uint64_t InSize = Context.getTypeSize(InTy);
1437 if (OutSize == InSize && InputDomain == OutputDomain &&
1438 InputDomain != AD_Other)
1439 continue;
1440
1441 // If the smaller input/output operand is not mentioned in the asm string,
1442 // then we can promote it and the asm string won't notice. Check this
1443 // case now.
1444 bool SmallerValueMentioned = false;
1445 for (unsigned p = 0, e = Pieces.size(); p != e; ++p) {
1446 AsmStmt::AsmStringPiece &Piece = Pieces[p];
1447 if (!Piece.isOperand()) continue;
Mike Stump11289f42009-09-09 15:08:12 +00001448
Chris Lattnerdb6d5cb2010-04-23 17:27:29 +00001449 // If this is a reference to the input and if the input was the smaller
1450 // one, then we have to reject this asm.
1451 if (Piece.getOperandNo() == i+NumOutputs) {
1452 if (InSize < OutSize) {
1453 SmallerValueMentioned = true;
1454 break;
Chris Lattnercc1cde92009-05-03 08:32:32 +00001455 }
Chris Lattnercb66c732009-05-03 07:04:21 +00001456 }
Mike Stump11289f42009-09-09 15:08:12 +00001457
Chris Lattnerdb6d5cb2010-04-23 17:27:29 +00001458 // If this is a reference to the input and if the input was the smaller
1459 // one, then we have to reject this asm.
1460 if (Piece.getOperandNo() == TiedTo) {
1461 if (InSize > OutSize) {
1462 SmallerValueMentioned = true;
1463 break;
1464 }
1465 }
Chris Lattner34b51e82009-05-03 05:55:43 +00001466 }
Mike Stump11289f42009-09-09 15:08:12 +00001467
Chris Lattnerdb6d5cb2010-04-23 17:27:29 +00001468 // If the smaller value wasn't mentioned in the asm string, and if the
1469 // output was a register, just extend the shorter one to the size of the
1470 // larger one.
1471 if (!SmallerValueMentioned && InputDomain != AD_Other &&
1472 OutputConstraintInfos[TiedTo].allowsRegister())
1473 continue;
1474
Chris Lattner28b05c82009-05-03 06:50:40 +00001475 Diag(InputExpr->getLocStart(),
Chris Lattner34b51e82009-05-03 05:55:43 +00001476 diag::err_asm_tying_incompatible_types)
Chris Lattner2c295cf2009-05-03 05:59:17 +00001477 << InTy << OutTy << OutputExpr->getSourceRange()
Chris Lattner34b51e82009-05-03 05:55:43 +00001478 << InputExpr->getSourceRange();
1479 DeleteStmt(NS);
1480 return StmtError();
1481 }
Mike Stump11289f42009-09-09 15:08:12 +00001482
Chris Lattnerd8c7ba22009-03-10 23:41:04 +00001483 return Owned(NS);
Chris Lattner73c56c02007-10-29 04:04:16 +00001484}
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001485
Sebastian Redl481bf3f2009-01-18 17:43:11 +00001486Action::OwningStmtResult
1487Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001488 SourceLocation RParen, DeclPtrTy Parm,
Douglas Gregor96c79492010-04-23 22:50:49 +00001489 StmtArg Body) {
Douglas Gregorf3564192010-04-26 17:32:49 +00001490 VarDecl *Var = cast_or_null<VarDecl>(Parm.getAs<Decl>());
1491 if (Var && Var->isInvalidDecl())
1492 return StmtError();
1493
1494 return Owned(new (Context) ObjCAtCatchStmt(AtLoc, RParen, Var,
Douglas Gregor96c79492010-04-23 22:50:49 +00001495 Body.takeAs<Stmt>()));
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001496}
1497
Sebastian Redl481bf3f2009-01-18 17:43:11 +00001498Action::OwningStmtResult
1499Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) {
Ted Kremenek5a201952009-02-07 01:47:29 +00001500 return Owned(new (Context) ObjCAtFinallyStmt(AtLoc,
1501 static_cast<Stmt*>(Body.release())));
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001502}
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001503
Sebastian Redl481bf3f2009-01-18 17:43:11 +00001504Action::OwningStmtResult
Douglas Gregor96c79492010-04-23 22:50:49 +00001505Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc, StmtArg Try,
1506 MultiStmtArg CatchStmts, StmtArg Finally) {
Douglas Gregor9a28e842010-03-01 23:15:13 +00001507 FunctionNeedsScopeChecking() = true;
Douglas Gregor96c79492010-04-23 22:50:49 +00001508 unsigned NumCatchStmts = CatchStmts.size();
1509 return Owned(ObjCAtTryStmt::Create(Context, AtLoc, Try.takeAs<Stmt>(),
1510 (Stmt **)CatchStmts.release(),
1511 NumCatchStmts,
1512 Finally.takeAs<Stmt>()));
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001513}
1514
Douglas Gregor2900c162010-04-22 21:44:01 +00001515Sema::OwningStmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc,
1516 ExprArg ThrowE) {
1517 Expr *Throw = static_cast<Expr *>(ThrowE.get());
1518 if (Throw) {
1519 QualType ThrowType = Throw->getType();
1520 // Make sure the expression type is an ObjC pointer or "void *".
1521 if (!ThrowType->isDependentType() &&
1522 !ThrowType->isObjCObjectPointerType()) {
1523 const PointerType *PT = ThrowType->getAs<PointerType>();
1524 if (!PT || !PT->getPointeeType()->isVoidType())
1525 return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
1526 << Throw->getType() << Throw->getSourceRange());
1527 }
1528 }
1529
1530 return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowE.takeAs<Expr>()));
1531}
1532
Sebastian Redl481bf3f2009-01-18 17:43:11 +00001533Action::OwningStmtResult
Douglas Gregor2900c162010-04-22 21:44:01 +00001534Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg Throw,
1535 Scope *CurScope) {
1536 if (!Throw.get()) {
Steve Naroff5ee2c022009-02-11 20:05:44 +00001537 // @throw without an expression designates a rethrow (which much occur
1538 // in the context of an @catch clause).
1539 Scope *AtCatchParent = CurScope;
1540 while (AtCatchParent && !AtCatchParent->isAtCatchScope())
1541 AtCatchParent = AtCatchParent->getParent();
1542 if (!AtCatchParent)
Steve Naroffc49b22a2009-02-12 18:09:32 +00001543 return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
Douglas Gregor2900c162010-04-22 21:44:01 +00001544 }
1545
1546 return BuildObjCAtThrowStmt(AtLoc, move(Throw));
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001547}
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001548
Sebastian Redl481bf3f2009-01-18 17:43:11 +00001549Action::OwningStmtResult
1550Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr,
1551 StmtArg SynchBody) {
Douglas Gregor9a28e842010-03-01 23:15:13 +00001552 FunctionNeedsScopeChecking() = true;
Chris Lattnerc70dd562009-04-21 06:01:00 +00001553
Chris Lattner3501d432009-04-21 06:11:25 +00001554 // Make sure the expression type is an ObjC pointer or "void *".
1555 Expr *SyncExpr = static_cast<Expr*>(SynchExpr.get());
Douglas Gregor6148de72010-04-22 22:01:21 +00001556 if (!SyncExpr->getType()->isDependentType() &&
1557 !SyncExpr->getType()->isObjCObjectPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001558 const PointerType *PT = SyncExpr->getType()->getAs<PointerType>();
Chris Lattner3501d432009-04-21 06:11:25 +00001559 if (!PT || !PT->getPointeeType()->isVoidType())
1560 return StmtError(Diag(AtLoc, diag::error_objc_synchronized_expects_object)
1561 << SyncExpr->getType() << SyncExpr->getSourceRange());
1562 }
Mike Stump11289f42009-09-09 15:08:12 +00001563
1564 return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc,
Anders Carlssonb781bcd2009-05-01 19:49:17 +00001565 SynchExpr.takeAs<Stmt>(),
1566 SynchBody.takeAs<Stmt>()));
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001567}
Sebastian Redl54c04d42008-12-22 19:15:10 +00001568
1569/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
1570/// and creates a proper catch handler from them.
1571Action::OwningStmtResult
Chris Lattner83f095c2009-03-28 19:18:32 +00001572Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclPtrTy ExDecl,
Sebastian Redl54c04d42008-12-22 19:15:10 +00001573 StmtArg HandlerBlock) {
1574 // There's nothing to test that ActOnExceptionDecl didn't already test.
Ted Kremenek5a201952009-02-07 01:47:29 +00001575 return Owned(new (Context) CXXCatchStmt(CatchLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001576 cast_or_null<VarDecl>(ExDecl.getAs<Decl>()),
Anders Carlssonb781bcd2009-05-01 19:49:17 +00001577 HandlerBlock.takeAs<Stmt>()));
Sebastian Redl54c04d42008-12-22 19:15:10 +00001578}
Sebastian Redl9b244a82008-12-22 21:35:02 +00001579
Sebastian Redl63c4da02009-07-29 17:15:45 +00001580class TypeWithHandler {
1581 QualType t;
1582 CXXCatchStmt *stmt;
1583public:
1584 TypeWithHandler(const QualType &type, CXXCatchStmt *statement)
1585 : t(type), stmt(statement) {}
1586
John McCall8ccfcb52009-09-24 19:53:00 +00001587 // An arbitrary order is fine as long as it places identical
1588 // types next to each other.
Sebastian Redl63c4da02009-07-29 17:15:45 +00001589 bool operator<(const TypeWithHandler &y) const {
John McCall8ccfcb52009-09-24 19:53:00 +00001590 if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr())
Sebastian Redl63c4da02009-07-29 17:15:45 +00001591 return true;
John McCall8ccfcb52009-09-24 19:53:00 +00001592 if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr())
Sebastian Redl63c4da02009-07-29 17:15:45 +00001593 return false;
1594 else
1595 return getTypeSpecStartLoc() < y.getTypeSpecStartLoc();
1596 }
Mike Stump11289f42009-09-09 15:08:12 +00001597
Sebastian Redl63c4da02009-07-29 17:15:45 +00001598 bool operator==(const TypeWithHandler& other) const {
John McCall8ccfcb52009-09-24 19:53:00 +00001599 return t == other.t;
Sebastian Redl63c4da02009-07-29 17:15:45 +00001600 }
Mike Stump11289f42009-09-09 15:08:12 +00001601
Sebastian Redl63c4da02009-07-29 17:15:45 +00001602 QualType getQualType() const { return t; }
1603 CXXCatchStmt *getCatchStmt() const { return stmt; }
1604 SourceLocation getTypeSpecStartLoc() const {
1605 return stmt->getExceptionDecl()->getTypeSpecStartLoc();
1606 }
1607};
1608
Sebastian Redl9b244a82008-12-22 21:35:02 +00001609/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
1610/// handlers and creates a try statement from them.
1611Action::OwningStmtResult
1612Sema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock,
1613 MultiStmtArg RawHandlers) {
1614 unsigned NumHandlers = RawHandlers.size();
1615 assert(NumHandlers > 0 &&
1616 "The parser shouldn't call this if there are no handlers.");
1617 Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get());
1618
Sebastian Redl63c4da02009-07-29 17:15:45 +00001619 llvm::SmallVector<TypeWithHandler, 8> TypesWithHandlers;
Mike Stump11289f42009-09-09 15:08:12 +00001620
1621 for (unsigned i = 0; i < NumHandlers; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +00001622 CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
Sebastian Redl63c4da02009-07-29 17:15:45 +00001623 if (!Handler->getExceptionDecl()) {
1624 if (i < NumHandlers - 1)
1625 return StmtError(Diag(Handler->getLocStart(),
1626 diag::err_early_catch_all));
Mike Stump11289f42009-09-09 15:08:12 +00001627
Sebastian Redl63c4da02009-07-29 17:15:45 +00001628 continue;
1629 }
Mike Stump11289f42009-09-09 15:08:12 +00001630
Sebastian Redl63c4da02009-07-29 17:15:45 +00001631 const QualType CaughtType = Handler->getCaughtType();
1632 const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType);
1633 TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler));
Sebastian Redl9b244a82008-12-22 21:35:02 +00001634 }
Sebastian Redl63c4da02009-07-29 17:15:45 +00001635
1636 // Detect handlers for the same type as an earlier one.
1637 if (NumHandlers > 1) {
1638 llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end());
Mike Stump11289f42009-09-09 15:08:12 +00001639
Sebastian Redl63c4da02009-07-29 17:15:45 +00001640 TypeWithHandler prev = TypesWithHandlers[0];
1641 for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) {
1642 TypeWithHandler curr = TypesWithHandlers[i];
Mike Stump11289f42009-09-09 15:08:12 +00001643
Sebastian Redl63c4da02009-07-29 17:15:45 +00001644 if (curr == prev) {
1645 Diag(curr.getTypeSpecStartLoc(),
1646 diag::warn_exception_caught_by_earlier_handler)
1647 << curr.getCatchStmt()->getCaughtType().getAsString();
1648 Diag(prev.getTypeSpecStartLoc(),
1649 diag::note_previous_exception_handler)
1650 << prev.getCatchStmt()->getCaughtType().getAsString();
1651 }
Mike Stump11289f42009-09-09 15:08:12 +00001652
Sebastian Redl63c4da02009-07-29 17:15:45 +00001653 prev = curr;
1654 }
1655 }
Mike Stump11289f42009-09-09 15:08:12 +00001656
Sebastian Redl9b244a82008-12-22 21:35:02 +00001657 // FIXME: We should detect handlers that cannot catch anything because an
1658 // earlier handler catches a superclass. Need to find a method that is not
1659 // quadratic for this.
1660 // Neither of these are explicitly forbidden, but every compiler detects them
1661 // and warns.
1662
Douglas Gregor9a28e842010-03-01 23:15:13 +00001663 FunctionNeedsScopeChecking() = true;
Sebastian Redl9b244a82008-12-22 21:35:02 +00001664 RawHandlers.release();
Sam Weiniga16b0dd2010-02-03 03:56:39 +00001665 return Owned(CXXTryStmt::Create(Context, TryLoc,
1666 static_cast<Stmt*>(TryBlock.release()),
1667 Handlers, NumHandlers));
Sebastian Redl9b244a82008-12-22 21:35:02 +00001668}