SemaStmt.cpp revision ad762fcdc16b9e4705b12b09d92b8c026212b906
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements semantic analysis for statements. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h" 155f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/Scope.h" 16781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "clang/Sema/ScopeInfo.h" 17e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Initialization.h" 18ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith#include "clang/Sema/Lookup.h" 1951fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson#include "clang/AST/APValue.h" 20f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner#include "clang/AST/ASTContext.h" 21c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/AST/ExprCXX.h" 23419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner#include "clang/AST/ExprObjC.h" 2416f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h" 2516f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtCXX.h" 26209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall#include "clang/AST/TypeLoc.h" 2784fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/Lex/Preprocessor.h" 286fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson#include "clang/Basic/TargetInfo.h" 29ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner#include "llvm/ADT/ArrayRef.h" 30c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/STLExtras.h" 31c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/SmallVector.h" 325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 33781472fe99a120098c631b0cbe33c89f8cef5e70John McCallusing namespace sema; 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnExprStmt(FullExprArg expr) { 369ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *E = expr.get(); 37bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor if (!E) // FIXME: FullExprArg has no error state? 38bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor return StmtError(); 39bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor 40834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // C99 6.8.3p2: The expression in an expression statement is evaluated as a 41834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // void expression for its side effects. Conversion to void allows any 42834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // operand, even incomplete types. 43a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl 44834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // Same thing in for stmt first clause (when expr) and third clause. 45a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl return Owned(static_cast<Stmt*>(E)); 465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4944aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios KyrtzidisStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc, bool LeadingEmptyMacro) { 5044aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis return Owned(new (Context) NullStmt(SemiLoc, LeadingEmptyMacro)); 515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 53337e550218128e7d922c09bb354fbc71de90c568Chris LattnerStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg, SourceLocation StartLoc, 54337e550218128e7d922c09bb354fbc71de90c568Chris Lattner SourceLocation EndLoc) { 5520401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); 561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 57682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner // If we have an invalid decl, just return an error. 5820401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner if (DG.isNull()) return StmtError(); 591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6024e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc)); 615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 63a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanianvoid Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) { 64a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); 65dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 66a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian // If we have an invalid decl, just return. 67a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian if (DG.isNull() || !DG.isSingleDecl()) return; 68a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian // suppress any potential 'unused variable' warning. 69a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian DG.getSingleDecl()->setUsed(); 70a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian} 71a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian 72636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlssonvoid Sema::DiagnoseUnusedExprResult(const Stmt *S) { 73d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) 74d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis return DiagnoseUnusedExprResult(Label->getSubStmt()); 75d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis 76754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson const Expr *E = dyn_cast_or_null<Expr>(S); 77636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson if (!E) 78636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson return; 79636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson 8011ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis if (E->isBoundMemberFunction(Context)) { 8111ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) 8211ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis << E->getSourceRange(); 8311ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis return; 8411ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis } 8511ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis 86636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson SourceLocation Loc; 87636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson SourceRange R1, R2; 88df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump if (!E->isUnusedResultAWarning(Loc, R1, R2, Context)) 89636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson return; 901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 91419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // Okay, we have an unused result. Depending on what the base expression is, 92419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // we might want to make a more specific diagnostic. Check for one of these 93419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // cases now. 94419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner unsigned DiagID = diag::warn_unused_expr; 954765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall if (const ExprWithCleanups *Temps = dyn_cast<ExprWithCleanups>(E)) 964dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor E = Temps->getSubExpr(); 9734d49471e0b6386aefdc0f6bd15e4a4876ce5db1Chandler Carruth if (const CXXBindTemporaryExpr *TempExpr = dyn_cast<CXXBindTemporaryExpr>(E)) 9834d49471e0b6386aefdc0f6bd15e4a4876ce5db1Chandler Carruth E = TempExpr->getSubExpr(); 9912f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall 100f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall E = E->IgnoreParenImpCasts(); 101bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 1020faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall if (E->getType()->isVoidType()) 1030faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall return; 1040faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall 105bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner // If the callee has attribute pure, const, or warn_unused_result, warn with 106bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner // a more specific message to make it clear what is happening. 107d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes if (const Decl *FD = CE->getCalleeDecl()) { 108bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<WarnUnusedResultAttr>()) { 109bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result"; 110bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 111bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 112bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<PureAttr>()) { 113bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure"; 114bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 115bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 116bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<ConstAttr>()) { 117bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const"; 118bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 119bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 120dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 12112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall } else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) { 122f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian const ObjCMethodDecl *MD = ME->getMethodDecl(); 123f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian if (MD && MD->getAttr<WarnUnusedResultAttr>()) { 124f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result"; 125f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian return; 126f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian } 12712f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall } else if (isa<ObjCPropertyRefExpr>(E)) { 12812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall DiagID = diag::warn_unused_property_expr; 129d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor } else if (const CXXFunctionalCastExpr *FC 130d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor = dyn_cast<CXXFunctionalCastExpr>(E)) { 131d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor if (isa<CXXConstructExpr>(FC->getSubExpr()) || 132d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor isa<CXXTemporaryObjectExpr>(FC->getSubExpr())) 133d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor return; 134f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian } 135209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall // Diagnose "(void*) blah" as a typo for "(void) blah". 136209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) { 137209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); 138209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall QualType T = TI->getType(); 139209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 140209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall // We really do want to use the non-canonical type here. 141209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall if (T == Context.VoidPtrTy) { 142209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc()); 143209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 144209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall Diag(Loc, diag::warn_unused_voidptr) 145209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall << FixItHint::CreateRemoval(TL.getStarLoc()); 146209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall return; 147209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall } 148209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall } 149209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 150351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek DiagRuntimeBehavior(Loc, 0, PDiag(DiagID) << R1 << R2); 151636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson} 152636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson 15360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1541b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, 155a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl MultiStmtArg elts, bool isStmtExpr) { 156a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl unsigned NumElts = elts.size(); 157a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl Stmt **Elts = reinterpret_cast<Stmt**>(elts.release()); 158c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // If we're in C89 mode, check that we don't have any decls after stmts. If 159c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // so, emit an extension diagnostic. 160c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { 161c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // Note that __extension__ can be around a decl. 162c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner unsigned i = 0; 163c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // Skip over all declarations. 164c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) 165c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner /*empty*/; 166c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner 167c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // We found the end of the list or a statement. Scan for another declstmt. 168c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) 169c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner /*empty*/; 1701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 171c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner if (i != NumElts) { 1724afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin(); 173c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner Diag(D->getLocation(), diag::ext_mixed_decls_code); 174c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner } 175c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner } 17698414c1b7d1944a57156d52e29bd41c005de09acChris Lattner // Warn about unused expressions in statements. 17798414c1b7d1944a57156d52e29bd41c005de09acChris Lattner for (unsigned i = 0; i != NumElts; ++i) { 178636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson // Ignore statements that are last in a statement expression. 179636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson if (isStmtExpr && i == NumElts - 1) 18098414c1b7d1944a57156d52e29bd41c005de09acChris Lattner continue; 1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 182636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson DiagnoseUnusedExprResult(Elts[i]); 18398414c1b7d1944a57156d52e29bd41c005de09acChris Lattner } 184a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl 1858189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R)); 1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1899ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCaseStmt(SourceLocation CaseLoc, Expr *LHSVal, 1909ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SourceLocation DotDotDotLoc, Expr *RHSVal, 19124e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner SourceLocation ColonLoc) { 1929ae2f076ca5ab1feb3ba95629099ec2319833701John McCall assert((LHSVal != 0) && "missing expression in case statement"); 193117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.4.2p3: The expression shall be an integer constant. 1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // However, GCC allows any evaluatable integer expression. 1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent() && 197dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor VerifyIntegerConstantExpression(LHSVal)) 19824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return StmtError(); 1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2006c36be5b383875b490684bcf439d6d427298c1afChris Lattner // GCC extension: The expression shall be an integer constant. 201117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 202dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent() && 203dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor VerifyIntegerConstantExpression(RHSVal)) { 204f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner RHSVal = 0; // Recover by just forgetting about it. 205117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl } 206117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 207781472fe99a120098c631b0cbe33c89f8cef5e70John McCall if (getCurFunction()->SwitchStack.empty()) { 2088a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner Diag(CaseLoc, diag::err_case_not_in_switch); 20924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return StmtError(); 2108a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner } 2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 212dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc, 213dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor ColonLoc); 214781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.back()->addSwitchCase(CS); 215117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(CS); 2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 21824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner/// ActOnCaseStmtBody - This installs a statement as the body of a case. 2199ae2f076ca5ab1feb3ba95629099ec2319833701John McCallvoid Sema::ActOnCaseStmtBody(Stmt *caseStmt, Stmt *SubStmt) { 22024e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner CaseStmt *CS = static_cast<CaseStmt*>(caseStmt); 22124e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner CS->setSubStmt(SubStmt); 22224e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner} 22324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner 22460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2251eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, 2269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *SubStmt, Scope *CurScope) { 227781472fe99a120098c631b0cbe33c89f8cef5e70John McCall if (getCurFunction()->SwitchStack.empty()) { 2280fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner Diag(DefaultLoc, diag::err_default_not_in_switch); 229117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(SubStmt); 2300fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner } 231117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 232dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt); 233781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.back()->addSwitchCase(DS); 234117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(DS); 2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 23857ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerSema::ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl, 23957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner SourceLocation ColonLoc, Stmt *SubStmt) { 24057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner 241ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner // If the label was multiply defined, reject it now. 242ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner if (TheDecl->getStmt()) { 243ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Diag(IdentLoc, diag::err_redefinition_of_label) << TheDecl->getDeclName(); 244ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Diag(TheDecl->getLocation(), diag::note_previous_definition); 245de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(SubStmt); 2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 247de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 248ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner // Otherwise, things are good. Fill in the declaration and return it. 249ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner LabelStmt *LS = new (Context) LabelStmt(IdentLoc, TheDecl, SubStmt); 250ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner TheDecl->setStmt(LS); 251203548ba4b72e7e59320d352afc1eb0b5ab131deAbramo Bagnara if (!TheDecl->isGnuLocal()) 252203548ba4b72e7e59320d352afc1eb0b5ab131deAbramo Bagnara TheDecl->setLocation(IdentLoc); 253ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner return Owned(LS); 2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 25660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 257d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, Decl *CondVar, 25844aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis Stmt *thenStmt, SourceLocation ElseLoc, 25944aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis Stmt *elseStmt) { 26060d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult(CondVal.release()); 2611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2628cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor VarDecl *ConditionVar = 0; 263d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 264d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 265586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor CondResult = CheckConditionVariable(ConditionVar, IfLoc, true); 26699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (CondResult.isInvalid()) 26799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 2688cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor } 26999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Expr *ConditionExpr = CondResult.takeAs<Expr>(); 27099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!ConditionExpr) 27199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 272dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 273754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(thenStmt); 2745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2752d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // Warn if the if block has a null body without an else value. 2762d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // this helps prevent bugs due to typos, such as 2772d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // if (condition); 2782d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // do_stuff(); 279b3198178daf9d33466fc18e009e2c6692fdf29c0Ted Kremenek // 2809ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!elseStmt) { 2812d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt)) 282a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // But do not warn if the body is a macro that expands to nothing, e.g: 283a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // 284a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // #define CALL(x) 285a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // if (condition) 286a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // CALL(0); 287a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // 28844aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis if (!stmt->hasLeadingEmptyMacro()) 289b3198178daf9d33466fc18e009e2c6692fdf29c0Ted Kremenek Diag(stmt->getSemiLoc(), diag::warn_empty_if_body); 2902d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson } 2912d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson 292754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(elseStmt); 2931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 294dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi return Owned(new (Context) IfStmt(Context, IfLoc, ConditionVar, ConditionExpr, 29544aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis thenStmt, ElseLoc, elseStmt)); 2965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 298f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have 299f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified width and sign. If an overflow occurs, detect it and emit 300f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified diagnostic. 301f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattnervoid Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, 302f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner unsigned NewWidth, bool NewSign, 3031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SourceLocation Loc, 304f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner unsigned DiagID) { 305f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Perform a conversion to the promoted condition type if needed. 306f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (NewWidth > Val.getBitWidth()) { 307f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // If this is an extension, just do it. 3089f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.extend(NewWidth); 309f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.setIsSigned(NewSign); 310f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor 311f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // If the input was signed and negative and the output is 312f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // unsigned, don't bother to warn: this is implementation-defined 313f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // behavior. 314f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // FIXME: Introduce a second, default-ignored warning for this case? 315f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else if (NewWidth < Val.getBitWidth()) { 316f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // If this is a truncation, check for overflow. 317f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner llvm::APSInt ConvVal(Val); 3189f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad ConvVal = ConvVal.trunc(NewWidth); 319b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner ConvVal.setIsSigned(NewSign); 3209f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad ConvVal = ConvVal.extend(Val.getBitWidth()); 321b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner ConvVal.setIsSigned(Val.isSigned()); 322f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (ConvVal != Val) 323d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10); 3241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 325f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Regardless of whether a diagnostic was emitted, really do the 326f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // truncation. 3279f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.trunc(NewWidth); 328b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner Val.setIsSigned(NewSign); 329f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else if (NewSign != Val.isSigned()) { 330f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Convert the sign to match the sign of the condition. This can cause 331f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // overflow as well: unsigned(INTMIN) 332dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // We don't diagnose this overflow, because it is implementation-defined 3332853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor // behavior. 3342853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor // FIXME: Introduce a second, default-ignored warning for this case? 335f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner llvm::APSInt OldVal(Val); 336f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.setIsSigned(NewSign); 337f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 338f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner} 339f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 3400471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattnernamespace { 3410471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner struct CaseCompareFunctor { 3420471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 3430471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner const llvm::APSInt &RHS) { 3440471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner return LHS.first < RHS; 3450471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 3460e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 3470e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 3480e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner return LHS.first < RHS.first; 3490e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner } 3500471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner bool operator()(const llvm::APSInt &LHS, 3510471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 3520471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner return LHS < RHS.first; 3530471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 3540471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner }; 3550471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner} 3560471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner 357764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// CmpCaseVals - Comparison predicate for sorting case values. 358764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// 359764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattnerstatic bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, 360764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner const std::pair<llvm::APSInt, CaseStmt*>& rhs) { 361764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner if (lhs.first < rhs.first) 362764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return true; 363764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner 364764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner if (lhs.first == rhs.first && 365764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner lhs.second->getCaseLoc().getRawEncoding() 366764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner < rhs.second->getCaseLoc().getRawEncoding()) 367764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return true; 368764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return false; 369764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner} 370764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner 371ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// CmpEnumVals - Comparison predicate for sorting enumeration values. 372ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// 373ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, 374ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) 375ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{ 376ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor return lhs.first < rhs.first; 377ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor} 378ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 379ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// EqEnumVals - Comparison preficate for uniqing enumeration values. 380ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// 381ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, 382ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) 383ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{ 384ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor return lhs.first == rhs.first; 385ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor} 386ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 3875f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of 3885f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// potentially integral-promoted expression @p expr. 3895f04881eb025f61396d0555d8173730fe2759e0aChris Lattnerstatic QualType GetTypeBeforeIntegralPromotion(const Expr* expr) { 3906907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall if (const CastExpr *ImplicitCast = dyn_cast<ImplicitCastExpr>(expr)) { 3915f04881eb025f61396d0555d8173730fe2759e0aChris Lattner const Expr *ExprBeforePromotion = ImplicitCast->getSubExpr(); 3925f04881eb025f61396d0555d8173730fe2759e0aChris Lattner QualType TypeBeforePromotion = ExprBeforePromotion->getType(); 3932ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor if (TypeBeforePromotion->isIntegralOrEnumerationType()) { 3945f04881eb025f61396d0555d8173730fe2759e0aChris Lattner return TypeBeforePromotion; 3955f04881eb025f61396d0555d8173730fe2759e0aChris Lattner } 3965f04881eb025f61396d0555d8173730fe2759e0aChris Lattner } 3975f04881eb025f61396d0555d8173730fe2759e0aChris Lattner return expr->getType(); 3985f04881eb025f61396d0555d8173730fe2759e0aChris Lattner} 3995f04881eb025f61396d0555d8173730fe2759e0aChris Lattner 40060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 401dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, Expr *Cond, 402d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *CondVar) { 40360d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult; 4049ae2f076ca5ab1feb3ba95629099ec2319833701John McCall 405586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor VarDecl *ConditionVar = 0; 406d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 407d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 4089ae2f076ca5ab1feb3ba95629099ec2319833701John McCall CondResult = CheckConditionVariable(ConditionVar, SourceLocation(), false); 4099ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 410586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 411dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4129ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.release(); 413586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor } 414dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4159ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!Cond) 416586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 417dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4189ae2f076ca5ab1feb3ba95629099ec2319833701John McCall CondResult 419dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi = ConvertToIntegralOrEnumerationType(SwitchLoc, Cond, 420c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor PDiag(diag::err_typecheck_statement_requires_integer), 421c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor PDiag(diag::err_switch_incomplete_class_type) 4229ae2f076ca5ab1feb3ba95629099ec2319833701John McCall << Cond->getSourceRange(), 423c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor PDiag(diag::err_switch_explicit_conversion), 424c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor PDiag(diag::note_switch_conversion), 425c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor PDiag(diag::err_switch_multiple_conversions), 4266bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor PDiag(diag::note_switch_conversion), 4276bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor PDiag(0)); 4289ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) return StmtError(); 4299ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 430dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 431d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (!CondVar) { 432b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(Cond, SwitchLoc); 4334765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall CondResult = MaybeCreateExprWithCleanups(Cond); 4349ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 435586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 4369ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 437586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor } 438b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 439781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchIntoScope(); 440dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4419ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SwitchStmt *SS = new (Context) SwitchStmt(Context, ConditionVar, Cond); 442781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.push_back(SS); 443586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return Owned(SS); 4447e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner} 4457e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner 44628164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greifstatic void AdjustAPSInt(llvm::APSInt &Val, unsigned BitWidth, bool IsSigned) { 44728164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif if (Val.getBitWidth() < BitWidth) 4489f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.extend(BitWidth); 44928164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif else if (Val.getBitWidth() > BitWidth) 4509f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.trunc(BitWidth); 45128164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Val.setIsSigned(IsSigned); 45228164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif} 45328164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif 45460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 4559ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, 4569ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *BodyStmt) { 4579ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SwitchStmt *SS = cast<SwitchStmt>(Switch); 458781472fe99a120098c631b0cbe33c89f8cef5e70John McCall assert(SS == getCurFunction()->SwitchStack.back() && 459781472fe99a120098c631b0cbe33c89f8cef5e70John McCall "switch stack missing push/pop!"); 460de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 4619dcbfa450d751bd68fc4af8b75da381d4f6984b9Steve Naroff SS->setBody(BodyStmt, SwitchLoc); 462781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.pop_back(); 463c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson 464ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor if (SS->getCond() == 0) 465be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor return StmtError(); 466dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 467f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Expr *CondExpr = SS->getCond(); 4680fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Expr *CondExprBeforePromotion = CondExpr; 46984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor QualType CondTypeBeforePromotion = 47084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor GetTypeBeforeIntegralPromotion(CondExpr); 471de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 4720de55e7e6b8a53c5d1f2e9a811fd0a4ea13ed5b0Douglas Gregor // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. 473429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult CondResult = UsualUnaryConversions(CondExpr); 474429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (CondResult.isInvalid()) 475429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 476429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley CondExpr = CondResult.take(); 477a0d3ca1ea5578bc736bb71bcec50ab41fefc87b9Douglas Gregor QualType CondType = CondExpr->getType(); 47884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor SS->setCond(CondExpr); 47984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor 4805f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // C++ 6.4.2.p2: 4815f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // Integral promotions are performed (on the switch condition). 4825f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // 4835f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // A case value unrepresentable by the original switch condition 4845f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // type (before the promotion) doesn't make sense, even when it can 4855f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // be represented by the promoted type. Therefore we need to find 4865f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // the pre-promotion type of the switch condition. 48712356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan if (!CondExpr->isTypeDependent()) { 488acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor // We have already converted the expression to an integral or enumeration 489dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // type, when we started the switch statement. If we don't have an 490acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor // appropriate type now, just return an error. 491acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor if (!CondType->isIntegralOrEnumerationType()) 49212356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan return StmtError(); 49312356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan 4942b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner if (CondExpr->isKnownToHaveBooleanValue()) { 49512356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // switch(bool_expr) {...} is often a programmer error, e.g. 49612356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // switch(n && mask) { ... } // Doh - should be "n & mask". 49712356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // One can always use an if statement instead of switch(bool_expr). 49812356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan Diag(SwitchLoc, diag::warn_bool_switch_condition) 49912356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan << CondExpr->getSourceRange(); 50012356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan } 501c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson } 502de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 503f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Get the bitwidth of the switched-on value before promotions. We must 504f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // convert the integer case values to this width before comparison. 5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump bool HasDependentValue 506dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor = CondExpr->isTypeDependent() || CondExpr->isValueDependent(); 5071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump unsigned CondWidth 5081d6ab7af99a1fc059a6aa5da083640c1d94b07f7Chris Lattner = HasDependentValue ? 0 : Context.getIntWidth(CondTypeBeforePromotion); 5095f04881eb025f61396d0555d8173730fe2759e0aChris Lattner bool CondIsSigned = CondTypeBeforePromotion->isSignedIntegerType(); 5101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 511f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Accumulate all of the case values in a vector so that we can sort them 512f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // and detect duplicates. This vector contains the APInt for the case after 513f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // it has been converted to the condition type. 5140471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; 5150471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner CaseValsTy CaseVals; 5161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 517f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Keep track of any GNU case ranges we see. The APSInt is the low value. 518ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy; 519ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseRangesTy CaseRanges; 5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 521f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner DefaultStmt *TheDefaultStmt = 0; 5221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 523b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner bool CaseListIsErroneous = false; 5241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 525dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue; 526c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson SC = SC->getNextSwitchCase()) { 5271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 528c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { 529f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (TheDefaultStmt) { 530f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); 5315f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev); 532de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 533f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // FIXME: Remove the default statement from the switch block so that 534390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // we'll return a valid AST. This requires recursing down the AST and 535390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // finding it, not something we are set up to do right now. For now, 536390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // just lop the entire switch stmt out of the AST. 537b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CaseListIsErroneous = true; 538c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson } 539f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner TheDefaultStmt = DS; 5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 541f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else { 542f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner CaseStmt *CS = cast<CaseStmt>(SC); 5431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 544f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // We already verified that the expression has a i-c-e value (C99 545f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // 6.8.4.2p3) - get that value now. 5461e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner Expr *Lo = CS->getLHS(); 547dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 548dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (Lo->isTypeDependent() || Lo->isValueDependent()) { 549dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HasDependentValue = true; 550dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor break; 551dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 5521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 55351fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson llvm::APSInt LoVal = Lo->EvaluateAsInt(Context); 5541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 555f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Convert the value to the same width/sign as the condition. 556f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, 55728164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Lo->getLocStart(), 558f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner diag::warn_case_value_overflow); 5596c36be5b383875b490684bcf439d6d427298c1afChris Lattner 5601e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner // If the LHS is not the same type as the condition, insert an implicit 5611e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner // cast. 562429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Lo = ImpCastExprToType(Lo, CondType, CK_IntegralCast).take(); 5631e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner CS->setLHS(Lo); 5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 565b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // If this is a case range, remember it in CaseRanges, otherwise CaseVals. 566dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (CS->getRHS()) { 5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (CS->getRHS()->isTypeDependent() || 568dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CS->getRHS()->isValueDependent()) { 569dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HasDependentValue = true; 570dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor break; 571dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 572f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner CaseRanges.push_back(std::make_pair(LoVal, CS)); 5731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else 574b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CaseVals.push_back(std::make_pair(LoVal, CS)); 575f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 576f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 577b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 578dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!HasDependentValue) { 5790fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // If we don't have a default statement, check whether the 5800fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // condition is constant. 5810fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall llvm::APSInt ConstantCondValue; 5820fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall bool HasConstantCond = false; 5830fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall bool ShouldCheckConstantCond = false; 5840fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (!HasDependentValue && !TheDefaultStmt) { 5850fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Expr::EvalResult Result; 5860fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall HasConstantCond = CondExprBeforePromotion->Evaluate(Result, Context); 5870fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (HasConstantCond) { 5880fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall assert(Result.Val.isInt() && "switch condition evaluated to non-int"); 5890fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ConstantCondValue = Result.Val.getInt(); 5900fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ShouldCheckConstantCond = true; 5910fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 5920fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall assert(ConstantCondValue.getBitWidth() == CondWidth && 5930fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ConstantCondValue.isSigned() == CondIsSigned); 5940fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall } 5950fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall } 5960fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 597dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Sort all the scalar case values so we can easily detect duplicates. 598dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); 599dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 600dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!CaseVals.empty()) { 6010fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) { 6020fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (ShouldCheckConstantCond && 6030fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall CaseVals[i].first == ConstantCondValue) 6040fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ShouldCheckConstantCond = false; 6050fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 6060fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) { 607dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If we have a duplicate, report it. 6081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(CaseVals[i].second->getLHS()->getLocStart(), 6090fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall diag::err_duplicate_case) << CaseVals[i].first.toString(10); 6100fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Diag(CaseVals[i-1].second->getLHS()->getLocStart(), 611dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::note_duplicate_case_prev); 612390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: We really want to remove the bogus case stmt from the 613390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // substmt, but we have no way to do this right now. 614dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseListIsErroneous = true; 615dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 6166efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner } 617b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 6181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 619dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Detect duplicate case ranges, which usually don't exist at all in 620dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // the first place. 621dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!CaseRanges.empty()) { 622dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Sort all the case ranges by their low value so we can easily detect 623dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // overlaps between ranges. 624dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::stable_sort(CaseRanges.begin(), CaseRanges.end()); 6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 626dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Scan the ranges, computing the high values and removing empty ranges. 627dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::vector<llvm::APSInt> HiVals; 628dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 6290fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall llvm::APSInt &LoVal = CaseRanges[i].first; 630dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CR = CaseRanges[i].second; 631dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Expr *Hi = CR->getRHS(); 632dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt HiVal = Hi->EvaluateAsInt(Context); 6331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 634dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Convert the value to the same width/sign as the condition. 635dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, 63628164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Hi->getLocStart(), 637dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::warn_case_value_overflow); 6381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 639dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If the LHS is not the same type as the condition, insert an implicit 640dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // cast. 641429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Hi = ImpCastExprToType(Hi, CondType, CK_IntegralCast).take(); 642dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CR->setRHS(Hi); 6431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 644dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If the low value is bigger than the high value, the case is empty. 6450fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (LoVal > HiVal) { 646dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range) 647dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor << SourceRange(CR->getLHS()->getLocStart(), 64828164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Hi->getLocEnd()); 649dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseRanges.erase(CaseRanges.begin()+i); 650dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor --i, --e; 651dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor continue; 652dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 6530fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 6540fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (ShouldCheckConstantCond && 6550fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall LoVal <= ConstantCondValue && 6560fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ConstantCondValue <= HiVal) 6570fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ShouldCheckConstantCond = false; 6580fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 659dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HiVals.push_back(HiVal); 6600471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 6611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 662dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Rescan the ranges, looking for overlap with singleton values and other 663dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // ranges. Since the range list is sorted, we only need to compare case 664dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // ranges with their neighbors. 665dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 666dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt &CRLo = CaseRanges[i].first; 667dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt &CRHi = HiVals[i]; 668dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CR = CaseRanges[i].second; 6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 670dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Check to see whether the case range overlaps with any 671dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // singleton cases. 672dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *OverlapStmt = 0; 673dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt OverlapVal(32); 6741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 675dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Find the smallest value >= the lower bound. If I is in the 676dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // case range, then we have overlap. 677dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), 678dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseVals.end(), CRLo, 679dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseCompareFunctor()); 680dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (I != CaseVals.end() && I->first < CRHi) { 681dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = I->first; // Found overlap with scalar. 682dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = I->second; 683dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 6841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 685dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Find the smallest value bigger than the upper bound. 686dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); 687dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (I != CaseVals.begin() && (I-1)->first >= CRLo) { 688dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = (I-1)->first; // Found overlap with scalar. 689dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = (I-1)->second; 690dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 6911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 692dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Check to see if this case stmt overlaps with the subsequent 693dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // case range. 694dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (i && CRLo <= HiVals[i-1]) { 695dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = HiVals[i-1]; // Found overlap with range. 696dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = CaseRanges[i-1].second; 697dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 699dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (OverlapStmt) { 700dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If we have a duplicate, report it. 701dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case) 702dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor << OverlapVal.toString(10); 7031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(OverlapStmt->getLHS()->getLocStart(), 704dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::note_duplicate_case_prev); 705390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: We really want to remove the bogus case stmt from the 706390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // substmt, but we have no way to do this right now. 707dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseListIsErroneous = true; 708dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 7090471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 710b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 711ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 7120fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Complain if we have a constant condition and we didn't find a match. 7130fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (!CaseListIsErroneous && ShouldCheckConstantCond) { 7140fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // TODO: it would be nice if we printed enums as enums, chars as 7150fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // chars, etc. 7160fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition) 7170fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall << ConstantCondValue.toString(10) 7180fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall << CondExpr->getSourceRange(); 7190fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall } 7200fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 7210fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Check to see if switch is over an Enum and handles all of its 722559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // values. We only issue a warning if there is not 'default:', but 723559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // we still do the analysis to preserve this information in the AST 724559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // (which can be used by flow-based analyes). 7250fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // 726ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner const EnumType *ET = CondTypeBeforePromotion->getAs<EnumType>(); 727559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 728ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor // If switch has default case, then ignore it. 729559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (!CaseListIsErroneous && !HasConstantCond && ET) { 730ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const EnumDecl *ED = ET->getDecl(); 731ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor typedef llvm::SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> EnumValsTy; 732ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor EnumValsTy EnumVals; 733ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 7340fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Gather all enum values, set their type and sort them, 7350fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // allowing easier comparison with CaseVals. 7360fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin(); 73728164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif EDI != ED->enumerator_end(); ++EDI) { 73828164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif llvm::APSInt Val = EDI->getInitVal(); 73928164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif AdjustAPSInt(Val, CondWidth, CondIsSigned); 74028164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif EnumVals.push_back(std::make_pair(Val, *EDI)); 741ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 742ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); 7430fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall EnumValsTy::iterator EIend = 7440fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); 745559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 746559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // See which case values aren't in enum. 747559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // TODO: we might want to check whether case values are out of the 748559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // enum even if we don't want to check whether all cases are handled. 749559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (!TheDefaultStmt) { 75047bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek EnumValsTy::const_iterator EI = EnumVals.begin(); 75147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek for (CaseValsTy::const_iterator CI = CaseVals.begin(); 7520fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall CI != CaseVals.end(); CI++) { 75347bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek while (EI != EIend && EI->first < CI->first) 75447bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek EI++; 75547bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek if (EI == EIend || EI->first > CI->first) 7560fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Diag(CI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum) 7570fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall << ED->getDeclName(); 75847bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 75947bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek // See which of case ranges aren't in enum 76047bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek EI = EnumVals.begin(); 76147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek for (CaseRangesTy::const_iterator RI = CaseRanges.begin(); 7620fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall RI != CaseRanges.end() && EI != EIend; RI++) { 76347bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek while (EI != EIend && EI->first < RI->first) 76447bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek EI++; 765dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 76647bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek if (EI == EIend || EI->first != RI->first) { 76747bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek Diag(RI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum) 76847bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek << ED->getDeclName(); 76947bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 770e0ba9d1beeba01a96808c2fc61f9ca89acec313bTed Kremenek 77147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context); 77228164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif AdjustAPSInt(Hi, CondWidth, CondIsSigned); 77347bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek while (EI != EIend && EI->first < Hi) 77447bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek EI++; 77547bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek if (EI == EIend || EI->first != Hi) 77647bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek Diag(RI->second->getRHS()->getExprLoc(), diag::warn_not_in_enum) 77747bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek << ED->getDeclName(); 77847bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 779ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 780dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 781559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // Check which enum vals aren't in switch 782ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseValsTy::const_iterator CI = CaseVals.begin(); 783ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseRangesTy::const_iterator RI = CaseRanges.begin(); 784559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek bool hasCasesNotInSwitch = false; 785559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 786ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner llvm::SmallVector<DeclarationName,8> UnhandledNames; 787dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 788559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek for (EnumValsTy::const_iterator EI = EnumVals.begin(); EI != EIend; EI++){ 789ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner // Drop unneeded case values 790ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor llvm::APSInt CIVal; 791ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor while (CI != CaseVals.end() && CI->first < EI->first) 792ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CI++; 793dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 794ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor if (CI != CaseVals.end() && CI->first == EI->first) 795ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor continue; 796ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 797559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // Drop unneeded case ranges 798ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor for (; RI != CaseRanges.end(); RI++) { 799ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context); 80028164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif AdjustAPSInt(Hi, CondWidth, CondIsSigned); 801ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor if (EI->first <= Hi) 802ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor break; 803ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 804ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 805559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (RI == CaseRanges.end() || EI->first < RI->first) { 80647bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek hasCasesNotInSwitch = true; 80747bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek if (!TheDefaultStmt) 808ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner UnhandledNames.push_back(EI->second->getDeclName()); 80947bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 810ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 811dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 812ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner // Produce a nice diagnostic if multiple values aren't handled. 813ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner switch (UnhandledNames.size()) { 814ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 0: break; 815ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 1: 816ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner Diag(CondExpr->getExprLoc(), diag::warn_missing_case1) 817ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0]; 818ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 819ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 2: 820ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner Diag(CondExpr->getExprLoc(), diag::warn_missing_case2) 821ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1]; 822ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 823ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 3: 824ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner Diag(CondExpr->getExprLoc(), diag::warn_missing_case3) 825ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2]; 826ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 827ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner default: 828ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner Diag(CondExpr->getExprLoc(), diag::warn_missing_cases) 829ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << (unsigned)UnhandledNames.size() 830ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2]; 831ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 832ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner } 833559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 834559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (!hasCasesNotInSwitch) 83547bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek SS->setAllEnumCasesCovered(); 836ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 837b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 838dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 839390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: If the case list was broken is some way, we don't have a good system 840390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // to patch it up. Instead, just return the whole substmt as broken. 841b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner if (CaseListIsErroneous) 842de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return StmtError(); 843de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 844de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(SS); 8455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 84760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 848dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond, 8499ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Decl *CondVar, Stmt *Body) { 85060d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult(Cond.release()); 851dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 8525656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor VarDecl *ConditionVar = 0; 853d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 854d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 855586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor CondResult = CheckConditionVariable(ConditionVar, WhileLoc, true); 85699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (CondResult.isInvalid()) 85799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 8585656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor } 8599ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *ConditionExpr = CondResult.take(); 86099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!ConditionExpr) 86199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 862dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 8639ae2f076ca5ab1feb3ba95629099ec2319833701John McCall DiagnoseUnusedExprResult(Body); 8641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 86543dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor return Owned(new (Context) WhileStmt(Context, ConditionVar, ConditionExpr, 8669ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Body, WhileLoc)); 8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 86960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 8709ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body, 871989135901c750af61ef012b6b0a0368be415bc46Chris Lattner SourceLocation WhileLoc, SourceLocation CondLParen, 8729ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *Cond, SourceLocation CondRParen) { 8739ae2f076ca5ab1feb3ba95629099ec2319833701John McCall assert(Cond && "ActOnDoStmt(): missing expression"); 874f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 875429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult CondResult = CheckBooleanCondition(Cond, DoLoc); 876429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (CondResult.isInvalid() || CondResult.isInvalid()) 8775a881bb09928b7ade891efc680088aaad276f8d6John McCall return StmtError(); 878429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Cond = CondResult.take(); 8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 880b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(Cond, DoLoc); 881429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley CondResult = MaybeCreateExprWithCleanups(Cond); 8829ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 883586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 8849ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 885dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 8869ae2f076ca5ab1feb3ba95629099ec2319833701John McCall DiagnoseUnusedExprResult(Body); 887754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson 8889ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen)); 8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 89160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 892f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, 8939ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *First, FullExprArg second, Decl *secondVar, 89499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor FullExprArg third, 8959ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SourceLocation RParenLoc, Stmt *Body) { 8965921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (!getLangOptions().CPlusPlus) { 8975921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { 898f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // C99 6.8.5p3: The declaration part of a 'for' statement shall only 899f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // declare identifiers for objects having storage class 'auto' or 900f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // 'register'. 9015921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); 9025921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis DI!=DE; ++DI) { 9035921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis VarDecl *VD = dyn_cast<VarDecl>(*DI); 904b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall if (VD && VD->isLocalVarDecl() && !VD->hasLocalStorage()) 9055921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis VD = 0; 9065921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (VD == 0) 9075921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for); 9085921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // FIXME: mark decl erroneous! 9095921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis } 910ae3b701f59e78e058b83344be17206af3bf5d277Chris Lattner } 9115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 91299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor 91360d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult SecondResult(second.release()); 91499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor VarDecl *ConditionVar = 0; 915d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (secondVar) { 916d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(secondVar); 917586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor SecondResult = CheckConditionVariable(ConditionVar, ForLoc, true); 91899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (SecondResult.isInvalid()) 91999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 92099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor } 921dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 92299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Expr *Third = third.release().takeAs<Expr>(); 923dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 9243af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson DiagnoseUnusedExprResult(First); 9253af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson DiagnoseUnusedExprResult(Third); 926754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(Body); 927754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson 928dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi return Owned(new (Context) ForStmt(Context, First, 929dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi SecondResult.take(), ConditionVar, 930dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi Third, Body, ForLoc, LParenLoc, 93143dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor RParenLoc)); 9325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 9335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 934f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// In an Objective C collection iteration statement: 935f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// for (x in y) 936f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// x can be an arbitrary l-value expression. Bind it up as a 937f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// full-expression. 938f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallStmtResult Sema::ActOnForEachLValueExpr(Expr *E) { 939f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall CheckImplicitConversions(E); 9404765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall ExprResult Result = MaybeCreateExprWithCleanups(E); 941f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall if (Result.isInvalid()) return StmtError(); 942f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall return Owned(static_cast<Stmt*>(Result.get())); 943f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall} 944f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall 94560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 946f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, 947f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl SourceLocation LParenLoc, 9489ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *First, Expr *Second, 9499ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SourceLocation RParenLoc, Stmt *Body) { 95020552d2842245692b649e0d25380670922f954a2Fariborz Jahanian if (First) { 95120552d2842245692b649e0d25380670922f954a2Fariborz Jahanian QualType FirstType; 95220552d2842245692b649e0d25380670922f954a2Fariborz Jahanian if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { 9537e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner if (!DS->isSingleDecl()) 954f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag((*DS->decl_begin())->getLocation(), 955f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_toomany_element_decls)); 956f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 9577e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner Decl *D = DS->getSingleDecl(); 958f34afeed9a0112bf31fee185b6c80556111d3834Ted Kremenek FirstType = cast<ValueDecl>(D)->getType(); 959f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // C99 6.8.5p3: The declaration part of a 'for' statement shall only 960f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // declare identifiers for objects having storage class 'auto' or 961f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // 'register'. 962248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff VarDecl *VD = cast<VarDecl>(D); 963b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall if (VD->isLocalVarDecl() && !VD->hasLocalStorage()) 964f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag(VD->getLocation(), 965f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_non_variable_decl_in_for)); 9661fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson } else { 967c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor Expr *FirstE = cast<Expr>(First); 9687eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall if (!FirstE->isTypeDependent() && !FirstE->isLValue()) 969f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag(First->getLocStart(), 970f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_selector_element_not_lvalue) 971f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl << First->getSourceRange()); 9721fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson 9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FirstType = static_cast<Expr*>(First)->getType(); 9741fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson } 975c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor if (!FirstType->isDependentType() && 976c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor !FirstType->isObjCObjectPointerType() && 977a5e42a82ce055f29f3733f3a1f10da6cb9877deeFariborz Jahanian !FirstType->isBlockPointerType()) 978dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner Diag(ForLoc, diag::err_selector_element_type) 979d162584991885ab004a02573a73ce06422b921fcChris Lattner << FirstType << First->getSourceRange(); 9803ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian } 981c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor if (Second && !Second->isTypeDependent()) { 982429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultFunctionArrayLvalueConversion(Second); 983429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 984429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 985429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Second = Result.take(); 9863ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian QualType SecondType = Second->getType(); 987f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff if (!SecondType->isObjCObjectPointerType()) 988dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner Diag(ForLoc, diag::err_collection_expr_type) 989d162584991885ab004a02573a73ce06422b921fcChris Lattner << SecondType << Second->getSourceRange(); 990ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian else if (const ObjCObjectPointerType *OPT = 991ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian SecondType->getAsObjCInterfacePointerType()) { 992ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian llvm::SmallVector<IdentifierInfo *, 4> KeyIdents; 993dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi IdentifierInfo* selIdent = 994ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian &Context.Idents.get("countByEnumeratingWithState"); 995ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian KeyIdents.push_back(selIdent); 996ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian selIdent = &Context.Idents.get("objects"); 997ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian KeyIdents.push_back(selIdent); 998ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian selIdent = &Context.Idents.get("count"); 999ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian KeyIdents.push_back(selIdent); 1000ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian Selector CSelector = Context.Selectors.getSelector(3, &KeyIdents[0]); 1001ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian if (ObjCInterfaceDecl *IDecl = OPT->getInterfaceDecl()) { 1002dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (!IDecl->isForwardDecl() && 100361478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian !IDecl->lookupInstanceMethod(CSelector) && 100461478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian !LookupMethodInQualifiedType(CSelector, OPT, true)) { 100580a785c2452c73b7c41d4a867edcf5a090c78c46Fariborz Jahanian // Must further look into private implementation methods. 1006ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian if (!LookupPrivateInstanceMethod(CSelector, IDecl)) 1007ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian Diag(ForLoc, diag::warn_collection_expr_type) 1008ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian << SecondType << CSelector << Second->getSourceRange(); 1009ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian } 1010ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian } 1011ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian } 10123ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian } 10138189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body, 10148189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek ForLoc, RParenLoc)); 10153ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian} 10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1017ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithnamespace { 1018ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1019ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithenum BeginEndFunction { 1020ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_begin, 1021ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_end 1022ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}; 1023ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1024ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Build a variable declaration for a for-range statement. 1025ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstatic VarDecl *BuildForRangeVarDecl(Sema &SemaRef, SourceLocation Loc, 1026ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType Type, const char *Name) { 1027ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclContext *DC = SemaRef.CurContext; 1028ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1029ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1030ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *Decl = VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, 1031ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith TInfo, SC_Auto, SC_None); 1032ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return Decl; 1033ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1034ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1035ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Finish building a variable declaration for a for-range statement. 1036ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// \return true if an error occurs. 1037ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstatic bool FinishForRangeVarDecl(Sema &SemaRef, VarDecl *Decl, Expr *Init, 1038ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation Loc, int diag) { 1039ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Deduce the type for the iterator variable now rather than leaving it to 1040ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // AddInitializerToDecl, so we can produce a more suitable diagnostic. 1041ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith TypeSourceInfo *InitTSI = 0; 1042ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (Init->getType()->isVoidType() || 1043ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith !SemaRef.DeduceAutoType(Decl->getTypeSourceInfo(), Init, InitTSI)) 1044ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.Diag(Loc, diag) << Init->getType(); 1045ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!InitTSI) { 1046ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setInvalidDecl(); 1047ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return true; 1048ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1049ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setTypeSourceInfo(InitTSI); 1050ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setType(InitTSI->getType()); 1051ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1052ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.AddInitializerToDecl(Decl, Init, /*DirectInit=*/false, 1053ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*TypeMayContainAuto=*/false); 1054ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.FinalizeDeclaration(Decl); 1055ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return false; 1056ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1057ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1058ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Produce a note indicating which begin/end function was implicitly called 1059ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// by a C++0x for-range statement. This is often not obvious from the code, 1060ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// nor from the diagnostics produced when analysing the implicit expressions 1061ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// required in a for-range statement. 1062ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid NoteForRangeBeginEndFunction(Sema &SemaRef, Expr *E, 1063ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginEndFunction BEF) { 1064ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith CallExpr *CE = dyn_cast<CallExpr>(E); 1065ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!CE) 1066ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return; 1067ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith FunctionDecl *D = dyn_cast<FunctionDecl>(CE->getCalleeDecl()); 1068ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!D) 1069ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return; 1070ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation Loc = D->getLocation(); 1071ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1072ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith std::string Description; 1073ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith bool IsTemplate = false; 1074ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FunctionTemplateDecl *FunTmpl = D->getPrimaryTemplate()) { 1075ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Description = SemaRef.getTemplateArgumentBindingsText( 1076ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith FunTmpl->getTemplateParameters(), *D->getTemplateSpecializationArgs()); 1077ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IsTemplate = true; 1078ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1079ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1080ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.Diag(Loc, diag::note_for_range_begin_end) 1081ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << BEF << IsTemplate << Description << E->getType(); 1082ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1083ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1084ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Build a call to 'begin' or 'end' for a C++0x for-range statement. If the 1085ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// given LookupResult is non-empty, it is assumed to describe a member which 1086ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// will be invoked. Otherwise, the function will be found via argument 1087ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// dependent lookup. 1088ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstatic ExprResult BuildForRangeBeginEndCall(Sema &SemaRef, Scope *S, 1089ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation Loc, 1090ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *Decl, 1091ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginEndFunction BEF, 1092ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith const DeclarationNameInfo &NameInfo, 1093ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupResult &MemberLookup, 1094ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Expr *Range) { 1095ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult CallExpr; 1096ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!MemberLookup.empty()) { 1097ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult MemberRef = 1098ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.BuildMemberReferenceExpr(Range, Range->getType(), Loc, 1099ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*IsPtr=*/false, CXXScopeSpec(), 1100ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*Qualifier=*/0, MemberLookup, 1101ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*TemplateArgs=*/0); 1102ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (MemberRef.isInvalid()) 1103ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1104ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith CallExpr = SemaRef.ActOnCallExpr(S, MemberRef.get(), Loc, MultiExprArg(), 1105ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Loc, 0); 1106ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (CallExpr.isInvalid()) 1107ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1108ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } else { 1109ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith UnresolvedSet<0> FoundNames; 1110ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // C++0x [stmt.ranged]p1: For the purposes of this name lookup, namespace 1111ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // std is an associated namespace. 1112ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith UnresolvedLookupExpr *Fn = 1113ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith UnresolvedLookupExpr::Create(SemaRef.Context, /*NamingClass=*/0, 1114ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NestedNameSpecifierLoc(), NameInfo, 1115ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*NeedsADL=*/true, /*Overloaded=*/false, 1116ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith FoundNames.begin(), FoundNames.end(), 1117ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*LookInStdNamespace=*/true); 1118ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith CallExpr = SemaRef.BuildOverloadedCallExpr(S, Fn, Fn, Loc, &Range, 1, Loc, 1119ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 0); 1120ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (CallExpr.isInvalid()) { 1121ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.Diag(Range->getLocStart(), diag::note_for_range_type) 1122ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << Range->getType(); 1123ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1124ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1125ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1126ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(SemaRef, Decl, CallExpr.get(), Loc, 1127ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_iter_deduction_failure)) { 1128ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(SemaRef, CallExpr.get(), BEF); 1129ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1130ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1131ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return CallExpr; 1132ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1133ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1134ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1135ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1136ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// ActOnCXXForRangeStmt - Check and build a C++0x for-range statement. 1137ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1138ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// C++0x [stmt.ranged]: 1139ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// A range-based for statement is equivalent to 1140ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1141ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// { 1142ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// auto && __range = range-init; 1143ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// for ( auto __begin = begin-expr, 1144ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// __end = end-expr; 1145ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// __begin != __end; 1146ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// ++__begin ) { 1147ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// for-range-declaration = *__begin; 1148ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// statement 1149ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// } 1150ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// } 1151ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1152ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// The body of the loop is not available yet, since it cannot be analysed until 1153ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// we have determined the type of the for-range-declaration. 1154ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult 1155ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithSema::ActOnCXXForRangeStmt(SourceLocation ForLoc, SourceLocation LParenLoc, 1156ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Stmt *First, SourceLocation ColonLoc, Expr *Range, 1157ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RParenLoc) { 1158ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!First || !Range) 1159ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1160ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1161ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *DS = dyn_cast<DeclStmt>(First); 1162ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith assert(DS && "first part of for range not a decl stmt"); 1163ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1164ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!DS->isSingleDecl()) { 1165ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Diag(DS->getStartLoc(), diag::err_type_defined_in_for_range); 1166ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1167ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1168ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DS->getSingleDecl()->isInvalidDecl()) 1169ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1170ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1171ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DiagnoseUnexpandedParameterPack(Range, UPPC_Expression)) 1172ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1173ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1174ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build auto && __range = range-init 1175ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RangeLoc = Range->getLocStart(); 1176ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *RangeVar = BuildForRangeVarDecl(*this, RangeLoc, 1177ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Context.getAutoRRefDeductType(), 1178ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__range"); 1179ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(*this, RangeVar, Range, RangeLoc, 1180ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_deduction_failure)) 1181ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1182ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1183ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Claim the type doesn't contain auto: we've already done the checking. 1184ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclGroupPtrTy RangeGroup = 1185ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BuildDeclaratorGroup((Decl**)&RangeVar, 1, /*TypeMayContainAuto=*/false); 1186ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith StmtResult RangeDecl = ActOnDeclStmt(RangeGroup, RangeLoc, RangeLoc); 1187ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (RangeDecl.isInvalid()) 1188ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1189ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1190ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return BuildCXXForRangeStmt(ForLoc, ColonLoc, RangeDecl.get(), 1191ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*BeginEndDecl=*/0, /*Cond=*/0, /*Inc=*/0, DS, 1192ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith RParenLoc); 1193ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1194ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1195ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// BuildCXXForRangeStmt - Build or instantiate a C++0x for-range statement. 1196ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult 1197ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithSema::BuildCXXForRangeStmt(SourceLocation ForLoc, SourceLocation ColonLoc, 1198ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Stmt *RangeDecl, Stmt *BeginEnd, Expr *Cond, 1199ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Expr *Inc, Stmt *LoopVarDecl, 1200ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RParenLoc) { 1201ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Scope *S = getCurScope(); 1202ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1203ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *RangeDS = cast<DeclStmt>(RangeDecl); 1204ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *RangeVar = cast<VarDecl>(RangeDS->getSingleDecl()); 1205ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType RangeVarType = RangeVar->getType(); 1206ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1207ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *LoopVarDS = cast<DeclStmt>(LoopVarDecl); 1208ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *LoopVar = cast<VarDecl>(LoopVarDS->getSingleDecl()); 1209ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1210ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith StmtResult BeginEndDecl = BeginEnd; 1211ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult NotEqExpr = Cond, IncrExpr = Inc; 1212ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1213ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!BeginEndDecl.get() && !RangeVarType->isDependentType()) { 1214ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RangeLoc = RangeVar->getLocation(); 1215ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1216ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult RangeRef = BuildDeclRefExpr(RangeVar, 1217ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith RangeVarType.getNonReferenceType(), 1218ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VK_LValue, ColonLoc); 1219ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (RangeRef.isInvalid()) 1220ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1221ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1222ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType AutoType = Context.getAutoDeductType(); 1223ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Expr *Range = RangeVar->getInit(); 1224ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Range) 1225ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1226ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType RangeType = Range->getType(); 1227ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1228ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (RequireCompleteType(RangeLoc, RangeType, 1229ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith PDiag(diag::err_for_range_incomplete_type))) 1230ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1231ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1232ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build auto __begin = begin-expr, __end = end-expr. 1233ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *BeginVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, 1234ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__begin"); 1235ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *EndVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, 1236ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__end"); 1237ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1238ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build begin-expr and end-expr and attach to __begin and __end variables. 1239ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult BeginExpr, EndExpr; 1240ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (const ArrayType *UnqAT = RangeType->getAsArrayTypeUnsafe()) { 1241ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // - if _RangeT is an array type, begin-expr and end-expr are __range and 1242ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // __range + __bound, respectively, where __bound is the array bound. If 1243ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // _RangeT is an array of unknown size or an array of incomplete type, 1244ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // the program is ill-formed; 1245ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1246ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // begin-expr is __range. 1247ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginExpr = RangeRef; 1248ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(*this, BeginVar, RangeRef.get(), ColonLoc, 1249ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_iter_deduction_failure)) { 1250ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1251ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1252ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1253ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1254ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Find the array bound. 1255ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult BoundExpr; 1256ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(UnqAT)) 1257ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr = Owned(IntegerLiteral::Create(Context, CAT->getSize(), 1258ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Context.IntTy, RangeLoc)); 1259ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith else if (const VariableArrayType *VAT = 1260ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith dyn_cast<VariableArrayType>(UnqAT)) 1261ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr = VAT->getSizeExpr(); 1262ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith else { 1263ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Can't be a DependentSizedArrayType or an IncompleteArrayType since 1264ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // UnqAT is not incomplete and Range is not type-dependent. 1265ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith assert(0 && "Unexpected array type in for-range"); 1266ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1267ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1268ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1269ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // end-expr is __range + __bound. 1270ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EndExpr = ActOnBinOp(S, ColonLoc, tok::plus, RangeRef.get(), 1271ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr.get()); 1272ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (EndExpr.isInvalid()) 1273ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1274ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(*this, EndVar, EndExpr.get(), ColonLoc, 1275ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_iter_deduction_failure)) { 1276ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1277ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1278ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1279ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } else { 1280ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclarationNameInfo BeginNameInfo(&PP.getIdentifierTable().get("begin"), 1281ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ColonLoc); 1282ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclarationNameInfo EndNameInfo(&PP.getIdentifierTable().get("end"), 1283ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ColonLoc); 1284ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1285ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupResult BeginMemberLookup(*this, BeginNameInfo, LookupMemberName); 1286ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupResult EndMemberLookup(*this, EndNameInfo, LookupMemberName); 1287ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1288ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (CXXRecordDecl *D = RangeType->getAsCXXRecordDecl()) { 1289ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // - if _RangeT is a class type, the unqualified-ids begin and end are 1290ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // looked up in the scope of class _RangeT as if by class member access 1291ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // lookup (3.4.5), and if either (or both) finds at least one 1292ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // declaration, begin-expr and end-expr are __range.begin() and 1293ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // __range.end(), respectively; 1294ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupQualifiedName(BeginMemberLookup, D); 1295ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupQualifiedName(EndMemberLookup, D); 1296ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1297ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (BeginMemberLookup.empty() != EndMemberLookup.empty()) { 1298ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Diag(ColonLoc, diag::err_for_range_member_begin_end_mismatch) 1299ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << RangeType << BeginMemberLookup.empty(); 1300ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1301ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1302ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } else { 1303ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // - otherwise, begin-expr and end-expr are begin(__range) and 1304ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // end(__range), respectively, where begin and end are looked up with 1305ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // argument-dependent lookup (3.4.2). For the purposes of this name 1306ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // lookup, namespace std is an associated namespace. 1307ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1308ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1309ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginExpr = BuildForRangeBeginEndCall(*this, S, ColonLoc, BeginVar, 1310ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_begin, BeginNameInfo, 1311ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginMemberLookup, RangeRef.get()); 1312ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (BeginExpr.isInvalid()) 1313ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1314ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1315ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EndExpr = BuildForRangeBeginEndCall(*this, S, ColonLoc, EndVar, 1316ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_end, EndNameInfo, 1317ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EndMemberLookup, RangeRef.get()); 1318ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (EndExpr.isInvalid()) 1319ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1320ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1321ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1322ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // C++0x [decl.spec.auto]p6: BeginType and EndType must be the same. 1323ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType BeginType = BeginVar->getType(), EndType = EndVar->getType(); 1324ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Context.hasSameType(BeginType, EndType)) { 1325ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Diag(RangeLoc, diag::err_for_range_begin_end_types_differ) 1326ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << BeginType << EndType; 1327ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1328ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1329ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1330ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1331ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl *BeginEndDecls[] = { BeginVar, EndVar }; 1332ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Claim the type doesn't contain auto: we've already done the checking. 1333ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclGroupPtrTy BeginEndGroup = 1334ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BuildDeclaratorGroup(BeginEndDecls, 2, /*TypeMayContainAuto=*/false); 1335ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginEndDecl = ActOnDeclStmt(BeginEndGroup, ColonLoc, ColonLoc); 1336ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1337ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult BeginRef = BuildDeclRefExpr(BeginVar, 1338ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginType.getNonReferenceType(), 1339ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VK_LValue, ColonLoc); 1340ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult EndRef = BuildDeclRefExpr(EndVar, EndType.getNonReferenceType(), 1341ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VK_LValue, ColonLoc); 1342ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1343ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check __begin != __end expression. 1344ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnBinOp(S, ColonLoc, tok::exclaimequal, 1345ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginRef.get(), EndRef.get()); 1346ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnBooleanCondition(S, ColonLoc, NotEqExpr.get()); 1347ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnFinishFullExpr(NotEqExpr.get()); 1348ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (NotEqExpr.isInvalid()) { 1349ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1350ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Context.hasSameType(BeginType, EndType)) 1351ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1352ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1353ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1354ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1355ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check ++__begin expression. 1356ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IncrExpr = ActOnUnaryOp(S, ColonLoc, tok::plusplus, BeginRef.get()); 1357ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IncrExpr = ActOnFinishFullExpr(IncrExpr.get()); 1358ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (IncrExpr.isInvalid()) { 1359ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1360ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1361ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1362ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1363ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check *__begin expression. 1364ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult DerefExpr = ActOnUnaryOp(S, ColonLoc, tok::star, BeginRef.get()); 1365ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DerefExpr.isInvalid()) { 1366ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1367ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1368ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1369ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1370ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Attach *__begin as initializer for VD. 1371ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!LoopVar->isInvalidDecl()) { 1372ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith AddInitializerToDecl(LoopVar, DerefExpr.get(), /*DirectInit=*/false, 1373ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*TypeMayContainAuto=*/true); 1374ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (LoopVar->isInvalidDecl()) 1375ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1376ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1377ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1378ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1379ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return Owned(new (Context) CXXForRangeStmt(RangeDS, 1380ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith cast_or_null<DeclStmt>(BeginEndDecl.get()), 1381ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr.take(), IncrExpr.take(), 1382ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LoopVarDS, /*Body=*/0, ForLoc, 1383ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ColonLoc, RParenLoc)); 1384ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1385ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1386ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// FinishCXXForRangeStmt - Attach the body to a C++0x for-range statement. 1387ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// This is a separate step from ActOnCXXForRangeStmt because analysis of the 1388ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// body cannot be performed until after the type of the range variable is 1389ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// determined. 1390ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult Sema::FinishCXXForRangeStmt(Stmt *S, Stmt *B) { 1391ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!S || !B) 1392ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1393ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1394ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith cast<CXXForRangeStmt>(S)->setBody(B); 1395ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return S; 1396ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1397ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 139857ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerStmtResult Sema::ActOnGotoStmt(SourceLocation GotoLoc, 139957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner SourceLocation LabelLoc, 140057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner LabelDecl *TheDecl) { 140157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner getCurFunction()->setHasBranchIntoScope(); 1402ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner TheDecl->setUsed(); 1403ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner return Owned(new (Context) GotoStmt(TheDecl, GotoLoc, LabelLoc)); 14045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 14055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 140660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1407ad56d684259f706b7c0ae5ad9c23adb4f2926817Chris LattnerSema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc, 14089ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *E) { 1409bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman // Convert operand to void* 14105f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor if (!E->isTypeDependent()) { 14115f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor QualType ETy = E->getType(); 14122877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth QualType DestTy = Context.getPointerType(Context.VoidTy.withConst()); 1413429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult ExprRes = Owned(E); 14145f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor AssignConvertType ConvTy = 1415429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley CheckSingleAssignmentConstraints(DestTy, ExprRes); 1416429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (ExprRes.isInvalid()) 1417429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 1418429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley E = ExprRes.take(); 14192877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing)) 14205f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor return StmtError(); 14215f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor } 1422b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 1423781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasIndirectGoto(); 1424b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 14255f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E)); 14265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 142860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 14291b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { 14305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Scope *S = CurScope->getContinueParent(); 14315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!S) { 14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.6.2p1: A break shall appear only in or as a loop body. 14334cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop)); 14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14354cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 14368189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ContinueStmt(ContinueLoc)); 14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 14385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 143960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 14401b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { 14415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Scope *S = CurScope->getBreakParent(); 14425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!S) { 14435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. 14444cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch)); 14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14464cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 14478189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) BreakStmt(BreakLoc)); 14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1450dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Determine whether the given expression is a candidate for 1451f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// copy elision in either a return statement or a throw expression. 14525077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 1453f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param ReturnType If we're determining the copy elision candidate for 1454f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// a return statement, this is the return type of the function. If we're 1455f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// determining the copy elision candidate for a throw expression, this will 1456f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// be a NULL type. 14575077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 1458f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param E The expression being returned from the function or block, or 1459f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// being thrown. 14605077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 1461f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param AllowFunctionParameter 14625077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 14635077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \returns The NRVO candidate variable, if the return statement may use the 14645077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// NRVO, or NULL if there is no such candidate. 1465f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregorconst VarDecl *Sema::getCopyElisionCandidate(QualType ReturnType, 1466f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor Expr *E, 1467f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor bool AllowFunctionParameter) { 1468f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor QualType ExprType = E->getType(); 14693c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor // - in a return statement in a function with ... 14703c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor // ... a class return type ... 1471f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!ReturnType.isNull()) { 1472f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!ReturnType->isRecordType()) 1473f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor return 0; 1474f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor // ... the same cv-unqualified type as the function return type ... 1475f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!Context.hasSameUnqualifiedType(ReturnType, ExprType)) 1476f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor return 0; 1477f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor } 1478dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1479dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // ... the expression is the name of a non-volatile automatic object 1480f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor // (other than a function or catch-clause parameter)) ... 1481f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()); 14823c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor if (!DR) 14835077c3876beeaed32280af88244e8050078619a8Douglas Gregor return 0; 14843c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); 14853c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor if (!VD) 14865077c3876beeaed32280af88244e8050078619a8Douglas Gregor return 0; 1487dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1488f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (VD->hasLocalStorage() && !VD->isExceptionVariable() && 1489d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor !VD->getType()->isReferenceType() && !VD->hasAttr<BlocksAttr>() && 1490f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor !VD->getType().isVolatileQualified() && 14914a46c77813af1241139b81a086b539e4d734cb86Douglas Gregor ((VD->getKind() == Decl::Var) || 14924a46c77813af1241139b81a086b539e4d734cb86Douglas Gregor (AllowFunctionParameter && VD->getKind() == Decl::ParmVar))) 14935077c3876beeaed32280af88244e8050078619a8Douglas Gregor return VD; 1494dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 14955077c3876beeaed32280af88244e8050078619a8Douglas Gregor return 0; 14963c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor} 14973c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor 149807f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor/// \brief Perform the initialization of a potentially-movable value, which 149907f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor/// is the result of return value. 1500cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// 1501cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// This routine implements C++0x [class.copy]p33, which attempts to treat 1502cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// returned lvalues as rvalues in certain cases (to prefer move construction), 1503cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// then falls back to treating them as lvalues if that failed. 1504dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiExprResult 150507f402cff25354c5f06f307f19b0c57c09d964bdDouglas GregorSema::PerformMoveOrCopyInitialization(const InitializedEntity &Entity, 150607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor const VarDecl *NRVOCandidate, 150707f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor QualType ResultType, 150807f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Expr *Value) { 1509cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // C++0x [class.copy]p33: 1510dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // When the criteria for elision of a copy operation are met or would 1511dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // be met save for the fact that the source object is a function 1512dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // parameter, and the object to be copied is designated by an lvalue, 1513cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // overload resolution to select the constructor for the copy is first 1514cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // performed as if the object were designated by an rvalue. 1515cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor ExprResult Res = ExprError(); 151607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor if (NRVOCandidate || getCopyElisionCandidate(ResultType, Value, true)) { 1517dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi ImplicitCastExpr AsRvalue(ImplicitCastExpr::OnStack, 151807f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value->getType(), CK_LValueToRValue, 151907f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value, VK_XValue); 1520dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1521cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor Expr *InitExpr = &AsRvalue; 1522dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi InitializationKind Kind 152307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor = InitializationKind::CreateCopy(Value->getLocStart(), 152407f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value->getLocStart()); 152507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor InitializationSequence Seq(*this, Entity, Kind, &InitExpr, 1); 1526dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1527dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // [...] If overload resolution fails, or if the type of the first 1528cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // parameter of the selected constructor is not an rvalue reference 15290099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi // to the object's type (possibly cv-qualified), overload resolution 1530cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // is performed again, considering the object as an lvalue. 1531cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor if (Seq.getKind() != InitializationSequence::FailedSequence) { 1532cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor for (InitializationSequence::step_iterator Step = Seq.step_begin(), 1533cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor StepEnd = Seq.step_end(); 1534cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor Step != StepEnd; ++Step) { 1535dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (Step->Kind 1536cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor != InitializationSequence::SK_ConstructorInitialization) 1537cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor continue; 1538dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1539dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi CXXConstructorDecl *Constructor 1540cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor = cast<CXXConstructorDecl>(Step->Function.Function); 1541dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1542cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor const RValueReferenceType *RRefType 154307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor = Constructor->getParamDecl(0)->getType() 154407f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor ->getAs<RValueReferenceType>(); 1545dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1546cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // If we don't meet the criteria, break out now. 1547dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (!RRefType || 154807f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor !Context.hasSameUnqualifiedType(RRefType->getPointeeType(), 154907f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Context.getTypeDeclType(Constructor->getParent()))) 1550cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor break; 1551dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1552cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Promote "AsRvalue" to the heap, since we now need this 1553cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // expression node to persist. 155407f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value = ImplicitCastExpr::Create(Context, Value->getType(), 1555dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi CK_LValueToRValue, Value, 0, 155607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor VK_XValue); 1557dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1558cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Complete type-checking the initialization of the return type 1559cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // using the constructor we found. 156007f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Res = Seq.Perform(*this, Entity, Kind, MultiExprArg(&Value, 1)); 1561cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 1562cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 1563cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 1564dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1565cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Either we didn't meet the criteria for treating an lvalue as an rvalue, 1566dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // above, or overload resolution failed. Either way, we need to try 1567cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // (again) now with the return value expression as written. 1568cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor if (Res.isInvalid()) 156907f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Res = PerformCopyInitialization(Entity, SourceLocation(), Value); 1570dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1571cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor return Res; 1572cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor} 1573cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor 157427c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// ActOnBlockReturnStmt - Utility routine to figure out block's return type. 15754eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff/// 157660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 15774eb206bebcdab28ababe8df55c6185cec2cdc071Steve NaroffSema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { 15784eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // If this is the first return we've seen in the block, infer the type of 15794eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // the block from it. 15809ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor BlockScopeInfo *CurBlock = getCurBlock(); 15817d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian if (CurBlock->ReturnType.isNull()) { 1582c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff if (RetValExp) { 158316564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff // Don't call UsualUnaryConversions(), since we don't want to do 158416564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff // integer promotions here. 1585429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultFunctionArrayLvalueConversion(RetValExp); 1586429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 1587429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 1588429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley RetValExp = Result.take(); 15897d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian CurBlock->ReturnType = RetValExp->getType(); 15907d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(RetValExp)) { 15917d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian // We have to remove a 'const' added to copied-in variable which was 15927d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian // part of the implementation spec. and not the actual qualifier for 15937d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian // the variable. 15947d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian if (CDRE->isConstQualAdded()) 159549f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall CurBlock->ReturnType.removeLocalConst(); // FIXME: local??? 15967d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian } 1597c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff } else 15987d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian CurBlock->ReturnType = Context.VoidTy; 15994eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff } 16007d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian QualType FnRetType = CurBlock->ReturnType; 16014cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 1602711c52bb20d0c69063b52a99826fb7d2835501f1John McCall if (CurBlock->FunctionType->getAs<FunctionType>()->getNoReturnAttr()) { 16036c92fa75e62937f9738696840efcb258560f4568Mike Stump Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr) 16046c92fa75e62937f9738696840efcb258560f4568Mike Stump << getCurFunctionOrMethodDecl()->getDeclName(); 16056c92fa75e62937f9738696840efcb258560f4568Mike Stump return StmtError(); 16066c92fa75e62937f9738696840efcb258560f4568Mike Stump } 16076c92fa75e62937f9738696840efcb258560f4568Mike Stump 16084eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // Otherwise, verify that this result type matches the previous one. We are 16094eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // pickier with blocks than for normal functions because we don't have GCC 16104eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // compatibility to worry about here. 16115077c3876beeaed32280af88244e8050078619a8Douglas Gregor ReturnStmt *Result = 0; 16124eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff if (CurBlock->ReturnType->isVoidType()) { 16134eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff if (RetValExp) { 16144eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff Diag(ReturnLoc, diag::err_return_block_has_expr); 16154eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff RetValExp = 0; 16164eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff } 16175077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0); 16185077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else if (!RetValExp) { 16194cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr)); 16205077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else { 16215077c3876beeaed32280af88244e8050078619a8Douglas Gregor const VarDecl *NRVOCandidate = 0; 1622dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 16235077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { 16245077c3876beeaed32280af88244e8050078619a8Douglas Gregor // we have a non-void block with an expression, continue checking 16255077c3876beeaed32280af88244e8050078619a8Douglas Gregor 16265077c3876beeaed32280af88244e8050078619a8Douglas Gregor // C99 6.8.6.4p3(136): The return statement is not an assignment. The 16275077c3876beeaed32280af88244e8050078619a8Douglas Gregor // overlap restriction of subclause 6.5.16.1 does not apply to the case of 16285077c3876beeaed32280af88244e8050078619a8Douglas Gregor // function return. 16295077c3876beeaed32280af88244e8050078619a8Douglas Gregor 16305077c3876beeaed32280af88244e8050078619a8Douglas Gregor // In C++ the return statement is handled via a copy initialization. 16315077c3876beeaed32280af88244e8050078619a8Douglas Gregor // the C version of which boils down to CheckSingleAssignmentConstraints. 1632f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); 1633dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, 163407f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor FnRetType, 163507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor NRVOCandidate != 0); 1636dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, 163707f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor FnRetType, RetValExp); 16385077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (Res.isInvalid()) { 16395077c3876beeaed32280af88244e8050078619a8Douglas Gregor // FIXME: Cleanup temporaries here, anyway? 16405077c3876beeaed32280af88244e8050078619a8Douglas Gregor return StmtError(); 16415077c3876beeaed32280af88244e8050078619a8Douglas Gregor } 1642dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1643b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall if (RetValExp) { 1644b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(RetValExp, ReturnLoc); 16454765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall RetValExp = MaybeCreateExprWithCleanups(RetValExp); 1646b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall } 16474cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 16485077c3876beeaed32280af88244e8050078619a8Douglas Gregor RetValExp = Res.takeAs<Expr>(); 1649dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (RetValExp) 16505077c3876beeaed32280af88244e8050078619a8Douglas Gregor CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); 1651c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson } 1652dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 16535077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate); 165498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump } 16554cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 1656dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // If we need to check for the named return value optimization, save the 16575077c3876beeaed32280af88244e8050078619a8Douglas Gregor // return statement in our scope for later processing. 16585077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (getLangOptions().CPlusPlus && FnRetType->isRecordType() && 16595077c3876beeaed32280af88244e8050078619a8Douglas Gregor !CurContext->isDependentContext()) 16605077c3876beeaed32280af88244e8050078619a8Douglas Gregor FunctionScopes.back()->Returns.push_back(Result); 1661dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 16625077c3876beeaed32280af88244e8050078619a8Douglas Gregor return Owned(Result); 16634eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff} 16645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 166560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 16669ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { 16679ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor if (getCurBlock()) 16684eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff return ActOnBlockReturnStmt(ReturnLoc, RetValExp); 16694cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 1670371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner QualType FnRetType; 1671f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump if (const FunctionDecl *FD = getCurFunctionDecl()) { 1672371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner FnRetType = FD->getResultType(); 167304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall if (FD->hasAttr<NoReturnAttr>() || 167404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall FD->getType()->getAs<FunctionType>()->getNoReturnAttr()) 16758662587fa75d3fb04f873e265841c9314c7f5523Chris Lattner Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr) 1676f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump << getCurFunctionOrMethodDecl()->getDeclName(); 1677f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump } else if (ObjCMethodDecl *MD = getCurMethodDecl()) 1678c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff FnRetType = MD->getResultType(); 1679c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff else // If we don't have a function/method context, bail. 1680c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff return StmtError(); 16811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16825077c3876beeaed32280af88244e8050078619a8Douglas Gregor ReturnStmt *Result = 0; 16835cf216b7fa64b933b60743b0b26053e8e7aa87beChris Lattner if (FnRetType->isVoidType()) { 16841be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor if (RetValExp && !RetValExp->isTypeDependent()) { 16851be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor // C99 6.8.6.4p1 (ext_ since GCC warns) 168665ce04bef06696379682410f399f37b43996d824Chris Lattner unsigned D = diag::ext_return_has_expr; 168765ce04bef06696379682410f399f37b43996d824Chris Lattner if (RetValExp->getType()->isVoidType()) 168865ce04bef06696379682410f399f37b43996d824Chris Lattner D = diag::ext_return_has_void_expr; 1689f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall else { 1690429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = Owned(RetValExp); 1691429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Result = IgnoredValueConversions(Result.take()); 1692429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 1693429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 1694429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley RetValExp = Result.take(); 1695429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley RetValExp = ImpCastExprToType(RetValExp, Context.VoidTy, CK_ToVoid).take(); 1696f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall } 16974cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 1698e878eb035b343d7d819c092102364ec9849716aeChris Lattner // return (some void expression); is legal in C++. 1699e878eb035b343d7d819c092102364ec9849716aeChris Lattner if (D != diag::ext_return_has_void_expr || 1700e878eb035b343d7d819c092102364ec9849716aeChris Lattner !getLangOptions().CPlusPlus) { 1701e878eb035b343d7d819c092102364ec9849716aeChris Lattner NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); 1702e878eb035b343d7d819c092102364ec9849716aeChris Lattner Diag(ReturnLoc, D) 1703e878eb035b343d7d819c092102364ec9849716aeChris Lattner << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl) 1704e878eb035b343d7d819c092102364ec9849716aeChris Lattner << RetValExp->getSourceRange(); 1705e878eb035b343d7d819c092102364ec9849716aeChris Lattner } 17061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1707b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(RetValExp, ReturnLoc); 17084765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall RetValExp = MaybeCreateExprWithCleanups(RetValExp); 17095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1710dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 17115077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0); 17125077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else if (!RetValExp && !FnRetType->isDependentType()) { 17133c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4 17143c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner // C99 6.8.6.4p1 (ext_ since GCC warns) 17153c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr; 17163c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner 17173c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner if (FunctionDecl *FD = getCurFunctionDecl()) 171808631c5fa053867146b5ee8be658c229f6bf127cChris Lattner Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; 17193c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner else 172008631c5fa053867146b5ee8be658c229f6bf127cChris Lattner Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; 17215077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc); 17225077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else { 17235077c3876beeaed32280af88244e8050078619a8Douglas Gregor const VarDecl *NRVOCandidate = 0; 17245077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { 17255077c3876beeaed32280af88244e8050078619a8Douglas Gregor // we have a non-void function with an expression, continue checking 17265077c3876beeaed32280af88244e8050078619a8Douglas Gregor 17275077c3876beeaed32280af88244e8050078619a8Douglas Gregor // C99 6.8.6.4p3(136): The return statement is not an assignment. The 17285077c3876beeaed32280af88244e8050078619a8Douglas Gregor // overlap restriction of subclause 6.5.16.1 does not apply to the case of 17295077c3876beeaed32280af88244e8050078619a8Douglas Gregor // function return. 17305077c3876beeaed32280af88244e8050078619a8Douglas Gregor 17315077c3876beeaed32280af88244e8050078619a8Douglas Gregor // In C++ the return statement is handled via a copy initialization. 17325077c3876beeaed32280af88244e8050078619a8Douglas Gregor // the C version of which boils down to CheckSingleAssignmentConstraints. 1733f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); 1734dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, 173507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor FnRetType, 173607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor NRVOCandidate != 0); 1737dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, 173807f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor FnRetType, RetValExp); 17395077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (Res.isInvalid()) { 17405077c3876beeaed32280af88244e8050078619a8Douglas Gregor // FIXME: Cleanup temporaries here, anyway? 17415077c3876beeaed32280af88244e8050078619a8Douglas Gregor return StmtError(); 17425077c3876beeaed32280af88244e8050078619a8Douglas Gregor } 17434cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 17445077c3876beeaed32280af88244e8050078619a8Douglas Gregor RetValExp = Res.takeAs<Expr>(); 1745dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (RetValExp) 17465077c3876beeaed32280af88244e8050078619a8Douglas Gregor CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); 174766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor } 1748dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1749b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall if (RetValExp) { 1750b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(RetValExp, ReturnLoc); 17514765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall RetValExp = MaybeCreateExprWithCleanups(RetValExp); 1752b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall } 17535077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate); 1754898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor } 1755dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1756dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // If we need to check for the named return value optimization, save the 17575077c3876beeaed32280af88244e8050078619a8Douglas Gregor // return statement in our scope for later processing. 17585077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (getLangOptions().CPlusPlus && FnRetType->isRecordType() && 17595077c3876beeaed32280af88244e8050078619a8Douglas Gregor !CurContext->isDependentContext()) 17605077c3876beeaed32280af88244e8050078619a8Douglas Gregor FunctionScopes.back()->Returns.push_back(Result); 1761dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 17625077c3876beeaed32280af88244e8050078619a8Douglas Gregor return Owned(Result); 17635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 17645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1765810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently 1766810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// ignore "noop" casts in places where an lvalue is required by an inline asm. 1767810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// We emulate this behavior when -fheinous-gnu-extensions is specified, but 1768810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// provide a strong guidance to not use it. 1769810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// 1770810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// This method checks to see if the argument is an acceptable l-value and 1771810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// returns false if it is a case we can handle. 1772810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattnerstatic bool CheckAsmLValue(const Expr *E, Sema &S) { 1773703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson // Type dependent expressions will be checked during instantiation. 1774703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson if (E->isTypeDependent()) 1775703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson return false; 1776dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 17777eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall if (E->isLValue()) 1778810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return false; // Cool, this is an lvalue. 1779810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 1780810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // Okay, this is not an lvalue, but perhaps it is the result of a cast that we 1781810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // are supposed to allow. 1782810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); 17837eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall if (E != E2 && E2->isLValue()) { 1784810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (!S.getLangOptions().HeinousExtensions) 1785810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue) 1786810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner << E->getSourceRange(); 1787810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner else 1788810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue) 1789810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner << E->getSourceRange(); 1790810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // Accept, even if we emitted an error diagnostic. 1791810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return false; 1792810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner } 1793810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 1794810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // None of the above, just randomly invalid non-lvalue. 1795810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return true; 1796810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner} 1797810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 1798ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner/// isOperandMentioned - Return true if the specified operand # is mentioned 1799ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner/// anywhere in the decomposed asm string. 1800ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattnerstatic bool isOperandMentioned(unsigned OpNo, 1801ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner llvm::ArrayRef<AsmStmt::AsmStringPiece> AsmStrPieces) { 1802ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) { 1803ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner const AsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; 1804ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner if (!Piece.isOperand()) continue; 1805ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner 1806ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // If this is a reference to the input and if the input was the smaller 1807ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // one, then we have to reject this asm. 1808ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner if (Piece.getOperandNo() == OpNo) 1809ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner return true; 1810ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner } 1811ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner 1812ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner return false; 1813ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner} 1814810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 1815ca57b4b7658a031b74cda5ac504311998be8e343Chris LattnerStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, bool IsSimple, 1816ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner bool IsVolatile, unsigned NumOutputs, 1817ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner unsigned NumInputs, IdentifierInfo **Names, 1818ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner MultiExprArg constraints, MultiExprArg exprs, 1819ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner Expr *asmString, MultiExprArg clobbers, 1820ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner SourceLocation RParenLoc, bool MSAsm) { 18213037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl unsigned NumClobbers = clobbers.size(); 18223037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl StringLiteral **Constraints = 18233037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl reinterpret_cast<StringLiteral**>(constraints.get()); 18249ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr **Exprs = exprs.get(); 18259ae2f076ca5ab1feb3ba95629099ec2319833701John McCall StringLiteral *AsmString = cast<StringLiteral>(asmString); 18263037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get()); 18273037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 182803eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; 18291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18301708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner // The parser verifies that there is a string literal here. 18316bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner if (AsmString->isWide()) 18323037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) 18333037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << AsmString->getSourceRange()); 18343037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 18351708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner for (unsigned i = 0; i != NumOutputs; i++) { 18361708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Constraints[i]; 18376bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner if (Literal->isWide()) 18383037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 18393037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 18403037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 1841ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson llvm::StringRef OutputName; 1842ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson if (Names[i]) 1843ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson OutputName = Names[i]->getName(); 1844ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson 1845ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); 1846432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner if (!Context.Target.validateOutputConstraint(Info)) 18473037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 1848432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner diag::err_asm_invalid_output_constraint) 1849432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner << Info.getConstraintStr()); 18503037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 1851d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson // Check that the output exprs are valid lvalues. 185272056a237c536ee63285ab0850cb50f299281767Eli Friedman Expr *OutputExpr = Exprs[i]; 1853810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (CheckAsmLValue(OutputExpr, *this)) { 185472056a237c536ee63285ab0850cb50f299281767Eli Friedman return StmtError(Diag(OutputExpr->getLocStart(), 1855dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner diag::err_asm_invalid_lvalue_in_output) 185672056a237c536ee63285ab0850cb50f299281767Eli Friedman << OutputExpr->getSourceRange()); 185704728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 18581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 185944def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner OutputConstraintInfos.push_back(Info); 186004728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 18613037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 1862806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner llvm::SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; 1863806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner 186404728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { 18651708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Constraints[i]; 18666bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner if (Literal->isWide()) 18673037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 18683037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 18693037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 1870ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson llvm::StringRef InputName; 1871ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson if (Names[i]) 1872ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson InputName = Names[i]->getName(); 1873ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson 1874ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); 1875beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad if (!Context.Target.validateInputConstraint(OutputConstraintInfos.data(), 18762819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner NumOutputs, Info)) { 18773037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 1878432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner diag::err_asm_invalid_input_constraint) 1879432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner << Info.getConstraintStr()); 1880d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson } 18813037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 188272056a237c536ee63285ab0850cb50f299281767Eli Friedman Expr *InputExpr = Exprs[i]; 18833037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 1884d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson // Only allow void types for memory constraints. 188544def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner if (Info.allowsMemory() && !Info.allowsRegister()) { 1886810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (CheckAsmLValue(InputExpr, *this)) 188772056a237c536ee63285ab0850cb50f299281767Eli Friedman return StmtError(Diag(InputExpr->getLocStart(), 1888d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson diag::err_asm_invalid_lvalue_in_input) 1889432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner << Info.getConstraintStr() 189072056a237c536ee63285ab0850cb50f299281767Eli Friedman << InputExpr->getSourceRange()); 189104728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 18923037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 189344def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner if (Info.allowsRegister()) { 1894d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson if (InputExpr->getType()->isVoidType()) { 189572056a237c536ee63285ab0850cb50f299281767Eli Friedman return StmtError(Diag(InputExpr->getLocStart(), 1896d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson diag::err_asm_invalid_type_in_input) 18971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump << InputExpr->getType() << Info.getConstraintStr() 189872056a237c536ee63285ab0850cb50f299281767Eli Friedman << InputExpr->getSourceRange()); 1899d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson } 1900d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson } 19011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1902429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]); 1903429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 1904429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 19051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1906429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Exprs[i] = Result.take(); 1907806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner InputConstraintInfos.push_back(Info); 190804728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 19093037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 19106fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson // Check that the clobbers are valid. 19111708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner for (unsigned i = 0; i != NumClobbers; i++) { 19121708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Clobbers[i]; 19136bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner if (Literal->isWide()) 19143037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 19153037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 19163037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 1917fdba9c069023f686e2608affde02c82131ee1cf8Anders Carlsson llvm::StringRef Clobber = Literal->getString(); 19183037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 1919fdba9c069023f686e2608affde02c82131ee1cf8Anders Carlsson if (!Context.Target.isValidGCCRegisterName(Clobber)) 19203037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 19217765934ad7e157b5fcf925792a38e01b1edbcf8aDaniel Dunbar diag::err_asm_unknown_register_name) << Clobber); 19226fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson } 19233037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 1924fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner AsmStmt *NS = 1925dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, MSAsm, 1926dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi NumOutputs, NumInputs, Names, Constraints, Exprs, 1927966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson AsmString, NumClobbers, Clobbers, RParenLoc); 1928fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner // Validate the asm string, ensuring it makes sense given the operands we 1929fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner // have. 1930fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces; 1931fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner unsigned DiagOffs; 1932fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { 19332ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) 19342ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner << AsmString->getSourceRange(); 1935fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner return StmtError(); 1936fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner } 19371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1938806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // Validate tied input operands for type mismatches. 1939806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { 1940806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; 19411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1942806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // If this is a tied constraint, verify that the output and input have 1943806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // either exactly the same type, or that they are int/ptr operands with the 1944806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // same size (int/long, int*/long, are ok etc). 1945806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner if (!Info.hasTiedOperand()) continue; 19461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1947806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner unsigned TiedTo = Info.getTiedOperand(); 1948935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner unsigned InputOpNo = i+NumOutputs; 1949f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner Expr *OutputExpr = Exprs[TiedTo]; 1950935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner Expr *InputExpr = Exprs[InputOpNo]; 19517adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner QualType InTy = InputExpr->getType(); 19527adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner QualType OutTy = OutputExpr->getType(); 19537adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner if (Context.hasSameType(InTy, OutTy)) 1954806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner continue; // All types can be tied to themselves. 19551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1956aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // Decide if the input and output are in the same domain (integer/ptr or 1957aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // floating point. 1958aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner enum AsmDomain { 1959aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner AD_Int, AD_FP, AD_Other 1960aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner } InputDomain, OutputDomain; 1961dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1962aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (InTy->isIntegerType() || InTy->isPointerType()) 1963aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain = AD_Int; 19640c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor else if (InTy->isRealFloatingType()) 1965aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain = AD_FP; 1966aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner else 1967aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain = AD_Other; 19683351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner 1969aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (OutTy->isIntegerType() || OutTy->isPointerType()) 1970aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputDomain = AD_Int; 19710c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor else if (OutTy->isRealFloatingType()) 1972aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputDomain = AD_FP; 1973aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner else 1974aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputDomain = AD_Other; 1975dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1976aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // They are ok if they are the same size and in the same domain. This 1977aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // allows tying things like: 1978aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // void* to int* 1979aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // void* to int if they are the same size. 1980aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // double to long double if they are the same size. 1981dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // 1982aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner uint64_t OutSize = Context.getTypeSize(OutTy); 1983aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner uint64_t InSize = Context.getTypeSize(InTy); 1984aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (OutSize == InSize && InputDomain == OutputDomain && 1985aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain != AD_Other) 1986aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner continue; 1987dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1988aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // If the smaller input/output operand is not mentioned in the asm string, 1989f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner // then we can promote the smaller one to a larger input and the asm string 1990f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner // won't notice. 1991aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner bool SmallerValueMentioned = false; 1992ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner 1993ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // If this is a reference to the input and if the input was the smaller 1994ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // one, then we have to reject this asm. 1995935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner if (isOperandMentioned(InputOpNo, Pieces)) { 1996ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // This is a use in the asm string of the smaller operand. Since we 1997ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // codegen this by promoting to a wider value, the asm will get printed 1998ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // "wrong". 1999f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner SmallerValueMentioned |= InSize < OutSize; 2000ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner } 2001f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner if (isOperandMentioned(TiedTo, Pieces)) { 2002ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // If this is a reference to the output, and if the output is the larger 2003ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // value, then it's ok because we'll promote the input to the larger type. 2004f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner SmallerValueMentioned |= OutSize < InSize; 2005806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner } 20061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2007aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // If the smaller value wasn't mentioned in the asm string, and if the 2008aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // output was a register, just extend the shorter one to the size of the 2009aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // larger one. 2010aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (!SmallerValueMentioned && InputDomain != AD_Other && 2011aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputConstraintInfos[TiedTo].allowsRegister()) 2012aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner continue; 2013f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner 2014935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // Either both of the operands were mentioned or the smaller one was 2015935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // mentioned. One more special case that we'll allow: if the tied input is 2016935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // integer, unmentioned, and is a constant, then we'll allow truncating it 2017935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // down to the size of the destination. 2018935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner if (InputDomain == AD_Int && OutputDomain == AD_Int && 2019935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner !isOperandMentioned(InputOpNo, Pieces) && 2020935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner InputExpr->isEvaluatable(Context)) { 2021429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley InputExpr = ImpCastExprToType(InputExpr, OutTy, CK_IntegralCast).take(); 2022935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner Exprs[InputOpNo] = InputExpr; 2023935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner NS->setInputExpr(i, InputExpr); 2024935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner continue; 2025935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner } 2026935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner 2027c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner Diag(InputExpr->getLocStart(), 2028806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner diag::err_asm_tying_incompatible_types) 20297adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner << InTy << OutTy << OutputExpr->getSourceRange() 2030806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner << InputExpr->getSourceRange(); 2031806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner return StmtError(); 2032806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner } 20331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2034fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner return Owned(NS); 2035fe795956194141c91ae555985c9b930595bff43fChris Lattner} 20363b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian 203760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2038431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, 2039d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall SourceLocation RParen, Decl *Parm, 20409ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *Body) { 2041d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall VarDecl *Var = cast_or_null<VarDecl>(Parm); 2042160b5630aa781ac348303e1ae088d27016637778Douglas Gregor if (Var && Var->isInvalidDecl()) 2043160b5630aa781ac348303e1ae088d27016637778Douglas Gregor return StmtError(); 2044dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 20459ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtCatchStmt(AtLoc, RParen, Var, Body)); 20463b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian} 20473b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian 204860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 20499ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, Stmt *Body) { 20509ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, Body)); 2051161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian} 2052bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 205360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2054dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnObjCAtTryStmt(SourceLocation AtLoc, Stmt *Try, 20559ae2f076ca5ab1feb3ba95629099ec2319833701John McCall MultiStmtArg CatchStmts, Stmt *Finally) { 2056da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson if (!getLangOptions().ObjCExceptions) 2057da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@try"; 2058da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson 2059781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchProtectedScope(); 20608f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor unsigned NumCatchStmts = CatchStmts.size(); 20619ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(ObjCAtTryStmt::Create(Context, AtLoc, Try, 20629ae2f076ca5ab1feb3ba95629099ec2319833701John McCall CatchStmts.release(), 20638f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor NumCatchStmts, 20649ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Finally)); 2065bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian} 2066bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 206760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc, 20689ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *Throw) { 2069d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (Throw) { 2070429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultLvalueConversion(Throw); 2071429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 2072429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 20735e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall 2074429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Throw = Result.take(); 2075d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor QualType ThrowType = Throw->getType(); 2076d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor // Make sure the expression type is an ObjC pointer or "void *". 2077d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (!ThrowType->isDependentType() && 2078d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor !ThrowType->isObjCObjectPointerType()) { 2079d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor const PointerType *PT = ThrowType->getAs<PointerType>(); 2080d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (!PT || !PT->getPointeeType()->isVoidType()) 2081d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object) 2082d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor << Throw->getType() << Throw->getSourceRange()); 2083d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor } 2084d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor } 2085dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 20869ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtThrowStmt(AtLoc, Throw)); 2087d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor} 2088d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor 208960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2090dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw, 2091d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor Scope *CurScope) { 2092da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson if (!getLangOptions().ObjCExceptions) 2093da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@throw"; 2094da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson 20959ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!Throw) { 2096e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff // @throw without an expression designates a rethrow (which much occur 2097e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff // in the context of an @catch clause). 2098e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff Scope *AtCatchParent = CurScope; 2099e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff while (AtCatchParent && !AtCatchParent->isAtCatchScope()) 2100e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff AtCatchParent = AtCatchParent->getParent(); 2101e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff if (!AtCatchParent) 21024ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch)); 2103dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 2104dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 21059ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return BuildObjCAtThrowStmt(AtLoc, Throw); 210639f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian} 2107bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 210860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 21099ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, Expr *SyncExpr, 21109ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *SyncBody) { 2111781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchProtectedScope(); 211246c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner 2113429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultLvalueConversion(SyncExpr); 2114429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 2115429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 21165e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall 2117429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley SyncExpr = Result.take(); 2118a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner // Make sure the expression type is an ObjC pointer or "void *". 21198fdc13a78a43f09ac396e682c35d57ca0b48216dDouglas Gregor if (!SyncExpr->getType()->isDependentType() && 21208fdc13a78a43f09ac396e682c35d57ca0b48216dDouglas Gregor !SyncExpr->getType()->isObjCObjectPointerType()) { 21216217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const PointerType *PT = SyncExpr->getType()->getAs<PointerType>(); 2122a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner if (!PT || !PT->getPointeeType()->isVoidType()) 2123a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner return StmtError(Diag(AtLoc, diag::error_objc_synchronized_expects_object) 2124a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner << SyncExpr->getType() << SyncExpr->getSourceRange()); 2125a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner } 21261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21279ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, SyncExpr, SyncBody)); 2128fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian} 21294b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl 21304b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block 21314b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// and creates a proper catch handler from them. 213260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2133d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl, 21349ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *HandlerBlock) { 21354b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl // There's nothing to test that ActOnExceptionDecl didn't already test. 21368189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) CXXCatchStmt(CatchLoc, 2137d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall cast_or_null<VarDecl>(ExDecl), 21389ae2f076ca5ab1feb3ba95629099ec2319833701John McCall HandlerBlock)); 21394b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl} 21408351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 21413c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace { 21423c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 2143c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlclass TypeWithHandler { 2144c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl QualType t; 2145c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl CXXCatchStmt *stmt; 2146c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlpublic: 2147c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler(const QualType &type, CXXCatchStmt *statement) 2148c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl : t(type), stmt(statement) {} 2149c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 21500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall // An arbitrary order is fine as long as it places identical 21510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall // types next to each other. 2152c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl bool operator<(const TypeWithHandler &y) const { 21530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr()) 2154c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return true; 21550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr()) 2156c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return false; 2157c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl else 2158c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return getTypeSpecStartLoc() < y.getTypeSpecStartLoc(); 2159c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 21601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2161c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl bool operator==(const TypeWithHandler& other) const { 21620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall return t == other.t; 2163c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 21641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2165c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl CXXCatchStmt *getCatchStmt() const { return stmt; } 2166c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl SourceLocation getTypeSpecStartLoc() const { 2167c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return stmt->getExceptionDecl()->getTypeSpecStartLoc(); 2168c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 2169c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl}; 2170c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 21713c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman} 21723c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 21738351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// ActOnCXXTryBlock - Takes a try compound-statement and a number of 21748351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// handlers and creates a try statement from them. 217560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 21769ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock, 21778351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl MultiStmtArg RawHandlers) { 2178729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson // Don't report an error if 'try' is used in system headers. 217915348aeb81285c75b2e92b5bf8d2db3445d147c2Anders Carlsson if (!getLangOptions().CXXExceptions && 2180729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson !getSourceManager().isInSystemHeader(TryLoc)) 2181729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson Diag(TryLoc, diag::err_exceptions_disabled) << "try"; 21827f11d9cf5df1f8ce82af46eabc4ec5cec7d580b0Anders Carlsson 21838351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl unsigned NumHandlers = RawHandlers.size(); 21848351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl assert(NumHandlers > 0 && 21858351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl "The parser shouldn't call this if there are no handlers."); 21869ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt **Handlers = RawHandlers.get(); 21878351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 2188c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl llvm::SmallVector<TypeWithHandler, 8> TypesWithHandlers; 21891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (unsigned i = 0; i < NumHandlers; ++i) { 21918351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]); 2192c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (!Handler->getExceptionDecl()) { 2193c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (i < NumHandlers - 1) 2194c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return StmtError(Diag(Handler->getLocStart(), 2195c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::err_early_catch_all)); 21961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2197c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl continue; 2198c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 21991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2200c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl const QualType CaughtType = Handler->getCaughtType(); 2201c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType); 2202c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler)); 2203c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 2204c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 2205c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl // Detect handlers for the same type as an earlier one. 2206c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (NumHandlers > 1) { 2207c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end()); 22081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2209c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler prev = TypesWithHandlers[0]; 2210c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) { 2211c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler curr = TypesWithHandlers[i]; 22121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2213c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (curr == prev) { 2214c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl Diag(curr.getTypeSpecStartLoc(), 2215c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::warn_exception_caught_by_earlier_handler) 2216c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl << curr.getCatchStmt()->getCaughtType().getAsString(); 2217c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl Diag(prev.getTypeSpecStartLoc(), 2218c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::note_previous_exception_handler) 2219c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl << prev.getCatchStmt()->getCaughtType().getAsString(); 2220c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 22211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2222c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl prev = curr; 2223c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 22248351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl } 22251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2226781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchProtectedScope(); 2227b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 22288351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // FIXME: We should detect handlers that cannot catch anything because an 22298351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // earlier handler catches a superclass. Need to find a method that is not 22308351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // quadratic for this. 22318351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // Neither of these are explicitly forbidden, but every compiler detects them 22328351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // and warns. 22338351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 22349ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(CXXTryStmt::Create(Context, TryLoc, TryBlock, 2235a1a396df16c02b22983b5c9592022fd9237d4866Sam Weinig Handlers, NumHandlers)); 22368351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl} 2237