SemaStmt.cpp revision 79430e9983f5e67a378fc1f50cd6278f2cea8259
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" 19f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner#include "clang/AST/ASTContext.h" 201cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall#include "clang/AST/CharUnits.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 49b7d98d35ea723624345f06e5895ddce2e0388ef0Argyrios KyrtzidisStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc, 50e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis bool HasLeadingEmptyMacro) { 51e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis return Owned(new (Context) NullStmt(SemiLoc, HasLeadingEmptyMacro)); 525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 54337e550218128e7d922c09bb354fbc71de90c568Chris LattnerStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg, SourceLocation StartLoc, 55337e550218128e7d922c09bb354fbc71de90c568Chris Lattner SourceLocation EndLoc) { 5620401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); 571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 58682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner // If we have an invalid decl, just return an error. 5920401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner if (DG.isNull()) return StmtError(); 601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6124e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc)); 625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 64a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanianvoid Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) { 65a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); 66dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 67a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian // If we have an invalid decl, just return. 68a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian if (DG.isNull() || !DG.isSingleDecl()) return; 69f85e193739c953358c865005855253af4f68a497John McCall VarDecl *var = cast<VarDecl>(DG.getSingleDecl()); 70f85e193739c953358c865005855253af4f68a497John McCall 71a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian // suppress any potential 'unused variable' warning. 72f85e193739c953358c865005855253af4f68a497John McCall var->setUsed(); 73f85e193739c953358c865005855253af4f68a497John McCall 747acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // foreach variables are never actually initialized in the way that 757acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // the parser came up with. 767acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall var->setInit(0); 777acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall 787acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // In ARC, we don't need to retain the iteration variable of a fast 797acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // enumeration loop. Rather than actually trying to catch that 807acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // during declaration processing, we remove the consequences here. 81f85e193739c953358c865005855253af4f68a497John McCall if (getLangOptions().ObjCAutoRefCount) { 827acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall QualType type = var->getType(); 837acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall 847acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // Only do this if we inferred the lifetime. Inferred lifetime 857acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // will show up as a local qualifier because explicit lifetime 867acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // should have shown up as an AttributedType instead. 877acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (type.getLocalQualifiers().getObjCLifetime() == Qualifiers::OCL_Strong) { 887acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // Add 'const' and mark the variable as pseudo-strong. 897acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall var->setType(type.withConst()); 907acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall var->setARCPseudoStrong(true); 91f85e193739c953358c865005855253af4f68a497John McCall } 92f85e193739c953358c865005855253af4f68a497John McCall } 93a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian} 94a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian 95ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth/// \brief Diagnose unused '==' and '!=' as likely typos for '=' or '|='. 969d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth/// 979d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth/// Adding a cast to void (or other expression wrappers) will prevent the 989d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth/// warning from firing. 99ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruthstatic bool DiagnoseUnusedComparison(Sema &S, const Expr *E) { 1009d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth SourceLocation Loc; 10150bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth bool IsNotEqual, CanAssign; 1029d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1039d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (const BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { 1049d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (Op->getOpcode() != BO_EQ && Op->getOpcode() != BO_NE) 105ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1069d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1079d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth Loc = Op->getOperatorLoc(); 10850bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth IsNotEqual = Op->getOpcode() == BO_NE; 10950bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth CanAssign = Op->getLHS()->IgnoreParenImpCasts()->isLValue(); 1109d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth } else if (const CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { 1119d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (Op->getOperator() != OO_EqualEqual && 1129d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth Op->getOperator() != OO_ExclaimEqual) 113ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1149d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1159d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth Loc = Op->getOperatorLoc(); 11650bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth IsNotEqual = Op->getOperator() == OO_ExclaimEqual; 11750bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth CanAssign = Op->getArg(0)->IgnoreParenImpCasts()->isLValue(); 1189d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth } else { 1199d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth // Not a typo-prone comparison. 120ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1219d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth } 1229d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1239d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth // Suppress warnings when the operator, suspicious as it may be, comes from 1249d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth // a macro expansion. 1259d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (Loc.isMacroID()) 126ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1279d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 128ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth S.Diag(Loc, diag::warn_unused_comparison) 1299d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth << (unsigned)IsNotEqual << E->getSourceRange(); 1309d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 13150bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth // If the LHS is a plausible entity to assign to, provide a fixit hint to 13250bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth // correct common typos. 13350bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth if (CanAssign) { 13450bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth if (IsNotEqual) 13550bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth S.Diag(Loc, diag::note_inequality_comparison_to_or_assign) 13650bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth << FixItHint::CreateReplacement(Loc, "|="); 13750bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth else 13850bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth S.Diag(Loc, diag::note_equality_comparison_to_assign) 13950bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth << FixItHint::CreateReplacement(Loc, "="); 14050bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth } 141ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth 142ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return true; 1439d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth} 1449d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 145636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlssonvoid Sema::DiagnoseUnusedExprResult(const Stmt *S) { 146d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) 147d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis return DiagnoseUnusedExprResult(Label->getSubStmt()); 148d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis 149754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson const Expr *E = dyn_cast_or_null<Expr>(S); 150636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson if (!E) 151636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson return; 152636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson 153636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson SourceLocation Loc; 154636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson SourceRange R1, R2; 155df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump if (!E->isUnusedResultAWarning(Loc, R1, R2, Context)) 156636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson return; 1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 158419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // Okay, we have an unused result. Depending on what the base expression is, 159419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // we might want to make a more specific diagnostic. Check for one of these 160419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // cases now. 161419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner unsigned DiagID = diag::warn_unused_expr; 1624765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall if (const ExprWithCleanups *Temps = dyn_cast<ExprWithCleanups>(E)) 1634dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor E = Temps->getSubExpr(); 16434d49471e0b6386aefdc0f6bd15e4a4876ce5db1Chandler Carruth if (const CXXBindTemporaryExpr *TempExpr = dyn_cast<CXXBindTemporaryExpr>(E)) 16534d49471e0b6386aefdc0f6bd15e4a4876ce5db1Chandler Carruth E = TempExpr->getSubExpr(); 16612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall 167ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth if (DiagnoseUnusedComparison(*this, E)) 168ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return; 169ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth 170f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall E = E->IgnoreParenImpCasts(); 171bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 1720faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall if (E->getType()->isVoidType()) 1730faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall return; 1740faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall 175bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner // If the callee has attribute pure, const, or warn_unused_result, warn with 176bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner // a more specific message to make it clear what is happening. 177d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes if (const Decl *FD = CE->getCalleeDecl()) { 178bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<WarnUnusedResultAttr>()) { 17942d7b2d25b6f90dc397886d05502b10ab5a8b51bMatt Beaumont-Gay Diag(Loc, diag::warn_unused_result) << R1 << R2; 180bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 181bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 182bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<PureAttr>()) { 183bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure"; 184bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 185bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 186bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<ConstAttr>()) { 187bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const"; 188bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 189bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 190dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 19112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall } else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) { 192f85e193739c953358c865005855253af4f68a497John McCall if (getLangOptions().ObjCAutoRefCount && ME->isDelegateInitCall()) { 193f85e193739c953358c865005855253af4f68a497John McCall Diag(Loc, diag::err_arc_unused_init_message) << R1; 194f85e193739c953358c865005855253af4f68a497John McCall return; 195f85e193739c953358c865005855253af4f68a497John McCall } 196f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian const ObjCMethodDecl *MD = ME->getMethodDecl(); 197f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian if (MD && MD->getAttr<WarnUnusedResultAttr>()) { 19842d7b2d25b6f90dc397886d05502b10ab5a8b51bMatt Beaumont-Gay Diag(Loc, diag::warn_unused_result) << R1 << R2; 199f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian return; 200f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian } 2014b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else if (isa<PseudoObjectExpr>(E)) { 20212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall DiagID = diag::warn_unused_property_expr; 203d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor } else if (const CXXFunctionalCastExpr *FC 204d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor = dyn_cast<CXXFunctionalCastExpr>(E)) { 205d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor if (isa<CXXConstructExpr>(FC->getSubExpr()) || 206d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor isa<CXXTemporaryObjectExpr>(FC->getSubExpr())) 207d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor return; 208f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian } 209209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall // Diagnose "(void*) blah" as a typo for "(void) blah". 210209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) { 211209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); 212209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall QualType T = TI->getType(); 213209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 214209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall // We really do want to use the non-canonical type here. 215209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall if (T == Context.VoidPtrTy) { 216209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc()); 217209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 218209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall Diag(Loc, diag::warn_unused_voidptr) 219209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall << FixItHint::CreateRemoval(TL.getStarLoc()); 220209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall return; 221209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall } 222209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall } 223209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 224351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek DiagRuntimeBehavior(Loc, 0, PDiag(DiagID) << R1 << R2); 225636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson} 226636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson 22760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2281b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, 229a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl MultiStmtArg elts, bool isStmtExpr) { 230a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl unsigned NumElts = elts.size(); 231a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl Stmt **Elts = reinterpret_cast<Stmt**>(elts.release()); 232c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // If we're in C89 mode, check that we don't have any decls after stmts. If 233c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // so, emit an extension diagnostic. 234c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { 235c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // Note that __extension__ can be around a decl. 236c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner unsigned i = 0; 237c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // Skip over all declarations. 238c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) 239c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner /*empty*/; 240c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner 241c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // We found the end of the list or a statement. Scan for another declstmt. 242c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) 243c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner /*empty*/; 2441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 245c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner if (i != NumElts) { 2464afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin(); 247c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner Diag(D->getLocation(), diag::ext_mixed_decls_code); 248c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner } 249c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner } 25098414c1b7d1944a57156d52e29bd41c005de09acChris Lattner // Warn about unused expressions in statements. 25198414c1b7d1944a57156d52e29bd41c005de09acChris Lattner for (unsigned i = 0; i != NumElts; ++i) { 252636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson // Ignore statements that are last in a statement expression. 253636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson if (isStmtExpr && i == NumElts - 1) 25498414c1b7d1944a57156d52e29bd41c005de09acChris Lattner continue; 2551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 256636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson DiagnoseUnusedExprResult(Elts[i]); 25798414c1b7d1944a57156d52e29bd41c005de09acChris Lattner } 258a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl 2598189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R)); 2605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 26260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2639ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCaseStmt(SourceLocation CaseLoc, Expr *LHSVal, 2649ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SourceLocation DotDotDotLoc, Expr *RHSVal, 26524e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner SourceLocation ColonLoc) { 2669ae2f076ca5ab1feb3ba95629099ec2319833701John McCall assert((LHSVal != 0) && "missing expression in case statement"); 267117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.4.2p3: The expression shall be an integer constant. 2691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // However, GCC allows any evaluatable integer expression. 2701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent() && 271dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor VerifyIntegerConstantExpression(LHSVal)) 27224e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return StmtError(); 2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2746c36be5b383875b490684bcf439d6d427298c1afChris Lattner // GCC extension: The expression shall be an integer constant. 275117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 276dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent() && 277dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor VerifyIntegerConstantExpression(RHSVal)) { 278f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner RHSVal = 0; // Recover by just forgetting about it. 279117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl } 280117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 281781472fe99a120098c631b0cbe33c89f8cef5e70John McCall if (getCurFunction()->SwitchStack.empty()) { 2828a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner Diag(CaseLoc, diag::err_case_not_in_switch); 28324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return StmtError(); 2848a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner } 2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 286dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc, 287dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor ColonLoc); 288781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.back()->addSwitchCase(CS); 289117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(CS); 2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 29224e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner/// ActOnCaseStmtBody - This installs a statement as the body of a case. 2939ae2f076ca5ab1feb3ba95629099ec2319833701John McCallvoid Sema::ActOnCaseStmtBody(Stmt *caseStmt, Stmt *SubStmt) { 2945440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth DiagnoseUnusedExprResult(SubStmt); 2955440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth 29624e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner CaseStmt *CS = static_cast<CaseStmt*>(caseStmt); 29724e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner CS->setSubStmt(SubStmt); 29824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner} 29924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner 30060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 3011eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, 3029ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *SubStmt, Scope *CurScope) { 3035440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth DiagnoseUnusedExprResult(SubStmt); 3045440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth 305781472fe99a120098c631b0cbe33c89f8cef5e70John McCall if (getCurFunction()->SwitchStack.empty()) { 3060fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner Diag(DefaultLoc, diag::err_default_not_in_switch); 307117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(SubStmt); 3080fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner } 309117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 310dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt); 311781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.back()->addSwitchCase(DS); 312117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(DS); 3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 31560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 31657ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerSema::ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl, 31757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner SourceLocation ColonLoc, Stmt *SubStmt) { 31857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner 319ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner // If the label was multiply defined, reject it now. 320ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner if (TheDecl->getStmt()) { 321ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Diag(IdentLoc, diag::err_redefinition_of_label) << TheDecl->getDeclName(); 322ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Diag(TheDecl->getLocation(), diag::note_previous_definition); 323de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(SubStmt); 3245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 325de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 326ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner // Otherwise, things are good. Fill in the declaration and return it. 327ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner LabelStmt *LS = new (Context) LabelStmt(IdentLoc, TheDecl, SubStmt); 328ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner TheDecl->setStmt(LS); 329203548ba4b72e7e59320d352afc1eb0b5ab131deAbramo Bagnara if (!TheDecl->isGnuLocal()) 330203548ba4b72e7e59320d352afc1eb0b5ab131deAbramo Bagnara TheDecl->setLocation(IdentLoc); 331ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner return Owned(LS); 3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 33460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 335d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, Decl *CondVar, 33644aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis Stmt *thenStmt, SourceLocation ElseLoc, 33744aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis Stmt *elseStmt) { 33860d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult(CondVal.release()); 3391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3408cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor VarDecl *ConditionVar = 0; 341d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 342d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 343586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor CondResult = CheckConditionVariable(ConditionVar, IfLoc, true); 34499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (CondResult.isInvalid()) 34599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 3468cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor } 34799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Expr *ConditionExpr = CondResult.takeAs<Expr>(); 34899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!ConditionExpr) 34999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 350dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 351754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(thenStmt); 3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3532d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // Warn if the if block has a null body without an else value. 3542d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // this helps prevent bugs due to typos, such as 3552d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // if (condition); 3562d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // do_stuff(); 357b3198178daf9d33466fc18e009e2c6692fdf29c0Ted Kremenek // 3589ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!elseStmt) { 3592d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt)) 360a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // But do not warn if the body is a macro that expands to nothing, e.g: 361a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // 362a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // #define CALL(x) 363a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // if (condition) 364a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // CALL(0); 365a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis // 36644aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis if (!stmt->hasLeadingEmptyMacro()) 367b3198178daf9d33466fc18e009e2c6692fdf29c0Ted Kremenek Diag(stmt->getSemiLoc(), diag::warn_empty_if_body); 3682d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson } 3692d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson 370754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(elseStmt); 3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 372dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi return Owned(new (Context) IfStmt(Context, IfLoc, ConditionVar, ConditionExpr, 37344aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis thenStmt, ElseLoc, elseStmt)); 3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 376f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have 377f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified width and sign. If an overflow occurs, detect it and emit 378f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified diagnostic. 379f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattnervoid Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, 380f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner unsigned NewWidth, bool NewSign, 3811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SourceLocation Loc, 382f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner unsigned DiagID) { 383f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Perform a conversion to the promoted condition type if needed. 384f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (NewWidth > Val.getBitWidth()) { 385f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // If this is an extension, just do it. 3869f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.extend(NewWidth); 387f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.setIsSigned(NewSign); 388f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor 389f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // If the input was signed and negative and the output is 390f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // unsigned, don't bother to warn: this is implementation-defined 391f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // behavior. 392f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // FIXME: Introduce a second, default-ignored warning for this case? 393f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else if (NewWidth < Val.getBitWidth()) { 394f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // If this is a truncation, check for overflow. 395f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner llvm::APSInt ConvVal(Val); 3969f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad ConvVal = ConvVal.trunc(NewWidth); 397b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner ConvVal.setIsSigned(NewSign); 3989f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad ConvVal = ConvVal.extend(Val.getBitWidth()); 399b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner ConvVal.setIsSigned(Val.isSigned()); 400f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (ConvVal != Val) 401d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10); 4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 403f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Regardless of whether a diagnostic was emitted, really do the 404f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // truncation. 4059f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.trunc(NewWidth); 406b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner Val.setIsSigned(NewSign); 407f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else if (NewSign != Val.isSigned()) { 408f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Convert the sign to match the sign of the condition. This can cause 409f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // overflow as well: unsigned(INTMIN) 410dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // We don't diagnose this overflow, because it is implementation-defined 4112853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor // behavior. 4122853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor // FIXME: Introduce a second, default-ignored warning for this case? 413f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner llvm::APSInt OldVal(Val); 414f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.setIsSigned(NewSign); 415f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 416f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner} 417f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 4180471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattnernamespace { 4190471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner struct CaseCompareFunctor { 4200471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 4210471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner const llvm::APSInt &RHS) { 4220471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner return LHS.first < RHS; 4230471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 4240e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 4250e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 4260e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner return LHS.first < RHS.first; 4270e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner } 4280471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner bool operator()(const llvm::APSInt &LHS, 4290471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 4300471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner return LHS < RHS.first; 4310471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 4320471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner }; 4330471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner} 4340471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner 435764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// CmpCaseVals - Comparison predicate for sorting case values. 436764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// 437764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattnerstatic bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, 438764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner const std::pair<llvm::APSInt, CaseStmt*>& rhs) { 439764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner if (lhs.first < rhs.first) 440764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return true; 441764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner 442764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner if (lhs.first == rhs.first && 443764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner lhs.second->getCaseLoc().getRawEncoding() 444764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner < rhs.second->getCaseLoc().getRawEncoding()) 445764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return true; 446764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return false; 447764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner} 448764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner 449ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// CmpEnumVals - Comparison predicate for sorting enumeration values. 450ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// 451ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, 452ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) 453ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{ 454ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor return lhs.first < rhs.first; 455ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor} 456ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 457ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// EqEnumVals - Comparison preficate for uniqing enumeration values. 458ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// 459ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, 460ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) 461ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{ 462ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor return lhs.first == rhs.first; 463ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor} 464ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 4655f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of 4665f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// potentially integral-promoted expression @p expr. 467a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCallstatic QualType GetTypeBeforeIntegralPromotion(Expr *&expr) { 468a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(expr)) 469a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall expr = cleanups->getSubExpr(); 470a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall while (ImplicitCastExpr *impcast = dyn_cast<ImplicitCastExpr>(expr)) { 471a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (impcast->getCastKind() != CK_IntegralCast) break; 472a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall expr = impcast->getSubExpr(); 4735f04881eb025f61396d0555d8173730fe2759e0aChris Lattner } 4745f04881eb025f61396d0555d8173730fe2759e0aChris Lattner return expr->getType(); 4755f04881eb025f61396d0555d8173730fe2759e0aChris Lattner} 4765f04881eb025f61396d0555d8173730fe2759e0aChris Lattner 47760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 478dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, Expr *Cond, 479d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *CondVar) { 48060d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult; 4819ae2f076ca5ab1feb3ba95629099ec2319833701John McCall 482586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor VarDecl *ConditionVar = 0; 483d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 484d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 4859ae2f076ca5ab1feb3ba95629099ec2319833701John McCall CondResult = CheckConditionVariable(ConditionVar, SourceLocation(), false); 4869ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 487586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 488dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4899ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.release(); 490586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor } 491dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4929ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!Cond) 493586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 494dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 4953c3b7f90a863af43fa63043d396553ecf205351cJohn McCall CondResult = CheckPlaceholderExpr(Cond); 4963c3b7f90a863af43fa63043d396553ecf205351cJohn McCall if (CondResult.isInvalid()) 4973c3b7f90a863af43fa63043d396553ecf205351cJohn McCall return StmtError(); 4983c3b7f90a863af43fa63043d396553ecf205351cJohn McCall 4999ae2f076ca5ab1feb3ba95629099ec2319833701John McCall CondResult 5009c1ba407188fbb702e944fff66c30f21bdf99f8eFariborz Jahanian = ConvertToIntegralOrEnumerationType(SwitchLoc, CondResult.take(), 501c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor PDiag(diag::err_typecheck_statement_requires_integer), 502c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor PDiag(diag::err_switch_incomplete_class_type) 5039ae2f076ca5ab1feb3ba95629099ec2319833701John McCall << Cond->getSourceRange(), 504c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor PDiag(diag::err_switch_explicit_conversion), 505c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor PDiag(diag::note_switch_conversion), 506c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor PDiag(diag::err_switch_multiple_conversions), 5076bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor PDiag(diag::note_switch_conversion), 5086bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor PDiag(0)); 5099ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) return StmtError(); 5109ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 511dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 512a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. 513a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall CondResult = UsualUnaryConversions(Cond); 514a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (CondResult.isInvalid()) return StmtError(); 515a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall Cond = CondResult.take(); 516a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall 517d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (!CondVar) { 518b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(Cond, SwitchLoc); 5194765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall CondResult = MaybeCreateExprWithCleanups(Cond); 5209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 521586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 5229ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 523586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor } 524b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 525781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchIntoScope(); 526dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 5279ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SwitchStmt *SS = new (Context) SwitchStmt(Context, ConditionVar, Cond); 528781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.push_back(SS); 529586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return Owned(SS); 5307e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner} 5317e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner 53228164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greifstatic void AdjustAPSInt(llvm::APSInt &Val, unsigned BitWidth, bool IsSigned) { 53328164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif if (Val.getBitWidth() < BitWidth) 5349f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.extend(BitWidth); 53528164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif else if (Val.getBitWidth() > BitWidth) 5369f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.trunc(BitWidth); 53728164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Val.setIsSigned(IsSigned); 53828164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif} 53928164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif 54060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 5419ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, 5429ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *BodyStmt) { 5439ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SwitchStmt *SS = cast<SwitchStmt>(Switch); 544781472fe99a120098c631b0cbe33c89f8cef5e70John McCall assert(SS == getCurFunction()->SwitchStack.back() && 545781472fe99a120098c631b0cbe33c89f8cef5e70John McCall "switch stack missing push/pop!"); 546de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 5479dcbfa450d751bd68fc4af8b75da381d4f6984b9Steve Naroff SS->setBody(BodyStmt, SwitchLoc); 548781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.pop_back(); 549c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson 550f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Expr *CondExpr = SS->getCond(); 551a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (!CondExpr) return StmtError(); 552de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 553a0d3ca1ea5578bc736bb71bcec50ab41fefc87b9Douglas Gregor QualType CondType = CondExpr->getType(); 554a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall 555a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall Expr *CondExprBeforePromotion = CondExpr; 556a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall QualType CondTypeBeforePromotion = 557a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall GetTypeBeforeIntegralPromotion(CondExprBeforePromotion); 55884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor 5595f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // C++ 6.4.2.p2: 5605f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // Integral promotions are performed (on the switch condition). 5615f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // 5625f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // A case value unrepresentable by the original switch condition 5635f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // type (before the promotion) doesn't make sense, even when it can 5645f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // be represented by the promoted type. Therefore we need to find 5655f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // the pre-promotion type of the switch condition. 56612356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan if (!CondExpr->isTypeDependent()) { 567acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor // We have already converted the expression to an integral or enumeration 568dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // type, when we started the switch statement. If we don't have an 569acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor // appropriate type now, just return an error. 570acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor if (!CondType->isIntegralOrEnumerationType()) 57112356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan return StmtError(); 57212356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan 5732b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner if (CondExpr->isKnownToHaveBooleanValue()) { 57412356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // switch(bool_expr) {...} is often a programmer error, e.g. 57512356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // switch(n && mask) { ... } // Doh - should be "n & mask". 57612356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // One can always use an if statement instead of switch(bool_expr). 57712356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan Diag(SwitchLoc, diag::warn_bool_switch_condition) 57812356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan << CondExpr->getSourceRange(); 57912356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan } 580c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson } 581de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 582f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Get the bitwidth of the switched-on value before promotions. We must 583f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // convert the integer case values to this width before comparison. 5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump bool HasDependentValue 585dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor = CondExpr->isTypeDependent() || CondExpr->isValueDependent(); 5861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump unsigned CondWidth 5871d6ab7af99a1fc059a6aa5da083640c1d94b07f7Chris Lattner = HasDependentValue ? 0 : Context.getIntWidth(CondTypeBeforePromotion); 588575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor bool CondIsSigned 589575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor = CondTypeBeforePromotion->isSignedIntegerOrEnumerationType(); 5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 591f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Accumulate all of the case values in a vector so that we can sort them 592f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // and detect duplicates. This vector contains the APInt for the case after 593f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // it has been converted to the condition type. 5945f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner typedef SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; 5950471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner CaseValsTy CaseVals; 5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 597f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Keep track of any GNU case ranges we see. The APSInt is the low value. 598ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy; 599ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseRangesTy CaseRanges; 6001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 601f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner DefaultStmt *TheDefaultStmt = 0; 6021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 603b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner bool CaseListIsErroneous = false; 6041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 605dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue; 606c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson SC = SC->getNextSwitchCase()) { 6071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 608c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { 609f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (TheDefaultStmt) { 610f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); 6115f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev); 612de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 613f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // FIXME: Remove the default statement from the switch block so that 614390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // we'll return a valid AST. This requires recursing down the AST and 615390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // finding it, not something we are set up to do right now. For now, 616390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // just lop the entire switch stmt out of the AST. 617b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CaseListIsErroneous = true; 618c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson } 619f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner TheDefaultStmt = DS; 6201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 621f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else { 622f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner CaseStmt *CS = cast<CaseStmt>(SC); 6231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 624f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // We already verified that the expression has a i-c-e value (C99 625f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // 6.8.4.2p3) - get that value now. 6261e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner Expr *Lo = CS->getLHS(); 627dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 628dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (Lo->isTypeDependent() || Lo->isValueDependent()) { 629dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HasDependentValue = true; 630dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor break; 631dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 633a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt LoVal = Lo->EvaluateKnownConstInt(Context); 6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 635f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Convert the value to the same width/sign as the condition. 636f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, 63728164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Lo->getLocStart(), 638f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner diag::warn_case_value_overflow); 6396c36be5b383875b490684bcf439d6d427298c1afChris Lattner 6401e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner // If the LHS is not the same type as the condition, insert an implicit 6411e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner // cast. 6424f87062cb411d5a31cf39f1ac576bba4123930f2Richard Smith // FIXME: In C++11, the value is a converted constant expression of the 6434f87062cb411d5a31cf39f1ac576bba4123930f2Richard Smith // promoted type of the switch condition. 6444f87062cb411d5a31cf39f1ac576bba4123930f2Richard Smith Lo = DefaultLvalueConversion(Lo).take(); 645429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Lo = ImpCastExprToType(Lo, CondType, CK_IntegralCast).take(); 6461e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner CS->setLHS(Lo); 6471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 648b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // If this is a case range, remember it in CaseRanges, otherwise CaseVals. 649dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (CS->getRHS()) { 6501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (CS->getRHS()->isTypeDependent() || 651dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CS->getRHS()->isValueDependent()) { 652dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HasDependentValue = true; 653dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor break; 654dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 655f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner CaseRanges.push_back(std::make_pair(LoVal, CS)); 6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else 657b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CaseVals.push_back(std::make_pair(LoVal, CS)); 658f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 659f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 660b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 661dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!HasDependentValue) { 6620fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // If we don't have a default statement, check whether the 6630fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // condition is constant. 6640fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall llvm::APSInt ConstantCondValue; 6650fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall bool HasConstantCond = false; 6660fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (!HasDependentValue && !TheDefaultStmt) { 66751f4708c00110940ca3f337961915f2ca1668375Richard Smith HasConstantCond 66880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith = CondExprBeforePromotion->EvaluateAsInt(ConstantCondValue, Context, 66980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith Expr::SE_AllowSideEffects); 67080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith assert(!HasConstantCond || 67180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith (ConstantCondValue.getBitWidth() == CondWidth && 67280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith ConstantCondValue.isSigned() == CondIsSigned)); 6730fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall } 67480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith bool ShouldCheckConstantCond = HasConstantCond; 6750fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 676dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Sort all the scalar case values so we can easily detect duplicates. 677dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); 678dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 679dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!CaseVals.empty()) { 6800fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) { 6810fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (ShouldCheckConstantCond && 6820fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall CaseVals[i].first == ConstantCondValue) 6830fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ShouldCheckConstantCond = false; 6840fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 6850fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) { 686dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If we have a duplicate, report it. 6871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(CaseVals[i].second->getLHS()->getLocStart(), 6880fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall diag::err_duplicate_case) << CaseVals[i].first.toString(10); 6890fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Diag(CaseVals[i-1].second->getLHS()->getLocStart(), 690dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::note_duplicate_case_prev); 691390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: We really want to remove the bogus case stmt from the 692390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // substmt, but we have no way to do this right now. 693dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseListIsErroneous = true; 694dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 6956efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner } 696b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 6971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 698dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Detect duplicate case ranges, which usually don't exist at all in 699dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // the first place. 700dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!CaseRanges.empty()) { 701dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Sort all the case ranges by their low value so we can easily detect 702dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // overlaps between ranges. 703dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::stable_sort(CaseRanges.begin(), CaseRanges.end()); 7041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 705dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Scan the ranges, computing the high values and removing empty ranges. 706dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::vector<llvm::APSInt> HiVals; 707dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 7080fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall llvm::APSInt &LoVal = CaseRanges[i].first; 709dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CR = CaseRanges[i].second; 710dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Expr *Hi = CR->getRHS(); 711a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt HiVal = Hi->EvaluateKnownConstInt(Context); 7121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 713dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Convert the value to the same width/sign as the condition. 714dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, 71528164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Hi->getLocStart(), 716dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::warn_case_value_overflow); 7171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7184f87062cb411d5a31cf39f1ac576bba4123930f2Richard Smith // If the RHS is not the same type as the condition, insert an implicit 719dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // cast. 7204f87062cb411d5a31cf39f1ac576bba4123930f2Richard Smith // FIXME: In C++11, the value is a converted constant expression of the 7214f87062cb411d5a31cf39f1ac576bba4123930f2Richard Smith // promoted type of the switch condition. 7224f87062cb411d5a31cf39f1ac576bba4123930f2Richard Smith Hi = DefaultLvalueConversion(Hi).take(); 723429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Hi = ImpCastExprToType(Hi, CondType, CK_IntegralCast).take(); 724dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CR->setRHS(Hi); 7251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 726dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If the low value is bigger than the high value, the case is empty. 7270fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (LoVal > HiVal) { 728dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range) 729dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor << SourceRange(CR->getLHS()->getLocStart(), 73028164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Hi->getLocEnd()); 731dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseRanges.erase(CaseRanges.begin()+i); 732dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor --i, --e; 733dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor continue; 734dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 7350fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 7360fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (ShouldCheckConstantCond && 7370fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall LoVal <= ConstantCondValue && 7380fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ConstantCondValue <= HiVal) 7390fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ShouldCheckConstantCond = false; 7400fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 741dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HiVals.push_back(HiVal); 7420471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 744dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Rescan the ranges, looking for overlap with singleton values and other 745dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // ranges. Since the range list is sorted, we only need to compare case 746dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // ranges with their neighbors. 747dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 748dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt &CRLo = CaseRanges[i].first; 749dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt &CRHi = HiVals[i]; 750dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CR = CaseRanges[i].second; 7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 752dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Check to see whether the case range overlaps with any 753dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // singleton cases. 754dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *OverlapStmt = 0; 755dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt OverlapVal(32); 7561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 757dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Find the smallest value >= the lower bound. If I is in the 758dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // case range, then we have overlap. 759dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), 760dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseVals.end(), CRLo, 761dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseCompareFunctor()); 762dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (I != CaseVals.end() && I->first < CRHi) { 763dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = I->first; // Found overlap with scalar. 764dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = I->second; 765dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 7661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 767dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Find the smallest value bigger than the upper bound. 768dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); 769dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (I != CaseVals.begin() && (I-1)->first >= CRLo) { 770dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = (I-1)->first; // Found overlap with scalar. 771dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = (I-1)->second; 772dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 774dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Check to see if this case stmt overlaps with the subsequent 775dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // case range. 776dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (i && CRLo <= HiVals[i-1]) { 777dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = HiVals[i-1]; // Found overlap with range. 778dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = CaseRanges[i-1].second; 779dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 7801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 781dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (OverlapStmt) { 782dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If we have a duplicate, report it. 783dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case) 784dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor << OverlapVal.toString(10); 7851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(OverlapStmt->getLHS()->getLocStart(), 786dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::note_duplicate_case_prev); 787390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: We really want to remove the bogus case stmt from the 788390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // substmt, but we have no way to do this right now. 789dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseListIsErroneous = true; 790dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 7910471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 792b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 793ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 7940fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Complain if we have a constant condition and we didn't find a match. 7950fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (!CaseListIsErroneous && ShouldCheckConstantCond) { 7960fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // TODO: it would be nice if we printed enums as enums, chars as 7970fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // chars, etc. 7980fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition) 7990fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall << ConstantCondValue.toString(10) 8000fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall << CondExpr->getSourceRange(); 8010fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall } 8020fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 8030fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Check to see if switch is over an Enum and handles all of its 804559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // values. We only issue a warning if there is not 'default:', but 805559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // we still do the analysis to preserve this information in the AST 806559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // (which can be used by flow-based analyes). 8070fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // 808ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner const EnumType *ET = CondTypeBeforePromotion->getAs<EnumType>(); 809559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 810ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor // If switch has default case, then ignore it. 811559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (!CaseListIsErroneous && !HasConstantCond && ET) { 812ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const EnumDecl *ED = ET->getDecl(); 8135f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> 81458f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet EnumValsTy; 815ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor EnumValsTy EnumVals; 816ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 8170fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Gather all enum values, set their type and sort them, 8180fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // allowing easier comparison with CaseVals. 8190fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin(); 82028164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif EDI != ED->enumerator_end(); ++EDI) { 82128164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif llvm::APSInt Val = EDI->getInitVal(); 82228164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif AdjustAPSInt(Val, CondWidth, CondIsSigned); 82328164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif EnumVals.push_back(std::make_pair(Val, *EDI)); 824ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 825ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); 8260fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall EnumValsTy::iterator EIend = 8270fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); 828559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 829559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // See which case values aren't in enum. 830559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // TODO: we might want to check whether case values are out of the 831559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // enum even if we don't want to check whether all cases are handled. 832559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (!TheDefaultStmt) { 83347bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek EnumValsTy::const_iterator EI = EnumVals.begin(); 83447bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek for (CaseValsTy::const_iterator CI = CaseVals.begin(); 8350fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall CI != CaseVals.end(); CI++) { 83647bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek while (EI != EIend && EI->first < CI->first) 83747bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek EI++; 83847bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek if (EI == EIend || EI->first > CI->first) 8390fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Diag(CI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum) 8400fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall << ED->getDeclName(); 84147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 84247bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek // See which of case ranges aren't in enum 84347bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek EI = EnumVals.begin(); 84447bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek for (CaseRangesTy::const_iterator RI = CaseRanges.begin(); 8450fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall RI != CaseRanges.end() && EI != EIend; RI++) { 84647bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek while (EI != EIend && EI->first < RI->first) 84747bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek EI++; 848dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 84947bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek if (EI == EIend || EI->first != RI->first) { 85047bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek Diag(RI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum) 85147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek << ED->getDeclName(); 85247bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 853e0ba9d1beeba01a96808c2fc61f9ca89acec313bTed Kremenek 854a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt Hi = 855a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith RI->second->getRHS()->EvaluateKnownConstInt(Context); 85628164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif AdjustAPSInt(Hi, CondWidth, CondIsSigned); 85747bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek while (EI != EIend && EI->first < Hi) 85847bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek EI++; 85947bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek if (EI == EIend || EI->first != Hi) 86047bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek Diag(RI->second->getRHS()->getExprLoc(), diag::warn_not_in_enum) 86147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek << ED->getDeclName(); 86247bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 863ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 864dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 865559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // Check which enum vals aren't in switch 866ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseValsTy::const_iterator CI = CaseVals.begin(); 867ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseRangesTy::const_iterator RI = CaseRanges.begin(); 868559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek bool hasCasesNotInSwitch = false; 869559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 8705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<DeclarationName,8> UnhandledNames; 871dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 872559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek for (EnumValsTy::const_iterator EI = EnumVals.begin(); EI != EIend; EI++){ 873ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner // Drop unneeded case values 874ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor llvm::APSInt CIVal; 875ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor while (CI != CaseVals.end() && CI->first < EI->first) 876ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CI++; 877dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 878ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor if (CI != CaseVals.end() && CI->first == EI->first) 879ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor continue; 880ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 881559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // Drop unneeded case ranges 882ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor for (; RI != CaseRanges.end(); RI++) { 883a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt Hi = 884a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith RI->second->getRHS()->EvaluateKnownConstInt(Context); 88528164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif AdjustAPSInt(Hi, CondWidth, CondIsSigned); 886ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor if (EI->first <= Hi) 887ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor break; 888ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 889ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 890559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (RI == CaseRanges.end() || EI->first < RI->first) { 89147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek hasCasesNotInSwitch = true; 89247bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek if (!TheDefaultStmt) 893ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner UnhandledNames.push_back(EI->second->getDeclName()); 89447bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 895ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 896dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 897ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner // Produce a nice diagnostic if multiple values aren't handled. 898ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner switch (UnhandledNames.size()) { 899ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 0: break; 900ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 1: 901ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner Diag(CondExpr->getExprLoc(), diag::warn_missing_case1) 902ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0]; 903ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 904ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 2: 905ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner Diag(CondExpr->getExprLoc(), diag::warn_missing_case2) 906ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1]; 907ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 908ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 3: 909ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner Diag(CondExpr->getExprLoc(), diag::warn_missing_case3) 910ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2]; 911ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 912ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner default: 913ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner Diag(CondExpr->getExprLoc(), diag::warn_missing_cases) 914ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << (unsigned)UnhandledNames.size() 915ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2]; 916ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 917ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner } 918559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 919559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (!hasCasesNotInSwitch) 92047bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek SS->setAllEnumCasesCovered(); 921ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 922b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 923dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 924390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: If the case list was broken is some way, we don't have a good system 925390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // to patch it up. Instead, just return the whole substmt as broken. 926b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner if (CaseListIsErroneous) 927de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return StmtError(); 928de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 929de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(SS); 9305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 9315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 93260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 933dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond, 9349ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Decl *CondVar, Stmt *Body) { 93560d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult(Cond.release()); 936dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 9375656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor VarDecl *ConditionVar = 0; 938d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 939d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 940586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor CondResult = CheckConditionVariable(ConditionVar, WhileLoc, true); 94199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (CondResult.isInvalid()) 94299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 9435656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor } 9449ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *ConditionExpr = CondResult.take(); 94599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!ConditionExpr) 94699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 947dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 9489ae2f076ca5ab1feb3ba95629099ec2319833701John McCall DiagnoseUnusedExprResult(Body); 9491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 95043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor return Owned(new (Context) WhileStmt(Context, ConditionVar, ConditionExpr, 9519ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Body, WhileLoc)); 9525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 9535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 95460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 9559ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body, 956989135901c750af61ef012b6b0a0368be415bc46Chris Lattner SourceLocation WhileLoc, SourceLocation CondLParen, 9579ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *Cond, SourceLocation CondRParen) { 9589ae2f076ca5ab1feb3ba95629099ec2319833701John McCall assert(Cond && "ActOnDoStmt(): missing expression"); 959f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 960429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult CondResult = CheckBooleanCondition(Cond, DoLoc); 961429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (CondResult.isInvalid() || CondResult.isInvalid()) 9625a881bb09928b7ade891efc680088aaad276f8d6John McCall return StmtError(); 963429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Cond = CondResult.take(); 9645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 965b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(Cond, DoLoc); 966429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley CondResult = MaybeCreateExprWithCleanups(Cond); 9679ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 968586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 9699ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 970dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 9719ae2f076ca5ab1feb3ba95629099ec2319833701John McCall DiagnoseUnusedExprResult(Body); 972754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson 9739ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen)); 9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 97660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 977f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, 9789ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *First, FullExprArg second, Decl *secondVar, 97999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor FullExprArg third, 9809ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SourceLocation RParenLoc, Stmt *Body) { 9815921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (!getLangOptions().CPlusPlus) { 9825921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { 983f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // C99 6.8.5p3: The declaration part of a 'for' statement shall only 984f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // declare identifiers for objects having storage class 'auto' or 985f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // 'register'. 9865921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); 9875921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis DI!=DE; ++DI) { 9885921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis VarDecl *VD = dyn_cast<VarDecl>(*DI); 989b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall if (VD && VD->isLocalVarDecl() && !VD->hasLocalStorage()) 9905921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis VD = 0; 9915921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (VD == 0) 9925921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for); 9935921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // FIXME: mark decl erroneous! 9945921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis } 995ae3b701f59e78e058b83344be17206af3bf5d277Chris Lattner } 9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 99799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor 99860d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult SecondResult(second.release()); 99999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor VarDecl *ConditionVar = 0; 1000d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (secondVar) { 1001d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(secondVar); 1002586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor SecondResult = CheckConditionVariable(ConditionVar, ForLoc, true); 100399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (SecondResult.isInvalid()) 100499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 100599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor } 1006dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 100799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Expr *Third = third.release().takeAs<Expr>(); 1008dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 10093af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson DiagnoseUnusedExprResult(First); 10103af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson DiagnoseUnusedExprResult(Third); 1011754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(Body); 1012754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson 1013dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi return Owned(new (Context) ForStmt(Context, First, 1014dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi SecondResult.take(), ConditionVar, 1015dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi Third, Body, ForLoc, LParenLoc, 101643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor RParenLoc)); 10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1019f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// In an Objective C collection iteration statement: 1020f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// for (x in y) 1021f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// x can be an arbitrary l-value expression. Bind it up as a 1022f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// full-expression. 1023f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallStmtResult Sema::ActOnForEachLValueExpr(Expr *E) { 1024f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall CheckImplicitConversions(E); 10254765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall ExprResult Result = MaybeCreateExprWithCleanups(E); 1026f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall if (Result.isInvalid()) return StmtError(); 1027f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall return Owned(static_cast<Stmt*>(Result.get())); 1028f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall} 1029f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall 1030990567cb60e8530ba01b41d4e056e32b44b95ec0John McCallExprResult 1031990567cb60e8530ba01b41d4e056e32b44b95ec0John McCallSema::ActOnObjCForCollectionOperand(SourceLocation forLoc, Expr *collection) { 1032990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall assert(collection); 1033990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1034990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Bail out early if we've got a type-dependent expression. 1035990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (collection->isTypeDependent()) return Owned(collection); 1036990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1037990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Perform normal l-value conversion. 1038990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall ExprResult result = DefaultFunctionArrayLvalueConversion(collection); 1039990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (result.isInvalid()) 1040990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall return ExprError(); 1041990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall collection = result.take(); 1042990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1043990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // The operand needs to have object-pointer type. 1044990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // TODO: should we do a contextual conversion? 1045990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall const ObjCObjectPointerType *pointerType = 1046990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall collection->getType()->getAs<ObjCObjectPointerType>(); 1047990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (!pointerType) 1048990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall return Diag(forLoc, diag::err_collection_expr_type) 1049990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall << collection->getType() << collection->getSourceRange(); 1050990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1051990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Check that the operand provides 1052990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // - countByEnumeratingWithState:objects:count: 1053990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall const ObjCObjectType *objectType = pointerType->getObjectType(); 1054990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall ObjCInterfaceDecl *iface = objectType->getInterface(); 1055990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1056990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // If we have a forward-declared type, we can't do this check. 1057b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor // Under ARC, it is an error not to have a forward-declared class. 1058b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor if (iface && 1059b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor RequireCompleteType(forLoc, QualType(objectType, 0), 1060b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor getLangOptions().ObjCAutoRefCount 1061b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor ? PDiag(diag::err_arc_collection_forward) 1062b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor << collection->getSourceRange() 1063b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor : PDiag(0))) { 1064990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Otherwise, if we have any useful type information, check that 1065990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // the type declares the appropriate method. 1066990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } else if (iface || !objectType->qual_empty()) { 1067990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall IdentifierInfo *selectorIdents[] = { 1068990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall &Context.Idents.get("countByEnumeratingWithState"), 1069990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall &Context.Idents.get("objects"), 1070990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall &Context.Idents.get("count") 1071990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall }; 1072990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Selector selector = Context.Selectors.getSelector(3, &selectorIdents[0]); 1073990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1074990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall ObjCMethodDecl *method = 0; 1075990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1076990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // If there's an interface, look in both the public and private APIs. 1077990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (iface) { 1078990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall method = iface->lookupInstanceMethod(selector); 1079990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (!method) method = LookupPrivateInstanceMethod(selector, iface); 1080990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 1081990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1082990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Also check protocol qualifiers. 1083990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (!method) 1084990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall method = LookupMethodInQualifiedType(selector, pointerType, 1085990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall /*instance*/ true); 1086990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1087990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // If we didn't find it anywhere, give up. 1088990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (!method) { 1089990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Diag(forLoc, diag::warn_collection_expr_type) 1090990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall << collection->getType() << selector << collection->getSourceRange(); 1091990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 1092990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1093990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // TODO: check for an incompatible signature? 1094990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 1095990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1096990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Wrap up any cleanups in the expression. 1097990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall return Owned(MaybeCreateExprWithCleanups(collection)); 1098990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall} 1099990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 110060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1101f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, 1102f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl SourceLocation LParenLoc, 11039ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *First, Expr *Second, 11049ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SourceLocation RParenLoc, Stmt *Body) { 110520552d2842245692b649e0d25380670922f954a2Fariborz Jahanian if (First) { 110620552d2842245692b649e0d25380670922f954a2Fariborz Jahanian QualType FirstType; 110720552d2842245692b649e0d25380670922f954a2Fariborz Jahanian if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { 11087e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner if (!DS->isSingleDecl()) 1109f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag((*DS->decl_begin())->getLocation(), 1110f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_toomany_element_decls)); 1111f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 1112f85e193739c953358c865005855253af4f68a497John McCall VarDecl *D = cast<VarDecl>(DS->getSingleDecl()); 1113f85e193739c953358c865005855253af4f68a497John McCall FirstType = D->getType(); 1114f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // C99 6.8.5p3: The declaration part of a 'for' statement shall only 1115f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // declare identifiers for objects having storage class 'auto' or 1116f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // 'register'. 1117f85e193739c953358c865005855253af4f68a497John McCall if (!D->hasLocalStorage()) 1118f85e193739c953358c865005855253af4f68a497John McCall return StmtError(Diag(D->getLocation(), 1119f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_non_variable_decl_in_for)); 11201fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson } else { 1121c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor Expr *FirstE = cast<Expr>(First); 11227eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall if (!FirstE->isTypeDependent() && !FirstE->isLValue()) 1123f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag(First->getLocStart(), 1124f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_selector_element_not_lvalue) 1125f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl << First->getSourceRange()); 11261fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson 11271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FirstType = static_cast<Expr*>(First)->getType(); 11281fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson } 1129c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor if (!FirstType->isDependentType() && 1130c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor !FirstType->isObjCObjectPointerType() && 1131a5e42a82ce055f29f3733f3a1f10da6cb9877deeFariborz Jahanian !FirstType->isBlockPointerType()) 1132dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner Diag(ForLoc, diag::err_selector_element_type) 1133d162584991885ab004a02573a73ce06422b921fcChris Lattner << FirstType << First->getSourceRange(); 11343ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian } 1135990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 11368189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body, 11378189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek ForLoc, RParenLoc)); 11383ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian} 11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1140ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithnamespace { 1141ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1142ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithenum BeginEndFunction { 1143ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_begin, 1144ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_end 1145ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}; 1146ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1147ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Build a variable declaration for a for-range statement. 1148ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstatic VarDecl *BuildForRangeVarDecl(Sema &SemaRef, SourceLocation Loc, 1149ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType Type, const char *Name) { 1150ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclContext *DC = SemaRef.CurContext; 1151ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1152ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1153ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *Decl = VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, 1154ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith TInfo, SC_Auto, SC_None); 1155b403d6d746239095a2c7bac958c924d92434e2b4Richard Smith Decl->setImplicit(); 1156ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return Decl; 1157ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1158ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1159ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Finish building a variable declaration for a for-range statement. 1160ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// \return true if an error occurs. 1161ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstatic bool FinishForRangeVarDecl(Sema &SemaRef, VarDecl *Decl, Expr *Init, 1162ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation Loc, int diag) { 1163ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Deduce the type for the iterator variable now rather than leaving it to 1164ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // AddInitializerToDecl, so we can produce a more suitable diagnostic. 1165ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith TypeSourceInfo *InitTSI = 0; 1166ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (Init->getType()->isVoidType() || 1167ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith !SemaRef.DeduceAutoType(Decl->getTypeSourceInfo(), Init, InitTSI)) 1168ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.Diag(Loc, diag) << Init->getType(); 1169ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!InitTSI) { 1170ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setInvalidDecl(); 1171ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return true; 1172ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1173ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setTypeSourceInfo(InitTSI); 1174ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setType(InitTSI->getType()); 1175ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1176f85e193739c953358c865005855253af4f68a497John McCall // In ARC, infer lifetime. 1177f85e193739c953358c865005855253af4f68a497John McCall // FIXME: ARC may want to turn this into 'const __unsafe_unretained' if 1178f85e193739c953358c865005855253af4f68a497John McCall // we're doing the equivalent of fast iteration. 1179f85e193739c953358c865005855253af4f68a497John McCall if (SemaRef.getLangOptions().ObjCAutoRefCount && 1180f85e193739c953358c865005855253af4f68a497John McCall SemaRef.inferObjCARCLifetime(Decl)) 1181f85e193739c953358c865005855253af4f68a497John McCall Decl->setInvalidDecl(); 1182f85e193739c953358c865005855253af4f68a497John McCall 1183ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.AddInitializerToDecl(Decl, Init, /*DirectInit=*/false, 1184ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*TypeMayContainAuto=*/false); 1185ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.FinalizeDeclaration(Decl); 1186b403d6d746239095a2c7bac958c924d92434e2b4Richard Smith SemaRef.CurContext->addHiddenDecl(Decl); 1187ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return false; 1188ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1189ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1190ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Produce a note indicating which begin/end function was implicitly called 1191ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// by a C++0x for-range statement. This is often not obvious from the code, 1192ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// nor from the diagnostics produced when analysing the implicit expressions 1193ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// required in a for-range statement. 1194ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid NoteForRangeBeginEndFunction(Sema &SemaRef, Expr *E, 1195ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginEndFunction BEF) { 1196ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith CallExpr *CE = dyn_cast<CallExpr>(E); 1197ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!CE) 1198ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return; 1199ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith FunctionDecl *D = dyn_cast<FunctionDecl>(CE->getCalleeDecl()); 1200ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!D) 1201ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return; 1202ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation Loc = D->getLocation(); 1203ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1204ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith std::string Description; 1205ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith bool IsTemplate = false; 1206ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FunctionTemplateDecl *FunTmpl = D->getPrimaryTemplate()) { 1207ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Description = SemaRef.getTemplateArgumentBindingsText( 1208ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith FunTmpl->getTemplateParameters(), *D->getTemplateSpecializationArgs()); 1209ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IsTemplate = true; 1210ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1211ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1212ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.Diag(Loc, diag::note_for_range_begin_end) 1213ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << BEF << IsTemplate << Description << E->getType(); 1214ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1215ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1216ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Build a call to 'begin' or 'end' for a C++0x for-range statement. If the 1217ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// given LookupResult is non-empty, it is assumed to describe a member which 1218ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// will be invoked. Otherwise, the function will be found via argument 1219ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// dependent lookup. 1220ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstatic ExprResult BuildForRangeBeginEndCall(Sema &SemaRef, Scope *S, 1221ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation Loc, 1222ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *Decl, 1223ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginEndFunction BEF, 1224ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith const DeclarationNameInfo &NameInfo, 1225ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupResult &MemberLookup, 1226ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Expr *Range) { 1227ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult CallExpr; 1228ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!MemberLookup.empty()) { 1229ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult MemberRef = 1230ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.BuildMemberReferenceExpr(Range, Range->getType(), Loc, 1231ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*IsPtr=*/false, CXXScopeSpec(), 1232ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*Qualifier=*/0, MemberLookup, 1233ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*TemplateArgs=*/0); 1234ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (MemberRef.isInvalid()) 1235ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1236ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith CallExpr = SemaRef.ActOnCallExpr(S, MemberRef.get(), Loc, MultiExprArg(), 1237ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Loc, 0); 1238ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (CallExpr.isInvalid()) 1239ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1240ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } else { 1241ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith UnresolvedSet<0> FoundNames; 1242ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // C++0x [stmt.ranged]p1: For the purposes of this name lookup, namespace 1243ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // std is an associated namespace. 1244ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith UnresolvedLookupExpr *Fn = 1245ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith UnresolvedLookupExpr::Create(SemaRef.Context, /*NamingClass=*/0, 1246ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NestedNameSpecifierLoc(), NameInfo, 1247ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*NeedsADL=*/true, /*Overloaded=*/false, 1248ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith FoundNames.begin(), FoundNames.end(), 1249ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*LookInStdNamespace=*/true); 1250ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith CallExpr = SemaRef.BuildOverloadedCallExpr(S, Fn, Fn, Loc, &Range, 1, Loc, 1251ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 0); 1252ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (CallExpr.isInvalid()) { 1253ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.Diag(Range->getLocStart(), diag::note_for_range_type) 1254ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << Range->getType(); 1255ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1256ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1257ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1258ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(SemaRef, Decl, CallExpr.get(), Loc, 1259ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_iter_deduction_failure)) { 1260ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(SemaRef, CallExpr.get(), BEF); 1261ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1262ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1263ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return CallExpr; 1264ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1265ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1266ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1267ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1268ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// ActOnCXXForRangeStmt - Check and build a C++0x for-range statement. 1269ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1270ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// C++0x [stmt.ranged]: 1271ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// A range-based for statement is equivalent to 1272ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1273ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// { 1274ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// auto && __range = range-init; 1275ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// for ( auto __begin = begin-expr, 1276ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// __end = end-expr; 1277ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// __begin != __end; 1278ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// ++__begin ) { 1279ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// for-range-declaration = *__begin; 1280ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// statement 1281ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// } 1282ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// } 1283ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1284ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// The body of the loop is not available yet, since it cannot be analysed until 1285ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// we have determined the type of the for-range-declaration. 1286ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult 1287ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithSema::ActOnCXXForRangeStmt(SourceLocation ForLoc, SourceLocation LParenLoc, 1288ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Stmt *First, SourceLocation ColonLoc, Expr *Range, 1289ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RParenLoc) { 1290ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!First || !Range) 1291ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1292ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1293ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *DS = dyn_cast<DeclStmt>(First); 1294ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith assert(DS && "first part of for range not a decl stmt"); 1295ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1296ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!DS->isSingleDecl()) { 1297ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Diag(DS->getStartLoc(), diag::err_type_defined_in_for_range); 1298ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1299ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1300ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DS->getSingleDecl()->isInvalidDecl()) 1301ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1302ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1303ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DiagnoseUnexpandedParameterPack(Range, UPPC_Expression)) 1304ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1305ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1306ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build auto && __range = range-init 1307ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RangeLoc = Range->getLocStart(); 1308ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *RangeVar = BuildForRangeVarDecl(*this, RangeLoc, 1309ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Context.getAutoRRefDeductType(), 1310ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__range"); 1311ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(*this, RangeVar, Range, RangeLoc, 1312ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_deduction_failure)) 1313ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1314ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1315ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Claim the type doesn't contain auto: we've already done the checking. 1316ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclGroupPtrTy RangeGroup = 1317ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BuildDeclaratorGroup((Decl**)&RangeVar, 1, /*TypeMayContainAuto=*/false); 1318ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith StmtResult RangeDecl = ActOnDeclStmt(RangeGroup, RangeLoc, RangeLoc); 1319ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (RangeDecl.isInvalid()) 1320ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1321ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1322ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return BuildCXXForRangeStmt(ForLoc, ColonLoc, RangeDecl.get(), 1323ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*BeginEndDecl=*/0, /*Cond=*/0, /*Inc=*/0, DS, 1324ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith RParenLoc); 1325ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1326ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1327ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// BuildCXXForRangeStmt - Build or instantiate a C++0x for-range statement. 1328ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult 1329ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithSema::BuildCXXForRangeStmt(SourceLocation ForLoc, SourceLocation ColonLoc, 1330ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Stmt *RangeDecl, Stmt *BeginEnd, Expr *Cond, 1331ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Expr *Inc, Stmt *LoopVarDecl, 1332ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RParenLoc) { 1333ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Scope *S = getCurScope(); 1334ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1335ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *RangeDS = cast<DeclStmt>(RangeDecl); 1336ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *RangeVar = cast<VarDecl>(RangeDS->getSingleDecl()); 1337ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType RangeVarType = RangeVar->getType(); 1338ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1339ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *LoopVarDS = cast<DeclStmt>(LoopVarDecl); 1340ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *LoopVar = cast<VarDecl>(LoopVarDS->getSingleDecl()); 1341ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1342ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith StmtResult BeginEndDecl = BeginEnd; 1343ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult NotEqExpr = Cond, IncrExpr = Inc; 1344ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1345ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!BeginEndDecl.get() && !RangeVarType->isDependentType()) { 1346ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RangeLoc = RangeVar->getLocation(); 1347ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1348e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek const QualType RangeVarNonRefType = RangeVarType.getNonReferenceType(); 1349e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1350e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek ExprResult BeginRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, 1351e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1352e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRangeRef.isInvalid()) 1353e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1354e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1355e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek ExprResult EndRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, 1356e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1357e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (EndRangeRef.isInvalid()) 1358ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1359ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1360ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType AutoType = Context.getAutoDeductType(); 1361ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Expr *Range = RangeVar->getInit(); 1362ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Range) 1363ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1364ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType RangeType = Range->getType(); 1365ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1366ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (RequireCompleteType(RangeLoc, RangeType, 1367ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith PDiag(diag::err_for_range_incomplete_type))) 1368ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1369ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1370ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build auto __begin = begin-expr, __end = end-expr. 1371ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *BeginVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, 1372ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__begin"); 1373ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *EndVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, 1374ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__end"); 1375ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1376ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build begin-expr and end-expr and attach to __begin and __end variables. 1377ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult BeginExpr, EndExpr; 1378ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (const ArrayType *UnqAT = RangeType->getAsArrayTypeUnsafe()) { 1379ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // - if _RangeT is an array type, begin-expr and end-expr are __range and 1380ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // __range + __bound, respectively, where __bound is the array bound. If 1381ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // _RangeT is an array of unknown size or an array of incomplete type, 1382ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // the program is ill-formed; 1383ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1384ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // begin-expr is __range. 1385e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginExpr = BeginRangeRef; 1386e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (FinishForRangeVarDecl(*this, BeginVar, BeginRangeRef.get(), ColonLoc, 1387ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_iter_deduction_failure)) { 1388ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1389ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1390ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1391ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1392ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Find the array bound. 1393ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult BoundExpr; 1394ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(UnqAT)) 1395ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr = Owned(IntegerLiteral::Create(Context, CAT->getSize(), 13961dd986dff9ddfbec687975700770bb377988e9edRichard Trieu Context.getPointerDiffType(), 13971dd986dff9ddfbec687975700770bb377988e9edRichard Trieu RangeLoc)); 1398ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith else if (const VariableArrayType *VAT = 1399ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith dyn_cast<VariableArrayType>(UnqAT)) 1400ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr = VAT->getSizeExpr(); 1401ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith else { 1402ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Can't be a DependentSizedArrayType or an IncompleteArrayType since 1403ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // UnqAT is not incomplete and Range is not type-dependent. 1404b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Unexpected array type in for-range"); 1405ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1406ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1407ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // end-expr is __range + __bound. 1408e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek EndExpr = ActOnBinOp(S, ColonLoc, tok::plus, EndRangeRef.get(), 1409ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr.get()); 1410ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (EndExpr.isInvalid()) 1411ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1412ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(*this, EndVar, EndExpr.get(), ColonLoc, 1413ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_iter_deduction_failure)) { 1414ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1415ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1416ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1417ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } else { 1418ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclarationNameInfo BeginNameInfo(&PP.getIdentifierTable().get("begin"), 1419ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ColonLoc); 1420ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclarationNameInfo EndNameInfo(&PP.getIdentifierTable().get("end"), 1421ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ColonLoc); 1422ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1423ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupResult BeginMemberLookup(*this, BeginNameInfo, LookupMemberName); 1424ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupResult EndMemberLookup(*this, EndNameInfo, LookupMemberName); 1425ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1426ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (CXXRecordDecl *D = RangeType->getAsCXXRecordDecl()) { 1427ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // - if _RangeT is a class type, the unqualified-ids begin and end are 1428ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // looked up in the scope of class _RangeT as if by class member access 1429ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // lookup (3.4.5), and if either (or both) finds at least one 1430ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // declaration, begin-expr and end-expr are __range.begin() and 1431ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // __range.end(), respectively; 1432ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupQualifiedName(BeginMemberLookup, D); 1433ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupQualifiedName(EndMemberLookup, D); 1434ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1435ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (BeginMemberLookup.empty() != EndMemberLookup.empty()) { 1436ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Diag(ColonLoc, diag::err_for_range_member_begin_end_mismatch) 1437ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << RangeType << BeginMemberLookup.empty(); 1438ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1439ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1440ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } else { 1441ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // - otherwise, begin-expr and end-expr are begin(__range) and 1442ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // end(__range), respectively, where begin and end are looked up with 1443ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // argument-dependent lookup (3.4.2). For the purposes of this name 1444ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // lookup, namespace std is an associated namespace. 1445ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1446ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1447ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginExpr = BuildForRangeBeginEndCall(*this, S, ColonLoc, BeginVar, 1448ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_begin, BeginNameInfo, 1449e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginMemberLookup, 1450e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginRangeRef.get()); 1451ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (BeginExpr.isInvalid()) 1452ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1453ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1454ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EndExpr = BuildForRangeBeginEndCall(*this, S, ColonLoc, EndVar, 1455ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_end, EndNameInfo, 1456e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek EndMemberLookup, EndRangeRef.get()); 1457ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (EndExpr.isInvalid()) 1458ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1459ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1460ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1461ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // C++0x [decl.spec.auto]p6: BeginType and EndType must be the same. 1462ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType BeginType = BeginVar->getType(), EndType = EndVar->getType(); 1463ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Context.hasSameType(BeginType, EndType)) { 1464ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Diag(RangeLoc, diag::err_for_range_begin_end_types_differ) 1465ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << BeginType << EndType; 1466ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1467ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1468ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1469ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1470ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl *BeginEndDecls[] = { BeginVar, EndVar }; 1471ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Claim the type doesn't contain auto: we've already done the checking. 1472ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclGroupPtrTy BeginEndGroup = 1473ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BuildDeclaratorGroup(BeginEndDecls, 2, /*TypeMayContainAuto=*/false); 1474ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginEndDecl = ActOnDeclStmt(BeginEndGroup, ColonLoc, ColonLoc); 1475ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1476e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek const QualType BeginRefNonRefType = BeginType.getNonReferenceType(); 1477e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek ExprResult BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, 1478ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VK_LValue, ColonLoc); 1479e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRef.isInvalid()) 1480e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1481e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1482ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult EndRef = BuildDeclRefExpr(EndVar, EndType.getNonReferenceType(), 1483ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VK_LValue, ColonLoc); 1484e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (EndRef.isInvalid()) 1485e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1486ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1487ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check __begin != __end expression. 1488ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnBinOp(S, ColonLoc, tok::exclaimequal, 1489ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginRef.get(), EndRef.get()); 1490ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnBooleanCondition(S, ColonLoc, NotEqExpr.get()); 1491ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnFinishFullExpr(NotEqExpr.get()); 1492ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (NotEqExpr.isInvalid()) { 1493ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1494ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Context.hasSameType(BeginType, EndType)) 1495ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1496ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1497ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1498ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1499ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check ++__begin expression. 1500e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, 1501e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1502e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRef.isInvalid()) 1503e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1504e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1505ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IncrExpr = ActOnUnaryOp(S, ColonLoc, tok::plusplus, BeginRef.get()); 1506ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IncrExpr = ActOnFinishFullExpr(IncrExpr.get()); 1507ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (IncrExpr.isInvalid()) { 1508ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1509ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1510ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1511ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1512ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check *__begin expression. 1513e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, 1514e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1515e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRef.isInvalid()) 1516e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1517e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1518ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult DerefExpr = ActOnUnaryOp(S, ColonLoc, tok::star, BeginRef.get()); 1519ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DerefExpr.isInvalid()) { 1520ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1521ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1522ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1523ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1524ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Attach *__begin as initializer for VD. 1525ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!LoopVar->isInvalidDecl()) { 1526ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith AddInitializerToDecl(LoopVar, DerefExpr.get(), /*DirectInit=*/false, 1527ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*TypeMayContainAuto=*/true); 1528ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (LoopVar->isInvalidDecl()) 1529ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1530ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1531cd6f36693ed40cef8c8639c04438d865187c1f73Richard Smith } else { 1532cd6f36693ed40cef8c8639c04438d865187c1f73Richard Smith // The range is implicitly used as a placeholder when it is dependent. 1533cd6f36693ed40cef8c8639c04438d865187c1f73Richard Smith RangeVar->setUsed(); 1534ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1535ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1536ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return Owned(new (Context) CXXForRangeStmt(RangeDS, 1537ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith cast_or_null<DeclStmt>(BeginEndDecl.get()), 1538ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr.take(), IncrExpr.take(), 1539ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LoopVarDS, /*Body=*/0, ForLoc, 1540ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ColonLoc, RParenLoc)); 1541ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1542ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1543ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// FinishCXXForRangeStmt - Attach the body to a C++0x for-range statement. 1544ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// This is a separate step from ActOnCXXForRangeStmt because analysis of the 1545ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// body cannot be performed until after the type of the range variable is 1546ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// determined. 1547ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult Sema::FinishCXXForRangeStmt(Stmt *S, Stmt *B) { 1548ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!S || !B) 1549ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1550ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1551ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith cast<CXXForRangeStmt>(S)->setBody(B); 1552ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return S; 1553ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1554ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 155557ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerStmtResult Sema::ActOnGotoStmt(SourceLocation GotoLoc, 155657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner SourceLocation LabelLoc, 155757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner LabelDecl *TheDecl) { 155857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner getCurFunction()->setHasBranchIntoScope(); 1559ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner TheDecl->setUsed(); 1560ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner return Owned(new (Context) GotoStmt(TheDecl, GotoLoc, LabelLoc)); 15615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 156360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1564ad56d684259f706b7c0ae5ad9c23adb4f2926817Chris LattnerSema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc, 15659ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *E) { 1566bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman // Convert operand to void* 15675f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor if (!E->isTypeDependent()) { 15685f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor QualType ETy = E->getType(); 15692877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth QualType DestTy = Context.getPointerType(Context.VoidTy.withConst()); 1570429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult ExprRes = Owned(E); 15715f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor AssignConvertType ConvTy = 1572429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley CheckSingleAssignmentConstraints(DestTy, ExprRes); 1573429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (ExprRes.isInvalid()) 1574429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 1575429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley E = ExprRes.take(); 15762877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing)) 15775f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor return StmtError(); 15785f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor } 1579b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 1580781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasIndirectGoto(); 1581b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 15825f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E)); 15835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 158560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 15861b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { 15875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Scope *S = CurScope->getContinueParent(); 15885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!S) { 15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.6.2p1: A break shall appear only in or as a loop body. 15904cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop)); 15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15924cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 15938189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ContinueStmt(ContinueLoc)); 15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 159660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 15971b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { 15985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Scope *S = CurScope->getBreakParent(); 15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!S) { 16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. 16014cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch)); 16025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16034cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 16048189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) BreakStmt(BreakLoc)); 16055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 16065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1607dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Determine whether the given expression is a candidate for 1608f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// copy elision in either a return statement or a throw expression. 16095077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 1610f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param ReturnType If we're determining the copy elision candidate for 1611f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// a return statement, this is the return type of the function. If we're 1612f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// determining the copy elision candidate for a throw expression, this will 1613f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// be a NULL type. 16145077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 1615f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param E The expression being returned from the function or block, or 1616f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// being thrown. 16175077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 16184926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// \param AllowFunctionParameter Whether we allow function parameters to 16194926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// be considered NRVO candidates. C++ prohibits this for NRVO itself, but 16204926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// we re-use this logic to determine whether we should try to move as part of 16214926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// a return or throw (which does allow function parameters). 16225077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 16235077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \returns The NRVO candidate variable, if the return statement may use the 16245077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// NRVO, or NULL if there is no such candidate. 1625f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregorconst VarDecl *Sema::getCopyElisionCandidate(QualType ReturnType, 1626f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor Expr *E, 1627f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor bool AllowFunctionParameter) { 1628f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor QualType ExprType = E->getType(); 16293c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor // - in a return statement in a function with ... 16303c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor // ... a class return type ... 1631f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!ReturnType.isNull()) { 1632f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!ReturnType->isRecordType()) 1633f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor return 0; 1634f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor // ... the same cv-unqualified type as the function return type ... 1635f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!Context.hasSameUnqualifiedType(ReturnType, ExprType)) 1636f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor return 0; 1637f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor } 1638dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1639dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // ... the expression is the name of a non-volatile automatic object 1640f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor // (other than a function or catch-clause parameter)) ... 1641f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()); 16423c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor if (!DR) 16435077c3876beeaed32280af88244e8050078619a8Douglas Gregor return 0; 16443c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); 16453c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor if (!VD) 16465077c3876beeaed32280af88244e8050078619a8Douglas Gregor return 0; 1647dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 16481cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // ...object (other than a function or catch-clause parameter)... 16491cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->getKind() != Decl::Var && 16501cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall !(AllowFunctionParameter && VD->getKind() == Decl::ParmVar)) 16511cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall return 0; 16521cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->isExceptionVariable()) return 0; 16531cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 16541cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // ...automatic... 16551cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (!VD->hasLocalStorage()) return 0; 16561cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 16571cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // ...non-volatile... 16581cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->getType().isVolatileQualified()) return 0; 16591cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->getType()->isReferenceType()) return 0; 16601cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 16611cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // __block variables can't be allocated in a way that permits NRVO. 16621cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->hasAttr<BlocksAttr>()) return 0; 16631cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 16641cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // Variables with higher required alignment than their type's ABI 16651cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // alignment cannot use NRVO. 16661cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->hasAttr<AlignedAttr>() && 16671cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall Context.getDeclAlign(VD) > Context.getTypeAlignInChars(VD->getType())) 16681cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall return 0; 1669dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 16701cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall return VD; 16713c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor} 16723c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor 167307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor/// \brief Perform the initialization of a potentially-movable value, which 167407f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor/// is the result of return value. 1675cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// 1676cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// This routine implements C++0x [class.copy]p33, which attempts to treat 1677cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// returned lvalues as rvalues in certain cases (to prefer move construction), 1678cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// then falls back to treating them as lvalues if that failed. 1679dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiExprResult 168007f402cff25354c5f06f307f19b0c57c09d964bdDouglas GregorSema::PerformMoveOrCopyInitialization(const InitializedEntity &Entity, 168107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor const VarDecl *NRVOCandidate, 168207f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor QualType ResultType, 1683bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor Expr *Value, 1684bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor bool AllowNRVO) { 1685cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // C++0x [class.copy]p33: 1686dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // When the criteria for elision of a copy operation are met or would 1687dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // be met save for the fact that the source object is a function 1688dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // parameter, and the object to be copied is designated by an lvalue, 1689cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // overload resolution to select the constructor for the copy is first 1690cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // performed as if the object were designated by an rvalue. 1691cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor ExprResult Res = ExprError(); 1692bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor if (AllowNRVO && 1693bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor (NRVOCandidate || getCopyElisionCandidate(ResultType, Value, true))) { 1694dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi ImplicitCastExpr AsRvalue(ImplicitCastExpr::OnStack, 169507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value->getType(), CK_LValueToRValue, 169607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value, VK_XValue); 1697dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1698cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor Expr *InitExpr = &AsRvalue; 1699dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi InitializationKind Kind 170007f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor = InitializationKind::CreateCopy(Value->getLocStart(), 170107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value->getLocStart()); 170207f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor InitializationSequence Seq(*this, Entity, Kind, &InitExpr, 1); 1703dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1704dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // [...] If overload resolution fails, or if the type of the first 1705cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // parameter of the selected constructor is not an rvalue reference 17060099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi // to the object's type (possibly cv-qualified), overload resolution 1707cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // is performed again, considering the object as an lvalue. 1708383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl if (Seq) { 1709cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor for (InitializationSequence::step_iterator Step = Seq.step_begin(), 1710cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor StepEnd = Seq.step_end(); 1711cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor Step != StepEnd; ++Step) { 1712383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl if (Step->Kind != InitializationSequence::SK_ConstructorInitialization) 1713cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor continue; 1714dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1715dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi CXXConstructorDecl *Constructor 1716cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor = cast<CXXConstructorDecl>(Step->Function.Function); 1717dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1718cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor const RValueReferenceType *RRefType 171907f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor = Constructor->getParamDecl(0)->getType() 172007f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor ->getAs<RValueReferenceType>(); 1721dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1722cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // If we don't meet the criteria, break out now. 1723dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (!RRefType || 172407f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor !Context.hasSameUnqualifiedType(RRefType->getPointeeType(), 172507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Context.getTypeDeclType(Constructor->getParent()))) 1726cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor break; 1727dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1728cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Promote "AsRvalue" to the heap, since we now need this 1729cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // expression node to persist. 173007f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value = ImplicitCastExpr::Create(Context, Value->getType(), 1731dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi CK_LValueToRValue, Value, 0, 173207f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor VK_XValue); 1733dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1734cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Complete type-checking the initialization of the return type 1735cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // using the constructor we found. 173607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Res = Seq.Perform(*this, Entity, Kind, MultiExprArg(&Value, 1)); 1737cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 1738cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 1739cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 1740dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1741cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Either we didn't meet the criteria for treating an lvalue as an rvalue, 1742dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // above, or overload resolution failed. Either way, we need to try 1743cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // (again) now with the return value expression as written. 1744cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor if (Res.isInvalid()) 174507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Res = PerformCopyInitialization(Entity, SourceLocation(), Value); 1746dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1747cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor return Res; 1748cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor} 1749cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor 175027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// ActOnBlockReturnStmt - Utility routine to figure out block's return type. 17514eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff/// 175260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 17534eb206bebcdab28ababe8df55c6185cec2cdc071Steve NaroffSema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { 17544eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // If this is the first return we've seen in the block, infer the type of 17554eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // the block from it. 17569ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor BlockScopeInfo *CurBlock = getCurBlock(); 1757649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian if (CurBlock->TheDecl->blockMissingReturnType()) { 1758649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian QualType BlockReturnT; 1759c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff if (RetValExp) { 176016564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff // Don't call UsualUnaryConversions(), since we don't want to do 176116564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff // integer promotions here. 1762429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultFunctionArrayLvalueConversion(RetValExp); 1763429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 1764429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 1765429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley RetValExp = Result.take(); 17666a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor 17676a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor if (!RetValExp->isTypeDependent()) { 1768649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian BlockReturnT = RetValExp->getType(); 17696a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(RetValExp)) { 17706a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor // We have to remove a 'const' added to copied-in variable which was 17716a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor // part of the implementation spec. and not the actual qualifier for 17726a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor // the variable. 17736a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor if (CDRE->isConstQualAdded()) 17746a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor CurBlock->ReturnType.removeLocalConst(); // FIXME: local??? 17756a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor } 17766a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor } else 1777649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian BlockReturnT = Context.DependentTy; 1778c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff } else 1779649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian BlockReturnT = Context.VoidTy; 1780649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian if (!CurBlock->ReturnType.isNull() && !CurBlock->ReturnType->isDependentType() 1781649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian && !BlockReturnT->isDependentType() 1782649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian // when block's return type is not specified, all return types 1783649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian // must strictly match. 1784649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian && !Context.hasSameType(BlockReturnT, CurBlock->ReturnType)) { 1785649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian Diag(ReturnLoc, diag::err_typecheck_missing_return_type_incompatible) 1786649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian << BlockReturnT << CurBlock->ReturnType; 1787649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian return StmtError(); 1788649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian } 1789649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian CurBlock->ReturnType = BlockReturnT; 17904eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff } 17917d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian QualType FnRetType = CurBlock->ReturnType; 17924cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 1793711c52bb20d0c69063b52a99826fb7d2835501f1John McCall if (CurBlock->FunctionType->getAs<FunctionType>()->getNoReturnAttr()) { 179479430e9983f5e67a378fc1f50cd6278f2cea8259Eli Friedman Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr); 17956c92fa75e62937f9738696840efcb258560f4568Mike Stump return StmtError(); 17966c92fa75e62937f9738696840efcb258560f4568Mike Stump } 17976c92fa75e62937f9738696840efcb258560f4568Mike Stump 17984eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // Otherwise, verify that this result type matches the previous one. We are 17994eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // pickier with blocks than for normal functions because we don't have GCC 18004eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // compatibility to worry about here. 1801d963c37eb912342c8325048749e449861cf0a6e3John McCall const VarDecl *NRVOCandidate = 0; 18020a7efe1142d241678c91bf93ee6adb51289863a4John McCall if (FnRetType->isDependentType()) { 18030a7efe1142d241678c91bf93ee6adb51289863a4John McCall // Delay processing for now. TODO: there are lots of dependent 18040a7efe1142d241678c91bf93ee6adb51289863a4John McCall // types we can conclusively prove aren't void. 18050a7efe1142d241678c91bf93ee6adb51289863a4John McCall } else if (FnRetType->isVoidType()) { 18060a7efe1142d241678c91bf93ee6adb51289863a4John McCall if (RetValExp && 18070a7efe1142d241678c91bf93ee6adb51289863a4John McCall !(getLangOptions().CPlusPlus && 18080a7efe1142d241678c91bf93ee6adb51289863a4John McCall (RetValExp->isTypeDependent() || 18090a7efe1142d241678c91bf93ee6adb51289863a4John McCall RetValExp->getType()->isVoidType()))) { 18104eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff Diag(ReturnLoc, diag::err_return_block_has_expr); 18114eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff RetValExp = 0; 18124eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff } 18135077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else if (!RetValExp) { 18140a7efe1142d241678c91bf93ee6adb51289863a4John McCall return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr)); 18150a7efe1142d241678c91bf93ee6adb51289863a4John McCall } else if (!RetValExp->isTypeDependent()) { 18160a7efe1142d241678c91bf93ee6adb51289863a4John McCall // we have a non-void block with an expression, continue checking 18170a7efe1142d241678c91bf93ee6adb51289863a4John McCall 18180a7efe1142d241678c91bf93ee6adb51289863a4John McCall // C99 6.8.6.4p3(136): The return statement is not an assignment. The 18190a7efe1142d241678c91bf93ee6adb51289863a4John McCall // overlap restriction of subclause 6.5.16.1 does not apply to the case of 18200a7efe1142d241678c91bf93ee6adb51289863a4John McCall // function return. 18210a7efe1142d241678c91bf93ee6adb51289863a4John McCall 18220a7efe1142d241678c91bf93ee6adb51289863a4John McCall // In C++ the return statement is handled via a copy initialization. 18230a7efe1142d241678c91bf93ee6adb51289863a4John McCall // the C version of which boils down to CheckSingleAssignmentConstraints. 18240a7efe1142d241678c91bf93ee6adb51289863a4John McCall NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); 18250a7efe1142d241678c91bf93ee6adb51289863a4John McCall InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, 18260a7efe1142d241678c91bf93ee6adb51289863a4John McCall FnRetType, 18270586520acb2f368c874943353a222be7f00c3068Fariborz Jahanian NRVOCandidate != 0); 18280a7efe1142d241678c91bf93ee6adb51289863a4John McCall ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, 18290a7efe1142d241678c91bf93ee6adb51289863a4John McCall FnRetType, RetValExp); 18300a7efe1142d241678c91bf93ee6adb51289863a4John McCall if (Res.isInvalid()) { 18310a7efe1142d241678c91bf93ee6adb51289863a4John McCall // FIXME: Cleanup temporaries here, anyway? 18320a7efe1142d241678c91bf93ee6adb51289863a4John McCall return StmtError(); 1833c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson } 18340a7efe1142d241678c91bf93ee6adb51289863a4John McCall RetValExp = Res.take(); 18350a7efe1142d241678c91bf93ee6adb51289863a4John McCall CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); 1836d963c37eb912342c8325048749e449861cf0a6e3John McCall } 1837dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1838d963c37eb912342c8325048749e449861cf0a6e3John McCall if (RetValExp) { 1839d963c37eb912342c8325048749e449861cf0a6e3John McCall CheckImplicitConversions(RetValExp, ReturnLoc); 1840d963c37eb912342c8325048749e449861cf0a6e3John McCall RetValExp = MaybeCreateExprWithCleanups(RetValExp); 184198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump } 18420a7efe1142d241678c91bf93ee6adb51289863a4John McCall ReturnStmt *Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 18430a7efe1142d241678c91bf93ee6adb51289863a4John McCall NRVOCandidate); 18444cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 1845dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // If we need to check for the named return value optimization, save the 18465077c3876beeaed32280af88244e8050078619a8Douglas Gregor // return statement in our scope for later processing. 18476a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor if (getLangOptions().CPlusPlus && FnRetType->isRecordType() && 18485077c3876beeaed32280af88244e8050078619a8Douglas Gregor !CurContext->isDependentContext()) 18495077c3876beeaed32280af88244e8050078619a8Douglas Gregor FunctionScopes.back()->Returns.push_back(Result); 1850dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 18515077c3876beeaed32280af88244e8050078619a8Douglas Gregor return Owned(Result); 18524eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff} 18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 185460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 18559ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { 1856fc92137eee708b632c00a9b0934ff87aeae234a5Douglas Gregor // Check for unexpanded parameter packs. 1857fc92137eee708b632c00a9b0934ff87aeae234a5Douglas Gregor if (RetValExp && DiagnoseUnexpandedParameterPack(RetValExp)) 1858fc92137eee708b632c00a9b0934ff87aeae234a5Douglas Gregor return StmtError(); 1859fc92137eee708b632c00a9b0934ff87aeae234a5Douglas Gregor 18609ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor if (getCurBlock()) 18614eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff return ActOnBlockReturnStmt(ReturnLoc, RetValExp); 18624cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 1863371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner QualType FnRetType; 1864926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor QualType DeclaredRetType; 1865f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump if (const FunctionDecl *FD = getCurFunctionDecl()) { 1866371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner FnRetType = FD->getResultType(); 1867926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor DeclaredRetType = FnRetType; 186804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall if (FD->hasAttr<NoReturnAttr>() || 186904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall FD->getType()->getAs<FunctionType>()->getNoReturnAttr()) 18708662587fa75d3fb04f873e265841c9314c7f5523Chris Lattner Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr) 187179430e9983f5e67a378fc1f50cd6278f2cea8259Eli Friedman << FD->getDeclName(); 1872926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } else if (ObjCMethodDecl *MD = getCurMethodDecl()) { 1873926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor DeclaredRetType = MD->getResultType(); 1874926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (MD->hasRelatedResultType() && MD->getClassInterface()) { 1875926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // In the implementation of a method with a related return type, the 1876926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // type used to type-check the validity of return statements within the 1877926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // method body is a pointer to the type of the class being implemented. 1878926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor FnRetType = Context.getObjCInterfaceType(MD->getClassInterface()); 1879926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor FnRetType = Context.getObjCObjectPointerType(FnRetType); 1880926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } else { 1881926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor FnRetType = DeclaredRetType; 1882926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } 1883926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } else // If we don't have a function/method context, bail. 1884c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff return StmtError(); 18851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18865077c3876beeaed32280af88244e8050078619a8Douglas Gregor ReturnStmt *Result = 0; 18875cf216b7fa64b933b60743b0b26053e8e7aa87beChris Lattner if (FnRetType->isVoidType()) { 18888d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (RetValExp) { 18898d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (!RetValExp->isTypeDependent()) { 18908d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky // C99 6.8.6.4p1 (ext_ since GCC warns) 18918d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky unsigned D = diag::ext_return_has_expr; 18928d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (RetValExp->getType()->isVoidType()) 18938d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky D = diag::ext_return_has_void_expr; 18948d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky else { 18958d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky ExprResult Result = Owned(RetValExp); 18968d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky Result = IgnoredValueConversions(Result.take()); 18978d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (Result.isInvalid()) 18988d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky return StmtError(); 18998d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky RetValExp = Result.take(); 19008d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky RetValExp = ImpCastExprToType(RetValExp, 19018d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky Context.VoidTy, CK_ToVoid).take(); 19028d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky } 19034cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 19048d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky // return (some void expression); is legal in C++. 19058d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (D != diag::ext_return_has_void_expr || 19068d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky !getLangOptions().CPlusPlus) { 19078d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); 1908ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth 1909ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth int FunctionKind = 0; 1910ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth if (isa<ObjCMethodDecl>(CurDecl)) 1911ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth FunctionKind = 1; 1912ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth else if (isa<CXXConstructorDecl>(CurDecl)) 1913ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth FunctionKind = 2; 1914ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth else if (isa<CXXDestructorDecl>(CurDecl)) 1915ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth FunctionKind = 3; 1916ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth 19178d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky Diag(ReturnLoc, D) 1918ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth << CurDecl->getDeclName() << FunctionKind 19198d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky << RetValExp->getSourceRange(); 19208d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky } 1921e878eb035b343d7d819c092102364ec9849716aeChris Lattner } 19221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1923b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(RetValExp, ReturnLoc); 19244765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall RetValExp = MaybeCreateExprWithCleanups(RetValExp); 19255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1926dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 19275077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0); 19285077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else if (!RetValExp && !FnRetType->isDependentType()) { 19293c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4 19303c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner // C99 6.8.6.4p1 (ext_ since GCC warns) 19313c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr; 19323c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner 19333c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner if (FunctionDecl *FD = getCurFunctionDecl()) 193408631c5fa053867146b5ee8be658c229f6bf127cChris Lattner Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; 19353c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner else 193608631c5fa053867146b5ee8be658c229f6bf127cChris Lattner Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; 19375077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc); 19385077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else { 19395077c3876beeaed32280af88244e8050078619a8Douglas Gregor const VarDecl *NRVOCandidate = 0; 19405077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { 19415077c3876beeaed32280af88244e8050078619a8Douglas Gregor // we have a non-void function with an expression, continue checking 19425077c3876beeaed32280af88244e8050078619a8Douglas Gregor 19435077c3876beeaed32280af88244e8050078619a8Douglas Gregor // C99 6.8.6.4p3(136): The return statement is not an assignment. The 19445077c3876beeaed32280af88244e8050078619a8Douglas Gregor // overlap restriction of subclause 6.5.16.1 does not apply to the case of 19455077c3876beeaed32280af88244e8050078619a8Douglas Gregor // function return. 19465077c3876beeaed32280af88244e8050078619a8Douglas Gregor 1947856d3798af7c2f7251e4a295f3da7a09ce4c62abJohn McCall // In C++ the return statement is handled via a copy initialization, 19485077c3876beeaed32280af88244e8050078619a8Douglas Gregor // the C version of which boils down to CheckSingleAssignmentConstraints. 1949f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); 1950dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, 195107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor FnRetType, 195258f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet NRVOCandidate != 0); 1953dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, 195407f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor FnRetType, RetValExp); 19555077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (Res.isInvalid()) { 19565077c3876beeaed32280af88244e8050078619a8Douglas Gregor // FIXME: Cleanup temporaries here, anyway? 19575077c3876beeaed32280af88244e8050078619a8Douglas Gregor return StmtError(); 19585077c3876beeaed32280af88244e8050078619a8Douglas Gregor } 19594cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 19605077c3876beeaed32280af88244e8050078619a8Douglas Gregor RetValExp = Res.takeAs<Expr>(); 1961dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (RetValExp) 19625077c3876beeaed32280af88244e8050078619a8Douglas Gregor CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); 196366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor } 1964dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1965b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall if (RetValExp) { 1966926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // If we type-checked an Objective-C method's return type based 1967926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // on a related return type, we may need to adjust the return 1968926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // type again. Do so now. 1969926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (DeclaredRetType != FnRetType) { 1970926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ExprResult result = PerformImplicitConversion(RetValExp, 1971926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor DeclaredRetType, 1972926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor AA_Returning); 1973926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (result.isInvalid()) return StmtError(); 1974926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor RetValExp = result.take(); 1975926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } 1976926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 1977b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(RetValExp, ReturnLoc); 19784765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall RetValExp = MaybeCreateExprWithCleanups(RetValExp); 1979b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall } 19805077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate); 1981898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor } 1982dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1983dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // If we need to check for the named return value optimization, save the 19845077c3876beeaed32280af88244e8050078619a8Douglas Gregor // return statement in our scope for later processing. 19855077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (getLangOptions().CPlusPlus && FnRetType->isRecordType() && 19865077c3876beeaed32280af88244e8050078619a8Douglas Gregor !CurContext->isDependentContext()) 19875077c3876beeaed32280af88244e8050078619a8Douglas Gregor FunctionScopes.back()->Returns.push_back(Result); 1988f85e193739c953358c865005855253af4f68a497John McCall 19895077c3876beeaed32280af88244e8050078619a8Douglas Gregor return Owned(Result); 19905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 19915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1992810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently 1993810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// ignore "noop" casts in places where an lvalue is required by an inline asm. 1994810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// We emulate this behavior when -fheinous-gnu-extensions is specified, but 1995810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// provide a strong guidance to not use it. 1996810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// 1997810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// This method checks to see if the argument is an acceptable l-value and 1998810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// returns false if it is a case we can handle. 1999810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattnerstatic bool CheckAsmLValue(const Expr *E, Sema &S) { 2000703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson // Type dependent expressions will be checked during instantiation. 2001703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson if (E->isTypeDependent()) 2002703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson return false; 2003dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 20047eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall if (E->isLValue()) 2005810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return false; // Cool, this is an lvalue. 2006810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 2007810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // Okay, this is not an lvalue, but perhaps it is the result of a cast that we 2008810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // are supposed to allow. 2009810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); 20107eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall if (E != E2 && E2->isLValue()) { 2011810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (!S.getLangOptions().HeinousExtensions) 2012810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue) 2013810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner << E->getSourceRange(); 2014810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner else 2015810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue) 2016810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner << E->getSourceRange(); 2017810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // Accept, even if we emitted an error diagnostic. 2018810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return false; 2019810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner } 2020810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 2021810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // None of the above, just randomly invalid non-lvalue. 2022810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return true; 2023810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner} 2024810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 2025ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner/// isOperandMentioned - Return true if the specified operand # is mentioned 2026ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner/// anywhere in the decomposed asm string. 2027ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattnerstatic bool isOperandMentioned(unsigned OpNo, 20282d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner ArrayRef<AsmStmt::AsmStringPiece> AsmStrPieces) { 2029ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) { 2030ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner const AsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; 2031ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner if (!Piece.isOperand()) continue; 2032ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner 2033ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // If this is a reference to the input and if the input was the smaller 2034ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // one, then we have to reject this asm. 2035ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner if (Piece.getOperandNo() == OpNo) 2036ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner return true; 2037ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner } 2038ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner 2039ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner return false; 2040ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner} 2041810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 2042ca57b4b7658a031b74cda5ac504311998be8e343Chris LattnerStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, bool IsSimple, 2043ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner bool IsVolatile, unsigned NumOutputs, 2044ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner unsigned NumInputs, IdentifierInfo **Names, 2045ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner MultiExprArg constraints, MultiExprArg exprs, 2046ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner Expr *asmString, MultiExprArg clobbers, 2047ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner SourceLocation RParenLoc, bool MSAsm) { 20483037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl unsigned NumClobbers = clobbers.size(); 20493037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl StringLiteral **Constraints = 20503037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl reinterpret_cast<StringLiteral**>(constraints.get()); 20519ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr **Exprs = exprs.get(); 20529ae2f076ca5ab1feb3ba95629099ec2319833701John McCall StringLiteral *AsmString = cast<StringLiteral>(asmString); 20533037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get()); 20543037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 20555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; 20561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20571708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner // The parser verifies that there is a string literal here. 20585cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (!AsmString->isAscii()) 20593037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) 20603037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << AsmString->getSourceRange()); 20613037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 20621708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner for (unsigned i = 0; i != NumOutputs; i++) { 20631708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Constraints[i]; 20645cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (!Literal->isAscii()) 20653037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 20663037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 20673037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 20685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef OutputName; 2069ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson if (Names[i]) 2070ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson OutputName = Names[i]->getName(); 2071ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson 2072ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); 2073bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor if (!Context.getTargetInfo().validateOutputConstraint(Info)) 20743037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 2075432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner diag::err_asm_invalid_output_constraint) 2076432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner << Info.getConstraintStr()); 20773037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 2078d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson // Check that the output exprs are valid lvalues. 207972056a237c536ee63285ab0850cb50f299281767Eli Friedman Expr *OutputExpr = Exprs[i]; 2080810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (CheckAsmLValue(OutputExpr, *this)) { 208172056a237c536ee63285ab0850cb50f299281767Eli Friedman return StmtError(Diag(OutputExpr->getLocStart(), 2082dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner diag::err_asm_invalid_lvalue_in_output) 208372056a237c536ee63285ab0850cb50f299281767Eli Friedman << OutputExpr->getSourceRange()); 208404728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 20851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 208644def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner OutputConstraintInfos.push_back(Info); 208704728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 20883037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 20895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; 2090806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner 209104728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { 20921708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Constraints[i]; 20935cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (!Literal->isAscii()) 20943037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 20953037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 20963037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 20975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef InputName; 2098ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson if (Names[i]) 2099ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson InputName = Names[i]->getName(); 2100ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson 2101ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); 2102bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos.data(), 21032819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner NumOutputs, Info)) { 21043037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 2105432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner diag::err_asm_invalid_input_constraint) 2106432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner << Info.getConstraintStr()); 2107d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson } 21083037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 210972056a237c536ee63285ab0850cb50f299281767Eli Friedman Expr *InputExpr = Exprs[i]; 21103037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 2111d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson // Only allow void types for memory constraints. 211244def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner if (Info.allowsMemory() && !Info.allowsRegister()) { 2113810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (CheckAsmLValue(InputExpr, *this)) 211472056a237c536ee63285ab0850cb50f299281767Eli Friedman return StmtError(Diag(InputExpr->getLocStart(), 2115d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson diag::err_asm_invalid_lvalue_in_input) 2116432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner << Info.getConstraintStr() 211772056a237c536ee63285ab0850cb50f299281767Eli Friedman << InputExpr->getSourceRange()); 211804728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 21193037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 212044def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner if (Info.allowsRegister()) { 2121d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson if (InputExpr->getType()->isVoidType()) { 212272056a237c536ee63285ab0850cb50f299281767Eli Friedman return StmtError(Diag(InputExpr->getLocStart(), 2123d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson diag::err_asm_invalid_type_in_input) 21241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump << InputExpr->getType() << Info.getConstraintStr() 212572056a237c536ee63285ab0850cb50f299281767Eli Friedman << InputExpr->getSourceRange()); 2126d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson } 2127d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson } 21281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2129429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]); 2130429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 2131429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 21321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2133429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Exprs[i] = Result.take(); 2134806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner InputConstraintInfos.push_back(Info); 213504728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 21363037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 21376fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson // Check that the clobbers are valid. 21381708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner for (unsigned i = 0; i != NumClobbers; i++) { 21391708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Clobbers[i]; 21405cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (!Literal->isAscii()) 21413037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 21423037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 21433037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 21445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Clobber = Literal->getString(); 21453037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 2146bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor if (!Context.getTargetInfo().isValidClobber(Clobber)) 21473037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 21487765934ad7e157b5fcf925792a38e01b1edbcf8aDaniel Dunbar diag::err_asm_unknown_register_name) << Clobber); 21496fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson } 21503037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 2151fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner AsmStmt *NS = 2152dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, MSAsm, 2153dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi NumOutputs, NumInputs, Names, Constraints, Exprs, 2154966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson AsmString, NumClobbers, Clobbers, RParenLoc); 2155fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner // Validate the asm string, ensuring it makes sense given the operands we 2156fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner // have. 21575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<AsmStmt::AsmStringPiece, 8> Pieces; 2158fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner unsigned DiagOffs; 2159fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { 21602ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) 21612ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner << AsmString->getSourceRange(); 2162fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner return StmtError(); 2163fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner } 21641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2165806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // Validate tied input operands for type mismatches. 2166806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { 2167806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; 21681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2169806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // If this is a tied constraint, verify that the output and input have 2170806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // either exactly the same type, or that they are int/ptr operands with the 2171806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // same size (int/long, int*/long, are ok etc). 2172806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner if (!Info.hasTiedOperand()) continue; 21731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2174806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner unsigned TiedTo = Info.getTiedOperand(); 2175935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner unsigned InputOpNo = i+NumOutputs; 2176f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner Expr *OutputExpr = Exprs[TiedTo]; 2177935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner Expr *InputExpr = Exprs[InputOpNo]; 2178f45b357c0d0df99e160a6320e279ef788dd91ba6Eli Friedman 2179f45b357c0d0df99e160a6320e279ef788dd91ba6Eli Friedman if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent()) 2180f45b357c0d0df99e160a6320e279ef788dd91ba6Eli Friedman continue; 2181f45b357c0d0df99e160a6320e279ef788dd91ba6Eli Friedman 21827adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner QualType InTy = InputExpr->getType(); 21837adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner QualType OutTy = OutputExpr->getType(); 21847adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner if (Context.hasSameType(InTy, OutTy)) 2185806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner continue; // All types can be tied to themselves. 21861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2187aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // Decide if the input and output are in the same domain (integer/ptr or 2188aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // floating point. 2189aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner enum AsmDomain { 2190aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner AD_Int, AD_FP, AD_Other 2191aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner } InputDomain, OutputDomain; 2192dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2193aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (InTy->isIntegerType() || InTy->isPointerType()) 2194aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain = AD_Int; 21950c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor else if (InTy->isRealFloatingType()) 2196aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain = AD_FP; 2197aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner else 2198aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain = AD_Other; 21993351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner 2200aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (OutTy->isIntegerType() || OutTy->isPointerType()) 2201aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputDomain = AD_Int; 22020c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor else if (OutTy->isRealFloatingType()) 2203aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputDomain = AD_FP; 2204aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner else 2205aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputDomain = AD_Other; 2206dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2207aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // They are ok if they are the same size and in the same domain. This 2208aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // allows tying things like: 2209aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // void* to int* 2210aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // void* to int if they are the same size. 2211aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // double to long double if they are the same size. 2212dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // 2213aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner uint64_t OutSize = Context.getTypeSize(OutTy); 2214aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner uint64_t InSize = Context.getTypeSize(InTy); 2215aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (OutSize == InSize && InputDomain == OutputDomain && 2216aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain != AD_Other) 2217aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner continue; 2218dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2219aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // If the smaller input/output operand is not mentioned in the asm string, 2220f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner // then we can promote the smaller one to a larger input and the asm string 2221f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner // won't notice. 2222aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner bool SmallerValueMentioned = false; 2223ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner 2224ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // If this is a reference to the input and if the input was the smaller 2225ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // one, then we have to reject this asm. 2226935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner if (isOperandMentioned(InputOpNo, Pieces)) { 2227ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // This is a use in the asm string of the smaller operand. Since we 2228ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // codegen this by promoting to a wider value, the asm will get printed 2229ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // "wrong". 2230f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner SmallerValueMentioned |= InSize < OutSize; 2231ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner } 2232f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner if (isOperandMentioned(TiedTo, Pieces)) { 2233ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // If this is a reference to the output, and if the output is the larger 2234ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // value, then it's ok because we'll promote the input to the larger type. 2235f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner SmallerValueMentioned |= OutSize < InSize; 2236806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner } 22371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2238aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // If the smaller value wasn't mentioned in the asm string, and if the 2239aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // output was a register, just extend the shorter one to the size of the 2240aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // larger one. 2241aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (!SmallerValueMentioned && InputDomain != AD_Other && 2242aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputConstraintInfos[TiedTo].allowsRegister()) 2243aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner continue; 2244f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner 2245935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // Either both of the operands were mentioned or the smaller one was 2246935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // mentioned. One more special case that we'll allow: if the tied input is 2247935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // integer, unmentioned, and is a constant, then we'll allow truncating it 2248935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // down to the size of the destination. 2249935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner if (InputDomain == AD_Int && OutputDomain == AD_Int && 2250935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner !isOperandMentioned(InputOpNo, Pieces) && 2251935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner InputExpr->isEvaluatable(Context)) { 22524da89c87b2b6ca31f1015ec19aae65a02971ea9aJohn McCall CastKind castKind = 22534da89c87b2b6ca31f1015ec19aae65a02971ea9aJohn McCall (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast); 22544da89c87b2b6ca31f1015ec19aae65a02971ea9aJohn McCall InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).take(); 2255935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner Exprs[InputOpNo] = InputExpr; 2256935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner NS->setInputExpr(i, InputExpr); 2257935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner continue; 2258935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner } 2259935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner 2260c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner Diag(InputExpr->getLocStart(), 2261806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner diag::err_asm_tying_incompatible_types) 22627adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner << InTy << OutTy << OutputExpr->getSourceRange() 2263806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner << InputExpr->getSourceRange(); 2264806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner return StmtError(); 2265806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner } 22661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2267fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner return Owned(NS); 2268fe795956194141c91ae555985c9b930595bff43fChris Lattner} 22693b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian 227060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2271431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, 2272d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall SourceLocation RParen, Decl *Parm, 22739ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *Body) { 2274d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall VarDecl *Var = cast_or_null<VarDecl>(Parm); 2275160b5630aa781ac348303e1ae088d27016637778Douglas Gregor if (Var && Var->isInvalidDecl()) 2276160b5630aa781ac348303e1ae088d27016637778Douglas Gregor return StmtError(); 2277dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 22789ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtCatchStmt(AtLoc, RParen, Var, Body)); 22793b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian} 22803b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian 228160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 22829ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, Stmt *Body) { 22839ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, Body)); 2284161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian} 2285bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 228660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2287dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnObjCAtTryStmt(SourceLocation AtLoc, Stmt *Try, 22889ae2f076ca5ab1feb3ba95629099ec2319833701John McCall MultiStmtArg CatchStmts, Stmt *Finally) { 2289da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson if (!getLangOptions().ObjCExceptions) 2290da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@try"; 2291da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson 2292781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchProtectedScope(); 22938f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor unsigned NumCatchStmts = CatchStmts.size(); 22949ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(ObjCAtTryStmt::Create(Context, AtLoc, Try, 22959ae2f076ca5ab1feb3ba95629099ec2319833701John McCall CatchStmts.release(), 22968f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor NumCatchStmts, 22979ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Finally)); 2298bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian} 2299bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 230060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc, 23019ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *Throw) { 2302d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (Throw) { 2303f2dd68fb37a62571a4985c8dd05310ac90d07ef0Fariborz Jahanian Throw = MaybeCreateExprWithCleanups(Throw); 2304429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultLvalueConversion(Throw); 2305429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 2306429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 23075e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall 2308429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Throw = Result.take(); 2309d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor QualType ThrowType = Throw->getType(); 2310d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor // Make sure the expression type is an ObjC pointer or "void *". 2311d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (!ThrowType->isDependentType() && 2312d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor !ThrowType->isObjCObjectPointerType()) { 2313d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor const PointerType *PT = ThrowType->getAs<PointerType>(); 2314d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (!PT || !PT->getPointeeType()->isVoidType()) 2315d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object) 2316d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor << Throw->getType() << Throw->getSourceRange()); 2317d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor } 2318d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor } 2319dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 23209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtThrowStmt(AtLoc, Throw)); 2321d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor} 2322d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor 232360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2324dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw, 2325d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor Scope *CurScope) { 2326da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson if (!getLangOptions().ObjCExceptions) 2327da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@throw"; 2328da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson 23299ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!Throw) { 2330e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff // @throw without an expression designates a rethrow (which much occur 2331e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff // in the context of an @catch clause). 2332e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff Scope *AtCatchParent = CurScope; 2333e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff while (AtCatchParent && !AtCatchParent->isAtCatchScope()) 2334e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff AtCatchParent = AtCatchParent->getParent(); 2335e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff if (!AtCatchParent) 23364ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch)); 2337dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 2338f2dd68fb37a62571a4985c8dd05310ac90d07ef0Fariborz Jahanian 23399ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return BuildObjCAtThrowStmt(AtLoc, Throw); 234039f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian} 2341bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 234207524039dce5c820f111a1b3f772b4261f004b4aJohn McCallExprResult 234307524039dce5c820f111a1b3f772b4261f004b4aJohn McCallSema::ActOnObjCAtSynchronizedOperand(SourceLocation atLoc, Expr *operand) { 234407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall ExprResult result = DefaultLvalueConversion(operand); 234507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall if (result.isInvalid()) 234607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall return ExprError(); 234707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall operand = result.take(); 23485e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall 2349a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner // Make sure the expression type is an ObjC pointer or "void *". 235007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall QualType type = operand->getType(); 235107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall if (!type->isDependentType() && 235207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall !type->isObjCObjectPointerType()) { 235307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall const PointerType *pointerType = type->getAs<PointerType>(); 235407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall if (!pointerType || !pointerType->getPointeeType()->isVoidType()) 235507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall return Diag(atLoc, diag::error_objc_synchronized_expects_object) 235607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall << type << operand->getSourceRange(); 2357a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner } 23581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 235907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall // The operand to @synchronized is a full-expression. 236007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall return MaybeCreateExprWithCleanups(operand); 236107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall} 236207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall 236307524039dce5c820f111a1b3f772b4261f004b4aJohn McCallStmtResult 236407524039dce5c820f111a1b3f772b4261f004b4aJohn McCallSema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, Expr *SyncExpr, 236507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall Stmt *SyncBody) { 236607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall // We can't jump into or indirect-jump out of a @synchronized block. 236707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall getCurFunction()->setHasBranchProtectedScope(); 23689ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, SyncExpr, SyncBody)); 2369fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian} 23704b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl 23714b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block 23724b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// and creates a proper catch handler from them. 237360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2374d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl, 23759ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *HandlerBlock) { 23764b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl // There's nothing to test that ActOnExceptionDecl didn't already test. 23778189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) CXXCatchStmt(CatchLoc, 2378d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall cast_or_null<VarDecl>(ExDecl), 23799ae2f076ca5ab1feb3ba95629099ec2319833701John McCall HandlerBlock)); 23804b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl} 23818351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 2382f85e193739c953358c865005855253af4f68a497John McCallStmtResult 2383f85e193739c953358c865005855253af4f68a497John McCallSema::ActOnObjCAutoreleasePoolStmt(SourceLocation AtLoc, Stmt *Body) { 2384f85e193739c953358c865005855253af4f68a497John McCall getCurFunction()->setHasBranchProtectedScope(); 2385f85e193739c953358c865005855253af4f68a497John McCall return Owned(new (Context) ObjCAutoreleasePoolStmt(AtLoc, Body)); 2386f85e193739c953358c865005855253af4f68a497John McCall} 2387f85e193739c953358c865005855253af4f68a497John McCall 23883c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace { 23893c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 2390c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlclass TypeWithHandler { 2391c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl QualType t; 2392c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl CXXCatchStmt *stmt; 2393c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlpublic: 2394c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler(const QualType &type, CXXCatchStmt *statement) 2395c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl : t(type), stmt(statement) {} 2396c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 23970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall // An arbitrary order is fine as long as it places identical 23980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall // types next to each other. 2399c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl bool operator<(const TypeWithHandler &y) const { 24000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr()) 2401c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return true; 24020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr()) 2403c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return false; 2404c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl else 2405c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return getTypeSpecStartLoc() < y.getTypeSpecStartLoc(); 2406c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 24071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2408c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl bool operator==(const TypeWithHandler& other) const { 24090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall return t == other.t; 2410c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 24111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2412c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl CXXCatchStmt *getCatchStmt() const { return stmt; } 2413c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl SourceLocation getTypeSpecStartLoc() const { 2414c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return stmt->getExceptionDecl()->getTypeSpecStartLoc(); 2415c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 2416c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl}; 2417c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 24183c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman} 24193c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 24208351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// ActOnCXXTryBlock - Takes a try compound-statement and a number of 24218351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// handlers and creates a try statement from them. 242260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 24239ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock, 24248351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl MultiStmtArg RawHandlers) { 2425729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson // Don't report an error if 'try' is used in system headers. 242615348aeb81285c75b2e92b5bf8d2db3445d147c2Anders Carlsson if (!getLangOptions().CXXExceptions && 2427729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson !getSourceManager().isInSystemHeader(TryLoc)) 2428729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson Diag(TryLoc, diag::err_exceptions_disabled) << "try"; 24297f11d9cf5df1f8ce82af46eabc4ec5cec7d580b0Anders Carlsson 24308351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl unsigned NumHandlers = RawHandlers.size(); 24318351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl assert(NumHandlers > 0 && 24328351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl "The parser shouldn't call this if there are no handlers."); 24339ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt **Handlers = RawHandlers.get(); 24348351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 24355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<TypeWithHandler, 8> TypesWithHandlers; 24361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (unsigned i = 0; i < NumHandlers; ++i) { 24385f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner CXXCatchStmt *Handler = cast<CXXCatchStmt>(Handlers[i]); 2439c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (!Handler->getExceptionDecl()) { 2440c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (i < NumHandlers - 1) 2441c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return StmtError(Diag(Handler->getLocStart(), 2442c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::err_early_catch_all)); 24431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2444c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl continue; 2445c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 24461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2447c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl const QualType CaughtType = Handler->getCaughtType(); 2448c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType); 2449c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler)); 2450c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 2451c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 2452c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl // Detect handlers for the same type as an earlier one. 2453c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (NumHandlers > 1) { 2454c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end()); 24551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2456c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler prev = TypesWithHandlers[0]; 2457c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) { 2458c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler curr = TypesWithHandlers[i]; 24591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2460c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (curr == prev) { 2461c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl Diag(curr.getTypeSpecStartLoc(), 2462c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::warn_exception_caught_by_earlier_handler) 2463c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl << curr.getCatchStmt()->getCaughtType().getAsString(); 2464c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl Diag(prev.getTypeSpecStartLoc(), 2465c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::note_previous_exception_handler) 2466c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl << prev.getCatchStmt()->getCaughtType().getAsString(); 2467c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 24681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2469c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl prev = curr; 2470c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 24718351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl } 24721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2473781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchProtectedScope(); 2474b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 24758351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // FIXME: We should detect handlers that cannot catch anything because an 24768351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // earlier handler catches a superclass. Need to find a method that is not 24778351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // quadratic for this. 24788351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // Neither of these are explicitly forbidden, but every compiler detects them 24798351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // and warns. 24808351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 24819ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(CXXTryStmt::Create(Context, TryLoc, TryBlock, 2482a1a396df16c02b22983b5c9592022fd9237d4866Sam Weinig Handlers, NumHandlers)); 24838351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl} 248428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 248528bbe4b8acc338476fe0825769b41fb32b423c72John WiegleyStmtResult 248628bbe4b8acc338476fe0825769b41fb32b423c72John WiegleySema::ActOnSEHTryBlock(bool IsCXXTry, 248728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley SourceLocation TryLoc, 248828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *TryBlock, 248928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *Handler) { 249028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley assert(TryBlock && Handler); 249128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 249228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley getCurFunction()->setHasBranchProtectedScope(); 249328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 249428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley return Owned(SEHTryStmt::Create(Context,IsCXXTry,TryLoc,TryBlock,Handler)); 249528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley} 249628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 249728bbe4b8acc338476fe0825769b41fb32b423c72John WiegleyStmtResult 249828bbe4b8acc338476fe0825769b41fb32b423c72John WiegleySema::ActOnSEHExceptBlock(SourceLocation Loc, 249928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Expr *FilterExpr, 250028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *Block) { 250128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley assert(FilterExpr && Block); 250228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 250328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley if(!FilterExpr->getType()->isIntegerType()) { 250458f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet return StmtError(Diag(FilterExpr->getExprLoc(), 250558f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet diag::err_filter_expression_integral) 250658f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet << FilterExpr->getType()); 250728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley } 250828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 250928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley return Owned(SEHExceptStmt::Create(Context,Loc,FilterExpr,Block)); 251028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley} 251128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 251228bbe4b8acc338476fe0825769b41fb32b423c72John WiegleyStmtResult 251328bbe4b8acc338476fe0825769b41fb32b423c72John WiegleySema::ActOnSEHFinallyBlock(SourceLocation Loc, 251428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *Block) { 251528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley assert(Block); 251628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley return Owned(SEHFinallyStmt::Create(Context,Loc,Block)); 251728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley} 2518ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor 2519ba0513de93d2fab6db5ab30b6927209fcc883078Douglas GregorStmtResult Sema::BuildMSDependentExistsStmt(SourceLocation KeywordLoc, 2520ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor bool IsIfExists, 2521ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor NestedNameSpecifierLoc QualifierLoc, 2522ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor DeclarationNameInfo NameInfo, 2523ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor Stmt *Nested) 2524ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor{ 2525ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor return new (Context) MSDependentExistsStmt(KeywordLoc, IsIfExists, 2526ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor QualifierLoc, NameInfo, 2527ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor cast<CompoundStmt>(Nested)); 2528ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor} 2529ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor 2530ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor 2531ba0513de93d2fab6db5ab30b6927209fcc883078Douglas GregorStmtResult Sema::ActOnMSDependentExistsStmt(SourceLocation KeywordLoc, 2532ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor bool IsIfExists, 2533ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor CXXScopeSpec &SS, 2534ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor UnqualifiedId &Name, 2535ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor Stmt *Nested) { 2536ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor return BuildMSDependentExistsStmt(KeywordLoc, IsIfExists, 2537ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor SS.getWithLocInContext(Context), 2538ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor GetNameFromUnqualifiedId(Name), 2539ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor Nested); 2540ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor} 2541