SemaStmt.cpp revision ab41fe914f63bb470dfa7e400876ada72f57a931
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" 22694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu#include "clang/AST/EvaluatedExprVisitor.h" 2384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/AST/ExprCXX.h" 24419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner#include "clang/AST/ExprObjC.h" 2516f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h" 2616f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtCXX.h" 27209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall#include "clang/AST/TypeLoc.h" 2884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/Lex/Preprocessor.h" 296fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson#include "clang/Basic/TargetInfo.h" 30ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner#include "llvm/ADT/ArrayRef.h" 31c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/STLExtras.h" 32694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu#include "llvm/ADT/SmallPtrSet.h" 33c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/SmallVector.h" 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 35781472fe99a120098c631b0cbe33c89f8cef5e70John McCallusing namespace sema; 365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnExprStmt(FullExprArg expr) { 389ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *E = expr.get(); 39bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor if (!E) // FIXME: FullExprArg has no error state? 40bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor return StmtError(); 41bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor 42834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // C99 6.8.3p2: The expression in an expression statement is evaluated as a 43834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // void expression for its side effects. Conversion to void allows any 44834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // operand, even incomplete types. 45a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl 46834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // Same thing in for stmt first clause (when expr) and third clause. 47a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl return Owned(static_cast<Stmt*>(E)); 485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 51b7d98d35ea723624345f06e5895ddce2e0388ef0Argyrios KyrtzidisStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc, 52e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis bool HasLeadingEmptyMacro) { 53e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis return Owned(new (Context) NullStmt(SemiLoc, HasLeadingEmptyMacro)); 545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 56337e550218128e7d922c09bb354fbc71de90c568Chris LattnerStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg, SourceLocation StartLoc, 57337e550218128e7d922c09bb354fbc71de90c568Chris Lattner SourceLocation EndLoc) { 5820401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); 591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 60682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner // If we have an invalid decl, just return an error. 6120401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner if (DG.isNull()) return StmtError(); 621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc)); 645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 66a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanianvoid Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) { 67a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); 68dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 69a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian // If we have an invalid decl, just return. 70a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian if (DG.isNull() || !DG.isSingleDecl()) return; 71f85e193739c953358c865005855253af4f68a497John McCall VarDecl *var = cast<VarDecl>(DG.getSingleDecl()); 72f85e193739c953358c865005855253af4f68a497John McCall 73a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian // suppress any potential 'unused variable' warning. 74f85e193739c953358c865005855253af4f68a497John McCall var->setUsed(); 75f85e193739c953358c865005855253af4f68a497John McCall 767acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // foreach variables are never actually initialized in the way that 777acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // the parser came up with. 787acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall var->setInit(0); 797acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall 807acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // In ARC, we don't need to retain the iteration variable of a fast 817acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // enumeration loop. Rather than actually trying to catch that 827acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // during declaration processing, we remove the consequences here. 834e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 847acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall QualType type = var->getType(); 857acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall 867acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // Only do this if we inferred the lifetime. Inferred lifetime 877acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // will show up as a local qualifier because explicit lifetime 887acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // should have shown up as an AttributedType instead. 897acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (type.getLocalQualifiers().getObjCLifetime() == Qualifiers::OCL_Strong) { 907acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // Add 'const' and mark the variable as pseudo-strong. 917acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall var->setType(type.withConst()); 927acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall var->setARCPseudoStrong(true); 93f85e193739c953358c865005855253af4f68a497John McCall } 94f85e193739c953358c865005855253af4f68a497John McCall } 95a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian} 96a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian 97ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth/// \brief Diagnose unused '==' and '!=' as likely typos for '=' or '|='. 989d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth/// 999d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth/// Adding a cast to void (or other expression wrappers) will prevent the 1009d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth/// warning from firing. 101ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruthstatic bool DiagnoseUnusedComparison(Sema &S, const Expr *E) { 1029d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth SourceLocation Loc; 10350bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth bool IsNotEqual, CanAssign; 1049d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1059d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (const BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { 1069d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (Op->getOpcode() != BO_EQ && Op->getOpcode() != BO_NE) 107ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1089d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1099d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth Loc = Op->getOperatorLoc(); 11050bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth IsNotEqual = Op->getOpcode() == BO_NE; 11150bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth CanAssign = Op->getLHS()->IgnoreParenImpCasts()->isLValue(); 1129d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth } else if (const CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { 1139d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (Op->getOperator() != OO_EqualEqual && 1149d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth Op->getOperator() != OO_ExclaimEqual) 115ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1169d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1179d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth Loc = Op->getOperatorLoc(); 11850bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth IsNotEqual = Op->getOperator() == OO_ExclaimEqual; 11950bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth CanAssign = Op->getArg(0)->IgnoreParenImpCasts()->isLValue(); 1209d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth } else { 1219d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth // Not a typo-prone comparison. 122ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1239d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth } 1249d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1259d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth // Suppress warnings when the operator, suspicious as it may be, comes from 1269d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth // a macro expansion. 1279d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (Loc.isMacroID()) 128ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1299d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 130ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth S.Diag(Loc, diag::warn_unused_comparison) 1319d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth << (unsigned)IsNotEqual << E->getSourceRange(); 1329d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 13350bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth // If the LHS is a plausible entity to assign to, provide a fixit hint to 13450bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth // correct common typos. 13550bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth if (CanAssign) { 13650bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth if (IsNotEqual) 13750bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth S.Diag(Loc, diag::note_inequality_comparison_to_or_assign) 13850bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth << FixItHint::CreateReplacement(Loc, "|="); 13950bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth else 14050bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth S.Diag(Loc, diag::note_equality_comparison_to_assign) 14150bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth << FixItHint::CreateReplacement(Loc, "="); 14250bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth } 143ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth 144ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return true; 1459d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth} 1469d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 147636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlssonvoid Sema::DiagnoseUnusedExprResult(const Stmt *S) { 148d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) 149d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis return DiagnoseUnusedExprResult(Label->getSubStmt()); 150d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis 151754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson const Expr *E = dyn_cast_or_null<Expr>(S); 152636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson if (!E) 153636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson return; 154636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson 155636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson SourceLocation Loc; 156636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson SourceRange R1, R2; 157d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay if (SourceMgr.isInSystemMacro(E->getExprLoc()) || 158d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay !E->isUnusedResultAWarning(Loc, R1, R2, Context)) 159636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson return; 1601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 161419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // Okay, we have an unused result. Depending on what the base expression is, 162419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // we might want to make a more specific diagnostic. Check for one of these 163419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // cases now. 164419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner unsigned DiagID = diag::warn_unused_expr; 1654765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall if (const ExprWithCleanups *Temps = dyn_cast<ExprWithCleanups>(E)) 1664dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor E = Temps->getSubExpr(); 16734d49471e0b6386aefdc0f6bd15e4a4876ce5db1Chandler Carruth if (const CXXBindTemporaryExpr *TempExpr = dyn_cast<CXXBindTemporaryExpr>(E)) 16834d49471e0b6386aefdc0f6bd15e4a4876ce5db1Chandler Carruth E = TempExpr->getSubExpr(); 16912f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall 170ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth if (DiagnoseUnusedComparison(*this, E)) 171ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return; 172ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth 173f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall E = E->IgnoreParenImpCasts(); 174bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 1750faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall if (E->getType()->isVoidType()) 1760faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall return; 1770faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall 178bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner // If the callee has attribute pure, const, or warn_unused_result, warn with 179bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner // a more specific message to make it clear what is happening. 180d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes if (const Decl *FD = CE->getCalleeDecl()) { 181bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<WarnUnusedResultAttr>()) { 18242d7b2d25b6f90dc397886d05502b10ab5a8b51bMatt Beaumont-Gay Diag(Loc, diag::warn_unused_result) << R1 << R2; 183bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 184bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 185bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<PureAttr>()) { 186bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure"; 187bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 188bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 189bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<ConstAttr>()) { 190bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const"; 191bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 192bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 193dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 19412f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall } else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) { 1954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount && ME->isDelegateInitCall()) { 196f85e193739c953358c865005855253af4f68a497John McCall Diag(Loc, diag::err_arc_unused_init_message) << R1; 197f85e193739c953358c865005855253af4f68a497John McCall return; 198f85e193739c953358c865005855253af4f68a497John McCall } 199f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian const ObjCMethodDecl *MD = ME->getMethodDecl(); 200f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian if (MD && MD->getAttr<WarnUnusedResultAttr>()) { 20142d7b2d25b6f90dc397886d05502b10ab5a8b51bMatt Beaumont-Gay Diag(Loc, diag::warn_unused_result) << R1 << R2; 202f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian return; 203f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian } 204ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } else if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { 205ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Source = POE->getSyntacticForm(); 206ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (isa<ObjCSubscriptRefExpr>(Source)) 207ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek DiagID = diag::warn_unused_container_subscript_expr; 208ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek else 209ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek DiagID = diag::warn_unused_property_expr; 210d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor } else if (const CXXFunctionalCastExpr *FC 211d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor = dyn_cast<CXXFunctionalCastExpr>(E)) { 212d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor if (isa<CXXConstructExpr>(FC->getSubExpr()) || 213d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor isa<CXXTemporaryObjectExpr>(FC->getSubExpr())) 214d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor return; 215f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian } 216209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall // Diagnose "(void*) blah" as a typo for "(void) blah". 217209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) { 218209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); 219209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall QualType T = TI->getType(); 220209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 221209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall // We really do want to use the non-canonical type here. 222209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall if (T == Context.VoidPtrTy) { 223209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc()); 224209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 225209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall Diag(Loc, diag::warn_unused_voidptr) 226209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall << FixItHint::CreateRemoval(TL.getStarLoc()); 227209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall return; 228209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall } 229209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall } 230209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 231351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek DiagRuntimeBehavior(Loc, 0, PDiag(DiagID) << R1 << R2); 232636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson} 233636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson 234625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::ActOnStartOfCompoundStmt() { 235625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko PushCompoundScope(); 236625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko} 237625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 238625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::ActOnFinishOfCompoundStmt() { 239625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko PopCompoundScope(); 240625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko} 241625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 242625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkosema::CompoundScopeInfo &Sema::getCurCompoundScope() const { 243625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return getCurFunction()->CompoundScopes.back(); 244625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko} 245625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 24660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2471b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, 248a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl MultiStmtArg elts, bool isStmtExpr) { 249a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl unsigned NumElts = elts.size(); 250a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl Stmt **Elts = reinterpret_cast<Stmt**>(elts.release()); 251c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // If we're in C89 mode, check that we don't have any decls after stmts. If 252c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // so, emit an extension diagnostic. 2534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().C99 && !getLangOpts().CPlusPlus) { 254c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // Note that __extension__ can be around a decl. 255c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner unsigned i = 0; 256c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // Skip over all declarations. 257c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) 258c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner /*empty*/; 259c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner 260c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // We found the end of the list or a statement. Scan for another declstmt. 261c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) 262c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner /*empty*/; 2631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 264c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner if (i != NumElts) { 2654afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin(); 266c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner Diag(D->getLocation(), diag::ext_mixed_decls_code); 267c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner } 268c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner } 26998414c1b7d1944a57156d52e29bd41c005de09acChris Lattner // Warn about unused expressions in statements. 27098414c1b7d1944a57156d52e29bd41c005de09acChris Lattner for (unsigned i = 0; i != NumElts; ++i) { 271636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson // Ignore statements that are last in a statement expression. 272636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson if (isStmtExpr && i == NumElts - 1) 27398414c1b7d1944a57156d52e29bd41c005de09acChris Lattner continue; 2741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 275636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson DiagnoseUnusedExprResult(Elts[i]); 27698414c1b7d1944a57156d52e29bd41c005de09acChris Lattner } 277a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl 278625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // Check for suspicious empty body (null statement) in `for' and `while' 279625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // statements. Don't do anything for template instantiations, this just adds 280625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // noise. 281625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (NumElts != 0 && !CurrentInstantiationScope && 282625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko getCurCompoundScope().HasEmptyLoopBodies) { 283625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko for (unsigned i = 0; i != NumElts - 1; ++i) 284625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko DiagnoseEmptyLoopBody(Elts[i], Elts[i + 1]); 285625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko } 286625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 2878189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R)); 2885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 29060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2919ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCaseStmt(SourceLocation CaseLoc, Expr *LHSVal, 2929ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SourceLocation DotDotDotLoc, Expr *RHSVal, 29324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner SourceLocation ColonLoc) { 2949ae2f076ca5ab1feb3ba95629099ec2319833701John McCall assert((LHSVal != 0) && "missing expression in case statement"); 295117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 2968ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith if (getCurFunction()->SwitchStack.empty()) { 2978ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Diag(CaseLoc, diag::err_case_not_in_switch); 29824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return StmtError(); 2998ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().CPlusPlus0x) { 3028ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // C99 6.8.4.2p3: The expression shall be an integer constant. 3038ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // However, GCC allows any evaluatable integer expression. 304282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent()) { 305282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith LHSVal = VerifyIntegerConstantExpression(LHSVal).take(); 306282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith if (!LHSVal) 307282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith return StmtError(); 308282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith } 309117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 3108ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // GCC extension: The expression shall be an integer constant. 311117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 312282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent()) { 313282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith RHSVal = VerifyIntegerConstantExpression(RHSVal).take(); 314282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith // Recover from an error by just forgetting about it. 3158ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 3168a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner } 3175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 318dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc, 319dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor ColonLoc); 320781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.back()->addSwitchCase(CS); 321117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(CS); 3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 32424e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner/// ActOnCaseStmtBody - This installs a statement as the body of a case. 3259ae2f076ca5ab1feb3ba95629099ec2319833701John McCallvoid Sema::ActOnCaseStmtBody(Stmt *caseStmt, Stmt *SubStmt) { 3265440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth DiagnoseUnusedExprResult(SubStmt); 3275440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth 32824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner CaseStmt *CS = static_cast<CaseStmt*>(caseStmt); 32924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner CS->setSubStmt(SubStmt); 33024e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner} 33124e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner 33260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 3331eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, 3349ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *SubStmt, Scope *CurScope) { 3355440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth DiagnoseUnusedExprResult(SubStmt); 3365440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth 337781472fe99a120098c631b0cbe33c89f8cef5e70John McCall if (getCurFunction()->SwitchStack.empty()) { 3380fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner Diag(DefaultLoc, diag::err_default_not_in_switch); 339117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(SubStmt); 3400fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner } 341117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 342dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt); 343781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.back()->addSwitchCase(DS); 344117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(DS); 3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 34760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 34857ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerSema::ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl, 34957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner SourceLocation ColonLoc, Stmt *SubStmt) { 350ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner // If the label was multiply defined, reject it now. 351ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner if (TheDecl->getStmt()) { 352ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Diag(IdentLoc, diag::err_redefinition_of_label) << TheDecl->getDeclName(); 353ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Diag(TheDecl->getLocation(), diag::note_previous_definition); 354de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(SubStmt); 3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 356de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 357ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner // Otherwise, things are good. Fill in the declaration and return it. 358ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner LabelStmt *LS = new (Context) LabelStmt(IdentLoc, TheDecl, SubStmt); 359ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner TheDecl->setStmt(LS); 360203548ba4b72e7e59320d352afc1eb0b5ab131deAbramo Bagnara if (!TheDecl->isGnuLocal()) 361203548ba4b72e7e59320d352afc1eb0b5ab131deAbramo Bagnara TheDecl->setLocation(IdentLoc); 362ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner return Owned(LS); 3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 365534986f2b21e6050bf00163cd6423fd92155a6edRichard SmithStmtResult Sema::ActOnAttributedStmt(SourceLocation AttrLoc, 366534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith const AttrVec &Attrs, 367534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith Stmt *SubStmt) { 368534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith // Fill in the declaration and return it. Variable length will require to 369534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith // change this to AttributedStmt::Create(Context, ....); 370534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith // and probably using ArrayRef 371534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith AttributedStmt *LS = new (Context) AttributedStmt(AttrLoc, Attrs, SubStmt); 372534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith return Owned(LS); 373534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith} 374534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith 37560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 376d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, Decl *CondVar, 37744aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis Stmt *thenStmt, SourceLocation ElseLoc, 37844aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis Stmt *elseStmt) { 37960d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult(CondVal.release()); 3801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3818cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor VarDecl *ConditionVar = 0; 382d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 383d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 384586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor CondResult = CheckConditionVariable(ConditionVar, IfLoc, true); 38599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (CondResult.isInvalid()) 38699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 3878cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor } 38899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Expr *ConditionExpr = CondResult.takeAs<Expr>(); 38999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!ConditionExpr) 39099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 391dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 392754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(thenStmt); 3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3949ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!elseStmt) { 395625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko DiagnoseEmptyStmtBody(ConditionExpr->getLocEnd(), thenStmt, 396625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko diag::warn_empty_if_body); 3972d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson } 3982d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson 399754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(elseStmt); 4001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 401dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi return Owned(new (Context) IfStmt(Context, IfLoc, ConditionVar, ConditionExpr, 40244aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis thenStmt, ElseLoc, elseStmt)); 4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 405f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have 406f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified width and sign. If an overflow occurs, detect it and emit 407f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified diagnostic. 408f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattnervoid Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, 409f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner unsigned NewWidth, bool NewSign, 4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SourceLocation Loc, 411f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner unsigned DiagID) { 412f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Perform a conversion to the promoted condition type if needed. 413f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (NewWidth > Val.getBitWidth()) { 414f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // If this is an extension, just do it. 4159f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.extend(NewWidth); 416f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.setIsSigned(NewSign); 417f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor 418f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // If the input was signed and negative and the output is 419f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // unsigned, don't bother to warn: this is implementation-defined 420f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // behavior. 421f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // FIXME: Introduce a second, default-ignored warning for this case? 422f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else if (NewWidth < Val.getBitWidth()) { 423f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // If this is a truncation, check for overflow. 424f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner llvm::APSInt ConvVal(Val); 4259f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad ConvVal = ConvVal.trunc(NewWidth); 426b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner ConvVal.setIsSigned(NewSign); 4279f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad ConvVal = ConvVal.extend(Val.getBitWidth()); 428b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner ConvVal.setIsSigned(Val.isSigned()); 429f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (ConvVal != Val) 430d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10); 4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 432f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Regardless of whether a diagnostic was emitted, really do the 433f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // truncation. 4349f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.trunc(NewWidth); 435b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner Val.setIsSigned(NewSign); 436f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else if (NewSign != Val.isSigned()) { 437f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Convert the sign to match the sign of the condition. This can cause 438f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // overflow as well: unsigned(INTMIN) 439dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // We don't diagnose this overflow, because it is implementation-defined 4402853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor // behavior. 4412853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor // FIXME: Introduce a second, default-ignored warning for this case? 442f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner llvm::APSInt OldVal(Val); 443f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.setIsSigned(NewSign); 444f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 445f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner} 446f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 4470471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattnernamespace { 4480471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner struct CaseCompareFunctor { 4490471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 4500471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner const llvm::APSInt &RHS) { 4510471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner return LHS.first < RHS; 4520471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 4530e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 4540e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 4550e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner return LHS.first < RHS.first; 4560e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner } 4570471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner bool operator()(const llvm::APSInt &LHS, 4580471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 4590471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner return LHS < RHS.first; 4600471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 4610471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner }; 4620471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner} 4630471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner 464764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// CmpCaseVals - Comparison predicate for sorting case values. 465764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// 466764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattnerstatic bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, 467764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner const std::pair<llvm::APSInt, CaseStmt*>& rhs) { 468764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner if (lhs.first < rhs.first) 469764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return true; 470764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner 471764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner if (lhs.first == rhs.first && 472764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner lhs.second->getCaseLoc().getRawEncoding() 473764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner < rhs.second->getCaseLoc().getRawEncoding()) 474764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return true; 475764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return false; 476764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner} 477764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner 478ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// CmpEnumVals - Comparison predicate for sorting enumeration values. 479ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// 480ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, 481ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) 482ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{ 483ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor return lhs.first < rhs.first; 484ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor} 485ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 486ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// EqEnumVals - Comparison preficate for uniqing enumeration values. 487ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// 488ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, 489ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) 490ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{ 491ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor return lhs.first == rhs.first; 492ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor} 493ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 4945f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of 4955f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// potentially integral-promoted expression @p expr. 496a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCallstatic QualType GetTypeBeforeIntegralPromotion(Expr *&expr) { 497a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(expr)) 498a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall expr = cleanups->getSubExpr(); 499a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall while (ImplicitCastExpr *impcast = dyn_cast<ImplicitCastExpr>(expr)) { 500a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (impcast->getCastKind() != CK_IntegralCast) break; 501a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall expr = impcast->getSubExpr(); 5025f04881eb025f61396d0555d8173730fe2759e0aChris Lattner } 5035f04881eb025f61396d0555d8173730fe2759e0aChris Lattner return expr->getType(); 5045f04881eb025f61396d0555d8173730fe2759e0aChris Lattner} 5055f04881eb025f61396d0555d8173730fe2759e0aChris Lattner 50660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 507dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, Expr *Cond, 508d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *CondVar) { 50960d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult; 5109ae2f076ca5ab1feb3ba95629099ec2319833701John McCall 511586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor VarDecl *ConditionVar = 0; 512d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 513d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 5149ae2f076ca5ab1feb3ba95629099ec2319833701John McCall CondResult = CheckConditionVariable(ConditionVar, SourceLocation(), false); 5159ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 516586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 517dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 5189ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.release(); 519586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor } 520dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 5219ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!Cond) 522586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 523dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 524ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor class SwitchConvertDiagnoser : public ICEConvertDiagnoser { 525ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor Expr *Cond; 526ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor 527ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor public: 528ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor SwitchConvertDiagnoser(Expr *Cond) 529ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor : ICEConvertDiagnoser(false, true), Cond(Cond) { } 530ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor 531ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 532ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType T) { 533ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Loc, diag::err_typecheck_statement_requires_integer) << T; 534ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 535ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor 536ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 537ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType T) { 538ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Loc, diag::err_switch_incomplete_class_type) 539ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor << T << Cond->getSourceRange(); 540ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 541ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor 542ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 543ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType T, 544ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType ConvTy) { 545ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Loc, diag::err_switch_explicit_conversion) << T << ConvTy; 546ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 547ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor 548ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 549ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType ConvTy) { 550ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Conv->getLocation(), diag::note_switch_conversion) 551ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor << ConvTy->isEnumeralType() << ConvTy; 552ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 553ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor 554ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 555ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType T) { 556ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Loc, diag::err_switch_multiple_conversions) << T; 557ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 558ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor 559ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 560ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType ConvTy) { 561ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Conv->getLocation(), diag::note_switch_conversion) 562ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor << ConvTy->isEnumeralType() << ConvTy; 563ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 564ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor 565ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, 566ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType T, 567ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType ConvTy) { 568ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return DiagnosticBuilder::getEmpty(); 569ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 570ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } SwitchDiagnoser(Cond); 571ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor 5729ae2f076ca5ab1feb3ba95629099ec2319833701John McCall CondResult 573ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor = ConvertToIntegralOrEnumerationType(SwitchLoc, Cond, SwitchDiagnoser, 574f39aec17b89f8f0dd78e78c50ad2fa08f12272e3Richard Smith /*AllowScopedEnumerations*/ true); 5759ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) return StmtError(); 5769ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 577dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 578a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. 579a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall CondResult = UsualUnaryConversions(Cond); 580a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (CondResult.isInvalid()) return StmtError(); 581a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall Cond = CondResult.take(); 582a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall 583d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (!CondVar) { 584b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(Cond, SwitchLoc); 5854765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall CondResult = MaybeCreateExprWithCleanups(Cond); 5869ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 587586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 5889ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 589586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor } 590b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 591781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchIntoScope(); 592dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 5939ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SwitchStmt *SS = new (Context) SwitchStmt(Context, ConditionVar, Cond); 594781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.push_back(SS); 595586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return Owned(SS); 5967e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner} 5977e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner 59828164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greifstatic void AdjustAPSInt(llvm::APSInt &Val, unsigned BitWidth, bool IsSigned) { 59928164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif if (Val.getBitWidth() < BitWidth) 6009f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.extend(BitWidth); 60128164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif else if (Val.getBitWidth() > BitWidth) 6029f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.trunc(BitWidth); 60328164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Val.setIsSigned(IsSigned); 60428164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif} 60528164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif 60660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 6079ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, 6089ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *BodyStmt) { 6099ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SwitchStmt *SS = cast<SwitchStmt>(Switch); 610781472fe99a120098c631b0cbe33c89f8cef5e70John McCall assert(SS == getCurFunction()->SwitchStack.back() && 611781472fe99a120098c631b0cbe33c89f8cef5e70John McCall "switch stack missing push/pop!"); 612de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 6139dcbfa450d751bd68fc4af8b75da381d4f6984b9Steve Naroff SS->setBody(BodyStmt, SwitchLoc); 614781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.pop_back(); 615c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson 616f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Expr *CondExpr = SS->getCond(); 617a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (!CondExpr) return StmtError(); 618de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 619a0d3ca1ea5578bc736bb71bcec50ab41fefc87b9Douglas Gregor QualType CondType = CondExpr->getType(); 620a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall 621a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall Expr *CondExprBeforePromotion = CondExpr; 622a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall QualType CondTypeBeforePromotion = 623a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall GetTypeBeforeIntegralPromotion(CondExprBeforePromotion); 62484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor 6255f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // C++ 6.4.2.p2: 6265f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // Integral promotions are performed (on the switch condition). 6275f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // 6285f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // A case value unrepresentable by the original switch condition 6295f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // type (before the promotion) doesn't make sense, even when it can 6305f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // be represented by the promoted type. Therefore we need to find 6315f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // the pre-promotion type of the switch condition. 63212356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan if (!CondExpr->isTypeDependent()) { 633acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor // We have already converted the expression to an integral or enumeration 634dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // type, when we started the switch statement. If we don't have an 635acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor // appropriate type now, just return an error. 636acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor if (!CondType->isIntegralOrEnumerationType()) 63712356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan return StmtError(); 63812356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan 6392b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner if (CondExpr->isKnownToHaveBooleanValue()) { 64012356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // switch(bool_expr) {...} is often a programmer error, e.g. 64112356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // switch(n && mask) { ... } // Doh - should be "n & mask". 64212356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // One can always use an if statement instead of switch(bool_expr). 64312356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan Diag(SwitchLoc, diag::warn_bool_switch_condition) 64412356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan << CondExpr->getSourceRange(); 64512356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan } 646c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson } 647de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 648f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Get the bitwidth of the switched-on value before promotions. We must 649f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // convert the integer case values to this width before comparison. 6501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump bool HasDependentValue 651dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor = CondExpr->isTypeDependent() || CondExpr->isValueDependent(); 6521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump unsigned CondWidth 6531d6ab7af99a1fc059a6aa5da083640c1d94b07f7Chris Lattner = HasDependentValue ? 0 : Context.getIntWidth(CondTypeBeforePromotion); 654575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor bool CondIsSigned 655575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor = CondTypeBeforePromotion->isSignedIntegerOrEnumerationType(); 6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 657f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Accumulate all of the case values in a vector so that we can sort them 658f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // and detect duplicates. This vector contains the APInt for the case after 659f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // it has been converted to the condition type. 6605f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner typedef SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; 6610471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner CaseValsTy CaseVals; 6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 663f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Keep track of any GNU case ranges we see. The APSInt is the low value. 664ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy; 665ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseRangesTy CaseRanges; 6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 667f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner DefaultStmt *TheDefaultStmt = 0; 6681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 669b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner bool CaseListIsErroneous = false; 6701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 671dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue; 672c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson SC = SC->getNextSwitchCase()) { 6731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 674c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { 675f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (TheDefaultStmt) { 676f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); 6775f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev); 678de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 679f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // FIXME: Remove the default statement from the switch block so that 680390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // we'll return a valid AST. This requires recursing down the AST and 681390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // finding it, not something we are set up to do right now. For now, 682390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // just lop the entire switch stmt out of the AST. 683b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CaseListIsErroneous = true; 684c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson } 685f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner TheDefaultStmt = DS; 6861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 687f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else { 688f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner CaseStmt *CS = cast<CaseStmt>(SC); 6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6901e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner Expr *Lo = CS->getLHS(); 691dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 692dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (Lo->isTypeDependent() || Lo->isValueDependent()) { 693dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HasDependentValue = true; 694dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor break; 695dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 6961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6978ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith llvm::APSInt LoVal; 6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6994e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus0x) { 7008ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // C++11 [stmt.switch]p2: the constant-expression shall be a converted 7018ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // constant expression of the promoted type of the switch condition. 7028ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith ExprResult ConvLo = 7038ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith CheckConvertedConstantExpression(Lo, CondType, LoVal, CCEK_CaseValue); 7048ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith if (ConvLo.isInvalid()) { 7058ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith CaseListIsErroneous = true; 7068ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith continue; 7078ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 7088ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Lo = ConvLo.take(); 7098ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } else { 7108ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // We already verified that the expression has a i-c-e value (C99 7118ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // 6.8.4.2p3) - get that value now. 7128ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith LoVal = Lo->EvaluateKnownConstInt(Context); 7138ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith 7148ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // If the LHS is not the same type as the condition, insert an implicit 7158ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // cast. 7168ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Lo = DefaultLvalueConversion(Lo).take(); 7178ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Lo = ImpCastExprToType(Lo, CondType, CK_IntegralCast).take(); 7188ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 7198ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith 7208ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // Convert the value to the same width/sign as the condition had prior to 7218ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // integral promotions. 7228ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // 7238ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // FIXME: This causes us to reject valid code: 7248ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // switch ((char)c) { case 256: case 0: return 0; } 7258ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // Here we claim there is a duplicated condition value, but there is not. 726f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, 72728164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Lo->getLocStart(), 728f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner diag::warn_case_value_overflow); 7296c36be5b383875b490684bcf439d6d427298c1afChris Lattner 7301e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner CS->setLHS(Lo); 7311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 732b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // If this is a case range, remember it in CaseRanges, otherwise CaseVals. 733dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (CS->getRHS()) { 7341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (CS->getRHS()->isTypeDependent() || 735dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CS->getRHS()->isValueDependent()) { 736dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HasDependentValue = true; 737dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor break; 738dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 739f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner CaseRanges.push_back(std::make_pair(LoVal, CS)); 7401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else 741b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CaseVals.push_back(std::make_pair(LoVal, CS)); 742f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 743f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 744b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 745dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!HasDependentValue) { 7460fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // If we don't have a default statement, check whether the 7470fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // condition is constant. 7480fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall llvm::APSInt ConstantCondValue; 7490fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall bool HasConstantCond = false; 7500fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (!HasDependentValue && !TheDefaultStmt) { 75151f4708c00110940ca3f337961915f2ca1668375Richard Smith HasConstantCond 75280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith = CondExprBeforePromotion->EvaluateAsInt(ConstantCondValue, Context, 75380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith Expr::SE_AllowSideEffects); 75480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith assert(!HasConstantCond || 75580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith (ConstantCondValue.getBitWidth() == CondWidth && 75680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith ConstantCondValue.isSigned() == CondIsSigned)); 7570fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall } 75880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith bool ShouldCheckConstantCond = HasConstantCond; 7590fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 760dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Sort all the scalar case values so we can easily detect duplicates. 761dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); 762dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 763dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!CaseVals.empty()) { 7640fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) { 7650fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (ShouldCheckConstantCond && 7660fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall CaseVals[i].first == ConstantCondValue) 7670fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ShouldCheckConstantCond = false; 7680fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 7690fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) { 770dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If we have a duplicate, report it. 7711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(CaseVals[i].second->getLHS()->getLocStart(), 7720fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall diag::err_duplicate_case) << CaseVals[i].first.toString(10); 7730fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Diag(CaseVals[i-1].second->getLHS()->getLocStart(), 774dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::note_duplicate_case_prev); 775390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: We really want to remove the bogus case stmt from the 776390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // substmt, but we have no way to do this right now. 777dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseListIsErroneous = true; 778dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 7796efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner } 780b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 7811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 782dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Detect duplicate case ranges, which usually don't exist at all in 783dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // the first place. 784dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!CaseRanges.empty()) { 785dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Sort all the case ranges by their low value so we can easily detect 786dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // overlaps between ranges. 787dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::stable_sort(CaseRanges.begin(), CaseRanges.end()); 7881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 789dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Scan the ranges, computing the high values and removing empty ranges. 790dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::vector<llvm::APSInt> HiVals; 791dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 7920fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall llvm::APSInt &LoVal = CaseRanges[i].first; 793dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CR = CaseRanges[i].second; 794dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Expr *Hi = CR->getRHS(); 7958ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith llvm::APSInt HiVal; 7968ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith 7974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus0x) { 7988ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // C++11 [stmt.switch]p2: the constant-expression shall be a converted 7998ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // constant expression of the promoted type of the switch condition. 8008ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith ExprResult ConvHi = 8018ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith CheckConvertedConstantExpression(Hi, CondType, HiVal, 8028ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith CCEK_CaseValue); 8038ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith if (ConvHi.isInvalid()) { 8048ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith CaseListIsErroneous = true; 8058ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith continue; 8068ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 8078ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Hi = ConvHi.take(); 8088ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } else { 8098ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith HiVal = Hi->EvaluateKnownConstInt(Context); 8108ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith 8118ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // If the RHS is not the same type as the condition, insert an 8128ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // implicit cast. 8138ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Hi = DefaultLvalueConversion(Hi).take(); 8148ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Hi = ImpCastExprToType(Hi, CondType, CK_IntegralCast).take(); 8158ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 8161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 817dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Convert the value to the same width/sign as the condition. 818dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, 81928164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Hi->getLocStart(), 820dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::warn_case_value_overflow); 8211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 822dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CR->setRHS(Hi); 8231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 824dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If the low value is bigger than the high value, the case is empty. 8250fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (LoVal > HiVal) { 826dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range) 827dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor << SourceRange(CR->getLHS()->getLocStart(), 82828164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Hi->getLocEnd()); 829dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseRanges.erase(CaseRanges.begin()+i); 830dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor --i, --e; 831dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor continue; 832dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 8330fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 8340fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (ShouldCheckConstantCond && 8350fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall LoVal <= ConstantCondValue && 8360fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ConstantCondValue <= HiVal) 8370fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ShouldCheckConstantCond = false; 8380fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 839dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HiVals.push_back(HiVal); 8400471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 8411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 842dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Rescan the ranges, looking for overlap with singleton values and other 843dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // ranges. Since the range list is sorted, we only need to compare case 844dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // ranges with their neighbors. 845dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 846dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt &CRLo = CaseRanges[i].first; 847dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt &CRHi = HiVals[i]; 848dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CR = CaseRanges[i].second; 8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 850dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Check to see whether the case range overlaps with any 851dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // singleton cases. 852dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *OverlapStmt = 0; 853dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt OverlapVal(32); 8541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 855dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Find the smallest value >= the lower bound. If I is in the 856dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // case range, then we have overlap. 857dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), 858dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseVals.end(), CRLo, 859dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseCompareFunctor()); 860dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (I != CaseVals.end() && I->first < CRHi) { 861dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = I->first; // Found overlap with scalar. 862dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = I->second; 863dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 8641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 865dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Find the smallest value bigger than the upper bound. 866dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); 867dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (I != CaseVals.begin() && (I-1)->first >= CRLo) { 868dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = (I-1)->first; // Found overlap with scalar. 869dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = (I-1)->second; 870dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 8711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 872dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Check to see if this case stmt overlaps with the subsequent 873dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // case range. 874dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (i && CRLo <= HiVals[i-1]) { 875dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = HiVals[i-1]; // Found overlap with range. 876dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = CaseRanges[i-1].second; 877dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 8781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 879dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (OverlapStmt) { 880dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If we have a duplicate, report it. 881dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case) 882dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor << OverlapVal.toString(10); 8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(OverlapStmt->getLHS()->getLocStart(), 884dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::note_duplicate_case_prev); 885390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: We really want to remove the bogus case stmt from the 886390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // substmt, but we have no way to do this right now. 887dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseListIsErroneous = true; 888dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 8890471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 890b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 891ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 8920fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Complain if we have a constant condition and we didn't find a match. 8930fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (!CaseListIsErroneous && ShouldCheckConstantCond) { 8940fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // TODO: it would be nice if we printed enums as enums, chars as 8950fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // chars, etc. 8960fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition) 8970fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall << ConstantCondValue.toString(10) 8980fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall << CondExpr->getSourceRange(); 8990fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall } 9000fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 9010fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Check to see if switch is over an Enum and handles all of its 902559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // values. We only issue a warning if there is not 'default:', but 903559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // we still do the analysis to preserve this information in the AST 904559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // (which can be used by flow-based analyes). 9050fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // 906ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner const EnumType *ET = CondTypeBeforePromotion->getAs<EnumType>(); 907559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 908ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor // If switch has default case, then ignore it. 909559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (!CaseListIsErroneous && !HasConstantCond && ET) { 910ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const EnumDecl *ED = ET->getDecl(); 9115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> 91258f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet EnumValsTy; 913ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor EnumValsTy EnumVals; 914ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 9150fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Gather all enum values, set their type and sort them, 9160fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // allowing easier comparison with CaseVals. 9170fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin(); 91828164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif EDI != ED->enumerator_end(); ++EDI) { 91928164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif llvm::APSInt Val = EDI->getInitVal(); 92028164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif AdjustAPSInt(Val, CondWidth, CondIsSigned); 921262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie EnumVals.push_back(std::make_pair(Val, &*EDI)); 922ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 923ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); 9240fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall EnumValsTy::iterator EIend = 9250fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); 926559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 927559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // See which case values aren't in enum. 9289366750a5a97c8aeae1df4898ea849b087865195David Blaikie EnumValsTy::const_iterator EI = EnumVals.begin(); 9299366750a5a97c8aeae1df4898ea849b087865195David Blaikie for (CaseValsTy::const_iterator CI = CaseVals.begin(); 9309366750a5a97c8aeae1df4898ea849b087865195David Blaikie CI != CaseVals.end(); CI++) { 9319366750a5a97c8aeae1df4898ea849b087865195David Blaikie while (EI != EIend && EI->first < CI->first) 9329366750a5a97c8aeae1df4898ea849b087865195David Blaikie EI++; 9339366750a5a97c8aeae1df4898ea849b087865195David Blaikie if (EI == EIend || EI->first > CI->first) 9349366750a5a97c8aeae1df4898ea849b087865195David Blaikie Diag(CI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum) 93554faba4f7f3f0e8f1376da1c459312596ad5486dFariborz Jahanian << CondTypeBeforePromotion; 9369366750a5a97c8aeae1df4898ea849b087865195David Blaikie } 9379366750a5a97c8aeae1df4898ea849b087865195David Blaikie // See which of case ranges aren't in enum 9389366750a5a97c8aeae1df4898ea849b087865195David Blaikie EI = EnumVals.begin(); 9399366750a5a97c8aeae1df4898ea849b087865195David Blaikie for (CaseRangesTy::const_iterator RI = CaseRanges.begin(); 9409366750a5a97c8aeae1df4898ea849b087865195David Blaikie RI != CaseRanges.end() && EI != EIend; RI++) { 9419366750a5a97c8aeae1df4898ea849b087865195David Blaikie while (EI != EIend && EI->first < RI->first) 9429366750a5a97c8aeae1df4898ea849b087865195David Blaikie EI++; 9439366750a5a97c8aeae1df4898ea849b087865195David Blaikie 9449366750a5a97c8aeae1df4898ea849b087865195David Blaikie if (EI == EIend || EI->first != RI->first) { 9459366750a5a97c8aeae1df4898ea849b087865195David Blaikie Diag(RI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum) 94654faba4f7f3f0e8f1376da1c459312596ad5486dFariborz Jahanian << CondTypeBeforePromotion; 94747bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 948e0ba9d1beeba01a96808c2fc61f9ca89acec313bTed Kremenek 9499366750a5a97c8aeae1df4898ea849b087865195David Blaikie llvm::APSInt Hi = 9509366750a5a97c8aeae1df4898ea849b087865195David Blaikie RI->second->getRHS()->EvaluateKnownConstInt(Context); 9519366750a5a97c8aeae1df4898ea849b087865195David Blaikie AdjustAPSInt(Hi, CondWidth, CondIsSigned); 9529366750a5a97c8aeae1df4898ea849b087865195David Blaikie while (EI != EIend && EI->first < Hi) 9539366750a5a97c8aeae1df4898ea849b087865195David Blaikie EI++; 9549366750a5a97c8aeae1df4898ea849b087865195David Blaikie if (EI == EIend || EI->first != Hi) 9559366750a5a97c8aeae1df4898ea849b087865195David Blaikie Diag(RI->second->getRHS()->getExprLoc(), diag::warn_not_in_enum) 95654faba4f7f3f0e8f1376da1c459312596ad5486dFariborz Jahanian << CondTypeBeforePromotion; 957ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 958dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 959559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // Check which enum vals aren't in switch 960ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseValsTy::const_iterator CI = CaseVals.begin(); 961ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseRangesTy::const_iterator RI = CaseRanges.begin(); 962559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek bool hasCasesNotInSwitch = false; 963559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 9645f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<DeclarationName,8> UnhandledNames; 965dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 9669366750a5a97c8aeae1df4898ea849b087865195David Blaikie for (EI = EnumVals.begin(); EI != EIend; EI++){ 967ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner // Drop unneeded case values 968ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor llvm::APSInt CIVal; 969ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor while (CI != CaseVals.end() && CI->first < EI->first) 970ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CI++; 971dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 972ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor if (CI != CaseVals.end() && CI->first == EI->first) 973ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor continue; 974ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 975559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // Drop unneeded case ranges 976ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor for (; RI != CaseRanges.end(); RI++) { 977a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt Hi = 978a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith RI->second->getRHS()->EvaluateKnownConstInt(Context); 97928164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif AdjustAPSInt(Hi, CondWidth, CondIsSigned); 980ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor if (EI->first <= Hi) 981ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor break; 982ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 983ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 984559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (RI == CaseRanges.end() || EI->first < RI->first) { 98547bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek hasCasesNotInSwitch = true; 98631ceb61172bca7ebc3fb90e9125864c7a29c55c0David Blaikie UnhandledNames.push_back(EI->second->getDeclName()); 98747bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 988ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 989dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 990585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie if (TheDefaultStmt && UnhandledNames.empty()) 991585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie Diag(TheDefaultStmt->getDefaultLoc(), diag::warn_unreachable_default); 99231ceb61172bca7ebc3fb90e9125864c7a29c55c0David Blaikie 993ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner // Produce a nice diagnostic if multiple values aren't handled. 994ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner switch (UnhandledNames.size()) { 995ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 0: break; 996ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 1: 997585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie Diag(CondExpr->getExprLoc(), TheDefaultStmt 998585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie ? diag::warn_def_missing_case1 : diag::warn_missing_case1) 999ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0]; 1000ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 1001ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 2: 1002585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie Diag(CondExpr->getExprLoc(), TheDefaultStmt 1003585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie ? diag::warn_def_missing_case2 : diag::warn_missing_case2) 1004ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1]; 1005ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 1006ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 3: 1007585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie Diag(CondExpr->getExprLoc(), TheDefaultStmt 1008585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie ? diag::warn_def_missing_case3 : diag::warn_missing_case3) 1009ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2]; 1010ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 1011ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner default: 1012585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie Diag(CondExpr->getExprLoc(), TheDefaultStmt 1013585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie ? diag::warn_def_missing_cases : diag::warn_missing_cases) 1014ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << (unsigned)UnhandledNames.size() 1015ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2]; 1016ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 1017ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner } 1018559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 1019559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (!hasCasesNotInSwitch) 102047bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek SS->setAllEnumCasesCovered(); 1021ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 1022b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 1023dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 1024625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko DiagnoseEmptyStmtBody(CondExpr->getLocEnd(), BodyStmt, 1025625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko diag::warn_empty_switch_body); 1026625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 1027390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: If the case list was broken is some way, we don't have a good system 1028390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // to patch it up. Instead, just return the whole substmt as broken. 1029b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner if (CaseListIsErroneous) 1030de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return StmtError(); 1031de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 1032de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(SS); 10335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 103560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1036dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond, 10379ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Decl *CondVar, Stmt *Body) { 103860d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult(Cond.release()); 1039dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 10405656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor VarDecl *ConditionVar = 0; 1041d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 1042d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 1043586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor CondResult = CheckConditionVariable(ConditionVar, WhileLoc, true); 104499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (CondResult.isInvalid()) 104599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 10465656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor } 10479ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *ConditionExpr = CondResult.take(); 104899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!ConditionExpr) 104999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 1050dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 10519ae2f076ca5ab1feb3ba95629099ec2319833701John McCall DiagnoseUnusedExprResult(Body); 10521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1053625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (isa<NullStmt>(Body)) 1054625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko getCurCompoundScope().setHasEmptyLoopBodies(); 1055625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 105643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor return Owned(new (Context) WhileStmt(Context, ConditionVar, ConditionExpr, 10579ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Body, WhileLoc)); 10585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 106060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 10619ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body, 1062989135901c750af61ef012b6b0a0368be415bc46Chris Lattner SourceLocation WhileLoc, SourceLocation CondLParen, 10639ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *Cond, SourceLocation CondRParen) { 10649ae2f076ca5ab1feb3ba95629099ec2319833701John McCall assert(Cond && "ActOnDoStmt(): missing expression"); 1065f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 1066429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult CondResult = CheckBooleanCondition(Cond, DoLoc); 1067429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (CondResult.isInvalid() || CondResult.isInvalid()) 10685a881bb09928b7ade891efc680088aaad276f8d6John McCall return StmtError(); 1069429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Cond = CondResult.take(); 10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1071b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(Cond, DoLoc); 1072429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley CondResult = MaybeCreateExprWithCleanups(Cond); 10739ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 1074586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 10759ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 1076dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 10779ae2f076ca5ab1feb3ba95629099ec2319833701John McCall DiagnoseUnusedExprResult(Body); 1078754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson 10799ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen)); 10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1082694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieunamespace { 1083694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // This visitor will traverse a conditional statement and store all 1084694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // the evaluated decls into a vector. Simple is set to true if none 1085694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // of the excluded constructs are used. 1086694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu class DeclExtractor : public EvaluatedExprVisitor<DeclExtractor> { 1087694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu llvm::SmallPtrSet<VarDecl*, 8> &Decls; 1088694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu llvm::SmallVector<SourceRange, 10> &Ranges; 1089694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu bool Simple; 1090694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PartialDiagnostic &PDiag; 1091694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieupublic: 1092694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu typedef EvaluatedExprVisitor<DeclExtractor> Inherited; 1093694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1094694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu DeclExtractor(Sema &S, llvm::SmallPtrSet<VarDecl*, 8> &Decls, 1095694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu llvm::SmallVector<SourceRange, 10> &Ranges, 1096694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PartialDiagnostic &PDiag) : 1097694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Inherited(S.Context), 1098694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Decls(Decls), 1099694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Ranges(Ranges), 1100694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Simple(true), 1101694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PDiag(PDiag) {} 1102694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1103694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu bool isSimple() { return Simple; } 1104694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1105694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Replaces the method in EvaluatedExprVisitor. 1106694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitMemberExpr(MemberExpr* E) { 1107694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Simple = false; 1108694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1109694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1110694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Any Stmt not whitelisted will cause the condition to be marked complex. 1111694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitStmt(Stmt *S) { 1112694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Simple = false; 1113694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1114694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1115694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitBinaryOperator(BinaryOperator *E) { 1116694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getLHS()); 1117694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getRHS()); 1118694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1119694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1120694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitCastExpr(CastExpr *E) { 1121694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getSubExpr()); 1122694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1123694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1124694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitUnaryOperator(UnaryOperator *E) { 1125694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Skip checking conditionals with derefernces. 1126694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (E->getOpcode() == UO_Deref) 1127694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Simple = false; 1128694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu else 1129694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getSubExpr()); 1130694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1131694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1132694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitConditionalOperator(ConditionalOperator *E) { 1133694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getCond()); 1134694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getTrueExpr()); 1135694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getFalseExpr()); 1136694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1137694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1138694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitParenExpr(ParenExpr *E) { 1139694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getSubExpr()); 1140694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1141694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1142694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { 1143694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getOpaqueValue()->getSourceExpr()); 1144694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getFalseExpr()); 1145694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1146694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1147694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitIntegerLiteral(IntegerLiteral *E) { } 1148694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitFloatingLiteral(FloatingLiteral *E) { } 1149694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { } 1150694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitCharacterLiteral(CharacterLiteral *E) { } 1151694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitGNUNullExpr(GNUNullExpr *E) { } 1152694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitImaginaryLiteral(ImaginaryLiteral *E) { } 1153694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1154694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitDeclRefExpr(DeclRefExpr *E) { 1155694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()); 1156694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (!VD) return; 1157694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1158694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Ranges.push_back(E->getSourceRange()); 1159694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1160694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Decls.insert(VD); 1161694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1162694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1163694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu }; // end class DeclExtractor 1164694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1165694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // DeclMatcher checks to see if the decls are used in a non-evauluated 1166694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // context. 1167694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu class DeclMatcher : public EvaluatedExprVisitor<DeclMatcher> { 1168694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu llvm::SmallPtrSet<VarDecl*, 8> &Decls; 1169694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu bool FoundDecl; 1170694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu //bool EvalDecl; 1171694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1172694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieupublic: 1173694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu typedef EvaluatedExprVisitor<DeclMatcher> Inherited; 1174694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1175694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu DeclMatcher(Sema &S, llvm::SmallPtrSet<VarDecl*, 8> &Decls, Stmt *Statement) : 1176694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Inherited(S.Context), Decls(Decls), FoundDecl(false) { 1177694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (!Statement) return; 1178694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1179694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(Statement); 1180694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1181694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1182694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitReturnStmt(ReturnStmt *S) { 1183694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu FoundDecl = true; 1184694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1185694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1186694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitBreakStmt(BreakStmt *S) { 1187694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu FoundDecl = true; 1188694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1189694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1190694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitGotoStmt(GotoStmt *S) { 1191694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu FoundDecl = true; 1192694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1193694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1194694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitCastExpr(CastExpr *E) { 1195694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (E->getCastKind() == CK_LValueToRValue) 1196694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckLValueToRValueCast(E->getSubExpr()); 1197694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu else 1198694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getSubExpr()); 1199694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1200694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1201694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void CheckLValueToRValueCast(Expr *E) { 1202694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu E = E->IgnoreParenImpCasts(); 1203694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1204694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (isa<DeclRefExpr>(E)) { 1205694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu return; 1206694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1207694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1208694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { 1209694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(CO->getCond()); 1210694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckLValueToRValueCast(CO->getTrueExpr()); 1211694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckLValueToRValueCast(CO->getFalseExpr()); 1212694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu return; 1213694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1214694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1215694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (BinaryConditionalOperator *BCO = 1216694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu dyn_cast<BinaryConditionalOperator>(E)) { 1217694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckLValueToRValueCast(BCO->getOpaqueValue()->getSourceExpr()); 1218694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckLValueToRValueCast(BCO->getFalseExpr()); 1219694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu return; 1220694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1221694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1222694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E); 1223694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1224694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1225694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitDeclRefExpr(DeclRefExpr *E) { 1226694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) 1227694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (Decls.count(VD)) 1228694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu FoundDecl = true; 1229694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1230694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1231694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu bool FoundDeclInUse() { return FoundDecl; } 1232694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1233694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu }; // end class DeclMatcher 1234694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1235694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void CheckForLoopConditionalStatement(Sema &S, Expr *Second, 1236694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Expr *Third, Stmt *Body) { 1237694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Condition is empty 1238694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (!Second) return; 1239694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1240694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (S.Diags.getDiagnosticLevel(diag::warn_variables_not_in_loop_body, 1241694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Second->getLocStart()) 1242694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu == DiagnosticsEngine::Ignored) 1243694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu return; 1244694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1245694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PartialDiagnostic PDiag = S.PDiag(diag::warn_variables_not_in_loop_body); 1246694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu llvm::SmallPtrSet<VarDecl*, 8> Decls; 1247694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu llvm::SmallVector<SourceRange, 10> Ranges; 1248694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu DeclExtractor DE(S, Decls, Ranges, PDiag); 1249694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu DE.Visit(Second); 1250694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1251694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Don't analyze complex conditionals. 1252694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (!DE.isSimple()) return; 1253694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1254694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // No decls found. 1255694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (Decls.size() == 0) return; 1256694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 12579087599e5ee64ecd30194b3d89f8748ac95c62f7Richard Trieu // Don't warn on volatile, static, or global variables. 1258694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu for (llvm::SmallPtrSet<VarDecl*, 8>::iterator I = Decls.begin(), 1259694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu E = Decls.end(); 1260694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu I != E; ++I) 12619087599e5ee64ecd30194b3d89f8748ac95c62f7Richard Trieu if ((*I)->getType().isVolatileQualified() || 12629087599e5ee64ecd30194b3d89f8748ac95c62f7Richard Trieu (*I)->hasGlobalStorage()) return; 1263694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1264694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (DeclMatcher(S, Decls, Second).FoundDeclInUse() || 1265694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu DeclMatcher(S, Decls, Third).FoundDeclInUse() || 1266694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu DeclMatcher(S, Decls, Body).FoundDeclInUse()) 1267694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu return; 1268694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1269694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Load decl names into diagnostic. 1270694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (Decls.size() > 4) 1271694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PDiag << 0; 1272694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu else { 1273694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PDiag << Decls.size(); 1274694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu for (llvm::SmallPtrSet<VarDecl*, 8>::iterator I = Decls.begin(), 1275694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu E = Decls.end(); 1276694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu I != E; ++I) 1277694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PDiag << (*I)->getDeclName(); 1278694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1279694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1280694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Load SourceRanges into diagnostic if there is room. 1281694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Otherwise, load the SourceRange of the conditional expression. 1282694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (Ranges.size() <= PartialDiagnostic::MaxArguments) 1283694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu for (llvm::SmallVector<SourceRange, 10>::iterator I = Ranges.begin(), 1284694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu E = Ranges.end(); 1285694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu I != E; ++I) 1286694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PDiag << *I; 1287694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu else 1288694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PDiag << Second->getSourceRange(); 1289694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1290694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu S.Diag(Ranges.begin()->getBegin(), PDiag); 1291694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1292694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1293694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu} // end namespace 1294694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 129560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1296f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, 12979ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *First, FullExprArg second, Decl *secondVar, 129899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor FullExprArg third, 12999ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SourceLocation RParenLoc, Stmt *Body) { 13004e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().CPlusPlus) { 13015921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { 1302f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // C99 6.8.5p3: The declaration part of a 'for' statement shall only 1303f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // declare identifiers for objects having storage class 'auto' or 1304f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // 'register'. 13055921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); 13065921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis DI!=DE; ++DI) { 13075921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis VarDecl *VD = dyn_cast<VarDecl>(*DI); 1308b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall if (VD && VD->isLocalVarDecl() && !VD->hasLocalStorage()) 13095921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis VD = 0; 13105921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (VD == 0) 13115921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for); 13125921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // FIXME: mark decl erroneous! 13135921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis } 1314ae3b701f59e78e058b83344be17206af3bf5d277Chris Lattner } 13155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 131699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor 1317694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckForLoopConditionalStatement(*this, second.get(), third.get(), Body); 1318694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 131960d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult SecondResult(second.release()); 132099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor VarDecl *ConditionVar = 0; 1321d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (secondVar) { 1322d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(secondVar); 1323586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor SecondResult = CheckConditionVariable(ConditionVar, ForLoc, true); 132499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (SecondResult.isInvalid()) 132599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 132699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor } 1327dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 132899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Expr *Third = third.release().takeAs<Expr>(); 1329dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 13303af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson DiagnoseUnusedExprResult(First); 13313af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson DiagnoseUnusedExprResult(Third); 1332754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(Body); 1333754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson 1334625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (isa<NullStmt>(Body)) 1335625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko getCurCompoundScope().setHasEmptyLoopBodies(); 1336625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 1337dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi return Owned(new (Context) ForStmt(Context, First, 1338dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi SecondResult.take(), ConditionVar, 1339dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi Third, Body, ForLoc, LParenLoc, 134043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor RParenLoc)); 13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1343f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// In an Objective C collection iteration statement: 1344f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// for (x in y) 1345f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// x can be an arbitrary l-value expression. Bind it up as a 1346f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// full-expression. 1347f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallStmtResult Sema::ActOnForEachLValueExpr(Expr *E) { 134829bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall // Reduce placeholder expressions here. Note that this rejects the 134929bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall // use of pseudo-object l-values in this position. 135029bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall ExprResult result = CheckPlaceholderExpr(E); 135129bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall if (result.isInvalid()) return StmtError(); 135229bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall E = result.take(); 135329bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall 1354f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall CheckImplicitConversions(E); 135529bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall 135629bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall result = MaybeCreateExprWithCleanups(E); 135729bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall if (result.isInvalid()) return StmtError(); 135829bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall 135929bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall return Owned(static_cast<Stmt*>(result.take())); 1360f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall} 1361f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall 1362990567cb60e8530ba01b41d4e056e32b44b95ec0John McCallExprResult 1363990567cb60e8530ba01b41d4e056e32b44b95ec0John McCallSema::ActOnObjCForCollectionOperand(SourceLocation forLoc, Expr *collection) { 1364990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall assert(collection); 1365990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1366990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Bail out early if we've got a type-dependent expression. 1367990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (collection->isTypeDependent()) return Owned(collection); 1368990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1369990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Perform normal l-value conversion. 1370990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall ExprResult result = DefaultFunctionArrayLvalueConversion(collection); 1371990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (result.isInvalid()) 1372990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall return ExprError(); 1373990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall collection = result.take(); 1374990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1375990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // The operand needs to have object-pointer type. 1376990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // TODO: should we do a contextual conversion? 1377990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall const ObjCObjectPointerType *pointerType = 1378990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall collection->getType()->getAs<ObjCObjectPointerType>(); 1379990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (!pointerType) 1380990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall return Diag(forLoc, diag::err_collection_expr_type) 1381990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall << collection->getType() << collection->getSourceRange(); 1382990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1383990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Check that the operand provides 1384990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // - countByEnumeratingWithState:objects:count: 1385990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall const ObjCObjectType *objectType = pointerType->getObjectType(); 1386990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall ObjCInterfaceDecl *iface = objectType->getInterface(); 1387990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1388990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // If we have a forward-declared type, we can't do this check. 1389b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor // Under ARC, it is an error not to have a forward-declared class. 1390b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor if (iface && 1391b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor RequireCompleteType(forLoc, QualType(objectType, 0), 13924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie getLangOpts().ObjCAutoRefCount 1393d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor ? diag::err_arc_collection_forward 1394d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor : 0, 1395d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor collection)) { 1396990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Otherwise, if we have any useful type information, check that 1397990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // the type declares the appropriate method. 1398990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } else if (iface || !objectType->qual_empty()) { 1399990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall IdentifierInfo *selectorIdents[] = { 1400990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall &Context.Idents.get("countByEnumeratingWithState"), 1401990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall &Context.Idents.get("objects"), 1402990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall &Context.Idents.get("count") 1403990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall }; 1404990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Selector selector = Context.Selectors.getSelector(3, &selectorIdents[0]); 1405990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1406990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall ObjCMethodDecl *method = 0; 1407990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1408990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // If there's an interface, look in both the public and private APIs. 1409990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (iface) { 1410990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall method = iface->lookupInstanceMethod(selector); 1411990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (!method) method = LookupPrivateInstanceMethod(selector, iface); 1412990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 1413990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1414990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Also check protocol qualifiers. 1415990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (!method) 1416990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall method = LookupMethodInQualifiedType(selector, pointerType, 1417990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall /*instance*/ true); 1418990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1419990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // If we didn't find it anywhere, give up. 1420990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (!method) { 1421990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Diag(forLoc, diag::warn_collection_expr_type) 1422990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall << collection->getType() << selector << collection->getSourceRange(); 1423990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 1424990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1425990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // TODO: check for an incompatible signature? 1426990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 1427990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1428990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Wrap up any cleanups in the expression. 1429990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall return Owned(MaybeCreateExprWithCleanups(collection)); 1430990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall} 1431990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 143260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1433f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, 1434f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl SourceLocation LParenLoc, 14359ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *First, Expr *Second, 14369ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SourceLocation RParenLoc, Stmt *Body) { 143720552d2842245692b649e0d25380670922f954a2Fariborz Jahanian if (First) { 143820552d2842245692b649e0d25380670922f954a2Fariborz Jahanian QualType FirstType; 143920552d2842245692b649e0d25380670922f954a2Fariborz Jahanian if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { 14407e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner if (!DS->isSingleDecl()) 1441f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag((*DS->decl_begin())->getLocation(), 1442f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_toomany_element_decls)); 1443f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 1444f85e193739c953358c865005855253af4f68a497John McCall VarDecl *D = cast<VarDecl>(DS->getSingleDecl()); 1445f85e193739c953358c865005855253af4f68a497John McCall FirstType = D->getType(); 1446f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // C99 6.8.5p3: The declaration part of a 'for' statement shall only 1447f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // declare identifiers for objects having storage class 'auto' or 1448f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // 'register'. 1449f85e193739c953358c865005855253af4f68a497John McCall if (!D->hasLocalStorage()) 1450f85e193739c953358c865005855253af4f68a497John McCall return StmtError(Diag(D->getLocation(), 1451f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_non_variable_decl_in_for)); 14521fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson } else { 1453c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor Expr *FirstE = cast<Expr>(First); 14547eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall if (!FirstE->isTypeDependent() && !FirstE->isLValue()) 1455f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag(First->getLocStart(), 1456f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_selector_element_not_lvalue) 1457f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl << First->getSourceRange()); 14581fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson 14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FirstType = static_cast<Expr*>(First)->getType(); 14601fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson } 1461c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor if (!FirstType->isDependentType() && 1462c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor !FirstType->isObjCObjectPointerType() && 1463a5e42a82ce055f29f3733f3a1f10da6cb9877deeFariborz Jahanian !FirstType->isBlockPointerType()) 1464dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner Diag(ForLoc, diag::err_selector_element_type) 1465d162584991885ab004a02573a73ce06422b921fcChris Lattner << FirstType << First->getSourceRange(); 14663ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian } 1467990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 14688189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body, 14698189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek ForLoc, RParenLoc)); 14703ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian} 14715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1472ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithnamespace { 1473ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1474ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithenum BeginEndFunction { 1475ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_begin, 1476ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_end 1477ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}; 1478ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1479ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Build a variable declaration for a for-range statement. 1480ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstatic VarDecl *BuildForRangeVarDecl(Sema &SemaRef, SourceLocation Loc, 1481ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType Type, const char *Name) { 1482ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclContext *DC = SemaRef.CurContext; 1483ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1484ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1485ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *Decl = VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, 1486ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith TInfo, SC_Auto, SC_None); 1487b403d6d746239095a2c7bac958c924d92434e2b4Richard Smith Decl->setImplicit(); 1488ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return Decl; 1489ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1490ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1491ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Finish building a variable declaration for a for-range statement. 1492ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// \return true if an error occurs. 1493ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstatic bool FinishForRangeVarDecl(Sema &SemaRef, VarDecl *Decl, Expr *Init, 1494ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation Loc, int diag) { 1495ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Deduce the type for the iterator variable now rather than leaving it to 1496ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // AddInitializerToDecl, so we can produce a more suitable diagnostic. 1497ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith TypeSourceInfo *InitTSI = 0; 149862b7cfb73e202051e7ab0dad42ba213acd0dec7eSebastian Redl if ((!isa<InitListExpr>(Init) && Init->getType()->isVoidType()) || 1499b832f6dea893f25b40500a04781286236281cb20Sebastian Redl SemaRef.DeduceAutoType(Decl->getTypeSourceInfo(), Init, InitTSI) == 1500b832f6dea893f25b40500a04781286236281cb20Sebastian Redl Sema::DAR_Failed) 1501ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.Diag(Loc, diag) << Init->getType(); 1502ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!InitTSI) { 1503ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setInvalidDecl(); 1504ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return true; 1505ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1506ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setTypeSourceInfo(InitTSI); 1507ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setType(InitTSI->getType()); 1508ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1509f85e193739c953358c865005855253af4f68a497John McCall // In ARC, infer lifetime. 1510f85e193739c953358c865005855253af4f68a497John McCall // FIXME: ARC may want to turn this into 'const __unsafe_unretained' if 1511f85e193739c953358c865005855253af4f68a497John McCall // we're doing the equivalent of fast iteration. 15124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (SemaRef.getLangOpts().ObjCAutoRefCount && 1513f85e193739c953358c865005855253af4f68a497John McCall SemaRef.inferObjCARCLifetime(Decl)) 1514f85e193739c953358c865005855253af4f68a497John McCall Decl->setInvalidDecl(); 1515f85e193739c953358c865005855253af4f68a497John McCall 1516ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.AddInitializerToDecl(Decl, Init, /*DirectInit=*/false, 1517ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*TypeMayContainAuto=*/false); 1518ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.FinalizeDeclaration(Decl); 1519b403d6d746239095a2c7bac958c924d92434e2b4Richard Smith SemaRef.CurContext->addHiddenDecl(Decl); 1520ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return false; 1521ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1522ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1523ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Produce a note indicating which begin/end function was implicitly called 1524ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// by a C++0x for-range statement. This is often not obvious from the code, 1525ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// nor from the diagnostics produced when analysing the implicit expressions 1526ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// required in a for-range statement. 1527ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid NoteForRangeBeginEndFunction(Sema &SemaRef, Expr *E, 1528ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginEndFunction BEF) { 1529ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith CallExpr *CE = dyn_cast<CallExpr>(E); 1530ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!CE) 1531ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return; 1532ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith FunctionDecl *D = dyn_cast<FunctionDecl>(CE->getCalleeDecl()); 1533ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!D) 1534ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return; 1535ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation Loc = D->getLocation(); 1536ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1537ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith std::string Description; 1538ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith bool IsTemplate = false; 1539ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FunctionTemplateDecl *FunTmpl = D->getPrimaryTemplate()) { 1540ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Description = SemaRef.getTemplateArgumentBindingsText( 1541ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith FunTmpl->getTemplateParameters(), *D->getTemplateSpecializationArgs()); 1542ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IsTemplate = true; 1543ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1544ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1545ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.Diag(Loc, diag::note_for_range_begin_end) 1546ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << BEF << IsTemplate << Description << E->getType(); 1547ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1548ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1549ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Build a call to 'begin' or 'end' for a C++0x for-range statement. If the 1550ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// given LookupResult is non-empty, it is assumed to describe a member which 1551ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// will be invoked. Otherwise, the function will be found via argument 1552ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// dependent lookup. 1553ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstatic ExprResult BuildForRangeBeginEndCall(Sema &SemaRef, Scope *S, 1554ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation Loc, 1555ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *Decl, 1556ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginEndFunction BEF, 1557ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith const DeclarationNameInfo &NameInfo, 1558ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupResult &MemberLookup, 1559ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Expr *Range) { 1560ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult CallExpr; 1561ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!MemberLookup.empty()) { 1562ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult MemberRef = 1563ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.BuildMemberReferenceExpr(Range, Range->getType(), Loc, 1564ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*IsPtr=*/false, CXXScopeSpec(), 1565e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara /*TemplateKWLoc=*/SourceLocation(), 1566e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara /*FirstQualifierInScope=*/0, 1567e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara MemberLookup, 1568ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*TemplateArgs=*/0); 1569ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (MemberRef.isInvalid()) 1570ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1571ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith CallExpr = SemaRef.ActOnCallExpr(S, MemberRef.get(), Loc, MultiExprArg(), 1572ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Loc, 0); 1573ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (CallExpr.isInvalid()) 1574ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1575ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } else { 1576ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith UnresolvedSet<0> FoundNames; 1577ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // C++0x [stmt.ranged]p1: For the purposes of this name lookup, namespace 1578ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // std is an associated namespace. 1579ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith UnresolvedLookupExpr *Fn = 1580ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith UnresolvedLookupExpr::Create(SemaRef.Context, /*NamingClass=*/0, 1581ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NestedNameSpecifierLoc(), NameInfo, 1582ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*NeedsADL=*/true, /*Overloaded=*/false, 1583ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith FoundNames.begin(), FoundNames.end(), 1584ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*LookInStdNamespace=*/true); 1585ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith CallExpr = SemaRef.BuildOverloadedCallExpr(S, Fn, Fn, Loc, &Range, 1, Loc, 15863943b1c0215da2a4171dd6c696cb75d19e5a04a9Kaelyn Uhrain 0, /*AllowTypoCorrection=*/false); 1587ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (CallExpr.isInvalid()) { 1588ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.Diag(Range->getLocStart(), diag::note_for_range_type) 1589ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << Range->getType(); 1590ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1591ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1592ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1593ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(SemaRef, Decl, CallExpr.get(), Loc, 1594ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_iter_deduction_failure)) { 1595ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(SemaRef, CallExpr.get(), BEF); 1596ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return ExprError(); 1597ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1598ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return CallExpr; 1599ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1600ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1601ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1602ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1603ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// ActOnCXXForRangeStmt - Check and build a C++0x for-range statement. 1604ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1605ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// C++0x [stmt.ranged]: 1606ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// A range-based for statement is equivalent to 1607ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1608ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// { 1609ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// auto && __range = range-init; 1610ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// for ( auto __begin = begin-expr, 1611ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// __end = end-expr; 1612ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// __begin != __end; 1613ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// ++__begin ) { 1614ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// for-range-declaration = *__begin; 1615ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// statement 1616ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// } 1617ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// } 1618ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1619ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// The body of the loop is not available yet, since it cannot be analysed until 1620ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// we have determined the type of the for-range-declaration. 1621ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult 1622ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithSema::ActOnCXXForRangeStmt(SourceLocation ForLoc, SourceLocation LParenLoc, 1623ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Stmt *First, SourceLocation ColonLoc, Expr *Range, 1624ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RParenLoc) { 1625ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!First || !Range) 1626ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1627ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1628ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *DS = dyn_cast<DeclStmt>(First); 1629ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith assert(DS && "first part of for range not a decl stmt"); 1630ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1631ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!DS->isSingleDecl()) { 1632ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Diag(DS->getStartLoc(), diag::err_type_defined_in_for_range); 1633ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1634ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1635ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DS->getSingleDecl()->isInvalidDecl()) 1636ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1637ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1638ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DiagnoseUnexpandedParameterPack(Range, UPPC_Expression)) 1639ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1640ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1641ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build auto && __range = range-init 1642ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RangeLoc = Range->getLocStart(); 1643ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *RangeVar = BuildForRangeVarDecl(*this, RangeLoc, 1644ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Context.getAutoRRefDeductType(), 1645ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__range"); 1646ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(*this, RangeVar, Range, RangeLoc, 1647ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_deduction_failure)) 1648ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1649ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1650ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Claim the type doesn't contain auto: we've already done the checking. 1651ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclGroupPtrTy RangeGroup = 1652ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BuildDeclaratorGroup((Decl**)&RangeVar, 1, /*TypeMayContainAuto=*/false); 1653ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith StmtResult RangeDecl = ActOnDeclStmt(RangeGroup, RangeLoc, RangeLoc); 1654ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (RangeDecl.isInvalid()) 1655ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1656ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1657ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return BuildCXXForRangeStmt(ForLoc, ColonLoc, RangeDecl.get(), 1658ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*BeginEndDecl=*/0, /*Cond=*/0, /*Inc=*/0, DS, 1659ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith RParenLoc); 1660ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1661ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1662ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// BuildCXXForRangeStmt - Build or instantiate a C++0x for-range statement. 1663ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult 1664ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithSema::BuildCXXForRangeStmt(SourceLocation ForLoc, SourceLocation ColonLoc, 1665ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Stmt *RangeDecl, Stmt *BeginEnd, Expr *Cond, 1666ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Expr *Inc, Stmt *LoopVarDecl, 1667ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RParenLoc) { 1668ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Scope *S = getCurScope(); 1669ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1670ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *RangeDS = cast<DeclStmt>(RangeDecl); 1671ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *RangeVar = cast<VarDecl>(RangeDS->getSingleDecl()); 1672ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType RangeVarType = RangeVar->getType(); 1673ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1674ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *LoopVarDS = cast<DeclStmt>(LoopVarDecl); 1675ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *LoopVar = cast<VarDecl>(LoopVarDS->getSingleDecl()); 1676ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1677ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith StmtResult BeginEndDecl = BeginEnd; 1678ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult NotEqExpr = Cond, IncrExpr = Inc; 1679ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1680ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!BeginEndDecl.get() && !RangeVarType->isDependentType()) { 1681ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RangeLoc = RangeVar->getLocation(); 1682ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1683e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek const QualType RangeVarNonRefType = RangeVarType.getNonReferenceType(); 1684e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1685e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek ExprResult BeginRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, 1686e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1687e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRangeRef.isInvalid()) 1688e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1689e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1690e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek ExprResult EndRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, 1691e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1692e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (EndRangeRef.isInvalid()) 1693ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1694ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1695ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType AutoType = Context.getAutoDeductType(); 1696ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Expr *Range = RangeVar->getInit(); 1697ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Range) 1698ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1699ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType RangeType = Range->getType(); 1700ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1701ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (RequireCompleteType(RangeLoc, RangeType, 1702d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor diag::err_for_range_incomplete_type)) 1703ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1704ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1705ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build auto __begin = begin-expr, __end = end-expr. 1706ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *BeginVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, 1707ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__begin"); 1708ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *EndVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, 1709ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__end"); 1710ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1711ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build begin-expr and end-expr and attach to __begin and __end variables. 1712ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult BeginExpr, EndExpr; 1713ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (const ArrayType *UnqAT = RangeType->getAsArrayTypeUnsafe()) { 1714ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // - if _RangeT is an array type, begin-expr and end-expr are __range and 1715ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // __range + __bound, respectively, where __bound is the array bound. If 1716ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // _RangeT is an array of unknown size or an array of incomplete type, 1717ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // the program is ill-formed; 1718ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1719ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // begin-expr is __range. 1720e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginExpr = BeginRangeRef; 1721e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (FinishForRangeVarDecl(*this, BeginVar, BeginRangeRef.get(), ColonLoc, 1722ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_iter_deduction_failure)) { 1723ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1724ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1725ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1726ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1727ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Find the array bound. 1728ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult BoundExpr; 1729ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(UnqAT)) 1730ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr = Owned(IntegerLiteral::Create(Context, CAT->getSize(), 17311dd986dff9ddfbec687975700770bb377988e9edRichard Trieu Context.getPointerDiffType(), 17321dd986dff9ddfbec687975700770bb377988e9edRichard Trieu RangeLoc)); 1733ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith else if (const VariableArrayType *VAT = 1734ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith dyn_cast<VariableArrayType>(UnqAT)) 1735ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr = VAT->getSizeExpr(); 1736ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith else { 1737ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Can't be a DependentSizedArrayType or an IncompleteArrayType since 1738ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // UnqAT is not incomplete and Range is not type-dependent. 1739b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Unexpected array type in for-range"); 1740ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1741ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1742ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // end-expr is __range + __bound. 1743e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek EndExpr = ActOnBinOp(S, ColonLoc, tok::plus, EndRangeRef.get(), 1744ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr.get()); 1745ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (EndExpr.isInvalid()) 1746ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1747ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(*this, EndVar, EndExpr.get(), ColonLoc, 1748ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_iter_deduction_failure)) { 1749ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1750ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1751ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1752ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } else { 1753ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclarationNameInfo BeginNameInfo(&PP.getIdentifierTable().get("begin"), 1754ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ColonLoc); 1755ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclarationNameInfo EndNameInfo(&PP.getIdentifierTable().get("end"), 1756ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ColonLoc); 1757ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1758ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupResult BeginMemberLookup(*this, BeginNameInfo, LookupMemberName); 1759ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupResult EndMemberLookup(*this, EndNameInfo, LookupMemberName); 1760ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1761ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (CXXRecordDecl *D = RangeType->getAsCXXRecordDecl()) { 1762ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // - if _RangeT is a class type, the unqualified-ids begin and end are 1763ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // looked up in the scope of class _RangeT as if by class member access 1764ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // lookup (3.4.5), and if either (or both) finds at least one 1765ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // declaration, begin-expr and end-expr are __range.begin() and 1766ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // __range.end(), respectively; 1767ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupQualifiedName(BeginMemberLookup, D); 1768ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LookupQualifiedName(EndMemberLookup, D); 1769ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1770ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (BeginMemberLookup.empty() != EndMemberLookup.empty()) { 1771ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Diag(ColonLoc, diag::err_for_range_member_begin_end_mismatch) 1772ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << RangeType << BeginMemberLookup.empty(); 1773ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1774ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1775ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } else { 1776ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // - otherwise, begin-expr and end-expr are begin(__range) and 1777ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // end(__range), respectively, where begin and end are looked up with 1778ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // argument-dependent lookup (3.4.2). For the purposes of this name 1779ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // lookup, namespace std is an associated namespace. 1780ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1781ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1782ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginExpr = BuildForRangeBeginEndCall(*this, S, ColonLoc, BeginVar, 1783ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_begin, BeginNameInfo, 1784e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginMemberLookup, 1785e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginRangeRef.get()); 1786ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (BeginExpr.isInvalid()) 1787ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1788ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1789ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith EndExpr = BuildForRangeBeginEndCall(*this, S, ColonLoc, EndVar, 1790ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BEF_end, EndNameInfo, 1791e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek EndMemberLookup, EndRangeRef.get()); 1792ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (EndExpr.isInvalid()) 1793ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1794ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1795ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1796ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // C++0x [decl.spec.auto]p6: BeginType and EndType must be the same. 1797ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType BeginType = BeginVar->getType(), EndType = EndVar->getType(); 1798ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Context.hasSameType(BeginType, EndType)) { 1799ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Diag(RangeLoc, diag::err_for_range_begin_end_types_differ) 1800ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << BeginType << EndType; 1801ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1802ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1803ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1804ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1805ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl *BeginEndDecls[] = { BeginVar, EndVar }; 1806ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Claim the type doesn't contain auto: we've already done the checking. 1807ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclGroupPtrTy BeginEndGroup = 1808ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BuildDeclaratorGroup(BeginEndDecls, 2, /*TypeMayContainAuto=*/false); 1809ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginEndDecl = ActOnDeclStmt(BeginEndGroup, ColonLoc, ColonLoc); 1810ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1811e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek const QualType BeginRefNonRefType = BeginType.getNonReferenceType(); 1812e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek ExprResult BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, 1813ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VK_LValue, ColonLoc); 1814e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRef.isInvalid()) 1815e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1816e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1817ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult EndRef = BuildDeclRefExpr(EndVar, EndType.getNonReferenceType(), 1818ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VK_LValue, ColonLoc); 1819e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (EndRef.isInvalid()) 1820e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1821ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1822ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check __begin != __end expression. 1823ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnBinOp(S, ColonLoc, tok::exclaimequal, 1824ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginRef.get(), EndRef.get()); 1825ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnBooleanCondition(S, ColonLoc, NotEqExpr.get()); 1826ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnFinishFullExpr(NotEqExpr.get()); 1827ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (NotEqExpr.isInvalid()) { 1828ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1829ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Context.hasSameType(BeginType, EndType)) 1830ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1831ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1832ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1833ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1834ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check ++__begin expression. 1835e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, 1836e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1837e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRef.isInvalid()) 1838e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1839e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1840ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IncrExpr = ActOnUnaryOp(S, ColonLoc, tok::plusplus, BeginRef.get()); 1841ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IncrExpr = ActOnFinishFullExpr(IncrExpr.get()); 1842ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (IncrExpr.isInvalid()) { 1843ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1844ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1845ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1846ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1847ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check *__begin expression. 1848e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, 1849e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1850e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRef.isInvalid()) 1851e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1852e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1853ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult DerefExpr = ActOnUnaryOp(S, ColonLoc, tok::star, BeginRef.get()); 1854ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DerefExpr.isInvalid()) { 1855ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1856ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1857ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1858ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1859ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Attach *__begin as initializer for VD. 1860ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!LoopVar->isInvalidDecl()) { 1861ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith AddInitializerToDecl(LoopVar, DerefExpr.get(), /*DirectInit=*/false, 1862ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*TypeMayContainAuto=*/true); 1863ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (LoopVar->isInvalidDecl()) 1864ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1865ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1866cd6f36693ed40cef8c8639c04438d865187c1f73Richard Smith } else { 1867cd6f36693ed40cef8c8639c04438d865187c1f73Richard Smith // The range is implicitly used as a placeholder when it is dependent. 1868cd6f36693ed40cef8c8639c04438d865187c1f73Richard Smith RangeVar->setUsed(); 1869ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1870ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1871ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return Owned(new (Context) CXXForRangeStmt(RangeDS, 1872ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith cast_or_null<DeclStmt>(BeginEndDecl.get()), 1873ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr.take(), IncrExpr.take(), 1874ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LoopVarDS, /*Body=*/0, ForLoc, 1875ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ColonLoc, RParenLoc)); 1876ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1877ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1878ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// FinishCXXForRangeStmt - Attach the body to a C++0x for-range statement. 1879ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// This is a separate step from ActOnCXXForRangeStmt because analysis of the 1880ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// body cannot be performed until after the type of the range variable is 1881ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// determined. 1882ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult Sema::FinishCXXForRangeStmt(Stmt *S, Stmt *B) { 1883ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!S || !B) 1884ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1885ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1886625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko CXXForRangeStmt *ForStmt = cast<CXXForRangeStmt>(S); 1887625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko ForStmt->setBody(B); 1888625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 1889625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko DiagnoseEmptyStmtBody(ForStmt->getRParenLoc(), B, 1890625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko diag::warn_empty_range_based_for_body); 1891625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 1892ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return S; 1893ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1894ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 189557ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerStmtResult Sema::ActOnGotoStmt(SourceLocation GotoLoc, 189657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner SourceLocation LabelLoc, 189757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner LabelDecl *TheDecl) { 189857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner getCurFunction()->setHasBranchIntoScope(); 1899ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner TheDecl->setUsed(); 1900ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner return Owned(new (Context) GotoStmt(TheDecl, GotoLoc, LabelLoc)); 19015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 19025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 190360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1904ad56d684259f706b7c0ae5ad9c23adb4f2926817Chris LattnerSema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc, 19059ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *E) { 1906bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman // Convert operand to void* 19075f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor if (!E->isTypeDependent()) { 19085f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor QualType ETy = E->getType(); 19092877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth QualType DestTy = Context.getPointerType(Context.VoidTy.withConst()); 1910429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult ExprRes = Owned(E); 19115f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor AssignConvertType ConvTy = 1912429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley CheckSingleAssignmentConstraints(DestTy, ExprRes); 1913429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (ExprRes.isInvalid()) 1914429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 1915429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley E = ExprRes.take(); 19162877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing)) 19175f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor return StmtError(); 1918d29975fd08713eb9d1777e60536addaa62df8995Eli Friedman E = MaybeCreateExprWithCleanups(E); 19195f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor } 1920b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 1921781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasIndirectGoto(); 1922b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 19235f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E)); 19245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 19255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 192660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 19271b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { 19285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Scope *S = CurScope->getContinueParent(); 19295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!S) { 19305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.6.2p1: A break shall appear only in or as a loop body. 19314cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop)); 19325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19334cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 19348189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ContinueStmt(ContinueLoc)); 19355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 19365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 193760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 19381b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { 19395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Scope *S = CurScope->getBreakParent(); 19405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!S) { 19415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. 19424cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch)); 19435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19444cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 19458189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) BreakStmt(BreakLoc)); 19465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 19475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1948dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Determine whether the given expression is a candidate for 1949f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// copy elision in either a return statement or a throw expression. 19505077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 1951f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param ReturnType If we're determining the copy elision candidate for 1952f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// a return statement, this is the return type of the function. If we're 1953f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// determining the copy elision candidate for a throw expression, this will 1954f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// be a NULL type. 19555077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 1956f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param E The expression being returned from the function or block, or 1957f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// being thrown. 19585077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 19594926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// \param AllowFunctionParameter Whether we allow function parameters to 19604926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// be considered NRVO candidates. C++ prohibits this for NRVO itself, but 19614926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// we re-use this logic to determine whether we should try to move as part of 19624926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// a return or throw (which does allow function parameters). 19635077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 19645077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \returns The NRVO candidate variable, if the return statement may use the 19655077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// NRVO, or NULL if there is no such candidate. 1966f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregorconst VarDecl *Sema::getCopyElisionCandidate(QualType ReturnType, 1967f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor Expr *E, 1968f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor bool AllowFunctionParameter) { 1969f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor QualType ExprType = E->getType(); 19703c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor // - in a return statement in a function with ... 19713c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor // ... a class return type ... 1972f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!ReturnType.isNull()) { 1973f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!ReturnType->isRecordType()) 1974f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor return 0; 1975f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor // ... the same cv-unqualified type as the function return type ... 1976f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!Context.hasSameUnqualifiedType(ReturnType, ExprType)) 1977f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor return 0; 1978f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor } 1979dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1980dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // ... the expression is the name of a non-volatile automatic object 1981f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor // (other than a function or catch-clause parameter)) ... 1982f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()); 19833c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor if (!DR) 19845077c3876beeaed32280af88244e8050078619a8Douglas Gregor return 0; 19853c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); 19863c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor if (!VD) 19875077c3876beeaed32280af88244e8050078619a8Douglas Gregor return 0; 1988dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 19891cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // ...object (other than a function or catch-clause parameter)... 19901cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->getKind() != Decl::Var && 19911cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall !(AllowFunctionParameter && VD->getKind() == Decl::ParmVar)) 19921cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall return 0; 19931cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->isExceptionVariable()) return 0; 19941cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 19951cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // ...automatic... 19961cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (!VD->hasLocalStorage()) return 0; 19971cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 19981cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // ...non-volatile... 19991cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->getType().isVolatileQualified()) return 0; 20001cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->getType()->isReferenceType()) return 0; 20011cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 20021cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // __block variables can't be allocated in a way that permits NRVO. 20031cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->hasAttr<BlocksAttr>()) return 0; 20041cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 20051cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // Variables with higher required alignment than their type's ABI 20061cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // alignment cannot use NRVO. 20071cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->hasAttr<AlignedAttr>() && 20081cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall Context.getDeclAlign(VD) > Context.getTypeAlignInChars(VD->getType())) 20091cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall return 0; 2010dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 20111cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall return VD; 20123c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor} 20133c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor 201407f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor/// \brief Perform the initialization of a potentially-movable value, which 201507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor/// is the result of return value. 2016cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// 2017cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// This routine implements C++0x [class.copy]p33, which attempts to treat 2018cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// returned lvalues as rvalues in certain cases (to prefer move construction), 2019cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// then falls back to treating them as lvalues if that failed. 2020dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiExprResult 202107f402cff25354c5f06f307f19b0c57c09d964bdDouglas GregorSema::PerformMoveOrCopyInitialization(const InitializedEntity &Entity, 202207f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor const VarDecl *NRVOCandidate, 202307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor QualType ResultType, 2024bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor Expr *Value, 2025bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor bool AllowNRVO) { 2026cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // C++0x [class.copy]p33: 2027dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // When the criteria for elision of a copy operation are met or would 2028dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // be met save for the fact that the source object is a function 2029dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // parameter, and the object to be copied is designated by an lvalue, 2030cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // overload resolution to select the constructor for the copy is first 2031cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // performed as if the object were designated by an rvalue. 2032cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor ExprResult Res = ExprError(); 2033bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor if (AllowNRVO && 2034bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor (NRVOCandidate || getCopyElisionCandidate(ResultType, Value, true))) { 2035dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi ImplicitCastExpr AsRvalue(ImplicitCastExpr::OnStack, 203607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value->getType(), CK_LValueToRValue, 203707f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value, VK_XValue); 2038dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2039cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor Expr *InitExpr = &AsRvalue; 2040dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi InitializationKind Kind 204107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor = InitializationKind::CreateCopy(Value->getLocStart(), 204207f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value->getLocStart()); 204307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor InitializationSequence Seq(*this, Entity, Kind, &InitExpr, 1); 2044dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2045dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // [...] If overload resolution fails, or if the type of the first 2046cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // parameter of the selected constructor is not an rvalue reference 20470099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi // to the object's type (possibly cv-qualified), overload resolution 2048cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // is performed again, considering the object as an lvalue. 2049383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl if (Seq) { 2050cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor for (InitializationSequence::step_iterator Step = Seq.step_begin(), 2051cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor StepEnd = Seq.step_end(); 2052cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor Step != StepEnd; ++Step) { 2053383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl if (Step->Kind != InitializationSequence::SK_ConstructorInitialization) 2054cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor continue; 2055dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2056dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi CXXConstructorDecl *Constructor 2057cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor = cast<CXXConstructorDecl>(Step->Function.Function); 2058dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2059cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor const RValueReferenceType *RRefType 206007f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor = Constructor->getParamDecl(0)->getType() 206107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor ->getAs<RValueReferenceType>(); 2062dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2063cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // If we don't meet the criteria, break out now. 2064dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (!RRefType || 206507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor !Context.hasSameUnqualifiedType(RRefType->getPointeeType(), 206607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Context.getTypeDeclType(Constructor->getParent()))) 2067cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor break; 2068dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2069cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Promote "AsRvalue" to the heap, since we now need this 2070cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // expression node to persist. 207107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value = ImplicitCastExpr::Create(Context, Value->getType(), 2072dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi CK_LValueToRValue, Value, 0, 207307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor VK_XValue); 2074dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2075cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Complete type-checking the initialization of the return type 2076cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // using the constructor we found. 207707f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Res = Seq.Perform(*this, Entity, Kind, MultiExprArg(&Value, 1)); 2078cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 2079cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 2080cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 2081dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2082cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Either we didn't meet the criteria for treating an lvalue as an rvalue, 2083dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // above, or overload resolution failed. Either way, we need to try 2084cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // (again) now with the return value expression as written. 2085cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor if (Res.isInvalid()) 208607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Res = PerformCopyInitialization(Entity, SourceLocation(), Value); 2087dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2088cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor return Res; 2089cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor} 2090cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor 209184b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman/// ActOnCapScopeReturnStmt - Utility routine to type-check return statements 209284b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman/// for capturing scopes. 20934eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff/// 209460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 209584b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli FriedmanSema::ActOnCapScopeReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { 209684b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman // If this is the first return we've seen, infer the return type. 209784b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman // [expr.prim.lambda]p4 in C++11; block literals follow a superset of those 209884b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman // rules which allows multiple return statements. 209984b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman CapturingScopeInfo *CurCap = cast<CapturingScopeInfo>(getCurFunction()); 210084b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman if (CurCap->HasImplicitReturnType) { 210184b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman QualType ReturnT; 2102a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor if (RetValExp && !isa<InitListExpr>(RetValExp)) { 2103429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultFunctionArrayLvalueConversion(RetValExp); 2104429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 2105429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 2106429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley RetValExp = Result.take(); 21076a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor 210884b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman if (!RetValExp->isTypeDependent()) 210984b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman ReturnT = RetValExp->getType(); 211084b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman else 211184b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman ReturnT = Context.DependentTy; 2112a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor } else { 2113a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor if (RetValExp) { 2114a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor // C++11 [expr.lambda.prim]p4 bans inferring the result from an 2115a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor // initializer list, because it is not an expression (even 2116a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor // though we represent it as one). We still deduce 'void'. 2117a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor Diag(ReturnLoc, diag::err_lambda_return_init_list) 2118a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor << RetValExp->getSourceRange(); 2119a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor } 2120a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor 212184b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman ReturnT = Context.VoidTy; 212284b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman } 212384b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman // We require the return types to strictly match here. 212484b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman if (!CurCap->ReturnType.isNull() && 212584b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman !CurCap->ReturnType->isDependentType() && 212684b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman !ReturnT->isDependentType() && 212784b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman !Context.hasSameType(ReturnT, CurCap->ReturnType)) { 212884b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman Diag(ReturnLoc, diag::err_typecheck_missing_return_type_incompatible) 21294e88df72e690498aeba2dc3d5089388b27be66faDouglas Gregor << ReturnT << CurCap->ReturnType 21300bcc3d87223d7796946bfa6dd6174af900d8eba4Douglas Gregor << (getCurLambda() != 0); 213184b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman return StmtError(); 2132649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian } 213384b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman CurCap->ReturnType = ReturnT; 21344eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff } 213584b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman QualType FnRetType = CurCap->ReturnType; 213684b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman assert(!FnRetType.isNull()); 21374cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 2138793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor if (BlockScopeInfo *CurBlock = dyn_cast<BlockScopeInfo>(CurCap)) { 213984b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman if (CurBlock->FunctionType->getAs<FunctionType>()->getNoReturnAttr()) { 214084b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr); 214184b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman return StmtError(); 214284b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman } 2143793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor } else { 2144793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CurCap); 2145793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor if (LSI->CallOperator->getType()->getAs<FunctionType>()->getNoReturnAttr()){ 2146793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor Diag(ReturnLoc, diag::err_noreturn_lambda_has_return_expr); 2147793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor return StmtError(); 2148793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor } 2149793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor } 21506c92fa75e62937f9738696840efcb258560f4568Mike Stump 21514eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // Otherwise, verify that this result type matches the previous one. We are 21524eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // pickier with blocks than for normal functions because we don't have GCC 21534eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // compatibility to worry about here. 2154d963c37eb912342c8325048749e449861cf0a6e3John McCall const VarDecl *NRVOCandidate = 0; 21550a7efe1142d241678c91bf93ee6adb51289863a4John McCall if (FnRetType->isDependentType()) { 21560a7efe1142d241678c91bf93ee6adb51289863a4John McCall // Delay processing for now. TODO: there are lots of dependent 21570a7efe1142d241678c91bf93ee6adb51289863a4John McCall // types we can conclusively prove aren't void. 21580a7efe1142d241678c91bf93ee6adb51289863a4John McCall } else if (FnRetType->isVoidType()) { 21595b38a0f98e4420dae1bd3e13959bc207c97a9e98Sebastian Redl if (RetValExp && !isa<InitListExpr>(RetValExp) && 21604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !(getLangOpts().CPlusPlus && 21610a7efe1142d241678c91bf93ee6adb51289863a4John McCall (RetValExp->isTypeDependent() || 21620a7efe1142d241678c91bf93ee6adb51289863a4John McCall RetValExp->getType()->isVoidType()))) { 21634e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian if (!getLangOpts().CPlusPlus && 21644e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian RetValExp->getType()->isVoidType()) 21659354f6aaa70e1543d122644fee0c3f834324d2fcFariborz Jahanian Diag(ReturnLoc, diag::ext_return_has_void_expr) << "literal" << 2; 21664e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian else { 21674e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian Diag(ReturnLoc, diag::err_return_block_has_expr); 21684e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian RetValExp = 0; 21694e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian } 21704eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff } 21715077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else if (!RetValExp) { 21720a7efe1142d241678c91bf93ee6adb51289863a4John McCall return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr)); 21730a7efe1142d241678c91bf93ee6adb51289863a4John McCall } else if (!RetValExp->isTypeDependent()) { 21740a7efe1142d241678c91bf93ee6adb51289863a4John McCall // we have a non-void block with an expression, continue checking 21750a7efe1142d241678c91bf93ee6adb51289863a4John McCall 21760a7efe1142d241678c91bf93ee6adb51289863a4John McCall // C99 6.8.6.4p3(136): The return statement is not an assignment. The 21770a7efe1142d241678c91bf93ee6adb51289863a4John McCall // overlap restriction of subclause 6.5.16.1 does not apply to the case of 21780a7efe1142d241678c91bf93ee6adb51289863a4John McCall // function return. 21790a7efe1142d241678c91bf93ee6adb51289863a4John McCall 21800a7efe1142d241678c91bf93ee6adb51289863a4John McCall // In C++ the return statement is handled via a copy initialization. 21810a7efe1142d241678c91bf93ee6adb51289863a4John McCall // the C version of which boils down to CheckSingleAssignmentConstraints. 21820a7efe1142d241678c91bf93ee6adb51289863a4John McCall NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); 21830a7efe1142d241678c91bf93ee6adb51289863a4John McCall InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, 21840a7efe1142d241678c91bf93ee6adb51289863a4John McCall FnRetType, 21850586520acb2f368c874943353a222be7f00c3068Fariborz Jahanian NRVOCandidate != 0); 21860a7efe1142d241678c91bf93ee6adb51289863a4John McCall ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, 21870a7efe1142d241678c91bf93ee6adb51289863a4John McCall FnRetType, RetValExp); 21880a7efe1142d241678c91bf93ee6adb51289863a4John McCall if (Res.isInvalid()) { 21890a7efe1142d241678c91bf93ee6adb51289863a4John McCall // FIXME: Cleanup temporaries here, anyway? 21900a7efe1142d241678c91bf93ee6adb51289863a4John McCall return StmtError(); 2191c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson } 21920a7efe1142d241678c91bf93ee6adb51289863a4John McCall RetValExp = Res.take(); 21930a7efe1142d241678c91bf93ee6adb51289863a4John McCall CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); 2194d963c37eb912342c8325048749e449861cf0a6e3John McCall } 2195dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2196d963c37eb912342c8325048749e449861cf0a6e3John McCall if (RetValExp) { 2197d963c37eb912342c8325048749e449861cf0a6e3John McCall CheckImplicitConversions(RetValExp, ReturnLoc); 2198d963c37eb912342c8325048749e449861cf0a6e3John McCall RetValExp = MaybeCreateExprWithCleanups(RetValExp); 219998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump } 22000a7efe1142d241678c91bf93ee6adb51289863a4John McCall ReturnStmt *Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 22010a7efe1142d241678c91bf93ee6adb51289863a4John McCall NRVOCandidate); 22024cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 2203dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // If we need to check for the named return value optimization, save the 22045077c3876beeaed32280af88244e8050078619a8Douglas Gregor // return statement in our scope for later processing. 22054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus && FnRetType->isRecordType() && 22065077c3876beeaed32280af88244e8050078619a8Douglas Gregor !CurContext->isDependentContext()) 22075077c3876beeaed32280af88244e8050078619a8Douglas Gregor FunctionScopes.back()->Returns.push_back(Result); 2208dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 22095077c3876beeaed32280af88244e8050078619a8Douglas Gregor return Owned(Result); 22104eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff} 22115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 221260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 22139ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { 2214fc92137eee708b632c00a9b0934ff87aeae234a5Douglas Gregor // Check for unexpanded parameter packs. 2215fc92137eee708b632c00a9b0934ff87aeae234a5Douglas Gregor if (RetValExp && DiagnoseUnexpandedParameterPack(RetValExp)) 2216fc92137eee708b632c00a9b0934ff87aeae234a5Douglas Gregor return StmtError(); 2217fc92137eee708b632c00a9b0934ff87aeae234a5Douglas Gregor 221884b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman if (isa<CapturingScopeInfo>(getCurFunction())) 221984b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman return ActOnCapScopeReturnStmt(ReturnLoc, RetValExp); 22204cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 2221371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner QualType FnRetType; 222238ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman QualType RelatedRetType; 2223f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump if (const FunctionDecl *FD = getCurFunctionDecl()) { 2224371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner FnRetType = FD->getResultType(); 222504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall if (FD->hasAttr<NoReturnAttr>() || 222604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall FD->getType()->getAs<FunctionType>()->getNoReturnAttr()) 22278662587fa75d3fb04f873e265841c9314c7f5523Chris Lattner Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr) 222879430e9983f5e67a378fc1f50cd6278f2cea8259Eli Friedman << FD->getDeclName(); 2229926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } else if (ObjCMethodDecl *MD = getCurMethodDecl()) { 223038ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman FnRetType = MD->getResultType(); 2231926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (MD->hasRelatedResultType() && MD->getClassInterface()) { 2232926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // In the implementation of a method with a related return type, the 2233926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // type used to type-check the validity of return statements within the 2234926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // method body is a pointer to the type of the class being implemented. 223538ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman RelatedRetType = Context.getObjCInterfaceType(MD->getClassInterface()); 223638ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman RelatedRetType = Context.getObjCObjectPointerType(RelatedRetType); 2237926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } 2238926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } else // If we don't have a function/method context, bail. 2239c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff return StmtError(); 22401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22415077c3876beeaed32280af88244e8050078619a8Douglas Gregor ReturnStmt *Result = 0; 22425cf216b7fa64b933b60743b0b26053e8e7aa87beChris Lattner if (FnRetType->isVoidType()) { 22438d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (RetValExp) { 224433deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl if (isa<InitListExpr>(RetValExp)) { 224533deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl // We simply never allow init lists as the return value of void 224633deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl // functions. This is compatible because this was never allowed before, 224733deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl // so there's no legacy code to deal with. 224833deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); 224933deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl int FunctionKind = 0; 225033deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl if (isa<ObjCMethodDecl>(CurDecl)) 225133deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl FunctionKind = 1; 225233deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl else if (isa<CXXConstructorDecl>(CurDecl)) 225333deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl FunctionKind = 2; 225433deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl else if (isa<CXXDestructorDecl>(CurDecl)) 225533deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl FunctionKind = 3; 225633deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl 225733deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl Diag(ReturnLoc, diag::err_return_init_list) 225833deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl << CurDecl->getDeclName() << FunctionKind 225933deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl << RetValExp->getSourceRange(); 226033deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl 226133deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl // Drop the expression. 226233deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl RetValExp = 0; 226333deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl } else if (!RetValExp->isTypeDependent()) { 22648d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky // C99 6.8.6.4p1 (ext_ since GCC warns) 22658d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky unsigned D = diag::ext_return_has_expr; 22668d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (RetValExp->getType()->isVoidType()) 22678d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky D = diag::ext_return_has_void_expr; 22688d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky else { 22698d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky ExprResult Result = Owned(RetValExp); 22708d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky Result = IgnoredValueConversions(Result.take()); 22718d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (Result.isInvalid()) 22728d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky return StmtError(); 22738d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky RetValExp = Result.take(); 22748d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky RetValExp = ImpCastExprToType(RetValExp, 22758d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky Context.VoidTy, CK_ToVoid).take(); 22768d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky } 22774cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 22788d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky // return (some void expression); is legal in C++. 22798d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (D != diag::ext_return_has_void_expr || 22804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !getLangOpts().CPlusPlus) { 22818d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); 2282ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth 2283ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth int FunctionKind = 0; 2284ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth if (isa<ObjCMethodDecl>(CurDecl)) 2285ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth FunctionKind = 1; 2286ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth else if (isa<CXXConstructorDecl>(CurDecl)) 2287ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth FunctionKind = 2; 2288ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth else if (isa<CXXDestructorDecl>(CurDecl)) 2289ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth FunctionKind = 3; 2290ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth 22918d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky Diag(ReturnLoc, D) 2292ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth << CurDecl->getDeclName() << FunctionKind 22938d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky << RetValExp->getSourceRange(); 22948d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky } 2295e878eb035b343d7d819c092102364ec9849716aeChris Lattner } 22961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 229733deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl if (RetValExp) { 229833deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl CheckImplicitConversions(RetValExp, ReturnLoc); 229933deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl RetValExp = MaybeCreateExprWithCleanups(RetValExp); 230033deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl } 23015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2302dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 23035077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0); 23045077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else if (!RetValExp && !FnRetType->isDependentType()) { 23053c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4 23063c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner // C99 6.8.6.4p1 (ext_ since GCC warns) 23074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().C99) DiagID = diag::ext_return_missing_expr; 23083c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner 23093c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner if (FunctionDecl *FD = getCurFunctionDecl()) 231008631c5fa053867146b5ee8be658c229f6bf127cChris Lattner Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; 23113c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner else 231208631c5fa053867146b5ee8be658c229f6bf127cChris Lattner Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; 23135077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc); 23145077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else { 23155077c3876beeaed32280af88244e8050078619a8Douglas Gregor const VarDecl *NRVOCandidate = 0; 23165077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { 23175077c3876beeaed32280af88244e8050078619a8Douglas Gregor // we have a non-void function with an expression, continue checking 23185077c3876beeaed32280af88244e8050078619a8Douglas Gregor 231938ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman if (!RelatedRetType.isNull()) { 232038ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman // If we have a related result type, perform an extra conversion here. 232138ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman // FIXME: The diagnostics here don't really describe what is happening. 232238ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman InitializedEntity Entity = 232338ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman InitializedEntity::InitializeTemporary(RelatedRetType); 232438ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman 232538ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman ExprResult Res = PerformCopyInitialization(Entity, SourceLocation(), 232638ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman RetValExp); 232738ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman if (Res.isInvalid()) { 232838ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman // FIXME: Cleanup temporaries here, anyway? 232938ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman return StmtError(); 233038ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman } 233138ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman RetValExp = Res.takeAs<Expr>(); 233238ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman } 233338ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman 23345077c3876beeaed32280af88244e8050078619a8Douglas Gregor // C99 6.8.6.4p3(136): The return statement is not an assignment. The 23355077c3876beeaed32280af88244e8050078619a8Douglas Gregor // overlap restriction of subclause 6.5.16.1 does not apply to the case of 23365077c3876beeaed32280af88244e8050078619a8Douglas Gregor // function return. 23375077c3876beeaed32280af88244e8050078619a8Douglas Gregor 2338856d3798af7c2f7251e4a295f3da7a09ce4c62abJohn McCall // In C++ the return statement is handled via a copy initialization, 23395077c3876beeaed32280af88244e8050078619a8Douglas Gregor // the C version of which boils down to CheckSingleAssignmentConstraints. 2340f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); 2341dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, 234207f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor FnRetType, 234358f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet NRVOCandidate != 0); 2344dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, 234507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor FnRetType, RetValExp); 23465077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (Res.isInvalid()) { 23475077c3876beeaed32280af88244e8050078619a8Douglas Gregor // FIXME: Cleanup temporaries here, anyway? 23485077c3876beeaed32280af88244e8050078619a8Douglas Gregor return StmtError(); 23495077c3876beeaed32280af88244e8050078619a8Douglas Gregor } 23504cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 23515077c3876beeaed32280af88244e8050078619a8Douglas Gregor RetValExp = Res.takeAs<Expr>(); 2352dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (RetValExp) 23535077c3876beeaed32280af88244e8050078619a8Douglas Gregor CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); 235466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor } 2355dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2356b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall if (RetValExp) { 2357b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(RetValExp, ReturnLoc); 23584765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall RetValExp = MaybeCreateExprWithCleanups(RetValExp); 2359b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall } 23605077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate); 2361898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor } 2362dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2363dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // If we need to check for the named return value optimization, save the 23645077c3876beeaed32280af88244e8050078619a8Douglas Gregor // return statement in our scope for later processing. 23654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus && FnRetType->isRecordType() && 23665077c3876beeaed32280af88244e8050078619a8Douglas Gregor !CurContext->isDependentContext()) 23675077c3876beeaed32280af88244e8050078619a8Douglas Gregor FunctionScopes.back()->Returns.push_back(Result); 2368f85e193739c953358c865005855253af4f68a497John McCall 23695077c3876beeaed32280af88244e8050078619a8Douglas Gregor return Owned(Result); 23705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 23715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2372810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently 2373810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// ignore "noop" casts in places where an lvalue is required by an inline asm. 2374810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// We emulate this behavior when -fheinous-gnu-extensions is specified, but 2375810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// provide a strong guidance to not use it. 2376810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// 2377810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// This method checks to see if the argument is an acceptable l-value and 2378810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// returns false if it is a case we can handle. 2379810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattnerstatic bool CheckAsmLValue(const Expr *E, Sema &S) { 2380703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson // Type dependent expressions will be checked during instantiation. 2381703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson if (E->isTypeDependent()) 2382703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson return false; 2383dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 23847eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall if (E->isLValue()) 2385810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return false; // Cool, this is an lvalue. 2386810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 2387810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // Okay, this is not an lvalue, but perhaps it is the result of a cast that we 2388810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // are supposed to allow. 2389810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); 23907eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall if (E != E2 && E2->isLValue()) { 23914e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!S.getLangOpts().HeinousExtensions) 2392810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue) 2393810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner << E->getSourceRange(); 2394810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner else 2395810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue) 2396810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner << E->getSourceRange(); 2397810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // Accept, even if we emitted an error diagnostic. 2398810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return false; 2399810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner } 2400810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 2401810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // None of the above, just randomly invalid non-lvalue. 2402810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return true; 2403810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner} 2404810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 2405ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner/// isOperandMentioned - Return true if the specified operand # is mentioned 2406ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner/// anywhere in the decomposed asm string. 2407ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattnerstatic bool isOperandMentioned(unsigned OpNo, 24082d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner ArrayRef<AsmStmt::AsmStringPiece> AsmStrPieces) { 2409ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) { 2410ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner const AsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; 2411ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner if (!Piece.isOperand()) continue; 2412ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner 2413ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // If this is a reference to the input and if the input was the smaller 2414ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // one, then we have to reject this asm. 2415ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner if (Piece.getOperandNo() == OpNo) 2416ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner return true; 2417ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner } 2418ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner 2419ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner return false; 2420ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner} 2421810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 2422ca57b4b7658a031b74cda5ac504311998be8e343Chris LattnerStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, bool IsSimple, 2423ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner bool IsVolatile, unsigned NumOutputs, 2424ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner unsigned NumInputs, IdentifierInfo **Names, 2425ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner MultiExprArg constraints, MultiExprArg exprs, 2426ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner Expr *asmString, MultiExprArg clobbers, 2427ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner SourceLocation RParenLoc, bool MSAsm) { 24283037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl unsigned NumClobbers = clobbers.size(); 24293037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl StringLiteral **Constraints = 24303037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl reinterpret_cast<StringLiteral**>(constraints.get()); 24319ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr **Exprs = exprs.get(); 24329ae2f076ca5ab1feb3ba95629099ec2319833701John McCall StringLiteral *AsmString = cast<StringLiteral>(asmString); 24333037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get()); 24343037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 24355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; 24361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24371708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner // The parser verifies that there is a string literal here. 24385cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (!AsmString->isAscii()) 24393037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) 24403037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << AsmString->getSourceRange()); 24413037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 24421708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner for (unsigned i = 0; i != NumOutputs; i++) { 24431708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Constraints[i]; 24445cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (!Literal->isAscii()) 24453037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 24463037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 24473037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 24485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef OutputName; 2449ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson if (Names[i]) 2450ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson OutputName = Names[i]->getName(); 2451ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson 2452ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); 2453bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor if (!Context.getTargetInfo().validateOutputConstraint(Info)) 24543037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 2455432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner diag::err_asm_invalid_output_constraint) 2456432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner << Info.getConstraintStr()); 24573037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 2458d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson // Check that the output exprs are valid lvalues. 245972056a237c536ee63285ab0850cb50f299281767Eli Friedman Expr *OutputExpr = Exprs[i]; 2460810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (CheckAsmLValue(OutputExpr, *this)) { 246172056a237c536ee63285ab0850cb50f299281767Eli Friedman return StmtError(Diag(OutputExpr->getLocStart(), 2462dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner diag::err_asm_invalid_lvalue_in_output) 246372056a237c536ee63285ab0850cb50f299281767Eli Friedman << OutputExpr->getSourceRange()); 246404728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 24651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 246644def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner OutputConstraintInfos.push_back(Info); 246704728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 24683037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 24695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; 2470806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner 247104728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { 24721708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Constraints[i]; 24735cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (!Literal->isAscii()) 24743037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 24753037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 24763037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 24775f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef InputName; 2478ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson if (Names[i]) 2479ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson InputName = Names[i]->getName(); 2480ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson 2481ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); 2482bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos.data(), 24832819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner NumOutputs, Info)) { 24843037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 2485432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner diag::err_asm_invalid_input_constraint) 2486432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner << Info.getConstraintStr()); 2487d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson } 24883037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 248972056a237c536ee63285ab0850cb50f299281767Eli Friedman Expr *InputExpr = Exprs[i]; 24903037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 2491d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson // Only allow void types for memory constraints. 249244def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner if (Info.allowsMemory() && !Info.allowsRegister()) { 2493810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (CheckAsmLValue(InputExpr, *this)) 249472056a237c536ee63285ab0850cb50f299281767Eli Friedman return StmtError(Diag(InputExpr->getLocStart(), 2495d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson diag::err_asm_invalid_lvalue_in_input) 2496432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner << Info.getConstraintStr() 249772056a237c536ee63285ab0850cb50f299281767Eli Friedman << InputExpr->getSourceRange()); 249804728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 24993037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 250044def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner if (Info.allowsRegister()) { 2501d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson if (InputExpr->getType()->isVoidType()) { 250272056a237c536ee63285ab0850cb50f299281767Eli Friedman return StmtError(Diag(InputExpr->getLocStart(), 2503d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson diag::err_asm_invalid_type_in_input) 25041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump << InputExpr->getType() << Info.getConstraintStr() 250572056a237c536ee63285ab0850cb50f299281767Eli Friedman << InputExpr->getSourceRange()); 2506d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson } 2507d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson } 25081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2509429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]); 2510429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 2511429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 25121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2513429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Exprs[i] = Result.take(); 2514806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner InputConstraintInfos.push_back(Info); 251504728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 25163037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 25176fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson // Check that the clobbers are valid. 25181708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner for (unsigned i = 0; i != NumClobbers; i++) { 25191708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Clobbers[i]; 25205cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (!Literal->isAscii()) 25213037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 25223037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 25233037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 25245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Clobber = Literal->getString(); 25253037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 2526bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor if (!Context.getTargetInfo().isValidClobber(Clobber)) 25273037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 25287765934ad7e157b5fcf925792a38e01b1edbcf8aDaniel Dunbar diag::err_asm_unknown_register_name) << Clobber); 25296fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson } 25303037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 2531fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner AsmStmt *NS = 2532dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, MSAsm, 2533dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi NumOutputs, NumInputs, Names, Constraints, Exprs, 2534966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson AsmString, NumClobbers, Clobbers, RParenLoc); 2535fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner // Validate the asm string, ensuring it makes sense given the operands we 2536fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner // have. 25375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<AsmStmt::AsmStringPiece, 8> Pieces; 2538fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner unsigned DiagOffs; 2539fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { 25402ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) 25412ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner << AsmString->getSourceRange(); 2542fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner return StmtError(); 2543fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner } 25441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2545806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // Validate tied input operands for type mismatches. 2546806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { 2547806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; 25481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2549806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // If this is a tied constraint, verify that the output and input have 2550806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // either exactly the same type, or that they are int/ptr operands with the 2551806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner // same size (int/long, int*/long, are ok etc). 2552806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner if (!Info.hasTiedOperand()) continue; 25531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2554806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner unsigned TiedTo = Info.getTiedOperand(); 2555935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner unsigned InputOpNo = i+NumOutputs; 2556f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner Expr *OutputExpr = Exprs[TiedTo]; 2557935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner Expr *InputExpr = Exprs[InputOpNo]; 2558f45b357c0d0df99e160a6320e279ef788dd91ba6Eli Friedman 2559f45b357c0d0df99e160a6320e279ef788dd91ba6Eli Friedman if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent()) 2560f45b357c0d0df99e160a6320e279ef788dd91ba6Eli Friedman continue; 2561f45b357c0d0df99e160a6320e279ef788dd91ba6Eli Friedman 25627adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner QualType InTy = InputExpr->getType(); 25637adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner QualType OutTy = OutputExpr->getType(); 25647adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner if (Context.hasSameType(InTy, OutTy)) 2565806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner continue; // All types can be tied to themselves. 25661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2567aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // Decide if the input and output are in the same domain (integer/ptr or 2568aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // floating point. 2569aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner enum AsmDomain { 2570aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner AD_Int, AD_FP, AD_Other 2571aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner } InputDomain, OutputDomain; 2572dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2573aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (InTy->isIntegerType() || InTy->isPointerType()) 2574aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain = AD_Int; 25750c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor else if (InTy->isRealFloatingType()) 2576aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain = AD_FP; 2577aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner else 2578aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain = AD_Other; 25793351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner 2580aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (OutTy->isIntegerType() || OutTy->isPointerType()) 2581aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputDomain = AD_Int; 25820c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor else if (OutTy->isRealFloatingType()) 2583aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputDomain = AD_FP; 2584aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner else 2585aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputDomain = AD_Other; 2586dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2587aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // They are ok if they are the same size and in the same domain. This 2588aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // allows tying things like: 2589aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // void* to int* 2590aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // void* to int if they are the same size. 2591aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // double to long double if they are the same size. 2592dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // 2593aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner uint64_t OutSize = Context.getTypeSize(OutTy); 2594aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner uint64_t InSize = Context.getTypeSize(InTy); 2595aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (OutSize == InSize && InputDomain == OutputDomain && 2596aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner InputDomain != AD_Other) 2597aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner continue; 2598dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2599aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // If the smaller input/output operand is not mentioned in the asm string, 2600f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner // then we can promote the smaller one to a larger input and the asm string 2601f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner // won't notice. 2602aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner bool SmallerValueMentioned = false; 2603ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner 2604ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // If this is a reference to the input and if the input was the smaller 2605ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // one, then we have to reject this asm. 2606935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner if (isOperandMentioned(InputOpNo, Pieces)) { 2607ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // This is a use in the asm string of the smaller operand. Since we 2608ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // codegen this by promoting to a wider value, the asm will get printed 2609ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // "wrong". 2610f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner SmallerValueMentioned |= InSize < OutSize; 2611ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner } 2612f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner if (isOperandMentioned(TiedTo, Pieces)) { 2613ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // If this is a reference to the output, and if the output is the larger 2614ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner // value, then it's ok because we'll promote the input to the larger type. 2615f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner SmallerValueMentioned |= OutSize < InSize; 2616806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner } 26171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2618aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // If the smaller value wasn't mentioned in the asm string, and if the 2619aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // output was a register, just extend the shorter one to the size of the 2620aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner // larger one. 2621aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner if (!SmallerValueMentioned && InputDomain != AD_Other && 2622aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner OutputConstraintInfos[TiedTo].allowsRegister()) 2623aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner continue; 2624f0c4d28020fb13fdbf7d6fccfab1b7b6a118ad0eChris Lattner 2625935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // Either both of the operands were mentioned or the smaller one was 2626935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // mentioned. One more special case that we'll allow: if the tied input is 2627935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // integer, unmentioned, and is a constant, then we'll allow truncating it 2628935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner // down to the size of the destination. 2629935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner if (InputDomain == AD_Int && OutputDomain == AD_Int && 2630935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner !isOperandMentioned(InputOpNo, Pieces) && 2631935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner InputExpr->isEvaluatable(Context)) { 26324da89c87b2b6ca31f1015ec19aae65a02971ea9aJohn McCall CastKind castKind = 26334da89c87b2b6ca31f1015ec19aae65a02971ea9aJohn McCall (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast); 26344da89c87b2b6ca31f1015ec19aae65a02971ea9aJohn McCall InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).take(); 2635935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner Exprs[InputOpNo] = InputExpr; 2636935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner NS->setInputExpr(i, InputExpr); 2637935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner continue; 2638935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner } 2639935f0f01c1ed3c2052b797ac035d57a85b78adc4Chris Lattner 2640c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner Diag(InputExpr->getLocStart(), 2641806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner diag::err_asm_tying_incompatible_types) 26427adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner << InTy << OutTy << OutputExpr->getSourceRange() 2643806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner << InputExpr->getSourceRange(); 2644806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner return StmtError(); 2645806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner } 26461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2647fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner return Owned(NS); 2648fe795956194141c91ae555985c9b930595bff43fChris Lattner} 26493b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian 265060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2651431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, 2652d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall SourceLocation RParen, Decl *Parm, 26539ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *Body) { 2654d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall VarDecl *Var = cast_or_null<VarDecl>(Parm); 2655160b5630aa781ac348303e1ae088d27016637778Douglas Gregor if (Var && Var->isInvalidDecl()) 2656160b5630aa781ac348303e1ae088d27016637778Douglas Gregor return StmtError(); 2657dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 26589ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtCatchStmt(AtLoc, RParen, Var, Body)); 26593b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian} 26603b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian 266160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 26629ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, Stmt *Body) { 26639ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, Body)); 2664161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian} 2665bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 266660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2667dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnObjCAtTryStmt(SourceLocation AtLoc, Stmt *Try, 26689ae2f076ca5ab1feb3ba95629099ec2319833701John McCall MultiStmtArg CatchStmts, Stmt *Finally) { 26694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().ObjCExceptions) 2670da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@try"; 2671da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson 2672781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchProtectedScope(); 26738f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor unsigned NumCatchStmts = CatchStmts.size(); 26749ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(ObjCAtTryStmt::Create(Context, AtLoc, Try, 26759ae2f076ca5ab1feb3ba95629099ec2319833701John McCall CatchStmts.release(), 26768f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor NumCatchStmts, 26779ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Finally)); 2678bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian} 2679bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 268060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc, 26819ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *Throw) { 2682d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (Throw) { 2683f2dd68fb37a62571a4985c8dd05310ac90d07ef0Fariborz Jahanian Throw = MaybeCreateExprWithCleanups(Throw); 2684429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultLvalueConversion(Throw); 2685429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 2686429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 26875e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall 2688429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Throw = Result.take(); 2689d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor QualType ThrowType = Throw->getType(); 2690d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor // Make sure the expression type is an ObjC pointer or "void *". 2691d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (!ThrowType->isDependentType() && 2692d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor !ThrowType->isObjCObjectPointerType()) { 2693d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor const PointerType *PT = ThrowType->getAs<PointerType>(); 2694d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (!PT || !PT->getPointeeType()->isVoidType()) 2695d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object) 2696d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor << Throw->getType() << Throw->getSourceRange()); 2697d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor } 2698d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor } 2699dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 27009ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtThrowStmt(AtLoc, Throw)); 2701d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor} 2702d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor 270360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2704dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw, 2705d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor Scope *CurScope) { 27064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().ObjCExceptions) 2707da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@throw"; 2708da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson 27099ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!Throw) { 2710e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff // @throw without an expression designates a rethrow (which much occur 2711e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff // in the context of an @catch clause). 2712e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff Scope *AtCatchParent = CurScope; 2713e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff while (AtCatchParent && !AtCatchParent->isAtCatchScope()) 2714e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff AtCatchParent = AtCatchParent->getParent(); 2715e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff if (!AtCatchParent) 27164ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch)); 2717dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 2718f2dd68fb37a62571a4985c8dd05310ac90d07ef0Fariborz Jahanian 27199ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return BuildObjCAtThrowStmt(AtLoc, Throw); 272039f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian} 2721bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 272207524039dce5c820f111a1b3f772b4261f004b4aJohn McCallExprResult 272307524039dce5c820f111a1b3f772b4261f004b4aJohn McCallSema::ActOnObjCAtSynchronizedOperand(SourceLocation atLoc, Expr *operand) { 272407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall ExprResult result = DefaultLvalueConversion(operand); 272507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall if (result.isInvalid()) 272607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall return ExprError(); 272707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall operand = result.take(); 27285e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall 2729a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner // Make sure the expression type is an ObjC pointer or "void *". 273007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall QualType type = operand->getType(); 273107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall if (!type->isDependentType() && 273207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall !type->isObjCObjectPointerType()) { 273307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall const PointerType *pointerType = type->getAs<PointerType>(); 273407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall if (!pointerType || !pointerType->getPointeeType()->isVoidType()) 273507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall return Diag(atLoc, diag::error_objc_synchronized_expects_object) 273607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall << type << operand->getSourceRange(); 2737a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner } 27381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 273907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall // The operand to @synchronized is a full-expression. 274007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall return MaybeCreateExprWithCleanups(operand); 274107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall} 274207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall 274307524039dce5c820f111a1b3f772b4261f004b4aJohn McCallStmtResult 274407524039dce5c820f111a1b3f772b4261f004b4aJohn McCallSema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, Expr *SyncExpr, 274507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall Stmt *SyncBody) { 274607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall // We can't jump into or indirect-jump out of a @synchronized block. 274707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall getCurFunction()->setHasBranchProtectedScope(); 27489ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, SyncExpr, SyncBody)); 2749fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian} 27504b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl 27514b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block 27524b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// and creates a proper catch handler from them. 275360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2754d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl, 27559ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *HandlerBlock) { 27564b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl // There's nothing to test that ActOnExceptionDecl didn't already test. 27578189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) CXXCatchStmt(CatchLoc, 2758d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall cast_or_null<VarDecl>(ExDecl), 27599ae2f076ca5ab1feb3ba95629099ec2319833701John McCall HandlerBlock)); 27604b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl} 27618351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 2762f85e193739c953358c865005855253af4f68a497John McCallStmtResult 2763f85e193739c953358c865005855253af4f68a497John McCallSema::ActOnObjCAutoreleasePoolStmt(SourceLocation AtLoc, Stmt *Body) { 2764f85e193739c953358c865005855253af4f68a497John McCall getCurFunction()->setHasBranchProtectedScope(); 2765f85e193739c953358c865005855253af4f68a497John McCall return Owned(new (Context) ObjCAutoreleasePoolStmt(AtLoc, Body)); 2766f85e193739c953358c865005855253af4f68a497John McCall} 2767f85e193739c953358c865005855253af4f68a497John McCall 27683c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace { 27693c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 2770c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlclass TypeWithHandler { 2771c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl QualType t; 2772c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl CXXCatchStmt *stmt; 2773c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlpublic: 2774c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler(const QualType &type, CXXCatchStmt *statement) 2775c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl : t(type), stmt(statement) {} 2776c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 27770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall // An arbitrary order is fine as long as it places identical 27780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall // types next to each other. 2779c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl bool operator<(const TypeWithHandler &y) const { 27800953e767ff7817f97b3ab20896b229891eeff45bJohn McCall if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr()) 2781c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return true; 27820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr()) 2783c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return false; 2784c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl else 2785c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return getTypeSpecStartLoc() < y.getTypeSpecStartLoc(); 2786c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 27871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2788c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl bool operator==(const TypeWithHandler& other) const { 27890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall return t == other.t; 2790c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 27911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2792c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl CXXCatchStmt *getCatchStmt() const { return stmt; } 2793c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl SourceLocation getTypeSpecStartLoc() const { 2794c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return stmt->getExceptionDecl()->getTypeSpecStartLoc(); 2795c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 2796c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl}; 2797c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 27983c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman} 27993c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 28008351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// ActOnCXXTryBlock - Takes a try compound-statement and a number of 28018351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// handlers and creates a try statement from them. 280260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 28039ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock, 28048351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl MultiStmtArg RawHandlers) { 2805729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson // Don't report an error if 'try' is used in system headers. 28064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().CXXExceptions && 2807729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson !getSourceManager().isInSystemHeader(TryLoc)) 2808729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson Diag(TryLoc, diag::err_exceptions_disabled) << "try"; 28097f11d9cf5df1f8ce82af46eabc4ec5cec7d580b0Anders Carlsson 28108351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl unsigned NumHandlers = RawHandlers.size(); 28118351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl assert(NumHandlers > 0 && 28128351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl "The parser shouldn't call this if there are no handlers."); 28139ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt **Handlers = RawHandlers.get(); 28148351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 28155f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<TypeWithHandler, 8> TypesWithHandlers; 28161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (unsigned i = 0; i < NumHandlers; ++i) { 28185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner CXXCatchStmt *Handler = cast<CXXCatchStmt>(Handlers[i]); 2819c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (!Handler->getExceptionDecl()) { 2820c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (i < NumHandlers - 1) 2821c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return StmtError(Diag(Handler->getLocStart(), 2822c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::err_early_catch_all)); 28231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2824c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl continue; 2825c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 28261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2827c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl const QualType CaughtType = Handler->getCaughtType(); 2828c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType); 2829c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler)); 2830c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 2831c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 2832c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl // Detect handlers for the same type as an earlier one. 2833c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (NumHandlers > 1) { 2834c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end()); 28351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2836c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler prev = TypesWithHandlers[0]; 2837c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) { 2838c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler curr = TypesWithHandlers[i]; 28391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2840c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (curr == prev) { 2841c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl Diag(curr.getTypeSpecStartLoc(), 2842c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::warn_exception_caught_by_earlier_handler) 2843c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl << curr.getCatchStmt()->getCaughtType().getAsString(); 2844c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl Diag(prev.getTypeSpecStartLoc(), 2845c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::note_previous_exception_handler) 2846c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl << prev.getCatchStmt()->getCaughtType().getAsString(); 2847c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 28481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2849c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl prev = curr; 2850c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 28518351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl } 28521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2853781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchProtectedScope(); 2854b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 28558351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // FIXME: We should detect handlers that cannot catch anything because an 28568351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // earlier handler catches a superclass. Need to find a method that is not 28578351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // quadratic for this. 28588351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // Neither of these are explicitly forbidden, but every compiler detects them 28598351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // and warns. 28608351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 28619ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(CXXTryStmt::Create(Context, TryLoc, TryBlock, 2862a1a396df16c02b22983b5c9592022fd9237d4866Sam Weinig Handlers, NumHandlers)); 28638351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl} 286428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 286528bbe4b8acc338476fe0825769b41fb32b423c72John WiegleyStmtResult 286628bbe4b8acc338476fe0825769b41fb32b423c72John WiegleySema::ActOnSEHTryBlock(bool IsCXXTry, 286728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley SourceLocation TryLoc, 286828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *TryBlock, 286928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *Handler) { 287028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley assert(TryBlock && Handler); 287128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 287228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley getCurFunction()->setHasBranchProtectedScope(); 287328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 287428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley return Owned(SEHTryStmt::Create(Context,IsCXXTry,TryLoc,TryBlock,Handler)); 287528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley} 287628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 287728bbe4b8acc338476fe0825769b41fb32b423c72John WiegleyStmtResult 287828bbe4b8acc338476fe0825769b41fb32b423c72John WiegleySema::ActOnSEHExceptBlock(SourceLocation Loc, 287928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Expr *FilterExpr, 288028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *Block) { 288128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley assert(FilterExpr && Block); 288228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 288328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley if(!FilterExpr->getType()->isIntegerType()) { 288458f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet return StmtError(Diag(FilterExpr->getExprLoc(), 288558f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet diag::err_filter_expression_integral) 288658f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet << FilterExpr->getType()); 288728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley } 288828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 288928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley return Owned(SEHExceptStmt::Create(Context,Loc,FilterExpr,Block)); 289028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley} 289128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 289228bbe4b8acc338476fe0825769b41fb32b423c72John WiegleyStmtResult 289328bbe4b8acc338476fe0825769b41fb32b423c72John WiegleySema::ActOnSEHFinallyBlock(SourceLocation Loc, 289428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *Block) { 289528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley assert(Block); 289628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley return Owned(SEHFinallyStmt::Create(Context,Loc,Block)); 289728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley} 2898ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor 2899ba0513de93d2fab6db5ab30b6927209fcc883078Douglas GregorStmtResult Sema::BuildMSDependentExistsStmt(SourceLocation KeywordLoc, 2900ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor bool IsIfExists, 2901ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor NestedNameSpecifierLoc QualifierLoc, 2902ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor DeclarationNameInfo NameInfo, 2903ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor Stmt *Nested) 2904ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor{ 2905ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor return new (Context) MSDependentExistsStmt(KeywordLoc, IsIfExists, 2906ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor QualifierLoc, NameInfo, 2907ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor cast<CompoundStmt>(Nested)); 2908ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor} 2909ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor 2910ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor 2911ba0513de93d2fab6db5ab30b6927209fcc883078Douglas GregorStmtResult Sema::ActOnMSDependentExistsStmt(SourceLocation KeywordLoc, 2912ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor bool IsIfExists, 2913ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor CXXScopeSpec &SS, 2914ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor UnqualifiedId &Name, 2915ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor Stmt *Nested) { 2916ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor return BuildMSDependentExistsStmt(KeywordLoc, IsIfExists, 2917ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor SS.getWithLocInContext(Context), 2918ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor GetNameFromUnqualifiedId(Name), 2919ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor Nested); 2920ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor} 2921