Sema.cpp revision f8cc02e50553b5c3bc6570bff0c47ac7db85fe8d
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
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 the actions class which performs semantic analysis and
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// builds an AST out of a parse stream.
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
159c3087b0b0bea2fd782205c1274ebfc4290265e0John McCall#include "clang/Sema/SemaInternal.h"
169c3087b0b0bea2fd782205c1274ebfc4290265e0John McCall#include "clang/Sema/DelayedDiagnostic.h"
1782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov#include "TargetAttributesSema.h"
18e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn#include "llvm/ADT/DenseMap.h"
19e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl#include "llvm/ADT/SmallSet.h"
20680523a91dd3351389667c8de17121ba7ae82673John McCall#include "llvm/ADT/APFloat.h"
219257664568bf375b7790131a84d9a4fa30a5b7e3John McCall#include "llvm/Support/CrashRecoveryContext.h"
225f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/CXXFieldCollector.h"
239b623639378d53a675921ddfa7316034d571881eDouglas Gregor#include "clang/Sema/TemplateDeduction.h"
2476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall#include "clang/Sema/ExternalSemaSource.h"
258c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl#include "clang/Sema/ObjCMethodList.h"
26f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
275f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/Scope.h"
28781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "clang/Sema/ScopeInfo.h"
2946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor#include "clang/Sema/SemaConsumer.h"
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
3179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/ASTDiagnostic.h"
32384aff8b94bb0d1ad6c5667b90621e5699815bb2John McCall#include "clang/AST/DeclCXX.h"
33f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper#include "clang/AST/DeclFriend.h"
34c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
35e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
36c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay#include "clang/AST/ExprCXX.h"
37ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner#include "clang/AST/StmtCXX.h"
3890db26000aefe9335370013eec64c85232d80227Douglas Gregor#include "clang/Lex/HeaderSearch.h"
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Preprocessor.h"
40f7572a64a1e5f6f700527583ec5206a58ce6d9b6Douglas Gregor#include "clang/Basic/FileManager.h"
4191a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
424d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner#include "clang/Basic/TargetInfo.h"
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
44781472fe99a120098c631b0cbe33c89f8cef5e70John McCallusing namespace sema;
459ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
469ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorFunctionScopeInfo::~FunctionScopeInfo() { }
479ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
488fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidisvoid FunctionScopeInfo::Clear() {
49b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasBranchProtectedScope = false;
50b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasBranchIntoScope = false;
51b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasIndirectGoto = false;
52b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
539ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  SwitchStack.clear();
545077c3876beeaed32280af88244e8050078619a8Douglas Gregor  Returns.clear();
558fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  ErrorTrap.reset();
56351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  PossiblyUnreachableDiags.clear();
579ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
589ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
599ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo::~BlockScopeInfo() { }
60ec9ea7200718478e8a976529defbe21942a11c9cEli FriedmanLambdaScopeInfo::~LambdaScopeInfo() { }
619ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
62ea8c59aaa6bd19976879142296f8fd12f8926738Argyrios KyrtzidisPrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context,
63ea8c59aaa6bd19976879142296f8fd12f8926738Argyrios Kyrtzidis                                       const Preprocessor &PP) {
648987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  PrintingPolicy Policy = Context.getPrintingPolicy();
654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  Policy.Bool = Context.getLangOpts().Bool;
668987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  if (!Policy.Bool) {
678987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor    if (MacroInfo *BoolMacro = PP.getMacroInfo(&Context.Idents.get("bool"))) {
688987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor      Policy.Bool = BoolMacro->isObjectLike() &&
698987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor        BoolMacro->getNumTokens() == 1 &&
708987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor        BoolMacro->getReplacementToken(0).is(tok::kw__Bool);
718987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor    }
728987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  }
738987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor
748987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  return Policy;
758987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor}
768987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor
77c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregorvoid Sema::ActOnTranslationUnitScope(Scope *S) {
788ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroff  TUScope = S;
7944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  PushDeclContext(S, Context.getTranslationUnitDecl());
801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall  VAListTagName = PP.getIdentifierInfo("__va_list_tag");
82b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
833b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff
84f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas GregorSema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
85467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor           TranslationUnitKind TUKind,
863a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           CodeCompleteConsumer *CodeCompleter)
874e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  : TheTargetAttributesSema(0), FPFeatures(pp.getLangOpts()),
884e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    LangOpts(pp.getLangOpts()), PP(pp), Context(ctxt), Consumer(consumer),
891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
905d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth    CollectStats(false), ExternalSource(0), CodeCompleter(CodeCompleter),
913a387441ae339363ee5b254658f295e97bd9e913Argyrios Kyrtzidis    CurContext(0), OriginalLexicalContext(0),
923a387441ae339363ee5b254658f295e97bd9e913Argyrios Kyrtzidis    PackContext(0), MSStructPragmaOn(false), VisContext(0),
9380ee6e878a169e6255d4686a91bb696151ff229fJohn McCall    ExprNeedsCleanups(false), LateTemplateParser(0), OpaqueParser(0),
94395e04dbd7294bae4640aef1705cec3d052578b0Sebastian Redl    IdResolver(pp), StdInitializerList(0), CXXTypeInfoDecl(0), MSVCGuidDecl(0),
95471131a05d4f9dca7f9f4f5109f2a9a4ce80bf65Benjamin Kramer    NSNumberDecl(0),
96471131a05d4f9dca7f9f4f5109f2a9a4ce80bf65Benjamin Kramer    NSStringDecl(0), StringWithUTF8StringMethod(0),
97471131a05d4f9dca7f9f4f5109f2a9a4ce80bf65Benjamin Kramer    NSArrayDecl(0), ArrayWithObjectsMethod(0),
98ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    NSDictionaryDecl(0), DictionaryWithObjectsMethod(0),
99b7566d8346ed8040a5cb7cefe96bd6ccbf015595Bill Wendling    GlobalNewDeleteDeclared(false),
1009a1ecf0522ccb7a45577f856150c15af0ee1df2aNico Weber    ObjCShouldCallSuperDealloc(false),
10180cb6e69d9e85231588ae604e4bc2bc9a07389afNico Weber    ObjCShouldCallSuperFinalize(false),
102467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    TUKind(TUKind),
10313489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall    NumSFINAEErrors(0), InFunctionDeclarator(0),
1041eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor    AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false),
1058491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor    NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1),
1068491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor    CurrentInstantiationScope(0), TyposCorrected(0),
107b7566d8346ed8040a5cb7cefe96bd6ccbf015595Bill Wendling    AnalysisWarnings(*this)
108f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor{
1093b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff  TUScope = 0;
110cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
111d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor  LoadedExternalKnownNamespaces = false;
112ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  for (unsigned I = 0; I != NSAPI::NumNSNumberLiteralMethods; ++I)
113ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    NSNumberLiteralMethods[I] = 0;
114ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
1154e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC1)
116ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    NSAPIObj.reset(new NSAPI(Context));
117ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
1184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
11907952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    FieldCollector.reset(new CXXFieldCollector());
1201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12122caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  // Tell diagnostics how to render things from the AST library.
12279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
12379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                       &Context);
1242afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor
1252afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor  ExprEvalContexts.push_back(
12676f3f69db1416425070177243e9f390122c553e0Richard Smith        ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0,
12776f3f69db1416425070177243e9f390122c553e0Richard Smith                                          false, 0, false));
128781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
1298fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(Diags));
13046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor}
13146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
13246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregorvoid Sema::Initialize() {
13346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the AST consumer about this Sema object.
13446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  Consumer.Initialize(Context);
13546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
13646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // FIXME: Isn't this redundant with the initialization above?
13746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
13846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->InitializeSema(*this);
13946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
14046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the external Sema source about this Sema object.
14146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
14246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor      = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
14346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->InitializeSema(*this);
1444dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor
145772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  // Initialize predefined 128-bit integer types, if needed.
146772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
147772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    // If either of the 128-bit integer types are unavailable to name lookup,
148772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    // define them now.
149772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    DeclarationName Int128 = &Context.Idents.get("__int128_t");
150eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Int128) == IdResolver.end())
151772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor      PushOnScopeChains(Context.getInt128Decl(), TUScope);
152772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
153772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    DeclarationName UInt128 = &Context.Idents.get("__uint128_t");
154eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(UInt128) == IdResolver.end())
155772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor      PushOnScopeChains(Context.getUInt128Decl(), TUScope);
156772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
157772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
158772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
1594dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  // Initialize predefined Objective-C types:
1604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (PP.getLangOpts().ObjC1) {
1617a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    // If 'SEL' does not yet refer to any declarations, make it refer to the
1627a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    // predefined 'SEL'.
1637a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    DeclarationName SEL = &Context.Idents.get("SEL");
164eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(SEL) == IdResolver.end())
1657a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor      PushOnScopeChains(Context.getObjCSelDecl(), TUScope);
1667a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor
1674dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    // If 'id' does not yet refer to any declarations, make it refer to the
1684dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    // predefined 'id'.
1694dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    DeclarationName Id = &Context.Idents.get("id");
170eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Id) == IdResolver.end())
1714dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor      PushOnScopeChains(Context.getObjCIdDecl(), TUScope);
17279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor
17379d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    // Create the built-in typedef for 'Class'.
17479d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    DeclarationName Class = &Context.Idents.get("Class");
175eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Class) == IdResolver.end())
17679d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor      PushOnScopeChains(Context.getObjCClassDecl(), TUScope);
177a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
178a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    // Create the built-in forward declaratino for 'Protocol'.
179a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    DeclarationName Protocol = &Context.Idents.get("Protocol");
180a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    if (IdResolver.begin(Protocol) == IdResolver.end())
181a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor      PushOnScopeChains(Context.getObjCProtocolDecl(), TUScope);
1824dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  }
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovSema::~Sema() {
18682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (PackContext) FreePackedContext();
187aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  if (VisContext) FreeVisContext();
18882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  delete TheTargetAttributesSema;
18962c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  MSStructPragmaOn = false;
190781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  // Kill all the active scopes.
191781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I)
192781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[I];
193781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1)
194781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[0];
19546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
19646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the SemaConsumer to forget about us; we're going out of scope.
19746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
19846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->ForgetSema();
19946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
20046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Detach from the external Sema source.
20146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
202914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor        = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
20346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->ForgetSema();
20482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
20582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
206f85e193739c953358c865005855253af4f68a497John McCall/// makeUnavailableInSystemHeader - There is an error in the current
207f85e193739c953358c865005855253af4f68a497John McCall/// context.  If we're still in a system header, and we can plausibly
208f85e193739c953358c865005855253af4f68a497John McCall/// make the relevant declaration unavailable instead of erroring, do
209f85e193739c953358c865005855253af4f68a497John McCall/// so and return true.
210f85e193739c953358c865005855253af4f68a497John McCallbool Sema::makeUnavailableInSystemHeader(SourceLocation loc,
2115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                         StringRef msg) {
212f85e193739c953358c865005855253af4f68a497John McCall  // If we're not in a function, it's an error.
213f85e193739c953358c865005855253af4f68a497John McCall  FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext);
214f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) return false;
215f85e193739c953358c865005855253af4f68a497John McCall
216f85e193739c953358c865005855253af4f68a497John McCall  // If we're in template instantiation, it's an error.
217f85e193739c953358c865005855253af4f68a497John McCall  if (!ActiveTemplateInstantiations.empty())
218f85e193739c953358c865005855253af4f68a497John McCall    return false;
219f85e193739c953358c865005855253af4f68a497John McCall
220f85e193739c953358c865005855253af4f68a497John McCall  // If that function's not in a system header, it's an error.
221f85e193739c953358c865005855253af4f68a497John McCall  if (!Context.getSourceManager().isInSystemHeader(loc))
222f85e193739c953358c865005855253af4f68a497John McCall    return false;
223f85e193739c953358c865005855253af4f68a497John McCall
224f85e193739c953358c865005855253af4f68a497John McCall  // If the function is already unavailable, it's not an error.
225f85e193739c953358c865005855253af4f68a497John McCall  if (fn->hasAttr<UnavailableAttr>()) return true;
226f85e193739c953358c865005855253af4f68a497John McCall
227f85e193739c953358c865005855253af4f68a497John McCall  fn->addAttr(new (Context) UnavailableAttr(loc, Context, msg));
228f85e193739c953358c865005855253af4f68a497John McCall  return true;
229f85e193739c953358c865005855253af4f68a497John McCall}
230f85e193739c953358c865005855253af4f68a497John McCall
23158a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian RedlASTMutationListener *Sema::getASTMutationListener() const {
23258a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl  return getASTConsumer().GetASTMutationListener();
23358a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl}
23458a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl
2355d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth/// \brief Print out statistics about the semantic analysis.
2365d98994c7749312a43ce6adf45537979a98e7afdChandler Carruthvoid Sema::PrintStats() const {
2375d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  llvm::errs() << "\n*** Semantic Analysis Stats:\n";
2385d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n";
2395d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth
2405d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  BumpAlloc.PrintStats();
2415d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  AnalysisWarnings.PrintStats();
2425d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth}
2435d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth
244c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
245c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith/// If there is already an implicit cast, merge into the existing one.
246c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith/// The result is of the given category.
247c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard SmithExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
248c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                   CastKind Kind, ExprValueKind VK,
249c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                   const CXXCastPath *BasePath,
250c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                   CheckedConversionKind CCK) {
2519c129f818038e0269ba6b095722aa70176dc321dRichard Smith#ifndef NDEBUG
2529c129f818038e0269ba6b095722aa70176dc321dRichard Smith  if (VK == VK_RValue && !E->isRValue()) {
2539c129f818038e0269ba6b095722aa70176dc321dRichard Smith    switch (Kind) {
2549c129f818038e0269ba6b095722aa70176dc321dRichard Smith    default:
2559c129f818038e0269ba6b095722aa70176dc321dRichard Smith      assert(0 && "can't implicitly cast lvalue to rvalue with this cast kind");
2569c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_LValueToRValue:
2579c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_ArrayToPointerDecay:
2589c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_FunctionToPointerDecay:
2599c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_ToVoid:
2609c129f818038e0269ba6b095722aa70176dc321dRichard Smith      break;
2619c129f818038e0269ba6b095722aa70176dc321dRichard Smith    }
2629c129f818038e0269ba6b095722aa70176dc321dRichard Smith  }
263acdfa4d504a8f2514d60569f9ce55d45f11795b9Richard Smith  assert((VK == VK_RValue || !E->isRValue()) && "can't cast rvalue to lvalue");
2649c129f818038e0269ba6b095722aa70176dc321dRichard Smith#endif
2659c129f818038e0269ba6b095722aa70176dc321dRichard Smith
266429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  QualType ExprTy = Context.getCanonicalType(E->getType());
2673a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType TypeTy = Context.getCanonicalType(Ty);
2681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2693a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  if (ExprTy == TypeTy)
270429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return Owned(E);
2711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2724e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
273f85e193739c953358c865005855253af4f68a497John McCall    CheckObjCARCConversion(SourceRange(), Ty, E, CCK);
274f85e193739c953358c865005855253af4f68a497John McCall
2756fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // If this is a derived-to-base cast to a through a virtual base, we
2766fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // need a vtable.
2772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  if (Kind == CK_DerivedToBase &&
278f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall      BasePathInvolvesVirtualBase(*BasePath)) {
279429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    QualType T = E->getType();
2806fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const PointerType *Pointer = T->getAs<PointerType>())
2816fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      T = Pointer->getPointeeType();
2826fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const RecordType *RecordTy = T->getAs<RecordType>())
283429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      MarkVTableUsed(E->getLocStart(),
2846fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor                     cast<CXXRecordDecl>(RecordTy->getDecl()));
2856fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  }
286c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith
287c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
288c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
289c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      ImpCast->setType(Ty);
290c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      ImpCast->setValueKind(VK);
291c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      return Owned(E);
292c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    }
2934c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson  }
2944c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson
295c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith  return Owned(ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK));
296906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl}
297906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
298737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara/// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
299737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara/// to the conversion from scalar type ScalarTy to the Boolean type.
300737d5447b5d20633992ee5388eca5270c28c8ae7Abramo BagnaraCastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) {
301737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  switch (ScalarTy->getScalarTypeKind()) {
302737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Bool: return CK_NoOp;
3031d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_CPointer: return CK_PointerToBoolean;
3041d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_BlockPointer: return CK_PointerToBoolean;
3051d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_ObjCObjectPointer: return CK_PointerToBoolean;
306737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_MemberPointer: return CK_MemberPointerToBoolean;
307737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Integral: return CK_IntegralToBoolean;
308737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Floating: return CK_FloatingToBoolean;
309737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean;
310737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean;
311737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  }
312737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  return CK_Invalid;
313737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara}
314737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara
315bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis/// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
316bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidisstatic bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
317bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (D->isUsed())
318bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    return true;
319bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
320bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
321bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
322bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
323bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const FunctionDecl *DeclToCheck;
324bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (FD->hasBody(DeclToCheck))
325bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
326bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
327bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
328bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
329ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    DeclToCheck = FD->getMostRecentDecl();
330bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != FD)
331bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
332bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
333bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
334bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
335bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
336bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
337bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const VarDecl *DeclToCheck = VD->getDefinition();
338bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck)
339bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
340bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
341bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
342bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
343ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    DeclToCheck = VD->getMostRecentDecl();
344bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != VD)
345bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
346bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
347bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
348bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  return false;
349bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis}
350bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
35115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallnamespace {
35215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  struct UndefinedInternal {
35315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    NamedDecl *decl;
35415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    FullSourceLoc useLoc;
35515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
35615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    UndefinedInternal(NamedDecl *decl, FullSourceLoc useLoc)
35715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      : decl(decl), useLoc(useLoc) {}
35815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  };
35915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
36015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  bool operator<(const UndefinedInternal &l, const UndefinedInternal &r) {
36115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    return l.useLoc.isBeforeInTranslationUnitThan(r.useLoc);
36215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
36315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
36415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
36515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall/// checkUndefinedInternals - Check for undefined objects with internal linkage.
36615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallstatic void checkUndefinedInternals(Sema &S) {
36715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (S.UndefinedInternals.empty()) return;
36815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
36915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Collect all the still-undefined entities with internal linkage.
3705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<UndefinedInternal, 16> undefined;
37115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  for (llvm::DenseMap<NamedDecl*,SourceLocation>::iterator
37215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall         i = S.UndefinedInternals.begin(), e = S.UndefinedInternals.end();
37315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall       i != e; ++i) {
37415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    NamedDecl *decl = i->first;
37515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
37615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // Ignore attributes that have become invalid.
37715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    if (decl->isInvalidDecl()) continue;
37815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
37915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // __attribute__((weakref)) is basically a definition.
38015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    if (decl->hasAttr<WeakRefAttr>()) continue;
38115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
38215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) {
38315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      if (fn->isPure() || fn->hasBody())
38415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        continue;
38515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    } else {
38615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      if (cast<VarDecl>(decl)->hasDefinition() != VarDecl::DeclarationOnly)
38715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        continue;
38815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    }
38915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
39015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // We build a FullSourceLoc so that we can sort with array_pod_sort.
39115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    FullSourceLoc loc(i->second, S.Context.getSourceManager());
39215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    undefined.push_back(UndefinedInternal(decl, loc));
39315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
39415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
39515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (undefined.empty()) return;
39615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
39715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Sort (in order of use site) so that we're not (as) dependent on
39815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // the iteration order through an llvm::DenseMap.
39915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  llvm::array_pod_sort(undefined.begin(), undefined.end());
40015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
4015f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  for (SmallVectorImpl<UndefinedInternal>::iterator
40215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall         i = undefined.begin(), e = undefined.end(); i != e; ++i) {
40315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    NamedDecl *decl = i->decl;
40415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    S.Diag(decl->getLocation(), diag::warn_undefined_internal)
40515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      << isa<VarDecl>(decl) << decl;
40615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    S.Diag(i->useLoc, diag::note_used_here);
40715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
40815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
40915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
41031e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregorvoid Sema::LoadExternalWeakUndeclaredIdentifiers() {
41131e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  if (!ExternalSource)
41231e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    return;
41331e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor
41431e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs;
41531e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs);
41631e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  for (unsigned I = 0, N = WeakIDs.size(); I != N; ++I) {
41731e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator Pos
41831e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor      = WeakUndeclaredIdentifiers.find(WeakIDs[I].first);
41931e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    if (Pos != WeakUndeclaredIdentifiers.end())
42031e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor      continue;
42131e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor
42231e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    WeakUndeclaredIdentifiers.insert(WeakIDs[I]);
42331e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  }
42431e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor}
42531e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor
426f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
427f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jaspertypedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap;
428f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
429f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// \brief Returns true, if all methods and nested classes of the given
430f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// CXXRecordDecl are defined in this translation unit.
431f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper///
432f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// Should only be called from ActOnEndOfTranslationUnit so that all
433f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// definitions are actually read.
434f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasperstatic bool MethodsAndNestedClassesComplete(const CXXRecordDecl *RD,
435f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                            RecordCompleteMap &MNCComplete) {
436f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  RecordCompleteMap::iterator Cache = MNCComplete.find(RD);
437f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  if (Cache != MNCComplete.end())
438f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    return Cache->second;
439f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  if (!RD->isCompleteDefinition())
440f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    return false;
441f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  bool Complete = true;
442f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  for (DeclContext::decl_iterator I = RD->decls_begin(),
443f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                  E = RD->decls_end();
444f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper       I != E && Complete; ++I) {
445f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    if (const CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(*I))
446f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      Complete = M->isDefined() || (M->isPure() && !isa<CXXDestructorDecl>(M));
447f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    else if (const CXXRecordDecl *R = dyn_cast<CXXRecordDecl>(*I)) {
448f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (R->isInjectedClassName())
449f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        continue;
450f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (R->hasDefinition())
451f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = MethodsAndNestedClassesComplete(R->getDefinition(),
452f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                                   MNCComplete);
453f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      else
454f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = false;
455f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    }
456f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  }
457f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  MNCComplete[RD] = Complete;
458f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  return Complete;
459f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper}
460f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
461f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// \brief Returns true, if the given CXXRecordDecl is fully defined in this
462f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// translation unit, i.e. all methods are defined or pure virtual and all
463f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// friends, friend functions and nested classes are fully defined in this
464f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// translation unit.
465f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper///
466f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// Should only be called from ActOnEndOfTranslationUnit so that all
467f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// definitions are actually read.
468f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasperstatic bool IsRecordFullyDefined(const CXXRecordDecl *RD,
469f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                 RecordCompleteMap &RecordsComplete,
470f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                 RecordCompleteMap &MNCComplete) {
471f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  RecordCompleteMap::iterator Cache = RecordsComplete.find(RD);
472f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  if (Cache != RecordsComplete.end())
473f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    return Cache->second;
474f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  bool Complete = MethodsAndNestedClassesComplete(RD, MNCComplete);
475f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  for (CXXRecordDecl::friend_iterator I = RD->friend_begin(),
476f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                      E = RD->friend_end();
477f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper       I != E && Complete; ++I) {
478f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    // Check if friend classes and methods are complete.
479f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    if (TypeSourceInfo *TSI = (*I)->getFriendType()) {
480f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      // Friend classes are available as the TypeSourceInfo of the FriendDecl.
481f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (CXXRecordDecl *FriendD = TSI->getType()->getAsCXXRecordDecl())
482f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = MethodsAndNestedClassesComplete(FriendD, MNCComplete);
483f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      else
484f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = false;
485f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    } else {
486f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      // Friend functions are available through the NamedDecl of FriendDecl.
487f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (const FunctionDecl *FD =
488f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper          dyn_cast<FunctionDecl>((*I)->getFriendDecl()))
489f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = FD->isDefined();
490f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      else
491f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        // This is a template friend, give up.
492f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = false;
493f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    }
494f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  }
495f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  RecordsComplete[RD] = Complete;
496f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  return Complete;
497f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper}
498f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
4999299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// ActOnEndOfTranslationUnit - This is called at the very end of the
5009299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// translation unit when EOF is reached and all but the top-level scope is
5019299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// popped.
5020e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidisvoid Sema::ActOnEndOfTranslationUnit() {
5039257664568bf375b7790131a84d9a4fa30a5b7e3John McCall  assert(DelayedDiagnostics.getCurrentPool() == NULL
5049257664568bf375b7790131a84d9a4fa30a5b7e3John McCall         && "reached end of translation unit with a pool attached?");
5059257664568bf375b7790131a84d9a4fa30a5b7e3John McCall
506467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  // Only complete translation units define vtables and perform implicit
507467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  // instantiations.
508467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  if (TUKind == TU_Complete) {
509849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    DiagnoseUseOfUnimplementedSelectors();
510849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
511aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // If any dynamic classes have their key function defined within
512aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // this translation unit, then those vtables are considered "used" and must
513aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // be emitted.
514a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor    for (DynamicClassesType::iterator I = DynamicClasses.begin(ExternalSource),
515a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor                                      E = DynamicClasses.end();
516a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor         I != E; ++I) {
517a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor      assert(!(*I)->isDependentType() &&
518a5c6c2a84cde5c9b8f8ec0610a9f89ffd54f44eeAnders Carlsson             "Should not see dependent types here!");
519a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor      if (const CXXMethodDecl *KeyFunction = Context.getKeyFunction(*I)) {
520aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth        const FunctionDecl *Definition = 0;
521aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth        if (KeyFunction->hasBody(Definition))
522a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor          MarkVTableUsed(Definition->getLocation(), *I, true);
523aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth      }
524aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    }
525aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth
5268155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // If DefinedUsedVTables ends up marking any virtual member functions it
5278155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // might lead to more pending template instantiations, which we then need
5288155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // to instantiate.
5298155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
5308155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
5318155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // C++: Perform implicit template instantiations.
5328155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    //
5338155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // FIXME: When we perform these implicit instantiations, we do not
5348155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // carefully keep track of the point of instantiation (C++ [temp.point]).
5358155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // This means that name lookup that occurs within the template
5368155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // instantiation will always happen at the end of the translation unit,
5378155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // so it will find some names that should not be found. Although this is
5388155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // common behavior for C++ compilers, it is technically wrong. In the
5398155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // future, we either need to be able to filter the results of name lookup
5408155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // or we need to perform template instantiations earlier.
5418155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    PerformPendingInstantiations();
5422a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
543d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson
54449b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Remove file scoped decls that turned out to be used.
545a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor  UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(0,
546a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor                                                                         true),
547bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                                             UnusedFileScopedDecls.end(),
548bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                              std::bind1st(std::ptr_fun(ShouldRemoveFromUnused),
549bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                                           this)),
55049b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis                              UnusedFileScopedDecls.end());
55147268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor
552467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  if (TUKind == TU_Prefix) {
553467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    // Translation unit prefixes don't need any of the checking below.
55487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    TUScope = 0;
55572b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    return;
55687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
55772b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis
55863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // Check for #pragma weak identifiers that were never declared
55963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // FIXME: This will cause diagnostics to be emitted in a non-determinstic
56063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // order!  Iterating over a densemap like this is bad.
56131e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  LoadExternalWeakUndeclaredIdentifiers();
562e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
56363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       I = WeakUndeclaredIdentifiers.begin(),
56463d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
56563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    if (I->second.getUsed()) continue;
5661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56763d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
56863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      << I->first;
569e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  }
570e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn
571467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  if (TUKind == TU_Module) {
57290db26000aefe9335370013eec64c85232d80227Douglas Gregor    // If we are building a module, resolve all of the exported declarations
57390db26000aefe9335370013eec64c85232d80227Douglas Gregor    // now.
57490db26000aefe9335370013eec64c85232d80227Douglas Gregor    if (Module *CurrentModule = PP.getCurrentModule()) {
57590db26000aefe9335370013eec64c85232d80227Douglas Gregor      ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
57690db26000aefe9335370013eec64c85232d80227Douglas Gregor
57790db26000aefe9335370013eec64c85232d80227Douglas Gregor      llvm::SmallVector<Module *, 2> Stack;
57890db26000aefe9335370013eec64c85232d80227Douglas Gregor      Stack.push_back(CurrentModule);
57990db26000aefe9335370013eec64c85232d80227Douglas Gregor      while (!Stack.empty()) {
58090db26000aefe9335370013eec64c85232d80227Douglas Gregor        Module *Mod = Stack.back();
58190db26000aefe9335370013eec64c85232d80227Douglas Gregor        Stack.pop_back();
58290db26000aefe9335370013eec64c85232d80227Douglas Gregor
58390db26000aefe9335370013eec64c85232d80227Douglas Gregor        // Resolve the exported declarations.
58490db26000aefe9335370013eec64c85232d80227Douglas Gregor        // FIXME: Actually complain, once we figure out how to teach the
58590db26000aefe9335370013eec64c85232d80227Douglas Gregor        // diagnostic client to deal with complains in the module map at this
58690db26000aefe9335370013eec64c85232d80227Douglas Gregor        // point.
58790db26000aefe9335370013eec64c85232d80227Douglas Gregor        ModMap.resolveExports(Mod, /*Complain=*/false);
58890db26000aefe9335370013eec64c85232d80227Douglas Gregor
58990db26000aefe9335370013eec64c85232d80227Douglas Gregor        // Queue the submodules, so their exports will also be resolved.
590b7a7819473709c01ea024a2dc15e99d38f0f8760Douglas Gregor        for (Module::submodule_iterator Sub = Mod->submodule_begin(),
591b7a7819473709c01ea024a2dc15e99d38f0f8760Douglas Gregor                                     SubEnd = Mod->submodule_end();
59290db26000aefe9335370013eec64c85232d80227Douglas Gregor             Sub != SubEnd; ++Sub) {
593b7a7819473709c01ea024a2dc15e99d38f0f8760Douglas Gregor          Stack.push_back(*Sub);
59490db26000aefe9335370013eec64c85232d80227Douglas Gregor        }
59590db26000aefe9335370013eec64c85232d80227Douglas Gregor      }
59690db26000aefe9335370013eec64c85232d80227Douglas Gregor    }
59790db26000aefe9335370013eec64c85232d80227Douglas Gregor
598467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    // Modules don't need any of the checking below.
599467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    TUScope = 0;
600467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    return;
601467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  }
602467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor
603275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  // C99 6.9.2p2:
604275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   A declaration of an identifier for an object that has file
605275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   scope without an initializer, and without a storage-class
606275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   specifier or with the storage-class specifier static,
607275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   constitutes a tentative definition. If a translation unit
608275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   contains one or more tentative definitions for an identifier,
609275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   and the translation unit contains no external definition for
610275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   that identifier, then the behavior is exactly as if the
611275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit contains a file scope declaration of that
612275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   identifier, with the composite type as of the end of the
613275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit, with an initializer equal to 0.
614e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  llvm::SmallSet<VarDecl *, 32> Seen;
615a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor  for (TentativeDefinitionsType::iterator
616a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor            T = TentativeDefinitions.begin(ExternalSource),
617a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor         TEnd = TentativeDefinitions.end();
618a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor       T != TEnd; ++T)
619a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor  {
620a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor    VarDecl *VD = (*T)->getActingDefinition();
621e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
622e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // If the tentative definition was completed, getActingDefinition() returns
623e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // null. If we've already seen this variable before, insert()'s second
624e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // return value is false.
625e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
626b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      continue;
627b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const IncompleteArrayType *ArrayT
629b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        = Context.getAsIncompleteArrayType(VD->getType())) {
6301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (RequireCompleteType(VD->getLocation(),
631b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                              ArrayT->getElementType(),
63263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner                              diag::err_tentative_def_incomplete_type_arr)) {
633b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        VD->setInvalidDecl();
63463d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner        continue;
635275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor      }
6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63763d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      // Set the length of the array to 1 (C99 6.9.2p5).
63863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
63963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
64046a617a792bfab0d9b1e057371ea3b9540802226John McCall      QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
64146a617a792bfab0d9b1e057371ea3b9540802226John McCall                                                One, ArrayType::Normal, 0);
64263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      VD->setType(T);
6431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
644b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                                   diag::err_tentative_def_incomplete_type))
645b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      VD->setInvalidDecl();
646b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
647b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    // Notify the consumer that we've completed a tentative definition.
648b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    if (!VD->isInvalidDecl())
649b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      Consumer.CompleteTentativeDefinition(VD);
650b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
651275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  }
65243f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis
653c1598700010cea9364a58a65e967b0b56361b6aaSean Hunt  if (LangOpts.CPlusPlus0x &&
654c1598700010cea9364a58a65e967b0b56361b6aaSean Hunt      Diags.getDiagnosticLevel(diag::warn_delegating_ctor_cycle,
655c1598700010cea9364a58a65e967b0b56361b6aaSean Hunt                               SourceLocation())
656d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie        != DiagnosticsEngine::Ignored)
657fe57eef44cc80e5bb51e4f484835be08b8d84256Sean Hunt    CheckDelegatingCtorCycles();
658fe57eef44cc80e5bb51e4f484835be08b8d84256Sean Hunt
65943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  // If there were errors, disable 'unused' warnings since they will mostly be
66043f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  // noise.
66143f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  if (!Diags.hasErrorOccurred()) {
66243f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis    // Output warning for unused file scoped decls.
663a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor    for (UnusedFileScopedDeclsType::iterator
664a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor           I = UnusedFileScopedDecls.begin(ExternalSource),
66543f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis           E = UnusedFileScopedDecls.end(); I != E; ++I) {
666a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor      if (ShouldRemoveFromUnused(this, *I))
667a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor        continue;
668a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor
66943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
67043f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        const FunctionDecl *DiagD;
67143f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        if (!FD->hasBody(DiagD))
67243f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis          DiagD = FD;
67348b89590f61575cbf365ba996a2bd1ba1561a4abArgyrios Kyrtzidis        if (DiagD->isDeleted())
67448b89590f61575cbf365ba996a2bd1ba1561a4abArgyrios Kyrtzidis          continue; // Deleted functions are supposed to be unused.
6756b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        if (DiagD->isReferenced()) {
6766b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          if (isa<CXXMethodDecl>(DiagD))
6776b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis            Diag(DiagD->getLocation(), diag::warn_unneeded_member_function)
6786b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                  << DiagD->getDeclName();
6796b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          else
6806b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis            Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
6816b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                  << /*function*/0 << DiagD->getDeclName();
6826b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        } else {
6836b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(),
6846b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis               isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function
6856b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                                         : diag::warn_unused_function)
6866b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << DiagD->getDeclName();
6876b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        }
68843f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      } else {
68943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
69043f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        if (!DiagD)
69143f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis          DiagD = cast<VarDecl>(*I);
6926b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        if (DiagD->isReferenced()) {
6936b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
6946b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << /*variable*/1 << DiagD->getDeclName();
6956b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        } else {
6966b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(), diag::warn_unused_variable)
6976b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << DiagD->getDeclName();
6986b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        }
69943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      }
700bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    }
70115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
70215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    checkUndefinedInternals(*this);
70349b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  }
70487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
705f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  if (Diags.getDiagnosticLevel(diag::warn_unused_private_field,
706f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                               SourceLocation())
707f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        != DiagnosticsEngine::Ignored) {
708f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    RecordCompleteMap RecordsComplete;
709f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    RecordCompleteMap MNCComplete;
710f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    for (NamedDeclSetType::iterator I = UnusedPrivateFields.begin(),
711f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper         E = UnusedPrivateFields.end(); I != E; ++I) {
712f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      const NamedDecl *D = *I;
713f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
714f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (RD && !RD->isUnion() &&
715f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper          IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) {
716f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Diag(D->getLocation(), diag::warn_unused_private_field)
717f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper              << D->getDeclName();
718f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      }
719f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    }
720f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  }
721f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
722483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // Check we've noticed that we're no longer parsing the initializer for every
723483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // variable. If we miss cases, then at best we have a performance issue and
724483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // at worst a rejects-valid bug.
725483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  assert(ParsingInitForAutoVars.empty() &&
726483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith         "Didn't unmark var as having its initializer parsed");
727483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
72887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  TUScope = 0;
7299299f3fa85796613cc787a2062c9562d07c8613eChris Lattner}
7309299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
7319299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
7325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
7335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper functions.
7345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
7355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7368517d9b731f065cdfc55ec0f3ddf5d564d988648Anders CarlssonDeclContext *Sema::getFunctionLevelDeclContext() {
737db0ee1da16e9dbec19b144c9cd96ee9f55fe0c53John McCall  DeclContext *DC = CurContext;
7381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
73972899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  while (true) {
74072899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) {
74172899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman      DC = DC->getParent();
74272899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    } else if (isa<CXXMethodDecl>(DC) &&
743215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor               cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call &&
74472899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman               cast<CXXRecordDecl>(DC->getParent())->isLambda()) {
74572899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman      DC = DC->getParent()->getParent();
74672899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    }
74772899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    else break;
74872899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  }
7491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7508517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  return DC;
7518517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson}
7528517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson
753371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// getCurFunctionDecl - If inside of a function body, this returns a pointer
754371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// to the function decl for the function being parsed.  If we're currently
755371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// in a 'block', this returns the containing context.
756371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerFunctionDecl *Sema::getCurFunctionDecl() {
7578517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
758371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return dyn_cast<FunctionDecl>(DC);
759371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
760371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
761c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel DunbarObjCMethodDecl *Sema::getCurMethodDecl() {
7628517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
763d7612e183bb09d04677d0ddde89ee29130ffb715Steve Naroff  return dyn_cast<ObjCMethodDecl>(DC);
764c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar}
765371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
766371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerNamedDecl *Sema::getCurFunctionOrMethodDecl() {
7678517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
768371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
7694afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return cast<NamedDecl>(DC);
770371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return 0;
771371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
772371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
773393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbarvoid Sema::EmitCurrentDiagnostic(unsigned DiagID) {
774393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // FIXME: It doesn't make sense to me that DiagID is an incoming argument here
775393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // and yet we also use the current diag ID on the DiagnosticsEngine. This has
776393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // been made more painfully obvious by the refactor that introduced this
777393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // function, but it is possible that the incoming argument can be
778393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // eliminnated. If it truly cannot be (for example, there is some reentrancy
779393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // issue I am not seeing yet), then there should at least be a clarifying
780393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // comment somewhere.
781393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  if (llvm::Optional<TemplateDeductionInfo*> Info = isSFINAEContext()) {
782393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar    switch (DiagnosticIDs::getDiagnosticSFINAEResponse(
783393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar              Diags.getCurrentDiagID())) {
78433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Report:
78577faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // We'll report the diagnostic below.
7869b623639378d53a675921ddfa7316034d571881eDouglas Gregor      break;
7879b623639378d53a675921ddfa7316034d571881eDouglas Gregor
78877faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    case DiagnosticIDs::SFINAE_SubstitutionFailure:
78977faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Count this failure so that we know that template argument deduction
79077faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // has failed.
791393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      ++NumSFINAEErrors;
792b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
793b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // Make a copy of this suppressed diagnostic and store it with the
794b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // template-deduction information.
795b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
796b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        Diagnostic DiagInfo(&Diags);
797b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
798b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
799b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      }
800b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
801393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
802393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.Clear();
80377faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      return;
80477faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
80577faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    case DiagnosticIDs::SFINAE_AccessControl: {
8067822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // Per C++ Core Issue 1170, access control is part of SFINAE.
80700b43848e4a320b0845c1004694fa50920096c75Daniel Dunbar      // Additionally, the AccessCheckingSFINAE flag can be used to temporarily
8087822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // make access control a part of SFINAE for the purposes of checking
8097822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // type traits.
810393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      if (!AccessCheckingSFINAE && !getLangOpts().CPlusPlus0x)
8111eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor        break;
81277faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
813393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      SourceLocation Loc = Diags.getCurrentDiagLoc();
81477faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
81577faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Suppress this diagnostic.
816393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      ++NumSFINAEErrors;
817b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
818b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // Make a copy of this suppressed diagnostic and store it with the
819b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // template-deduction information.
820b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
821b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        Diagnostic DiagInfo(&Diags);
822b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
823b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
824b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      }
825b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
826393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
827393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.Clear();
82877faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
82977faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Now the diagnostic state is clear, produce a C++98 compatibility
83077faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // warning.
831393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diag(Loc, diag::warn_cxx98_compat_sfinae_access_control);
83277faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
83377faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // The last diagnostic which Sema produced was ignored. Suppress any
83477faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // notes attached to it.
835393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
8369b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
83777faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    }
83877faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
83933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Suppress:
8409b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Make a copy of this suppressed diagnostic and store it with the
8419b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // template-deduction information;
842b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      if (*Info) {
843b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        Diagnostic DiagInfo(&Diags);
8441eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor        (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(),
845b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
846b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      }
847b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
848b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // Suppress this diagnostic.
849393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
850393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.Clear();
8519b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
8529b623639378d53a675921ddfa7316034d571881eDouglas Gregor    }
8539b623639378d53a675921ddfa7316034d571881eDouglas Gregor  }
8549b623639378d53a675921ddfa7316034d571881eDouglas Gregor
8558987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  // Set up the context's printing policy based on our current state.
856393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  Context.setPrintingPolicy(getPrintingPolicy());
8578987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor
8589b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // Emit the diagnostic.
859393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  if (!Diags.EmitCurrentDiagnostic())
8605e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    return;
8611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
86225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // If this is not a note, and we're in a template instantiation
86325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // that is different from the last template instantiation where
86425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // we emitted an error, print a template instantiation
86525a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // backtrace.
86633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  if (!DiagnosticIDs::isBuiltinNote(DiagID) &&
867393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      !ActiveTemplateInstantiations.empty() &&
868393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      ActiveTemplateInstantiations.back()
869393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar        != LastTemplateInstantiationErrorContext) {
870393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar    PrintInstantiationStack();
871393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar    LastTemplateInstantiationErrorContext = ActiveTemplateInstantiations.back();
87225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  }
87325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor}
8742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
87591a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::SemaDiagnosticBuilder
87691a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
87791a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
87891a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  PD.Emit(Builder);
8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88091a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  return Builder;
88191a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson}
88291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson
883108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// \brief Looks through the macro-expansion chain for the given
884108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// location, looking for a macro expansion with the given name.
885834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// If one is found, returns true and sets the location to that
886108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// expansion loc.
8875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerbool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) {
888834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  SourceLocation loc = locref;
889834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (!loc.isMacroID()) return false;
890834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
891834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // There's no good way right now to look at the intermediate
892108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  // expansions, so just jump to the expansion location.
893402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  loc = getSourceManager().getExpansionLoc(loc);
894834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
895834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // If that's written with the name, stop here.
8965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<char, 16> buffer;
897834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (getPreprocessor().getSpelling(loc, buffer) == name) {
898834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    locref = loc;
899834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    return true;
900834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  }
901834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  return false;
902834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall}
903834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
90423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \brief Determines the active Scope associated with the given declaration
90523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// context.
90623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
90723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// This routine maps a declaration context to the active Scope object that
90823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// represents that declaration context in the parser. It is typically used
90923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// from "scope-less" code (e.g., template instantiation, lazy creation of
91023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// declarations) that injects a name for name-lookup purposes and, therefore,
91123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// must update the Scope.
91223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
91323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \returns The scope corresponding to the given declaraion context, or NULL
91423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// if no such scope is open.
91523c94dbb6631fecdb55ba401aa93722803d980c6Douglas GregorScope *Sema::getScopeForContext(DeclContext *Ctx) {
91623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
91723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!Ctx)
91823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return 0;
91923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
92023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Ctx = Ctx->getPrimaryContext();
92123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
922cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // Ignore scopes that cannot have declarations. This is important for
923cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // out-of-line definitions of static class members.
924cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
925cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl      if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity()))
926cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl        if (Ctx == Entity->getPrimaryContext())
927cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl          return S;
92823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  }
92923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
93023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return 0;
93123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor}
9329ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
9339ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Enter a new function scope
9349ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushFunctionScope() {
935781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1) {
936781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // Use the "top" function scope rather than having to allocate
937781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // memory for a new scope.
9388fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis    FunctionScopes.back()->Clear();
939781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    FunctionScopes.push_back(FunctionScopes.back());
9409ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return;
9419ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  }
9429ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
9438fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics()));
9449ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
9459ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
9469ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
9478fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(),
9489ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor                                              BlockScope, Block));
9499ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
9509ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
95176e3da57b0e8cf72d221f44d54566ef206341668Douglas Gregorvoid Sema::PushLambdaScope(CXXRecordDecl *Lambda,
95276e3da57b0e8cf72d221f44d54566ef206341668Douglas Gregor                           CXXMethodDecl *CallOperator) {
95376e3da57b0e8cf72d221f44d54566ef206341668Douglas Gregor  FunctionScopes.push_back(new LambdaScopeInfo(getDiagnostics(), Lambda,
95476e3da57b0e8cf72d221f44d54566ef206341668Douglas Gregor                                               CallOperator));
955ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman}
956ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman
957ec9ea7200718478e8a976529defbe21942a11c9cEli Friedmanvoid Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP,
958ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman                                const Decl *D, const BlockExpr *blkExpr) {
9593ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  FunctionScopeInfo *Scope = FunctionScopes.pop_back_val();
960781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  assert(!FunctionScopes.empty() && "mismatched push/pop!");
9613ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek
9623ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  // Issue any analysis-based warnings.
9633ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  if (WP && D)
964283a358aecb75e30fcd486f2206f6c03c5e7f11dTed Kremenek    AnalysisWarnings.IssueWarnings(*WP, Scope, D, blkExpr);
965351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  else {
9665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator
967351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         i = Scope->PossiblyUnreachableDiags.begin(),
968351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         e = Scope->PossiblyUnreachableDiags.end();
969351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         i != e; ++i) {
970351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek      const sema::PossiblyUnreachableDiag &D = *i;
971351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek      Diag(D.Loc, D.PD);
972351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek    }
973351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  }
9743ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek
975351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  if (FunctionScopes.back() != Scope) {
976781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete Scope;
977351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  }
9789ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
9799ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
980625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::PushCompoundScope() {
981625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  getCurFunction()->CompoundScopes.push_back(CompoundScopeInfo());
982625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko}
983625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
984625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::PopCompoundScope() {
985625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  FunctionScopeInfo *CurFunction = getCurFunction();
986625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  assert(!CurFunction->CompoundScopes.empty() && "mismatched push/pop");
987625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
988625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  CurFunction->CompoundScopes.pop_back();
989625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko}
990625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
9919ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Determine whether any errors occurred within this function/method/
9929ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// block.
993f85e193739c953358c865005855253af4f68a497John McCallbool Sema::hasAnyUnrecoverableErrorsInThisFunction() const {
994f85e193739c953358c865005855253af4f68a497John McCall  return getCurFunction()->ErrorTrap.hasUnrecoverableErrorOccurred();
9959ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
9969ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
9979ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo *Sema::getCurBlock() {
9989ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.empty())
9999ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return 0;
10009ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
10019ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  return dyn_cast<BlockScopeInfo>(FunctionScopes.back());
10029ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
100376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
1004906a7e1c0f272f7e539c82dda01f4644031ce637Eli FriedmanLambdaScopeInfo *Sema::getCurLambda() {
1005906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  if (FunctionScopes.empty())
1006906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman    return 0;
1007906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman
1008906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  return dyn_cast<LambdaScopeInfo>(FunctionScopes.back());
1009906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman}
1010906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman
101176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall// Pin this vtable to this file.
101276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalSemaSource::~ExternalSemaSource() {}
1013f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
10145ac4b6917aa34fae6da64036539023a6155a3d48Douglas Gregorvoid ExternalSemaSource::ReadMethodPool(Selector Sel) { }
10158c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl
1016d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregorvoid ExternalSemaSource::ReadKnownNamespaces(
10175f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
1018d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor}
1019d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor
10205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid PrettyDeclStackTraceEntry::print(raw_ostream &OS) const {
1021f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  SourceLocation Loc = this->Loc;
1022f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
1023f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (Loc.isValid()) {
1024f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Loc.print(OS, S.getSourceManager());
1025f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    OS << ": ";
1026f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
1027f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << Message;
1028f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
1029f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (TheDecl && isa<NamedDecl>(TheDecl)) {
1030f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString();
1031f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    if (!Name.empty())
1032f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      OS << " '" << Name << '\'';
1033f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
1034f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
1035f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << '\n';
1036f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall}
1037c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1038c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \brief Figure out if an expression could be turned into a call.
1039c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1040c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// Use this when trying to recover from an error where the programmer may have
1041c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// written just the name of a function instead of actually calling it.
1042c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1043c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param E - The expression to examine.
1044c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param ZeroArgCallReturnTy - If the expression can be turned into a call
1045c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  with no arguments, this parameter is set to the type returned by such a
1046c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  call; otherwise, it is set to an empty QualType.
10476dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// \param OverloadSet - If the expression is an overloaded function
1048c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  name, this parameter is populated with the decls of the various overloads.
1049c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gaybool Sema::isExprCallable(const Expr &E, QualType &ZeroArgCallReturnTy,
10506dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                          UnresolvedSetImpl &OverloadSet) {
1051c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  ZeroArgCallReturnTy = QualType();
10526dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  OverloadSet.clear();
10536dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
10546dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (E.getType() == Context.OverloadTy) {
10556dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    OverloadExpr::FindResult FR = OverloadExpr::find(const_cast<Expr*>(&E));
10566dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    const OverloadExpr *Overloads = FR.Expression;
10576dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1058c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    for (OverloadExpr::decls_iterator it = Overloads->decls_begin(),
1059c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay         DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) {
10606dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      OverloadSet.addDecl(*it);
10616dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
10626dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      // Check whether the function is a non-template which takes no
10636dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      // arguments.
10646dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      if (const FunctionDecl *OverloadDecl
10656dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall            = dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl())) {
1066c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay        if (OverloadDecl->getMinRequiredArguments() == 0)
1067c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay          ZeroArgCallReturnTy = OverloadDecl->getResultType();
1068c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      }
1069c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
10706dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
107164a371ff8d525880e519a43fc522cbdc79fc4a89Douglas Gregor    // Ignore overloads that are pointer-to-member constants.
107264a371ff8d525880e519a43fc522cbdc79fc4a89Douglas Gregor    if (FR.HasFormOfMemberPointer)
10736dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      return false;
10746dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1075c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    return true;
1076c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
1077c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
10786dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) {
1079c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
1080c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      if (Fun->getMinRequiredArguments() == 0)
1081c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay        ZeroArgCallReturnTy = Fun->getResultType();
1082c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      return true;
1083c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
1084c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
1085c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1086c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  // We don't have an expression that's convenient to get a FunctionDecl from,
1087c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  // but we can at least check if the type is "function of 0 arguments".
1088c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  QualType ExprTy = E.getType();
1089c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  const FunctionType *FunTy = NULL;
10909389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay  QualType PointeeTy = ExprTy->getPointeeType();
10919389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay  if (!PointeeTy.isNull())
10929389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay    FunTy = PointeeTy->getAs<FunctionType>();
1093c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (!FunTy)
1094c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    FunTy = ExprTy->getAs<FunctionType>();
1095c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (!FunTy && ExprTy == Context.BoundMemberTy) {
1096c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // Look for the bound-member type.  If it's still overloaded, give up,
1097c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // although we probably should have fallen into the OverloadExpr case above
1098c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // if we actually have an overloaded bound member.
1099c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    QualType BoundMemberTy = Expr::findBoundMemberType(&E);
1100c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (!BoundMemberTy.isNull())
1101c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      FunTy = BoundMemberTy->castAs<FunctionType>();
1102c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
1103c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1104c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (const FunctionProtoType *FPT =
1105c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      dyn_cast_or_null<FunctionProtoType>(FunTy)) {
1106c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (FPT->getNumArgs() == 0)
1107c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      ZeroArgCallReturnTy = FunTy->getResultType();
1108c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    return true;
1109c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
1110c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  return false;
1111c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay}
1112c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1113c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \brief Give notes for a set of overloads.
1114c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1115c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// A companion to isExprCallable. In cases when the name that the programmer
1116c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// wrote was an overloaded function, we may be able to make some guesses about
1117c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// plausible overloads based on their return types; such guesses can be handed
1118c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// off to this method to be emitted as notes.
1119c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1120c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param Overloads - The overloads to note.
1121c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param FinalNoteLoc - If we've suppressed printing some overloads due to
1122c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  -fshow-overloads=best, this is the location to attach to the note about too
1123c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  many candidates. Typically this will be the location of the original
1124c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  ill-formed expression.
11256dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads,
11266dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                          const SourceLocation FinalNoteLoc) {
1127c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  int ShownOverloads = 0;
1128c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  int SuppressedOverloads = 0;
1129c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  for (UnresolvedSetImpl::iterator It = Overloads.begin(),
1130c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay       DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
1131c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // FIXME: Magic number for max shown overloads stolen from
1132c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // OverloadCandidateSet::NoteCandidates.
1133c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (ShownOverloads >= 4 &&
11346dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall        S.Diags.getShowOverloads() == DiagnosticsEngine::Ovl_Best) {
1135c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      ++SuppressedOverloads;
1136c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      continue;
1137c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
11386dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
11396dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    NamedDecl *Fn = (*It)->getUnderlyingDecl();
11409c0e1ec7b3afd833c1b958ce2aeedff71c7eb4c5Abramo Bagnara    S.Diag(Fn->getLocation(), diag::note_possible_target_of_call);
1141c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    ++ShownOverloads;
1142c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
11436dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1144c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (SuppressedOverloads)
11456dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    S.Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates)
11466dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << SuppressedOverloads;
11476dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
11486dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
11496dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic void notePlausibleOverloads(Sema &S, SourceLocation Loc,
11506dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                   const UnresolvedSetImpl &Overloads,
11516dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                   bool (*IsPlausibleResult)(QualType)) {
11526dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (!IsPlausibleResult)
11536dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return noteOverloads(S, Overloads, Loc);
11546dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
11556dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  UnresolvedSet<2> PlausibleOverloads;
11566dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  for (OverloadExpr::decls_iterator It = Overloads.begin(),
11576dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall         DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
11586dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It);
11596dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    QualType OverloadResultTy = OverloadDecl->getResultType();
11606dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    if (IsPlausibleResult(OverloadResultTy))
11616dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      PlausibleOverloads.addDecl(It.getDecl());
11626dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  }
11636dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  noteOverloads(S, PlausibleOverloads, Loc);
11646dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
11656dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
11666dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// Determine whether the given expression can be called by just
11676dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// putting parentheses after it.  Notably, expressions with unary
11686dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// operators can't be because the unary operator will start parsing
11696dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// outside the call.
11706dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool IsCallableWithAppend(Expr *E) {
11716dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  E = E->IgnoreImplicit();
11726dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return (!isa<CStyleCastExpr>(E) &&
11736dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<UnaryOperator>(E) &&
11746dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<BinaryOperator>(E) &&
11756dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<CXXOperatorCallExpr>(E));
11766dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
11776dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
11786dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallbool Sema::tryToRecoverWithCall(ExprResult &E, const PartialDiagnostic &PD,
11796dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                bool ForceComplain,
11806dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                bool (*IsPlausibleResult)(QualType)) {
11816dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  SourceLocation Loc = E.get()->getExprLoc();
11826dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  SourceRange Range = E.get()->getSourceRange();
11836dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
11846dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  QualType ZeroArgCallTy;
11856dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  UnresolvedSet<4> Overloads;
11866dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (isExprCallable(*E.get(), ZeroArgCallTy, Overloads) &&
11876dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      !ZeroArgCallTy.isNull() &&
11886dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      (!IsPlausibleResult || IsPlausibleResult(ZeroArgCallTy))) {
11896dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // At this point, we know E is potentially callable with 0
11906dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // arguments and that it returns something of a reasonable type,
11916dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // so we can emit a fixit and carry on pretending that E was
11926dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // actually a CallExpr.
11936dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    SourceLocation ParenInsertionLoc =
11946dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      PP.getLocForEndOfToken(Range.getEnd());
11956dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    Diag(Loc, PD)
11966dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << /*zero-arg*/ 1 << Range
11976dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << (IsCallableWithAppend(E.get())
11986dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          ? FixItHint::CreateInsertion(ParenInsertionLoc, "()")
11996dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          : FixItHint());
12006dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
12016dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
12026dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // FIXME: Try this before emitting the fixit, and suppress diagnostics
12036dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // while doing so.
12046dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    E = ActOnCallExpr(0, E.take(), ParenInsertionLoc,
12056dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                      MultiExprArg(*this, 0, 0),
12066dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                      ParenInsertionLoc.getLocWithOffset(1));
12076dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return true;
12086dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  }
12096dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
12106dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (!ForceComplain) return false;
12116dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
12126dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  Diag(Loc, PD) << /*not zero-arg*/ 0 << Range;
12136dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
12146dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  E = ExprError();
12156dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return true;
1216c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay}
1217