Sema.cpp revision 9c129f818038e0269ba6b095722aa70176dc321d
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"
215f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/CXXFieldCollector.h"
229b623639378d53a675921ddfa7316034d571881eDouglas Gregor#include "clang/Sema/TemplateDeduction.h"
2376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall#include "clang/Sema/ExternalSemaSource.h"
248c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl#include "clang/Sema/ObjCMethodList.h"
25f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
265f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/Scope.h"
27781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "clang/Sema/ScopeInfo.h"
2846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor#include "clang/Sema/SemaConsumer.h"
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
3079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/ASTDiagnostic.h"
31384aff8b94bb0d1ad6c5667b90621e5699815bb2John McCall#include "clang/AST/DeclCXX.h"
32c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
33e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
34c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay#include "clang/AST/ExprCXX.h"
35ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner#include "clang/AST/StmtCXX.h"
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Preprocessor.h"
37f7572a64a1e5f6f700527583ec5206a58ce6d9b6Douglas Gregor#include "clang/Basic/FileManager.h"
3891a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
394d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner#include "clang/Basic/TargetInfo.h"
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
41781472fe99a120098c631b0cbe33c89f8cef5e70John McCallusing namespace sema;
429ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
439ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorFunctionScopeInfo::~FunctionScopeInfo() { }
449ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
458fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidisvoid FunctionScopeInfo::Clear() {
46b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasBranchProtectedScope = false;
47b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasBranchIntoScope = false;
48b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasIndirectGoto = false;
49b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
509ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  SwitchStack.clear();
515077c3876beeaed32280af88244e8050078619a8Douglas Gregor  Returns.clear();
528fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  ErrorTrap.reset();
53351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  PossiblyUnreachableDiags.clear();
549ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
559ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
569ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo::~BlockScopeInfo() { }
579ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
588987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas GregorPrintingPolicy Sema::getPrintingPolicy() const {
598987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  PrintingPolicy Policy = Context.getPrintingPolicy();
608987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  Policy.Bool = getLangOptions().Bool;
618987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  if (!Policy.Bool) {
628987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor    if (MacroInfo *BoolMacro = PP.getMacroInfo(&Context.Idents.get("bool"))) {
638987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor      Policy.Bool = BoolMacro->isObjectLike() &&
648987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor        BoolMacro->getNumTokens() == 1 &&
658987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor        BoolMacro->getReplacementToken(0).is(tok::kw__Bool);
668987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor    }
678987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  }
688987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor
698987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  return Policy;
708987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor}
718987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor
72c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregorvoid Sema::ActOnTranslationUnitScope(Scope *S) {
738ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroff  TUScope = S;
7444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  PushDeclContext(S, Context.getTranslationUnitDecl());
751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall  VAListTagName = PP.getIdentifierInfo("__va_list_tag");
77c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall
78772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (PP.getLangOptions().ObjC1) {
79772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    // Synthesize "@class Protocol;
80772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    if (Context.getObjCProtoType().isNull()) {
81772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor      ObjCInterfaceDecl *ProtocolDecl =
82772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor        ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
83772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                  &Context.Idents.get("Protocol"),
84772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                  SourceLocation(), true);
85772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor      Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
86772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor      PushOnScopeChains(ProtocolDecl, TUScope, false);
87772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    }
884d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner  }
89b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
903b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff
91f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas GregorSema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
92467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor           TranslationUnitKind TUKind,
933a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           CodeCompleteConsumer *CodeCompleter)
94321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  : TheTargetAttributesSema(0), FPFeatures(pp.getLangOptions()),
9582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
975d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth    CollectStats(false), ExternalSource(0), CodeCompleter(CodeCompleter),
983a387441ae339363ee5b254658f295e97bd9e913Argyrios Kyrtzidis    CurContext(0), OriginalLexicalContext(0),
993a387441ae339363ee5b254658f295e97bd9e913Argyrios Kyrtzidis    PackContext(0), MSStructPragmaOn(false), VisContext(0),
100f85e193739c953358c865005855253af4f68a497John McCall    ExprNeedsCleanups(0), LateTemplateParser(0), OpaqueParser(0),
101eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    IdResolver(pp), CXXTypeInfoDecl(0), MSVCGuidDecl(0),
102b7566d8346ed8040a5cb7cefe96bd6ccbf015595Bill Wendling    GlobalNewDeleteDeclared(false),
1039a1ecf0522ccb7a45577f856150c15af0ee1df2aNico Weber    ObjCShouldCallSuperDealloc(false),
10480cb6e69d9e85231588ae604e4bc2bc9a07389afNico Weber    ObjCShouldCallSuperFinalize(false),
105467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    TUKind(TUKind),
1061eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor    NumSFINAEErrors(0), SuppressAccessChecking(false),
1071eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor    AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false),
1088491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor    NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1),
1098491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor    CurrentInstantiationScope(0), TyposCorrected(0),
110b7566d8346ed8040a5cb7cefe96bd6ccbf015595Bill Wendling    AnalysisWarnings(*this)
111f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor{
1123b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff  TUScope = 0;
113d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor  LoadedExternalKnownNamespaces = false;
114d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor
11507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis  if (getLangOptions().CPlusPlus)
11607952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    FieldCollector.reset(new CXXFieldCollector());
1171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11822caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  // Tell diagnostics how to render things from the AST library.
11979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
12079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                       &Context);
1212afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor
1222afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor  ExprEvalContexts.push_back(
123f85e193739c953358c865005855253af4f68a497John McCall        ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0, false));
124781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
1258fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(Diags));
12646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor}
12746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
12846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregorvoid Sema::Initialize() {
12946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the AST consumer about this Sema object.
13046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  Consumer.Initialize(Context);
13146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
13246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // FIXME: Isn't this redundant with the initialization above?
13346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
13446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->InitializeSema(*this);
13546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
13646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the external Sema source about this Sema object.
13746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
13846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor      = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
13946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->InitializeSema(*this);
1404dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor
141772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  // Initialize predefined 128-bit integer types, if needed.
142772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
143772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    // If either of the 128-bit integer types are unavailable to name lookup,
144772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    // define them now.
145772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    DeclarationName Int128 = &Context.Idents.get("__int128_t");
146eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Int128) == IdResolver.end())
147772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor      PushOnScopeChains(Context.getInt128Decl(), TUScope);
148772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
149772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    DeclarationName UInt128 = &Context.Idents.get("__uint128_t");
150eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(UInt128) == IdResolver.end())
151772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor      PushOnScopeChains(Context.getUInt128Decl(), TUScope);
152772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
153772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
154772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
1554dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  // Initialize predefined Objective-C types:
1564dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  if (PP.getLangOptions().ObjC1) {
1577a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    // If 'SEL' does not yet refer to any declarations, make it refer to the
1587a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    // predefined 'SEL'.
1597a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    DeclarationName SEL = &Context.Idents.get("SEL");
160eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(SEL) == IdResolver.end())
1617a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor      PushOnScopeChains(Context.getObjCSelDecl(), TUScope);
1627a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor
1634dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    // If 'id' does not yet refer to any declarations, make it refer to the
1644dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    // predefined 'id'.
1654dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    DeclarationName Id = &Context.Idents.get("id");
166eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Id) == IdResolver.end())
1674dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor      PushOnScopeChains(Context.getObjCIdDecl(), TUScope);
16879d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor
16979d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    // Create the built-in typedef for 'Class'.
17079d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    DeclarationName Class = &Context.Idents.get("Class");
171eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Class) == IdResolver.end())
17279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor      PushOnScopeChains(Context.getObjCClassDecl(), TUScope);
1734dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  }
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovSema::~Sema() {
17782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (PackContext) FreePackedContext();
178aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  if (VisContext) FreeVisContext();
17982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  delete TheTargetAttributesSema;
18062c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  MSStructPragmaOn = false;
181781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  // Kill all the active scopes.
182781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I)
183781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[I];
184781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1)
185781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[0];
18646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
18746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the SemaConsumer to forget about us; we're going out of scope.
18846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
18946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->ForgetSema();
19046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
19146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Detach from the external Sema source.
19246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
193914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor        = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
19446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->ForgetSema();
19582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
19682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
197f85e193739c953358c865005855253af4f68a497John McCall
198f85e193739c953358c865005855253af4f68a497John McCall/// makeUnavailableInSystemHeader - There is an error in the current
199f85e193739c953358c865005855253af4f68a497John McCall/// context.  If we're still in a system header, and we can plausibly
200f85e193739c953358c865005855253af4f68a497John McCall/// make the relevant declaration unavailable instead of erroring, do
201f85e193739c953358c865005855253af4f68a497John McCall/// so and return true.
202f85e193739c953358c865005855253af4f68a497John McCallbool Sema::makeUnavailableInSystemHeader(SourceLocation loc,
2035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                         StringRef msg) {
204f85e193739c953358c865005855253af4f68a497John McCall  // If we're not in a function, it's an error.
205f85e193739c953358c865005855253af4f68a497John McCall  FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext);
206f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) return false;
207f85e193739c953358c865005855253af4f68a497John McCall
208f85e193739c953358c865005855253af4f68a497John McCall  // If we're in template instantiation, it's an error.
209f85e193739c953358c865005855253af4f68a497John McCall  if (!ActiveTemplateInstantiations.empty())
210f85e193739c953358c865005855253af4f68a497John McCall    return false;
211f85e193739c953358c865005855253af4f68a497John McCall
212f85e193739c953358c865005855253af4f68a497John McCall  // If that function's not in a system header, it's an error.
213f85e193739c953358c865005855253af4f68a497John McCall  if (!Context.getSourceManager().isInSystemHeader(loc))
214f85e193739c953358c865005855253af4f68a497John McCall    return false;
215f85e193739c953358c865005855253af4f68a497John McCall
216f85e193739c953358c865005855253af4f68a497John McCall  // If the function is already unavailable, it's not an error.
217f85e193739c953358c865005855253af4f68a497John McCall  if (fn->hasAttr<UnavailableAttr>()) return true;
218f85e193739c953358c865005855253af4f68a497John McCall
219f85e193739c953358c865005855253af4f68a497John McCall  fn->addAttr(new (Context) UnavailableAttr(loc, Context, msg));
220f85e193739c953358c865005855253af4f68a497John McCall  return true;
221f85e193739c953358c865005855253af4f68a497John McCall}
222f85e193739c953358c865005855253af4f68a497John McCall
22358a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian RedlASTMutationListener *Sema::getASTMutationListener() const {
22458a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl  return getASTConsumer().GetASTMutationListener();
22558a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl}
22658a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl
2275d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth/// \brief Print out statistics about the semantic analysis.
2285d98994c7749312a43ce6adf45537979a98e7afdChandler Carruthvoid Sema::PrintStats() const {
2295d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  llvm::errs() << "\n*** Semantic Analysis Stats:\n";
2305d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n";
2315d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth
2325d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  BumpAlloc.PrintStats();
2335d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  AnalysisWarnings.PrintStats();
2345d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth}
2355d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth
2361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
2371e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner/// If there is already an implicit cast, merge into the existing one.
238906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl/// The result is of the given category.
239429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn WiegleyExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
240429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                   CastKind Kind, ExprValueKind VK,
241f85e193739c953358c865005855253af4f68a497John McCall                                   const CXXCastPath *BasePath,
242f85e193739c953358c865005855253af4f68a497John McCall                                   CheckedConversionKind CCK) {
2439c129f818038e0269ba6b095722aa70176dc321dRichard Smith#ifndef NDEBUG
2449c129f818038e0269ba6b095722aa70176dc321dRichard Smith  if (VK == VK_RValue && !E->isRValue()) {
2459c129f818038e0269ba6b095722aa70176dc321dRichard Smith    switch (Kind) {
2469c129f818038e0269ba6b095722aa70176dc321dRichard Smith    default:
2479c129f818038e0269ba6b095722aa70176dc321dRichard Smith      assert(0 && "can't implicitly cast lvalue to rvalue with this cast kind");
2489c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_LValueToRValue:
2499c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_ArrayToPointerDecay:
2509c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_FunctionToPointerDecay:
2519c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_ToVoid:
2529c129f818038e0269ba6b095722aa70176dc321dRichard Smith      break;
2539c129f818038e0269ba6b095722aa70176dc321dRichard Smith    }
2549c129f818038e0269ba6b095722aa70176dc321dRichard Smith  }
2559c129f818038e0269ba6b095722aa70176dc321dRichard Smith#endif
2569c129f818038e0269ba6b095722aa70176dc321dRichard Smith
257429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  QualType ExprTy = Context.getCanonicalType(E->getType());
2583a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType TypeTy = Context.getCanonicalType(Ty);
2591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2603a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  if (ExprTy == TypeTy)
261429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return Owned(E);
2621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
263f85e193739c953358c865005855253af4f68a497John McCall  if (getLangOptions().ObjCAutoRefCount)
264f85e193739c953358c865005855253af4f68a497John McCall    CheckObjCARCConversion(SourceRange(), Ty, E, CCK);
265f85e193739c953358c865005855253af4f68a497John McCall
2666fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // If this is a derived-to-base cast to a through a virtual base, we
2676fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // need a vtable.
2682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  if (Kind == CK_DerivedToBase &&
269f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall      BasePathInvolvesVirtualBase(*BasePath)) {
270429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    QualType T = E->getType();
2716fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const PointerType *Pointer = T->getAs<PointerType>())
2726fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      T = Pointer->getPointeeType();
2736fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const RecordType *RecordTy = T->getAs<RecordType>())
274429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      MarkVTableUsed(E->getLocStart(),
2756fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor                     cast<CXXRecordDecl>(RecordTy->getDecl()));
2766fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  }
2776fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor
278429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
279f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
2804c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      ImpCast->setType(Ty);
2815baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall      ImpCast->setValueKind(VK);
282429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return Owned(E);
2834c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson    }
2844c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson  }
2854c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson
286429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  return Owned(ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK));
287906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl}
288906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
289737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara/// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
290737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara/// to the conversion from scalar type ScalarTy to the Boolean type.
291737d5447b5d20633992ee5388eca5270c28c8ae7Abramo BagnaraCastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) {
292737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  switch (ScalarTy->getScalarTypeKind()) {
293737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Bool: return CK_NoOp;
2941d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_CPointer: return CK_PointerToBoolean;
2951d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_BlockPointer: return CK_PointerToBoolean;
2961d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_ObjCObjectPointer: return CK_PointerToBoolean;
297737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_MemberPointer: return CK_MemberPointerToBoolean;
298737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Integral: return CK_IntegralToBoolean;
299737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Floating: return CK_FloatingToBoolean;
300737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean;
301737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean;
302737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  }
303737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  return CK_Invalid;
304737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara}
305737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara
306bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis/// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
307bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidisstatic bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
308bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (D->isUsed())
309bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    return true;
310bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
311bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
312bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
313bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
314bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const FunctionDecl *DeclToCheck;
315bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (FD->hasBody(DeclToCheck))
316bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
317bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
318bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
319bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
320bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    DeclToCheck = FD->getMostRecentDeclaration();
321bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != FD)
322bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
323bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
324bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
325bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
326bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
327bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
328bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const VarDecl *DeclToCheck = VD->getDefinition();
329bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck)
330bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
331bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
332bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
333bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
334bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    DeclToCheck = VD->getMostRecentDeclaration();
335bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != VD)
336bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
337bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
338bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
339bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  return false;
340bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis}
341bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
34215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallnamespace {
34315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  struct UndefinedInternal {
34415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    NamedDecl *decl;
34515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    FullSourceLoc useLoc;
34615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
34715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    UndefinedInternal(NamedDecl *decl, FullSourceLoc useLoc)
34815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      : decl(decl), useLoc(useLoc) {}
34915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  };
35015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
35115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  bool operator<(const UndefinedInternal &l, const UndefinedInternal &r) {
35215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    return l.useLoc.isBeforeInTranslationUnitThan(r.useLoc);
35315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
35415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
35515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
35615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall/// checkUndefinedInternals - Check for undefined objects with internal linkage.
35715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallstatic void checkUndefinedInternals(Sema &S) {
35815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (S.UndefinedInternals.empty()) return;
35915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
36015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Collect all the still-undefined entities with internal linkage.
3615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<UndefinedInternal, 16> undefined;
36215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  for (llvm::DenseMap<NamedDecl*,SourceLocation>::iterator
36315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall         i = S.UndefinedInternals.begin(), e = S.UndefinedInternals.end();
36415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall       i != e; ++i) {
36515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    NamedDecl *decl = i->first;
36615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
36715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // Ignore attributes that have become invalid.
36815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    if (decl->isInvalidDecl()) continue;
36915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
37015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // __attribute__((weakref)) is basically a definition.
37115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    if (decl->hasAttr<WeakRefAttr>()) continue;
37215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
37315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) {
37415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      if (fn->isPure() || fn->hasBody())
37515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        continue;
37615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    } else {
37715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      if (cast<VarDecl>(decl)->hasDefinition() != VarDecl::DeclarationOnly)
37815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        continue;
37915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    }
38015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
38115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // We build a FullSourceLoc so that we can sort with array_pod_sort.
38215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    FullSourceLoc loc(i->second, S.Context.getSourceManager());
38315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    undefined.push_back(UndefinedInternal(decl, loc));
38415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
38515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
38615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (undefined.empty()) return;
38715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
38815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Sort (in order of use site) so that we're not (as) dependent on
38915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // the iteration order through an llvm::DenseMap.
39015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  llvm::array_pod_sort(undefined.begin(), undefined.end());
39115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
3925f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  for (SmallVectorImpl<UndefinedInternal>::iterator
39315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall         i = undefined.begin(), e = undefined.end(); i != e; ++i) {
39415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    NamedDecl *decl = i->decl;
39515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    S.Diag(decl->getLocation(), diag::warn_undefined_internal)
39615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      << isa<VarDecl>(decl) << decl;
39715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    S.Diag(i->useLoc, diag::note_used_here);
39815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
39915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
40015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
40131e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregorvoid Sema::LoadExternalWeakUndeclaredIdentifiers() {
40231e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  if (!ExternalSource)
40331e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    return;
40431e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor
40531e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs;
40631e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs);
40731e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  for (unsigned I = 0, N = WeakIDs.size(); I != N; ++I) {
40831e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator Pos
40931e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor      = WeakUndeclaredIdentifiers.find(WeakIDs[I].first);
41031e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    if (Pos != WeakUndeclaredIdentifiers.end())
41131e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor      continue;
41231e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor
41331e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    WeakUndeclaredIdentifiers.insert(WeakIDs[I]);
41431e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  }
41531e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor}
41631e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor
4179299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// ActOnEndOfTranslationUnit - This is called at the very end of the
4189299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// translation unit when EOF is reached and all but the top-level scope is
4199299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// popped.
4200e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidisvoid Sema::ActOnEndOfTranslationUnit() {
421467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  // Only complete translation units define vtables and perform implicit
422467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  // instantiations.
423467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  if (TUKind == TU_Complete) {
424aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // If any dynamic classes have their key function defined within
425aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // this translation unit, then those vtables are considered "used" and must
426aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // be emitted.
427a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor    for (DynamicClassesType::iterator I = DynamicClasses.begin(ExternalSource),
428a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor                                      E = DynamicClasses.end();
429a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor         I != E; ++I) {
430a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor      assert(!(*I)->isDependentType() &&
431a5c6c2a84cde5c9b8f8ec0610a9f89ffd54f44eeAnders Carlsson             "Should not see dependent types here!");
432a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor      if (const CXXMethodDecl *KeyFunction = Context.getKeyFunction(*I)) {
433aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth        const FunctionDecl *Definition = 0;
434aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth        if (KeyFunction->hasBody(Definition))
435a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor          MarkVTableUsed(Definition->getLocation(), *I, true);
436aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth      }
437aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    }
438aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth
4398155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // If DefinedUsedVTables ends up marking any virtual member functions it
4408155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // might lead to more pending template instantiations, which we then need
4418155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // to instantiate.
4428155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
4438155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
4448155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // C++: Perform implicit template instantiations.
4458155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    //
4468155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // FIXME: When we perform these implicit instantiations, we do not
4478155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // carefully keep track of the point of instantiation (C++ [temp.point]).
4488155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // This means that name lookup that occurs within the template
4498155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // instantiation will always happen at the end of the translation unit,
4508155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // so it will find some names that should not be found. Although this is
4518155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // common behavior for C++ compilers, it is technically wrong. In the
4528155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // future, we either need to be able to filter the results of name lookup
4538155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // or we need to perform template instantiations earlier.
4548155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    PerformPendingInstantiations();
4552a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
456d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson
45749b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Remove file scoped decls that turned out to be used.
458a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor  UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(0,
459a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor                                                                         true),
460bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                                             UnusedFileScopedDecls.end(),
461bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                              std::bind1st(std::ptr_fun(ShouldRemoveFromUnused),
462bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                                           this)),
46349b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis                              UnusedFileScopedDecls.end());
46447268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor
465467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  if (TUKind == TU_Prefix) {
466467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    // Translation unit prefixes don't need any of the checking below.
46787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    TUScope = 0;
46872b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    return;
46987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
47072b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis
47163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // Check for #pragma weak identifiers that were never declared
47263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // FIXME: This will cause diagnostics to be emitted in a non-determinstic
47363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // order!  Iterating over a densemap like this is bad.
47431e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  LoadExternalWeakUndeclaredIdentifiers();
475e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
47663d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       I = WeakUndeclaredIdentifiers.begin(),
47763d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
47863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    if (I->second.getUsed()) continue;
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
48163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      << I->first;
482e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  }
483e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn
484467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  if (TUKind == TU_Module) {
485467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    // Modules don't need any of the checking below.
486467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    TUScope = 0;
487467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    return;
488467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  }
489467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor
490275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  // C99 6.9.2p2:
491275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   A declaration of an identifier for an object that has file
492275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   scope without an initializer, and without a storage-class
493275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   specifier or with the storage-class specifier static,
494275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   constitutes a tentative definition. If a translation unit
495275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   contains one or more tentative definitions for an identifier,
496275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   and the translation unit contains no external definition for
497275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   that identifier, then the behavior is exactly as if the
498275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit contains a file scope declaration of that
499275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   identifier, with the composite type as of the end of the
500275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit, with an initializer equal to 0.
501e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  llvm::SmallSet<VarDecl *, 32> Seen;
502a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor  for (TentativeDefinitionsType::iterator
503a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor            T = TentativeDefinitions.begin(ExternalSource),
504a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor         TEnd = TentativeDefinitions.end();
505a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor       T != TEnd; ++T)
506a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor  {
507a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor    VarDecl *VD = (*T)->getActingDefinition();
508e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
509e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // If the tentative definition was completed, getActingDefinition() returns
510e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // null. If we've already seen this variable before, insert()'s second
511e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // return value is false.
512e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
513b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      continue;
514b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const IncompleteArrayType *ArrayT
516b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        = Context.getAsIncompleteArrayType(VD->getType())) {
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (RequireCompleteType(VD->getLocation(),
518b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                              ArrayT->getElementType(),
51963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner                              diag::err_tentative_def_incomplete_type_arr)) {
520b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        VD->setInvalidDecl();
52163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner        continue;
522275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor      }
5231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52463d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      // Set the length of the array to 1 (C99 6.9.2p5).
52563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
52663d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
52746a617a792bfab0d9b1e057371ea3b9540802226John McCall      QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
52846a617a792bfab0d9b1e057371ea3b9540802226John McCall                                                One, ArrayType::Normal, 0);
52963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      VD->setType(T);
5301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
531b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                                   diag::err_tentative_def_incomplete_type))
532b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      VD->setInvalidDecl();
533b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
534b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    // Notify the consumer that we've completed a tentative definition.
535b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    if (!VD->isInvalidDecl())
536b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      Consumer.CompleteTentativeDefinition(VD);
537b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
538275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  }
53943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis
540c1598700010cea9364a58a65e967b0b56361b6aaSean Hunt  if (LangOpts.CPlusPlus0x &&
541c1598700010cea9364a58a65e967b0b56361b6aaSean Hunt      Diags.getDiagnosticLevel(diag::warn_delegating_ctor_cycle,
542c1598700010cea9364a58a65e967b0b56361b6aaSean Hunt                               SourceLocation())
543d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie        != DiagnosticsEngine::Ignored)
544fe57eef44cc80e5bb51e4f484835be08b8d84256Sean Hunt    CheckDelegatingCtorCycles();
545fe57eef44cc80e5bb51e4f484835be08b8d84256Sean Hunt
54643f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  // If there were errors, disable 'unused' warnings since they will mostly be
54743f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  // noise.
54843f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  if (!Diags.hasErrorOccurred()) {
54943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis    // Output warning for unused file scoped decls.
550a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor    for (UnusedFileScopedDeclsType::iterator
551a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor           I = UnusedFileScopedDecls.begin(ExternalSource),
55243f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis           E = UnusedFileScopedDecls.end(); I != E; ++I) {
553a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor      if (ShouldRemoveFromUnused(this, *I))
554a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor        continue;
555a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor
55643f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
55743f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        const FunctionDecl *DiagD;
55843f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        if (!FD->hasBody(DiagD))
55943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis          DiagD = FD;
56048b89590f61575cbf365ba996a2bd1ba1561a4abArgyrios Kyrtzidis        if (DiagD->isDeleted())
56148b89590f61575cbf365ba996a2bd1ba1561a4abArgyrios Kyrtzidis          continue; // Deleted functions are supposed to be unused.
5626b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        if (DiagD->isReferenced()) {
5636b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          if (isa<CXXMethodDecl>(DiagD))
5646b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis            Diag(DiagD->getLocation(), diag::warn_unneeded_member_function)
5656b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                  << DiagD->getDeclName();
5666b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          else
5676b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis            Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
5686b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                  << /*function*/0 << DiagD->getDeclName();
5696b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        } else {
5706b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(),
5716b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis               isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function
5726b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                                         : diag::warn_unused_function)
5736b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << DiagD->getDeclName();
5746b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        }
57543f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      } else {
57643f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
57743f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        if (!DiagD)
57843f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis          DiagD = cast<VarDecl>(*I);
5796b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        if (DiagD->isReferenced()) {
5806b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
5816b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << /*variable*/1 << DiagD->getDeclName();
5826b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        } else {
5836b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(), diag::warn_unused_variable)
5846b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << DiagD->getDeclName();
5856b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        }
58643f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      }
587bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    }
58815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
58915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    checkUndefinedInternals(*this);
59049b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  }
59187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
592483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // Check we've noticed that we're no longer parsing the initializer for every
593483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // variable. If we miss cases, then at best we have a performance issue and
594483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // at worst a rejects-valid bug.
595483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  assert(ParsingInitForAutoVars.empty() &&
596483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith         "Didn't unmark var as having its initializer parsed");
597483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
59887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  TUScope = 0;
5999299f3fa85796613cc787a2062c9562d07c8613eChris Lattner}
6009299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
6019299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
6025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
6035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper functions.
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6068517d9b731f065cdfc55ec0f3ddf5d564d988648Anders CarlssonDeclContext *Sema::getFunctionLevelDeclContext() {
607db0ee1da16e9dbec19b144c9cd96ee9f55fe0c53John McCall  DeclContext *DC = CurContext;
6081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
609d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor  while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
6108517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson    DC = DC->getParent();
6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6128517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  return DC;
6138517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson}
6148517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson
615371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// getCurFunctionDecl - If inside of a function body, this returns a pointer
616371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// to the function decl for the function being parsed.  If we're currently
617371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// in a 'block', this returns the containing context.
618371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerFunctionDecl *Sema::getCurFunctionDecl() {
6198517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
620371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return dyn_cast<FunctionDecl>(DC);
621371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
622371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
623c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel DunbarObjCMethodDecl *Sema::getCurMethodDecl() {
6248517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
625d7612e183bb09d04677d0ddde89ee29130ffb715Steve Naroff  return dyn_cast<ObjCMethodDecl>(DC);
626c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar}
627371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
628371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerNamedDecl *Sema::getCurFunctionOrMethodDecl() {
6298517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
630371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
6314afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return cast<NamedDecl>(DC);
632371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return 0;
633371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
634371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
63525a88bbf042317976f0d9cbfa87dfe89426e8393Douglas GregorSema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
6369b623639378d53a675921ddfa7316034d571881eDouglas Gregor  if (!isActive())
6379b623639378d53a675921ddfa7316034d571881eDouglas Gregor    return;
6389b623639378d53a675921ddfa7316034d571881eDouglas Gregor
6391eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor  if (llvm::Optional<TemplateDeductionInfo*> Info = SemaRef.isSFINAEContext()) {
64033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    switch (DiagnosticIDs::getDiagnosticSFINAEResponse(getDiagID())) {
64133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Report:
64277faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // We'll report the diagnostic below.
6439b623639378d53a675921ddfa7316034d571881eDouglas Gregor      break;
6449b623639378d53a675921ddfa7316034d571881eDouglas Gregor
64577faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    case DiagnosticIDs::SFINAE_SubstitutionFailure:
64677faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Count this failure so that we know that template argument deduction
64777faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // has failed.
64877faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      ++SemaRef.NumSFINAEErrors;
64977faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      SemaRef.Diags.setLastDiagnosticIgnored();
65077faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      SemaRef.Diags.Clear();
65177faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      Clear();
65277faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      return;
65377faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
65477faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    case DiagnosticIDs::SFINAE_AccessControl: {
6557822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // Per C++ Core Issue 1170, access control is part of SFINAE.
6567822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // Additionally, the AccessCheckingSFINAE flag can be used to temporary
6577822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // make access control a part of SFINAE for the purposes of checking
6587822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // type traits.
6597822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      if (!SemaRef.AccessCheckingSFINAE &&
6607822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor          !SemaRef.getLangOptions().CPlusPlus0x)
6611eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor        break;
66277faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
66377faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      SourceLocation Loc = getLocation();
66477faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
66577faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Suppress this diagnostic.
6669b623639378d53a675921ddfa7316034d571881eDouglas Gregor      ++SemaRef.NumSFINAEErrors;
6679b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.setLastDiagnosticIgnored();
6689b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.Clear();
6699b623639378d53a675921ddfa7316034d571881eDouglas Gregor      Clear();
67077faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
67177faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Now the diagnostic state is clear, produce a C++98 compatibility
67277faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // warning.
67377faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      SemaRef.Diag(Loc, diag::warn_cxx98_compat_sfinae_access_control);
67477faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
67577faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // The last diagnostic which Sema produced was ignored. Suppress any
67677faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // notes attached to it.
67777faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      SemaRef.Diags.setLastDiagnosticIgnored();
6789b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
67977faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    }
68077faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
68133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Suppress:
6829b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Make a copy of this suppressed diagnostic and store it with the
6839b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // template-deduction information;
684b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor      FlushCounts();
68540847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie      Diagnostic DiagInfo(&SemaRef.Diags);
686b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor
6871eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor      if (*Info)
6881eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor        (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(),
6899b623639378d53a675921ddfa7316034d571881eDouglas Gregor                        PartialDiagnostic(DiagInfo,
6909b623639378d53a675921ddfa7316034d571881eDouglas Gregor                                          SemaRef.Context.getDiagAllocator()));
6919b623639378d53a675921ddfa7316034d571881eDouglas Gregor
6929b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Suppress this diagnostic.
6939b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.setLastDiagnosticIgnored();
6949b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.Clear();
6959b623639378d53a675921ddfa7316034d571881eDouglas Gregor      Clear();
6969b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
6979b623639378d53a675921ddfa7316034d571881eDouglas Gregor    }
6989b623639378d53a675921ddfa7316034d571881eDouglas Gregor  }
6999b623639378d53a675921ddfa7316034d571881eDouglas Gregor
7008987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  // Set up the context's printing policy based on our current state.
7018987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  SemaRef.Context.setPrintingPolicy(SemaRef.getPrintingPolicy());
7028987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor
7039b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // Emit the diagnostic.
7045e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  if (!this->Emit())
7055e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    return;
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
70725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // If this is not a note, and we're in a template instantiation
70825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // that is different from the last template instantiation where
70925a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // we emitted an error, print a template instantiation
71025a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // backtrace.
71133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  if (!DiagnosticIDs::isBuiltinNote(DiagID) &&
71225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      !SemaRef.ActiveTemplateInstantiations.empty() &&
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      SemaRef.ActiveTemplateInstantiations.back()
71425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor        != SemaRef.LastTemplateInstantiationErrorContext) {
71525a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    SemaRef.PrintInstantiationStack();
7161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.LastTemplateInstantiationErrorContext
71725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      = SemaRef.ActiveTemplateInstantiations.back();
71825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  }
71925a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor}
7202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
721eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas GregorSema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) {
72233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  DiagnosticBuilder DB = Diags.Report(Loc, DiagID);
723eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor  return SemaDiagnosticBuilder(DB, *this, DiagID);
724eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor}
725eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor
72691a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::SemaDiagnosticBuilder
72791a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
72891a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
72991a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  PD.Emit(Builder);
7301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
73191a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  return Builder;
73291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson}
73391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson
734108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// \brief Looks through the macro-expansion chain for the given
735108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// location, looking for a macro expansion with the given name.
736834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// If one is found, returns true and sets the location to that
737108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// expansion loc.
7385f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerbool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) {
739834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  SourceLocation loc = locref;
740834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (!loc.isMacroID()) return false;
741834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
742834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // There's no good way right now to look at the intermediate
743108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  // expansions, so just jump to the expansion location.
744402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  loc = getSourceManager().getExpansionLoc(loc);
745834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
746834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // If that's written with the name, stop here.
7475f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<char, 16> buffer;
748834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (getPreprocessor().getSpelling(loc, buffer) == name) {
749834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    locref = loc;
750834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    return true;
751834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  }
752834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  return false;
753834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall}
754834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
75523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \brief Determines the active Scope associated with the given declaration
75623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// context.
75723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
75823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// This routine maps a declaration context to the active Scope object that
75923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// represents that declaration context in the parser. It is typically used
76023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// from "scope-less" code (e.g., template instantiation, lazy creation of
76123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// declarations) that injects a name for name-lookup purposes and, therefore,
76223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// must update the Scope.
76323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
76423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \returns The scope corresponding to the given declaraion context, or NULL
76523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// if no such scope is open.
76623c94dbb6631fecdb55ba401aa93722803d980c6Douglas GregorScope *Sema::getScopeForContext(DeclContext *Ctx) {
76723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
76823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!Ctx)
76923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return 0;
77023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
77123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Ctx = Ctx->getPrimaryContext();
77223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
773cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // Ignore scopes that cannot have declarations. This is important for
774cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // out-of-line definitions of static class members.
775cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
776cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl      if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity()))
777cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl        if (Ctx == Entity->getPrimaryContext())
778cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl          return S;
77923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  }
78023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
78123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return 0;
78223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor}
7839ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
7849ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Enter a new function scope
7859ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushFunctionScope() {
786781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1) {
787781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // Use the "top" function scope rather than having to allocate
788781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // memory for a new scope.
7898fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis    FunctionScopes.back()->Clear();
790781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    FunctionScopes.push_back(FunctionScopes.back());
7919ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return;
7929ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  }
7939ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
7948fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics()));
7959ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
7969ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
7979ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
7988fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(),
7999ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor                                              BlockScope, Block));
8009ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
8019ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
8023ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenekvoid Sema::PopFunctionOrBlockScope(const AnalysisBasedWarnings::Policy *WP,
8033ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek                                   const Decl *D, const BlockExpr *blkExpr) {
8043ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  FunctionScopeInfo *Scope = FunctionScopes.pop_back_val();
805781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  assert(!FunctionScopes.empty() && "mismatched push/pop!");
8063ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek
8073ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  // Issue any analysis-based warnings.
8083ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  if (WP && D)
809283a358aecb75e30fcd486f2206f6c03c5e7f11dTed Kremenek    AnalysisWarnings.IssueWarnings(*WP, Scope, D, blkExpr);
810351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  else {
8115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator
812351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         i = Scope->PossiblyUnreachableDiags.begin(),
813351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         e = Scope->PossiblyUnreachableDiags.end();
814351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         i != e; ++i) {
815351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek      const sema::PossiblyUnreachableDiag &D = *i;
816351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek      Diag(D.Loc, D.PD);
817351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek    }
818351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  }
8193ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek
820351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  if (FunctionScopes.back() != Scope) {
821781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete Scope;
822351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  }
8239ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
8249ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
8259ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Determine whether any errors occurred within this function/method/
8269ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// block.
827f85e193739c953358c865005855253af4f68a497John McCallbool Sema::hasAnyUnrecoverableErrorsInThisFunction() const {
828f85e193739c953358c865005855253af4f68a497John McCall  return getCurFunction()->ErrorTrap.hasUnrecoverableErrorOccurred();
8299ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
8309ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
8319ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo *Sema::getCurBlock() {
8329ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.empty())
8339ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return 0;
8349ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
8359ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  return dyn_cast<BlockScopeInfo>(FunctionScopes.back());
8369ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
83776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
83876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall// Pin this vtable to this file.
83976bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalSemaSource::~ExternalSemaSource() {}
840f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
8418c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redlstd::pair<ObjCMethodList, ObjCMethodList>
8428c84571f3e262569ba51d107db7ab31a23de79b3Sebastian RedlExternalSemaSource::ReadMethodPool(Selector Sel) {
8438c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl  return std::pair<ObjCMethodList, ObjCMethodList>();
8448c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl}
8458c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl
846d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregorvoid ExternalSemaSource::ReadKnownNamespaces(
8475f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
848d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor}
849d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor
8505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid PrettyDeclStackTraceEntry::print(raw_ostream &OS) const {
851f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  SourceLocation Loc = this->Loc;
852f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
853f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (Loc.isValid()) {
854f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Loc.print(OS, S.getSourceManager());
855f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    OS << ": ";
856f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
857f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << Message;
858f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
859f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (TheDecl && isa<NamedDecl>(TheDecl)) {
860f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString();
861f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    if (!Name.empty())
862f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      OS << " '" << Name << '\'';
863f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
864f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
865f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << '\n';
866f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall}
867c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
868c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \brief Figure out if an expression could be turned into a call.
869c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
870c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// Use this when trying to recover from an error where the programmer may have
871c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// written just the name of a function instead of actually calling it.
872c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
873c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param E - The expression to examine.
874c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param ZeroArgCallReturnTy - If the expression can be turned into a call
875c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  with no arguments, this parameter is set to the type returned by such a
876c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  call; otherwise, it is set to an empty QualType.
8776dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// \param OverloadSet - If the expression is an overloaded function
878c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  name, this parameter is populated with the decls of the various overloads.
879c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gaybool Sema::isExprCallable(const Expr &E, QualType &ZeroArgCallReturnTy,
8806dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                          UnresolvedSetImpl &OverloadSet) {
881c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  ZeroArgCallReturnTy = QualType();
8826dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  OverloadSet.clear();
8836dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
8846dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (E.getType() == Context.OverloadTy) {
8856dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    OverloadExpr::FindResult FR = OverloadExpr::find(const_cast<Expr*>(&E));
8866dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    const OverloadExpr *Overloads = FR.Expression;
8876dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
888c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    for (OverloadExpr::decls_iterator it = Overloads->decls_begin(),
889c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay         DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) {
8906dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      OverloadSet.addDecl(*it);
8916dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
8926dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      // Check whether the function is a non-template which takes no
8936dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      // arguments.
8946dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      if (const FunctionDecl *OverloadDecl
8956dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall            = dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl())) {
896c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay        if (OverloadDecl->getMinRequiredArguments() == 0)
897c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay          ZeroArgCallReturnTy = OverloadDecl->getResultType();
898c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      }
899c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
9006dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
90164a371ff8d525880e519a43fc522cbdc79fc4a89Douglas Gregor    // Ignore overloads that are pointer-to-member constants.
90264a371ff8d525880e519a43fc522cbdc79fc4a89Douglas Gregor    if (FR.HasFormOfMemberPointer)
9036dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      return false;
9046dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
905c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    return true;
906c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
907c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
9086dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) {
909c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
910c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      if (Fun->getMinRequiredArguments() == 0)
911c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay        ZeroArgCallReturnTy = Fun->getResultType();
912c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      return true;
913c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
914c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
915c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
916c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  // We don't have an expression that's convenient to get a FunctionDecl from,
917c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  // but we can at least check if the type is "function of 0 arguments".
918c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  QualType ExprTy = E.getType();
919c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  const FunctionType *FunTy = NULL;
9209389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay  QualType PointeeTy = ExprTy->getPointeeType();
9219389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay  if (!PointeeTy.isNull())
9229389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay    FunTy = PointeeTy->getAs<FunctionType>();
923c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (!FunTy)
924c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    FunTy = ExprTy->getAs<FunctionType>();
925c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (!FunTy && ExprTy == Context.BoundMemberTy) {
926c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // Look for the bound-member type.  If it's still overloaded, give up,
927c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // although we probably should have fallen into the OverloadExpr case above
928c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // if we actually have an overloaded bound member.
929c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    QualType BoundMemberTy = Expr::findBoundMemberType(&E);
930c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (!BoundMemberTy.isNull())
931c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      FunTy = BoundMemberTy->castAs<FunctionType>();
932c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
933c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
934c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (const FunctionProtoType *FPT =
935c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      dyn_cast_or_null<FunctionProtoType>(FunTy)) {
936c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (FPT->getNumArgs() == 0)
937c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      ZeroArgCallReturnTy = FunTy->getResultType();
938c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    return true;
939c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
940c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  return false;
941c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay}
942c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
943c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \brief Give notes for a set of overloads.
944c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
945c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// A companion to isExprCallable. In cases when the name that the programmer
946c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// wrote was an overloaded function, we may be able to make some guesses about
947c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// plausible overloads based on their return types; such guesses can be handed
948c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// off to this method to be emitted as notes.
949c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
950c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param Overloads - The overloads to note.
951c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param FinalNoteLoc - If we've suppressed printing some overloads due to
952c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  -fshow-overloads=best, this is the location to attach to the note about too
953c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  many candidates. Typically this will be the location of the original
954c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  ill-formed expression.
9556dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads,
9566dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                          const SourceLocation FinalNoteLoc) {
957c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  int ShownOverloads = 0;
958c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  int SuppressedOverloads = 0;
959c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  for (UnresolvedSetImpl::iterator It = Overloads.begin(),
960c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay       DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
961c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // FIXME: Magic number for max shown overloads stolen from
962c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // OverloadCandidateSet::NoteCandidates.
963c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (ShownOverloads >= 4 &&
9646dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall        S.Diags.getShowOverloads() == DiagnosticsEngine::Ovl_Best) {
965c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      ++SuppressedOverloads;
966c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      continue;
967c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
9686dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
9696dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    NamedDecl *Fn = (*It)->getUnderlyingDecl();
9706dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    S.Diag(Fn->getLocStart(), diag::note_possible_target_of_call);
971c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    ++ShownOverloads;
972c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
9736dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
974c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (SuppressedOverloads)
9756dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    S.Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates)
9766dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << SuppressedOverloads;
9776dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
9786dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
9796dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic void notePlausibleOverloads(Sema &S, SourceLocation Loc,
9806dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                   const UnresolvedSetImpl &Overloads,
9816dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                   bool (*IsPlausibleResult)(QualType)) {
9826dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (!IsPlausibleResult)
9836dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return noteOverloads(S, Overloads, Loc);
9846dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
9856dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  UnresolvedSet<2> PlausibleOverloads;
9866dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  for (OverloadExpr::decls_iterator It = Overloads.begin(),
9876dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall         DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
9886dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It);
9896dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    QualType OverloadResultTy = OverloadDecl->getResultType();
9906dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    if (IsPlausibleResult(OverloadResultTy))
9916dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      PlausibleOverloads.addDecl(It.getDecl());
9926dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  }
9936dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  noteOverloads(S, PlausibleOverloads, Loc);
9946dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
9956dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
9966dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// Determine whether the given expression can be called by just
9976dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// putting parentheses after it.  Notably, expressions with unary
9986dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// operators can't be because the unary operator will start parsing
9996dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// outside the call.
10006dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool IsCallableWithAppend(Expr *E) {
10016dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  E = E->IgnoreImplicit();
10026dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return (!isa<CStyleCastExpr>(E) &&
10036dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<UnaryOperator>(E) &&
10046dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<BinaryOperator>(E) &&
10056dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<CXXOperatorCallExpr>(E));
10066dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
10076dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
10086dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallbool Sema::tryToRecoverWithCall(ExprResult &E, const PartialDiagnostic &PD,
10096dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                bool ForceComplain,
10106dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                bool (*IsPlausibleResult)(QualType)) {
10116dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  SourceLocation Loc = E.get()->getExprLoc();
10126dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  SourceRange Range = E.get()->getSourceRange();
10136dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
10146dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  QualType ZeroArgCallTy;
10156dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  UnresolvedSet<4> Overloads;
10166dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (isExprCallable(*E.get(), ZeroArgCallTy, Overloads) &&
10176dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      !ZeroArgCallTy.isNull() &&
10186dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      (!IsPlausibleResult || IsPlausibleResult(ZeroArgCallTy))) {
10196dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // At this point, we know E is potentially callable with 0
10206dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // arguments and that it returns something of a reasonable type,
10216dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // so we can emit a fixit and carry on pretending that E was
10226dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // actually a CallExpr.
10236dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    SourceLocation ParenInsertionLoc =
10246dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      PP.getLocForEndOfToken(Range.getEnd());
10256dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    Diag(Loc, PD)
10266dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << /*zero-arg*/ 1 << Range
10276dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << (IsCallableWithAppend(E.get())
10286dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          ? FixItHint::CreateInsertion(ParenInsertionLoc, "()")
10296dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          : FixItHint());
10306dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
10316dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
10326dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // FIXME: Try this before emitting the fixit, and suppress diagnostics
10336dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // while doing so.
10346dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    E = ActOnCallExpr(0, E.take(), ParenInsertionLoc,
10356dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                      MultiExprArg(*this, 0, 0),
10366dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                      ParenInsertionLoc.getLocWithOffset(1));
10376dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return true;
10386dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  }
10396dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
10406dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (!ForceComplain) return false;
10416dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
10426dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  Diag(Loc, PD) << /*not zero-arg*/ 0 << Range;
10436dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
10446dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  E = ExprError();
10456dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return true;
1046c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay}
1047