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"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/ASTDiagnostic.h"
18384aff8b94bb0d1ad6c5667b90621e5699815bb2John McCall#include "clang/AST/DeclCXX.h"
19f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper#include "clang/AST/DeclFriend.h"
20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
21e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
22c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay#include "clang/AST/ExprCXX.h"
23ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner#include "clang/AST/StmtCXX.h"
246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "clang/Basic/DiagnosticOptions.h"
25f7572a64a1e5f6f700527583ec5206a58ce6d9b6Douglas Gregor#include "clang/Basic/FileManager.h"
2691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
274d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner#include "clang/Basic/TargetInfo.h"
2855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/HeaderSearch.h"
2955fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/Preprocessor.h"
3055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/CXXFieldCollector.h"
3155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/DelayedDiagnostic.h"
3255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/ExternalSemaSource.h"
3355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/MultiplexExternalSemaSource.h"
3455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/ObjCMethodList.h"
3555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/PrettyDeclStackTrace.h"
3655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Scope.h"
3755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/ScopeInfo.h"
3855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/SemaConsumer.h"
3955fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/TemplateDeduction.h"
4055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/APFloat.h"
4155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/DenseMap.h"
4255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/SmallSet.h"
4355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/CrashRecoveryContext.h"
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
45781472fe99a120098c631b0cbe33c89f8cef5e70John McCallusing namespace sema;
469ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen HinesSourceLocation Sema::getLocForEndOfToken(SourceLocation Loc, unsigned Offset) {
486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen HinesModuleLoader &Sema::getModuleLoader() const { return PP.getModuleLoader(); }
526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
53ea8c59aaa6bd19976879142296f8fd12f8926738Argyrios KyrtzidisPrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context,
54ea8c59aaa6bd19976879142296f8fd12f8926738Argyrios Kyrtzidis                                       const Preprocessor &PP) {
558987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  PrintingPolicy Policy = Context.getPrintingPolicy();
564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  Policy.Bool = Context.getLangOpts().Bool;
578987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  if (!Policy.Bool) {
589818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis    if (const MacroInfo *
599818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis          BoolMacro = PP.getMacroInfo(&Context.Idents.get("bool"))) {
600116a40116616701282e12f3d92cf2949102d76aMichael Gottesman      Policy.Bool = BoolMacro->isObjectLike() &&
618987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor        BoolMacro->getNumTokens() == 1 &&
628987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor        BoolMacro->getReplacementToken(0).is(tok::kw__Bool);
638987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor    }
648987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  }
650116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
668987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  return Policy;
678987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor}
688987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor
69c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregorvoid Sema::ActOnTranslationUnitScope(Scope *S) {
708ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroff  TUScope = S;
7144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  PushDeclContext(S, Context.getTranslationUnitDecl());
721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
73c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall  VAListTagName = PP.getIdentifierInfo("__va_list_tag");
74b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
753b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff
76f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas GregorSema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
77467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor           TranslationUnitKind TUKind,
783a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           CodeCompleteConsumer *CodeCompleter)
796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  : ExternalSource(nullptr),
800ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    isMultiplexExternalSource(false), FPFeatures(pp.getLangOpts()),
814e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    LangOpts(pp.getLangOpts()), PP(pp), Context(ctxt), Consumer(consumer),
821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
830ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    CollectStats(false), CodeCompleter(CodeCompleter),
846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    CurContext(nullptr), OriginalLexicalContext(nullptr),
856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    PackContext(nullptr), MSStructPragmaOn(false),
86651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    MSPointerToMemberRepresentationMethod(
87651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        LangOpts.getMSPointerToMemberRepresentationMethod()),
88651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    VtorDispModeStack(1, MSVtorDispAttr::Mode(LangOpts.VtorDispMode)),
896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    DataSegStack(nullptr), BSSSegStack(nullptr), ConstSegStack(nullptr),
906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    CodeSegStack(nullptr), VisContext(nullptr),
91e49ff3ef3459e97fa76502bd9eae4ed9170fd048Richard Smith    IsBuildingRecoveryCallExpr(false),
926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ExprNeedsCleanups(false), LateTemplateParser(nullptr),
936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    OpaqueParser(nullptr), IdResolver(pp), StdInitializerList(nullptr),
946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    CXXTypeInfoDecl(nullptr), MSVCGuidDecl(nullptr),
956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    NSNumberDecl(nullptr),
966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    NSStringDecl(nullptr), StringWithUTF8StringMethod(nullptr),
976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    NSArrayDecl(nullptr), ArrayWithObjectsMethod(nullptr),
986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    NSDictionaryDecl(nullptr), DictionaryWithObjectsMethod(nullptr),
990116a40116616701282e12f3d92cf2949102d76aMichael Gottesman    GlobalNewDeleteDeclared(false),
100467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    TUKind(TUKind),
101651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    NumSFINAEErrors(0),
1021eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor    AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false),
1038491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor    NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1),
1046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    CurrentInstantiationScope(nullptr), DisableTypoCorrection(false),
1059bd3cdc3b78653275a36f15df81e1def3b2db8dbRichard Smith    TyposCorrected(0), AnalysisWarnings(*this),
1066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    VarDataSharingAttributesStack(nullptr), CurScope(nullptr),
1076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Ident_super(nullptr), Ident___float128(nullptr)
108f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor{
1096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  TUScope = nullptr;
1100116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
111d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor  LoadedExternalKnownNamespaces = false;
112ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  for (unsigned I = 0; I != NSAPI::NumNSNumberLiteralMethods; ++I)
1136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    NSNumberLiteralMethods[I] = nullptr;
114ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
1154e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC1)
116ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    NSAPIObj.reset(new NSAPI(Context));
117ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
1184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
11907952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    FieldCollector.reset(new CXXFieldCollector());
1201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12122caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  // Tell diagnostics how to render things from the AST library.
1220116a40116616701282e12f3d92cf2949102d76aMichael Gottesman  PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
12379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                       &Context);
1242afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor
1252afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor  ExprEvalContexts.push_back(
12676f3f69db1416425070177243e9f390122c553e0Richard Smith        ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0,
1276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                          false, nullptr, false));
128781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
1298fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(Diags));
1300c018357b8bbb1f96bbf622a5807421e626b4228Alexey Bataev
1310c018357b8bbb1f96bbf622a5807421e626b4228Alexey Bataev  // Initilization of data sharing attributes stack for OpenMP
1320c018357b8bbb1f96bbf622a5807421e626b4228Alexey Bataev  InitDataSharingAttributesStack();
13346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor}
13446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
135651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid Sema::addImplicitTypedef(StringRef Name, QualType T) {
136651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DeclarationName DN = &Context.Idents.get(Name);
137651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (IdResolver.begin(DN) == IdResolver.end())
138651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    PushOnScopeChains(Context.buildImplicitTypedef(T, Name), TUScope);
139651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
140651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
14146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregorvoid Sema::Initialize() {
14246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the AST consumer about this Sema object.
14346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  Consumer.Initialize(Context);
1440116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
14546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // FIXME: Isn't this redundant with the initialization above?
14646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
14746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->InitializeSema(*this);
1480116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
14946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the external Sema source about this Sema object.
15046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
15146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor      = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
15246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->InitializeSema(*this);
1534dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor
154772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  // Initialize predefined 128-bit integer types, if needed.
1556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (Context.getTargetInfo().hasInt128Type()) {
156772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    // If either of the 128-bit integer types are unavailable to name lookup,
157772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    // define them now.
158772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    DeclarationName Int128 = &Context.Idents.get("__int128_t");
159eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Int128) == IdResolver.end())
160772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor      PushOnScopeChains(Context.getInt128Decl(), TUScope);
161772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
162772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    DeclarationName UInt128 = &Context.Idents.get("__uint128_t");
163eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(UInt128) == IdResolver.end())
164772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor      PushOnScopeChains(Context.getUInt128Decl(), TUScope);
165772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
1660116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
167772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
1684dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  // Initialize predefined Objective-C types:
1694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (PP.getLangOpts().ObjC1) {
1707a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    // If 'SEL' does not yet refer to any declarations, make it refer to the
1717a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    // predefined 'SEL'.
1727a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    DeclarationName SEL = &Context.Idents.get("SEL");
173eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(SEL) == IdResolver.end())
1747a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor      PushOnScopeChains(Context.getObjCSelDecl(), TUScope);
1757a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor
1764dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    // If 'id' does not yet refer to any declarations, make it refer to the
1774dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    // predefined 'id'.
1784dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    DeclarationName Id = &Context.Idents.get("id");
179eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Id) == IdResolver.end())
1804dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor      PushOnScopeChains(Context.getObjCIdDecl(), TUScope);
1810116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
18279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    // Create the built-in typedef for 'Class'.
18379d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    DeclarationName Class = &Context.Idents.get("Class");
184eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Class) == IdResolver.end())
18579d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor      PushOnScopeChains(Context.getObjCClassDecl(), TUScope);
186a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
187a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    // Create the built-in forward declaratino for 'Protocol'.
188a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    DeclarationName Protocol = &Context.Idents.get("Protocol");
189a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    if (IdResolver.begin(Protocol) == IdResolver.end())
190a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor      PushOnScopeChains(Context.getObjCProtocolDecl(), TUScope);
1914dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  }
192c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
193651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Initialize Microsoft "predefined C++ types".
194651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (PP.getLangOpts().MSVCCompat && PP.getLangOpts().CPlusPlus) {
195651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (IdResolver.begin(&Context.Idents.get("type_info")) == IdResolver.end())
196651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      PushOnScopeChains(Context.buildImplicitRecord("type_info", TTK_Class),
197651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                        TUScope);
198651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
199651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    addImplicitTypedef("size_t", Context.getSizeType());
200651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
201651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
202651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Initialize predefined OpenCL types.
203651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (PP.getLangOpts().OpenCL) {
204651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    addImplicitTypedef("image1d_t", Context.OCLImage1dTy);
205651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    addImplicitTypedef("image1d_array_t", Context.OCLImage1dArrayTy);
206651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    addImplicitTypedef("image1d_buffer_t", Context.OCLImage1dBufferTy);
207651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    addImplicitTypedef("image2d_t", Context.OCLImage2dTy);
208651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    addImplicitTypedef("image2d_array_t", Context.OCLImage2dArrayTy);
209651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    addImplicitTypedef("image3d_t", Context.OCLImage3dTy);
210651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    addImplicitTypedef("sampler_t", Context.OCLSamplerTy);
211651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    addImplicitTypedef("event_t", Context.OCLEventTy);
212651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
213651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
214c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  DeclarationName BuiltinVaList = &Context.Idents.get("__builtin_va_list");
215c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  if (IdResolver.begin(BuiltinVaList) == IdResolver.end())
216c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    PushOnScopeChains(Context.getBuiltinVaListDecl(), TUScope);
2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovSema::~Sema() {
220651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::DeleteContainerSeconds(LateParsedTemplateMap);
22182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (PackContext) FreePackedContext();
222aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  if (VisContext) FreeVisContext();
223781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  // Kill all the active scopes.
224781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I)
225781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[I];
226781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1)
227781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[0];
2280116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
22946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the SemaConsumer to forget about us; we're going out of scope.
23046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
23146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->ForgetSema();
23246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
23346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Detach from the external Sema source.
23446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
235914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor        = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
23646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->ForgetSema();
2370ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann
2380ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  // If Sema's ExternalSource is the multiplexer - we own it.
2390ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  if (isMultiplexExternalSource)
2400ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    delete ExternalSource;
2410c018357b8bbb1f96bbf622a5807421e626b4228Alexey Bataev
2420c018357b8bbb1f96bbf622a5807421e626b4228Alexey Bataev  // Destroys data sharing attributes stack for OpenMP
2430c018357b8bbb1f96bbf622a5807421e626b4228Alexey Bataev  DestroyDataSharingAttributesStack();
24482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
24582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
246f85e193739c953358c865005855253af4f68a497John McCall/// makeUnavailableInSystemHeader - There is an error in the current
247f85e193739c953358c865005855253af4f68a497John McCall/// context.  If we're still in a system header, and we can plausibly
248f85e193739c953358c865005855253af4f68a497John McCall/// make the relevant declaration unavailable instead of erroring, do
249f85e193739c953358c865005855253af4f68a497John McCall/// so and return true.
250f85e193739c953358c865005855253af4f68a497John McCallbool Sema::makeUnavailableInSystemHeader(SourceLocation loc,
2515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                         StringRef msg) {
252f85e193739c953358c865005855253af4f68a497John McCall  // If we're not in a function, it's an error.
253f85e193739c953358c865005855253af4f68a497John McCall  FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext);
254f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) return false;
255f85e193739c953358c865005855253af4f68a497John McCall
256f85e193739c953358c865005855253af4f68a497John McCall  // If we're in template instantiation, it's an error.
257f85e193739c953358c865005855253af4f68a497John McCall  if (!ActiveTemplateInstantiations.empty())
258f85e193739c953358c865005855253af4f68a497John McCall    return false;
2590116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
260f85e193739c953358c865005855253af4f68a497John McCall  // If that function's not in a system header, it's an error.
261f85e193739c953358c865005855253af4f68a497John McCall  if (!Context.getSourceManager().isInSystemHeader(loc))
262f85e193739c953358c865005855253af4f68a497John McCall    return false;
263f85e193739c953358c865005855253af4f68a497John McCall
264f85e193739c953358c865005855253af4f68a497John McCall  // If the function is already unavailable, it's not an error.
265f85e193739c953358c865005855253af4f68a497John McCall  if (fn->hasAttr<UnavailableAttr>()) return true;
266f85e193739c953358c865005855253af4f68a497John McCall
267651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  fn->addAttr(UnavailableAttr::CreateImplicit(Context, msg, loc));
268f85e193739c953358c865005855253af4f68a497John McCall  return true;
269f85e193739c953358c865005855253af4f68a497John McCall}
270f85e193739c953358c865005855253af4f68a497John McCall
27158a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian RedlASTMutationListener *Sema::getASTMutationListener() const {
27258a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl  return getASTConsumer().GetASTMutationListener();
27358a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl}
27458a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl
2750ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann///\brief Registers an external source. If an external source already exists,
2760ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann/// creates a multiplex external source and appends to it.
2770ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann///
2780ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann///\param[in] E - A non-null external sema source.
2790ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann///
2800ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumannvoid Sema::addExternalSource(ExternalSemaSource *E) {
2810ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  assert(E && "Cannot use with NULL ptr");
2820ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann
2830ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  if (!ExternalSource) {
2840ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    ExternalSource = E;
2850ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    return;
2860ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  }
2870ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann
2880ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  if (isMultiplexExternalSource)
2890ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    static_cast<MultiplexExternalSemaSource*>(ExternalSource)->addSource(*E);
2900ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  else {
2910ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    ExternalSource = new MultiplexExternalSemaSource(*ExternalSource, *E);
2920ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    isMultiplexExternalSource = true;
2930ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  }
2940ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann}
2950ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann
2965d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth/// \brief Print out statistics about the semantic analysis.
2975d98994c7749312a43ce6adf45537979a98e7afdChandler Carruthvoid Sema::PrintStats() const {
2985d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  llvm::errs() << "\n*** Semantic Analysis Stats:\n";
2995d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n";
3005d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth
3015d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  BumpAlloc.PrintStats();
3025d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  AnalysisWarnings.PrintStats();
3035d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth}
3045d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth
305c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
306c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith/// If there is already an implicit cast, merge into the existing one.
307c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith/// The result is of the given category.
308c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard SmithExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
309c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                   CastKind Kind, ExprValueKind VK,
310c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                   const CXXCastPath *BasePath,
311c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                   CheckedConversionKind CCK) {
3129c129f818038e0269ba6b095722aa70176dc321dRichard Smith#ifndef NDEBUG
3139c129f818038e0269ba6b095722aa70176dc321dRichard Smith  if (VK == VK_RValue && !E->isRValue()) {
3149c129f818038e0269ba6b095722aa70176dc321dRichard Smith    switch (Kind) {
3159c129f818038e0269ba6b095722aa70176dc321dRichard Smith    default:
316ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      llvm_unreachable("can't implicitly cast lvalue to rvalue with this cast "
317ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                       "kind");
3189c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_LValueToRValue:
3199c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_ArrayToPointerDecay:
3209c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_FunctionToPointerDecay:
3219c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_ToVoid:
3229c129f818038e0269ba6b095722aa70176dc321dRichard Smith      break;
3239c129f818038e0269ba6b095722aa70176dc321dRichard Smith    }
3249c129f818038e0269ba6b095722aa70176dc321dRichard Smith  }
325acdfa4d504a8f2514d60569f9ce55d45f11795b9Richard Smith  assert((VK == VK_RValue || !E->isRValue()) && "can't cast rvalue to lvalue");
3269c129f818038e0269ba6b095722aa70176dc321dRichard Smith#endif
3279c129f818038e0269ba6b095722aa70176dc321dRichard Smith
328429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  QualType ExprTy = Context.getCanonicalType(E->getType());
3293a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType TypeTy = Context.getCanonicalType(Ty);
3301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3313a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  if (ExprTy == TypeTy)
332ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    return E;
3331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3346fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // If this is a derived-to-base cast to a through a virtual base, we
3356fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // need a vtable.
3360116a40116616701282e12f3d92cf2949102d76aMichael Gottesman  if (Kind == CK_DerivedToBase &&
337f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall      BasePathInvolvesVirtualBase(*BasePath)) {
338429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    QualType T = E->getType();
3396fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const PointerType *Pointer = T->getAs<PointerType>())
3406fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      T = Pointer->getPointeeType();
3416fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const RecordType *RecordTy = T->getAs<RecordType>())
3420116a40116616701282e12f3d92cf2949102d76aMichael Gottesman      MarkVTableUsed(E->getLocStart(),
3436fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor                     cast<CXXRecordDecl>(RecordTy->getDecl()));
3446fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  }
345c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith
346c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
347c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
348c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      ImpCast->setType(Ty);
349c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      ImpCast->setValueKind(VK);
350ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      return E;
351c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    }
3524c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson  }
3534c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson
354ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  return ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK);
355906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl}
356906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
357737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara/// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
358737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara/// to the conversion from scalar type ScalarTy to the Boolean type.
359737d5447b5d20633992ee5388eca5270c28c8ae7Abramo BagnaraCastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) {
360737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  switch (ScalarTy->getScalarTypeKind()) {
361737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Bool: return CK_NoOp;
3621d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_CPointer: return CK_PointerToBoolean;
3631d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_BlockPointer: return CK_PointerToBoolean;
3641d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_ObjCObjectPointer: return CK_PointerToBoolean;
365737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_MemberPointer: return CK_MemberPointerToBoolean;
366737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Integral: return CK_IntegralToBoolean;
367737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Floating: return CK_FloatingToBoolean;
368737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean;
369737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean;
370737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  }
371737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  return CK_Invalid;
372737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara}
373737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara
374bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis/// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
375bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidisstatic bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
376b9725cfb0a50731930a6331beb70f361b4d52a29Rafael Espindola  if (D->getMostRecentDecl()->isUsed())
377bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    return true;
378bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
379181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  if (D->isExternallyVisible())
3802d1b09641ecf2e754bf3fd244dc45dbf3e460c1bRafael Espindola    return true;
3812d1b09641ecf2e754bf3fd244dc45dbf3e460c1bRafael Espindola
382bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
383bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
384bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
385bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const FunctionDecl *DeclToCheck;
386bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (FD->hasBody(DeclToCheck))
387bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
388bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
389bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
390bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
391ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    DeclToCheck = FD->getMostRecentDecl();
392bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != FD)
393bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
394bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
395bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
396bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3971507bf55c958bdd4b14a3ee49f66af874f179cf2Eli Friedman    // If a variable usable in constant expressions is referenced,
3981507bf55c958bdd4b14a3ee49f66af874f179cf2Eli Friedman    // don't warn if it isn't used: if the value of a variable is required
3991507bf55c958bdd4b14a3ee49f66af874f179cf2Eli Friedman    // for the computation of a constant expression, it doesn't make sense to
4001507bf55c958bdd4b14a3ee49f66af874f179cf2Eli Friedman    // warn even if the variable isn't odr-used.  (isReferenced doesn't
4011507bf55c958bdd4b14a3ee49f66af874f179cf2Eli Friedman    // precisely reflect that, but it's a decent approximation.)
4021507bf55c958bdd4b14a3ee49f66af874f179cf2Eli Friedman    if (VD->isReferenced() &&
4031507bf55c958bdd4b14a3ee49f66af874f179cf2Eli Friedman        VD->isUsableInConstantExpressions(SemaRef->Context))
4041507bf55c958bdd4b14a3ee49f66af874f179cf2Eli Friedman      return true;
4051507bf55c958bdd4b14a3ee49f66af874f179cf2Eli Friedman
406bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
407bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
4080116a40116616701282e12f3d92cf2949102d76aMichael Gottesman    const VarDecl *DeclToCheck = VD->getDefinition();
409bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck)
410bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
411bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
412bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
413bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
414ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    DeclToCheck = VD->getMostRecentDecl();
415bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != VD)
416bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
417bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
418bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
419bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  return false;
420bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis}
421bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
422995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky/// Obtains a sorted list of functions that are undefined but ODR-used.
423cd0655b17249c4c4908ca91462657f62285017e6Nick Lewyckyvoid Sema::getUndefinedButUsed(
424995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined) {
425995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  for (llvm::DenseMap<NamedDecl *, SourceLocation>::iterator
426cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky         I = UndefinedButUsed.begin(), E = UndefinedButUsed.end();
427995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky       I != E; ++I) {
428995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    NamedDecl *ND = I->first;
42915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
43015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // Ignore attributes that have become invalid.
431995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    if (ND->isInvalidDecl()) continue;
43215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
43315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // __attribute__((weakref)) is basically a definition.
434995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    if (ND->hasAttr<WeakRefAttr>()) continue;
43515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
436995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
437995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky      if (FD->isDefined())
43815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        continue;
439181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola      if (FD->isExternallyVisible() &&
440cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky          !FD->getMostRecentDecl()->isInlined())
441cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky        continue;
44215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    } else {
443995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky      if (cast<VarDecl>(ND)->hasDefinition() != VarDecl::DeclarationOnly)
44415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        continue;
445181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola      if (ND->isExternallyVisible())
446cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky        continue;
44715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    }
44815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
449995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    Undefined.push_back(std::make_pair(ND, I->second));
450995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  }
451995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky
452cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky  // Sort (in order of use site) so that we're not dependent on the iteration
453cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky  // order through an llvm::DenseMap.
454651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SourceManager &SM = Context.getSourceManager();
455995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  std::sort(Undefined.begin(), Undefined.end(),
456651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines            [&SM](const std::pair<NamedDecl *, SourceLocation> &l,
457651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                  const std::pair<NamedDecl *, SourceLocation> &r) {
458651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (l.second.isValid() && !r.second.isValid())
459651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return true;
460651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (!l.second.isValid() && r.second.isValid())
461651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return false;
462651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (l.second != r.second)
463651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return SM.isBeforeInTranslationUnit(l.second, r.second);
464651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return SM.isBeforeInTranslationUnit(l.first->getLocation(),
465651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                        r.first->getLocation());
466651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  });
467995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky}
468995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky
469cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky/// checkUndefinedButUsed - Check for undefined objects with internal linkage
470cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky/// or that are inline.
471cd0655b17249c4c4908ca91462657f62285017e6Nick Lewyckystatic void checkUndefinedButUsed(Sema &S) {
472cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky  if (S.UndefinedButUsed.empty()) return;
473995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky
474995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  // Collect all the still-undefined entities with internal linkage.
475995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
476cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky  S.getUndefinedButUsed(Undefined);
477995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  if (Undefined.empty()) return;
478995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky
479995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
480995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky         I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
481995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    NamedDecl *ND = I->first;
482995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky
4836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (ND->hasAttr<DLLImportAttr>() || ND->hasAttr<DLLExportAttr>()) {
4846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // An exported function will always be emitted when defined, so even if
4856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // the function is inline, it doesn't have to be emitted in this TU. An
4866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      // imported function implies that it has been exported somewhere else.
4876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      continue;
4886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    }
4896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
490181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    if (!ND->isExternallyVisible()) {
491cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      S.Diag(ND->getLocation(), diag::warn_undefined_internal)
492cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky        << isa<VarDecl>(ND) << ND;
493cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky    } else {
494cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      assert(cast<FunctionDecl>(ND)->getMostRecentDecl()->isInlined() &&
495cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky             "used object requires definition but isn't inline or internal?");
496cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      S.Diag(ND->getLocation(), diag::warn_undefined_inline) << ND;
497cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky    }
498cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky    if (I->second.isValid())
499cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      S.Diag(I->second, diag::note_used_here);
50015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
50115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
50215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
50331e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregorvoid Sema::LoadExternalWeakUndeclaredIdentifiers() {
50431e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  if (!ExternalSource)
50531e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    return;
5060116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
50731e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs;
50831e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs);
50931e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  for (unsigned I = 0, N = WeakIDs.size(); I != N; ++I) {
51031e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator Pos
51131e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor      = WeakUndeclaredIdentifiers.find(WeakIDs[I].first);
51231e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    if (Pos != WeakUndeclaredIdentifiers.end())
51331e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor      continue;
5140116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
51531e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    WeakUndeclaredIdentifiers.insert(WeakIDs[I]);
51631e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  }
51731e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor}
51831e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor
519f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
520f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jaspertypedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap;
521f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
522f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// \brief Returns true, if all methods and nested classes of the given
523f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// CXXRecordDecl are defined in this translation unit.
524f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper///
525f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// Should only be called from ActOnEndOfTranslationUnit so that all
526f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// definitions are actually read.
527f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasperstatic bool MethodsAndNestedClassesComplete(const CXXRecordDecl *RD,
528f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                            RecordCompleteMap &MNCComplete) {
529f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  RecordCompleteMap::iterator Cache = MNCComplete.find(RD);
530f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  if (Cache != MNCComplete.end())
531f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    return Cache->second;
532f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  if (!RD->isCompleteDefinition())
533f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    return false;
534f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  bool Complete = true;
535f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  for (DeclContext::decl_iterator I = RD->decls_begin(),
536f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                  E = RD->decls_end();
537f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper       I != E && Complete; ++I) {
538f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    if (const CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(*I))
539f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      Complete = M->isDefined() || (M->isPure() && !isa<CXXDestructorDecl>(M));
5400e9e9f8d17e38b3c44f6e1323be3a812a793bdd1Daniel Jasper    else if (const FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(*I))
5410e9e9f8d17e38b3c44f6e1323be3a812a793bdd1Daniel Jasper      Complete = F->getTemplatedDecl()->isDefined();
542f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    else if (const CXXRecordDecl *R = dyn_cast<CXXRecordDecl>(*I)) {
543f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (R->isInjectedClassName())
544f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        continue;
545f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (R->hasDefinition())
546f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = MethodsAndNestedClassesComplete(R->getDefinition(),
547f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                                   MNCComplete);
548f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      else
549f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = false;
550f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    }
551f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  }
552f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  MNCComplete[RD] = Complete;
553f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  return Complete;
554f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper}
555f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
556f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// \brief Returns true, if the given CXXRecordDecl is fully defined in this
557f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// translation unit, i.e. all methods are defined or pure virtual and all
558f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// friends, friend functions and nested classes are fully defined in this
559f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// translation unit.
560f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper///
561f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// Should only be called from ActOnEndOfTranslationUnit so that all
562f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// definitions are actually read.
563f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasperstatic bool IsRecordFullyDefined(const CXXRecordDecl *RD,
564f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                 RecordCompleteMap &RecordsComplete,
565f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                 RecordCompleteMap &MNCComplete) {
566f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  RecordCompleteMap::iterator Cache = RecordsComplete.find(RD);
567f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  if (Cache != RecordsComplete.end())
568f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    return Cache->second;
569f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  bool Complete = MethodsAndNestedClassesComplete(RD, MNCComplete);
570f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  for (CXXRecordDecl::friend_iterator I = RD->friend_begin(),
571f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                      E = RD->friend_end();
572f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper       I != E && Complete; ++I) {
573f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    // Check if friend classes and methods are complete.
574f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    if (TypeSourceInfo *TSI = (*I)->getFriendType()) {
575f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      // Friend classes are available as the TypeSourceInfo of the FriendDecl.
576f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (CXXRecordDecl *FriendD = TSI->getType()->getAsCXXRecordDecl())
577f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = MethodsAndNestedClassesComplete(FriendD, MNCComplete);
578f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      else
579f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = false;
580f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    } else {
581f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      // Friend functions are available through the NamedDecl of FriendDecl.
582f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (const FunctionDecl *FD =
583f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper          dyn_cast<FunctionDecl>((*I)->getFriendDecl()))
584f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = FD->isDefined();
585f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      else
586f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        // This is a template friend, give up.
587f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = false;
588f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    }
589f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  }
590f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  RecordsComplete[RD] = Complete;
591f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  return Complete;
592f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper}
593f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
5949299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// ActOnEndOfTranslationUnit - This is called at the very end of the
5959299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// translation unit when EOF is reached and all but the top-level scope is
5969299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// popped.
5970e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidisvoid Sema::ActOnEndOfTranslationUnit() {
5986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  assert(DelayedDiagnostics.getCurrentPool() == nullptr
5999257664568bf375b7790131a84d9a4fa30a5b7e3John McCall         && "reached end of translation unit with a pool attached?");
6009257664568bf375b7790131a84d9a4fa30a5b7e3John McCall
601d82f5eb1f98825ad888fb0ab45e5d8ef2e0ec6a2Douglas Gregor  // If code completion is enabled, don't perform any end-of-translation-unit
602d82f5eb1f98825ad888fb0ab45e5d8ef2e0ec6a2Douglas Gregor  // work.
603d82f5eb1f98825ad888fb0ab45e5d8ef2e0ec6a2Douglas Gregor  if (PP.isCodeCompletionEnabled())
604d82f5eb1f98825ad888fb0ab45e5d8ef2e0ec6a2Douglas Gregor    return;
605d82f5eb1f98825ad888fb0ab45e5d8ef2e0ec6a2Douglas Gregor
606b775100fea6d8955149897dae1adca50ca471d17Richard Smith  // Complete translation units and modules define vtables and perform implicit
607b775100fea6d8955149897dae1adca50ca471d17Richard Smith  // instantiations. PCH files do not.
608b775100fea6d8955149897dae1adca50ca471d17Richard Smith  if (TUKind != TU_Prefix) {
609849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    DiagnoseUseOfUnimplementedSelectors();
610849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
611aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // If any dynamic classes have their key function defined within
612aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // this translation unit, then those vtables are considered "used" and must
613aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // be emitted.
614a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor    for (DynamicClassesType::iterator I = DynamicClasses.begin(ExternalSource),
615a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor                                      E = DynamicClasses.end();
616a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor         I != E; ++I) {
617a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor      assert(!(*I)->isDependentType() &&
618a5c6c2a84cde5c9b8f8ec0610a9f89ffd54f44eeAnders Carlsson             "Should not see dependent types here!");
6196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (const CXXMethodDecl *KeyFunction =
6206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              Context.getCurrentKeyFunction(*I)) {
6216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        const FunctionDecl *Definition = nullptr;
622aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth        if (KeyFunction->hasBody(Definition))
623a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor          MarkVTableUsed(Definition->getLocation(), *I, true);
624aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth      }
625aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    }
626aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth
6278155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // If DefinedUsedVTables ends up marking any virtual member functions it
6288155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // might lead to more pending template instantiations, which we then need
6298155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // to instantiate.
6308155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
6318155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
6328155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // C++: Perform implicit template instantiations.
6338155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    //
6348155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // FIXME: When we perform these implicit instantiations, we do not
6358155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // carefully keep track of the point of instantiation (C++ [temp.point]).
6368155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // This means that name lookup that occurs within the template
6378155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // instantiation will always happen at the end of the translation unit,
638b775100fea6d8955149897dae1adca50ca471d17Richard Smith    // so it will find some names that are not required to be found. This is
639b775100fea6d8955149897dae1adca50ca471d17Richard Smith    // valid, but we could do better by diagnosing if an instantiation uses a
640b775100fea6d8955149897dae1adca50ca471d17Richard Smith    // name that was not visible at its first point of instantiation.
641651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (ExternalSource) {
642651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // Load pending instantiations from the external source.
643651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      SmallVector<PendingImplicitInstantiation, 4> Pending;
644651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ExternalSource->ReadPendingInstantiations(Pending);
645651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      PendingInstantiations.insert(PendingInstantiations.begin(),
646651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                   Pending.begin(), Pending.end());
647651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
6488155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    PerformPendingInstantiations();
649651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
65008235661cf457978ba4645ec8e22697aebabe4faAlp Toker    CheckDelayedMemberExceptionSpecs();
6512a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
6520116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
65308235661cf457978ba4645ec8e22697aebabe4faAlp Toker  // All delayed member exception specs should be checked or we end up accepting
65408235661cf457978ba4645ec8e22697aebabe4faAlp Toker  // incompatible declarations.
65508235661cf457978ba4645ec8e22697aebabe4faAlp Toker  assert(DelayedDefaultedMemberExceptionSpecs.empty());
65608235661cf457978ba4645ec8e22697aebabe4faAlp Toker  assert(DelayedDestructorExceptionSpecChecks.empty());
65708235661cf457978ba4645ec8e22697aebabe4faAlp Toker
65849b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Remove file scoped decls that turned out to be used.
659676ea9dce44ed2d1dc3d7f431824045764d8a3e9Daniel Jasper  UnusedFileScopedDecls.erase(
6606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      std::remove_if(UnusedFileScopedDecls.begin(nullptr, true),
661676ea9dce44ed2d1dc3d7f431824045764d8a3e9Daniel Jasper                     UnusedFileScopedDecls.end(),
662676ea9dce44ed2d1dc3d7f431824045764d8a3e9Daniel Jasper                     std::bind1st(std::ptr_fun(ShouldRemoveFromUnused), this)),
663676ea9dce44ed2d1dc3d7f431824045764d8a3e9Daniel Jasper      UnusedFileScopedDecls.end());
66447268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor
665467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  if (TUKind == TU_Prefix) {
666467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    // Translation unit prefixes don't need any of the checking below.
6676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    TUScope = nullptr;
66872b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    return;
66987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
67072b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis
67163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // Check for #pragma weak identifiers that were never declared
67263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // FIXME: This will cause diagnostics to be emitted in a non-determinstic
67363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // order!  Iterating over a densemap like this is bad.
67431e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  LoadExternalWeakUndeclaredIdentifiers();
675e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
67663d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       I = WeakUndeclaredIdentifiers.begin(),
67763d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
67863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    if (I->second.getUsed()) continue;
6791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
68063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
68163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      << I->first;
682e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  }
683e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn
684975d353997a13f7dfdaf4ec7a547fe1adb15f35eArgyrios Kyrtzidis  if (LangOpts.CPlusPlus11 &&
685ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      !Diags.isIgnored(diag::warn_delegating_ctor_cycle, SourceLocation()))
686975d353997a13f7dfdaf4ec7a547fe1adb15f35eArgyrios Kyrtzidis    CheckDelegatingCtorCycles();
687975d353997a13f7dfdaf4ec7a547fe1adb15f35eArgyrios Kyrtzidis
688467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  if (TUKind == TU_Module) {
68990db26000aefe9335370013eec64c85232d80227Douglas Gregor    // If we are building a module, resolve all of the exported declarations
69090db26000aefe9335370013eec64c85232d80227Douglas Gregor    // now.
69190db26000aefe9335370013eec64c85232d80227Douglas Gregor    if (Module *CurrentModule = PP.getCurrentModule()) {
69290db26000aefe9335370013eec64c85232d80227Douglas Gregor      ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6930116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
694cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko      SmallVector<Module *, 2> Stack;
69590db26000aefe9335370013eec64c85232d80227Douglas Gregor      Stack.push_back(CurrentModule);
69690db26000aefe9335370013eec64c85232d80227Douglas Gregor      while (!Stack.empty()) {
697344472ebeded2fca2ed5013b9e87f81d09bfa908Robert Wilhelm        Module *Mod = Stack.pop_back_val();
6980116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
699906d66acc5cf2679453e10a4f0a67feedd765b21Douglas Gregor        // Resolve the exported declarations and conflicts.
70090db26000aefe9335370013eec64c85232d80227Douglas Gregor        // FIXME: Actually complain, once we figure out how to teach the
701906d66acc5cf2679453e10a4f0a67feedd765b21Douglas Gregor        // diagnostic client to deal with complaints in the module map at this
70290db26000aefe9335370013eec64c85232d80227Douglas Gregor        // point.
70390db26000aefe9335370013eec64c85232d80227Douglas Gregor        ModMap.resolveExports(Mod, /*Complain=*/false);
704ddd2dfc1d3f4a36cbe8cd775c588623a17049f9fDaniel Jasper        ModMap.resolveUses(Mod, /*Complain=*/false);
705906d66acc5cf2679453e10a4f0a67feedd765b21Douglas Gregor        ModMap.resolveConflicts(Mod, /*Complain=*/false);
7060116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
70790db26000aefe9335370013eec64c85232d80227Douglas Gregor        // Queue the submodules, so their exports will also be resolved.
708b7a7819473709c01ea024a2dc15e99d38f0f8760Douglas Gregor        for (Module::submodule_iterator Sub = Mod->submodule_begin(),
709b7a7819473709c01ea024a2dc15e99d38f0f8760Douglas Gregor                                     SubEnd = Mod->submodule_end();
71090db26000aefe9335370013eec64c85232d80227Douglas Gregor             Sub != SubEnd; ++Sub) {
711b7a7819473709c01ea024a2dc15e99d38f0f8760Douglas Gregor          Stack.push_back(*Sub);
71290db26000aefe9335370013eec64c85232d80227Douglas Gregor        }
71390db26000aefe9335370013eec64c85232d80227Douglas Gregor      }
71490db26000aefe9335370013eec64c85232d80227Douglas Gregor    }
7150116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
716467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    // Modules don't need any of the checking below.
7176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    TUScope = nullptr;
718467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    return;
719467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  }
7200116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
721275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  // C99 6.9.2p2:
722275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   A declaration of an identifier for an object that has file
723275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   scope without an initializer, and without a storage-class
724275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   specifier or with the storage-class specifier static,
725275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   constitutes a tentative definition. If a translation unit
726275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   contains one or more tentative definitions for an identifier,
727275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   and the translation unit contains no external definition for
728275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   that identifier, then the behavior is exactly as if the
729275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit contains a file scope declaration of that
730275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   identifier, with the composite type as of the end of the
731275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit, with an initializer equal to 0.
732e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  llvm::SmallSet<VarDecl *, 32> Seen;
7330116a40116616701282e12f3d92cf2949102d76aMichael Gottesman  for (TentativeDefinitionsType::iterator
734a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor            T = TentativeDefinitions.begin(ExternalSource),
735a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor         TEnd = TentativeDefinitions.end();
7360116a40116616701282e12f3d92cf2949102d76aMichael Gottesman       T != TEnd; ++T)
737a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor  {
738a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor    VarDecl *VD = (*T)->getActingDefinition();
739e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
740e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // If the tentative definition was completed, getActingDefinition() returns
741e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // null. If we've already seen this variable before, insert()'s second
742e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // return value is false.
7436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!VD || VD->isInvalidDecl() || !Seen.insert(VD))
744b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      continue;
745b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
7461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const IncompleteArrayType *ArrayT
747b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        = Context.getAsIncompleteArrayType(VD->getType())) {
74863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      // Set the length of the array to 1 (C99 6.9.2p5).
74963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
75063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
75146a617a792bfab0d9b1e057371ea3b9540802226John McCall      QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
75246a617a792bfab0d9b1e057371ea3b9540802226John McCall                                                One, ArrayType::Normal, 0);
75363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      VD->setType(T);
7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
755b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                                   diag::err_tentative_def_incomplete_type))
756b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      VD->setInvalidDecl();
757b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
758e4851f26eb7be1f71f919bb5890da7e3583f727dEli Friedman    CheckCompleteVariableDeclaration(VD);
759e4851f26eb7be1f71f919bb5890da7e3583f727dEli Friedman
760b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    // Notify the consumer that we've completed a tentative definition.
761b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    if (!VD->isInvalidDecl())
762b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      Consumer.CompleteTentativeDefinition(VD);
763b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
764275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  }
76543f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis
76643f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  // If there were errors, disable 'unused' warnings since they will mostly be
76743f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  // noise.
76843f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  if (!Diags.hasErrorOccurred()) {
76943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis    // Output warning for unused file scoped decls.
770a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor    for (UnusedFileScopedDeclsType::iterator
771a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor           I = UnusedFileScopedDecls.begin(ExternalSource),
77243f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis           E = UnusedFileScopedDecls.end(); I != E; ++I) {
773a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor      if (ShouldRemoveFromUnused(this, *I))
774a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor        continue;
7750116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
77643f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
77743f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        const FunctionDecl *DiagD;
77843f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        if (!FD->hasBody(DiagD))
77943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis          DiagD = FD;
78048b89590f61575cbf365ba996a2bd1ba1561a4abArgyrios Kyrtzidis        if (DiagD->isDeleted())
78148b89590f61575cbf365ba996a2bd1ba1561a4abArgyrios Kyrtzidis          continue; // Deleted functions are supposed to be unused.
7826b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        if (DiagD->isReferenced()) {
7836b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          if (isa<CXXMethodDecl>(DiagD))
7846b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis            Diag(DiagD->getLocation(), diag::warn_unneeded_member_function)
7856b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                  << DiagD->getDeclName();
78612d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian          else {
787d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola            if (FD->getStorageClass() == SC_Static &&
78812d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian                !FD->isInlineSpecified() &&
78924146975f1af8c1b4b14e8545f218129d0e7dfebEli Friedman                !SourceMgr.isInMainFile(
79012d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian                   SourceMgr.getExpansionLoc(FD->getLocation())))
79112d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian              Diag(DiagD->getLocation(), diag::warn_unneeded_static_internal_decl)
79212d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian                << DiagD->getDeclName();
79312d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian            else
79412d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian              Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
79512d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian                   << /*function*/0 << DiagD->getDeclName();
79612d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian          }
7976b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        } else {
7986b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(),
7996b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis               isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function
8006b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                                         : diag::warn_unused_function)
8016b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << DiagD->getDeclName();
8026b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        }
80343f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      } else {
80443f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
80543f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        if (!DiagD)
80643f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis          DiagD = cast<VarDecl>(*I);
8076b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        if (DiagD->isReferenced()) {
8086b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
8096b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << /*variable*/1 << DiagD->getDeclName();
81071fcba874e399eb33e0ebf7c6b19bc2a5e876993Daniel Jasper        } else if (DiagD->getType().isConstQualified()) {
81171fcba874e399eb33e0ebf7c6b19bc2a5e876993Daniel Jasper          Diag(DiagD->getLocation(), diag::warn_unused_const_variable)
81271fcba874e399eb33e0ebf7c6b19bc2a5e876993Daniel Jasper              << DiagD->getDeclName();
81339bd371610af27b073c792c54c6c28133329f6adEli Friedman        } else {
8146b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(), diag::warn_unused_variable)
81519b6a707a86302adc80d64464cbc3cb8a7a7f3a6Matt Beaumont-Gay              << DiagD->getDeclName();
8166b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        }
81743f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      }
818bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    }
81915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
82001a41140cd8ec9475ed0c33384310fbdd3b6de11Nick Lewycky    if (ExternalSource)
821cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      ExternalSource->ReadUndefinedButUsed(UndefinedButUsed);
822cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky    checkUndefinedButUsed(*this);
82349b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  }
82487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
825ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  if (!Diags.isIgnored(diag::warn_unused_private_field, SourceLocation())) {
826f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    RecordCompleteMap RecordsComplete;
827f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    RecordCompleteMap MNCComplete;
828f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    for (NamedDeclSetType::iterator I = UnusedPrivateFields.begin(),
829f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper         E = UnusedPrivateFields.end(); I != E; ++I) {
830f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      const NamedDecl *D = *I;
831f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
832f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (RD && !RD->isUnion() &&
833f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper          IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) {
834f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Diag(D->getLocation(), diag::warn_unused_private_field)
835f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper              << D->getDeclName();
836f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      }
837f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    }
838f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  }
839f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
840483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // Check we've noticed that we're no longer parsing the initializer for every
841483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // variable. If we miss cases, then at best we have a performance issue and
842483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // at worst a rejects-valid bug.
843483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  assert(ParsingInitForAutoVars.empty() &&
844483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith         "Didn't unmark var as having its initializer parsed");
845483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
8466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  TUScope = nullptr;
8479299f3fa85796613cc787a2062c9562d07c8613eChris Lattner}
8489299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
8499299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
8505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
8515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper functions.
8525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
8535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8548517d9b731f065cdfc55ec0f3ddf5d564d988648Anders CarlssonDeclContext *Sema::getFunctionLevelDeclContext() {
855db0ee1da16e9dbec19b144c9cd96ee9f55fe0c53John McCall  DeclContext *DC = CurContext;
8561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85772899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  while (true) {
8586afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj    if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC) || isa<CapturedDecl>(DC)) {
85972899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman      DC = DC->getParent();
86072899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    } else if (isa<CXXMethodDecl>(DC) &&
861215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor               cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call &&
86272899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman               cast<CXXRecordDecl>(DC->getParent())->isLambda()) {
86372899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman      DC = DC->getParent()->getParent();
86472899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    }
86572899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    else break;
86672899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  }
8671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8688517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  return DC;
8698517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson}
8708517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson
871371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// getCurFunctionDecl - If inside of a function body, this returns a pointer
872371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// to the function decl for the function being parsed.  If we're currently
873371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// in a 'block', this returns the containing context.
874371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerFunctionDecl *Sema::getCurFunctionDecl() {
8758517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
876371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return dyn_cast<FunctionDecl>(DC);
877371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
878371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
879c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel DunbarObjCMethodDecl *Sema::getCurMethodDecl() {
8808517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
88169f86d92ae940c5c8d979951d9c55fe9111627a0Fariborz Jahanian  while (isa<RecordDecl>(DC))
88269f86d92ae940c5c8d979951d9c55fe9111627a0Fariborz Jahanian    DC = DC->getParent();
883d7612e183bb09d04677d0ddde89ee29130ffb715Steve Naroff  return dyn_cast<ObjCMethodDecl>(DC);
884c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar}
885371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
886371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerNamedDecl *Sema::getCurFunctionOrMethodDecl() {
8878517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
888371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
8894afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return cast<NamedDecl>(DC);
8906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
891371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
892371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
893393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbarvoid Sema::EmitCurrentDiagnostic(unsigned DiagID) {
894393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // FIXME: It doesn't make sense to me that DiagID is an incoming argument here
895393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // and yet we also use the current diag ID on the DiagnosticsEngine. This has
896393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // been made more painfully obvious by the refactor that introduced this
897393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // function, but it is possible that the incoming argument can be
898393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // eliminnated. If it truly cannot be (for example, there is some reentrancy
899393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // issue I am not seeing yet), then there should at least be a clarifying
900393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // comment somewhere.
901dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie  if (Optional<TemplateDeductionInfo*> Info = isSFINAEContext()) {
902393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar    switch (DiagnosticIDs::getDiagnosticSFINAEResponse(
903393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar              Diags.getCurrentDiagID())) {
90433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Report:
90577faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // We'll report the diagnostic below.
9069b623639378d53a675921ddfa7316034d571881eDouglas Gregor      break;
9070116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
90877faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    case DiagnosticIDs::SFINAE_SubstitutionFailure:
90977faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Count this failure so that we know that template argument deduction
91077faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // has failed.
911393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      ++NumSFINAEErrors;
912b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
913b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // Make a copy of this suppressed diagnostic and store it with the
914b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // template-deduction information.
915b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
916b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        Diagnostic DiagInfo(&Diags);
917b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
918b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
919b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      }
920b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
921393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
922393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.Clear();
92377faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      return;
9240116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
92577faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    case DiagnosticIDs::SFINAE_AccessControl: {
9267822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // Per C++ Core Issue 1170, access control is part of SFINAE.
92700b43848e4a320b0845c1004694fa50920096c75Daniel Dunbar      // Additionally, the AccessCheckingSFINAE flag can be used to temporarily
9287822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // make access control a part of SFINAE for the purposes of checking
9297822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // type traits.
93080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      if (!AccessCheckingSFINAE && !getLangOpts().CPlusPlus11)
9311eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor        break;
93277faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
933393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      SourceLocation Loc = Diags.getCurrentDiagLoc();
93477faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
93577faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Suppress this diagnostic.
936393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      ++NumSFINAEErrors;
937b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
938b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // Make a copy of this suppressed diagnostic and store it with the
939b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // template-deduction information.
940b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
941b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        Diagnostic DiagInfo(&Diags);
942b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
943b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
944b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      }
945b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
946393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
947393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.Clear();
94877faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
94977faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Now the diagnostic state is clear, produce a C++98 compatibility
95077faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // warning.
951393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diag(Loc, diag::warn_cxx98_compat_sfinae_access_control);
95277faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
95377faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // The last diagnostic which Sema produced was ignored. Suppress any
95477faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // notes attached to it.
955393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
9569b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
95777faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    }
95877faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
95933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Suppress:
9609b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Make a copy of this suppressed diagnostic and store it with the
9619b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // template-deduction information;
962b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      if (*Info) {
963b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        Diagnostic DiagInfo(&Diags);
9641eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor        (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(),
965b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
966b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      }
967b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
968b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // Suppress this diagnostic.
969393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
970393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.Clear();
9719b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
9729b623639378d53a675921ddfa7316034d571881eDouglas Gregor    }
9739b623639378d53a675921ddfa7316034d571881eDouglas Gregor  }
9740116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
9758987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  // Set up the context's printing policy based on our current state.
976393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  Context.setPrintingPolicy(getPrintingPolicy());
9770116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
9789b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // Emit the diagnostic.
979393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  if (!Diags.EmitCurrentDiagnostic())
9805e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    return;
9811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
98225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // If this is not a note, and we're in a template instantiation
98325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // that is different from the last template instantiation where
98425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // we emitted an error, print a template instantiation
98525a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // backtrace.
98633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  if (!DiagnosticIDs::isBuiltinNote(DiagID) &&
987393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      !ActiveTemplateInstantiations.empty() &&
988393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      ActiveTemplateInstantiations.back()
989393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar        != LastTemplateInstantiationErrorContext) {
990393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar    PrintInstantiationStack();
991393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar    LastTemplateInstantiationErrorContext = ActiveTemplateInstantiations.back();
99225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  }
99325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor}
9942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
99591a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::SemaDiagnosticBuilder
99691a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
99791a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
99891a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  PD.Emit(Builder);
9991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
100091a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  return Builder;
100191a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson}
100291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson
1003108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// \brief Looks through the macro-expansion chain for the given
1004108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// location, looking for a macro expansion with the given name.
1005834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// If one is found, returns true and sets the location to that
1006108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// expansion loc.
10075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerbool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) {
1008834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  SourceLocation loc = locref;
1009834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (!loc.isMacroID()) return false;
1010834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
1011834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // There's no good way right now to look at the intermediate
1012108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  // expansions, so just jump to the expansion location.
1013402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  loc = getSourceManager().getExpansionLoc(loc);
1014834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
1015834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // If that's written with the name, stop here.
10165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<char, 16> buffer;
1017834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (getPreprocessor().getSpelling(loc, buffer) == name) {
1018834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    locref = loc;
1019834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    return true;
1020834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  }
1021834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  return false;
1022834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall}
1023834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
102423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \brief Determines the active Scope associated with the given declaration
102523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// context.
102623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
102723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// This routine maps a declaration context to the active Scope object that
102823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// represents that declaration context in the parser. It is typically used
102923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// from "scope-less" code (e.g., template instantiation, lazy creation of
103023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// declarations) that injects a name for name-lookup purposes and, therefore,
103123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// must update the Scope.
103223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
103323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \returns The scope corresponding to the given declaraion context, or NULL
103423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// if no such scope is open.
103523c94dbb6631fecdb55ba401aa93722803d980c6Douglas GregorScope *Sema::getScopeForContext(DeclContext *Ctx) {
10360116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
103723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!Ctx)
10386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
10390116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
104023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Ctx = Ctx->getPrimaryContext();
104123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
1042cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // Ignore scopes that cannot have declarations. This is important for
1043cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // out-of-line definitions of static class members.
1044cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
1045f0d5861d2db5e3075bd722ff7874e88c4bfedaaeTed Kremenek      if (DeclContext *Entity = S->getEntity())
1046cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl        if (Ctx == Entity->getPrimaryContext())
1047cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl          return S;
104823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  }
10490116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
10506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
105123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor}
10529ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
10539ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Enter a new function scope
10549ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushFunctionScope() {
1055781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1) {
1056781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // Use the "top" function scope rather than having to allocate
1057781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // memory for a new scope.
10588fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis    FunctionScopes.back()->Clear();
1059781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    FunctionScopes.push_back(FunctionScopes.back());
10609ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return;
10619ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  }
10620116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
10638fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics()));
10649ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
10659ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
10669ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
10678fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(),
10689ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor                                              BlockScope, Block));
10699ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
10709ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
1071126bd8f29558535f97511603769f9c81288c0c57Faisal ValiLambdaScopeInfo *Sema::PushLambdaScope() {
1072d78d6591fa2556b037d3571fde3631d0243c08d7Faisal Vali  LambdaScopeInfo *const LSI = new LambdaScopeInfo(getDiagnostics());
1073d78d6591fa2556b037d3571fde3631d0243c08d7Faisal Vali  FunctionScopes.push_back(LSI);
1074d78d6591fa2556b037d3571fde3631d0243c08d7Faisal Vali  return LSI;
1075fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali}
1076fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
1077fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Valivoid Sema::RecordParsingTemplateParameterDepth(unsigned Depth) {
1078fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali  if (LambdaScopeInfo *const LSI = getCurLambda()) {
1079fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    LSI->AutoTemplateParameterDepth = Depth;
1080fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    return;
1081fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali  }
1082fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali  llvm_unreachable(
1083fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali      "Remove assertion if intentionally called in a non-lambda context.");
1084ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman}
1085ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman
1086ec9ea7200718478e8a976529defbe21942a11c9cEli Friedmanvoid Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP,
1087ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman                                const Decl *D, const BlockExpr *blkExpr) {
10880116a40116616701282e12f3d92cf2949102d76aMichael Gottesman  FunctionScopeInfo *Scope = FunctionScopes.pop_back_val();
1089781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  assert(!FunctionScopes.empty() && "mismatched push/pop!");
10900116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
10913ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  // Issue any analysis-based warnings.
10923ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  if (WP && D)
1093283a358aecb75e30fcd486f2206f6c03c5e7f11dTed Kremenek    AnalysisWarnings.IssueWarnings(*WP, Scope, D, blkExpr);
10946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  else
10956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    for (const auto &PUD : Scope->PossiblyUnreachableDiags)
10966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Diag(PUD.Loc, PUD.PD);
10973ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek
10986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (FunctionScopes.back() != Scope)
1099781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete Scope;
11009ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
11019ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
1102625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::PushCompoundScope() {
1103625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  getCurFunction()->CompoundScopes.push_back(CompoundScopeInfo());
1104625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko}
1105625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
1106625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::PopCompoundScope() {
1107625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  FunctionScopeInfo *CurFunction = getCurFunction();
1108625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  assert(!CurFunction->CompoundScopes.empty() && "mismatched push/pop");
1109625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
1110625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  CurFunction->CompoundScopes.pop_back();
1111625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko}
1112625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
11139ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Determine whether any errors occurred within this function/method/
11149ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// block.
1115f85e193739c953358c865005855253af4f68a497John McCallbool Sema::hasAnyUnrecoverableErrorsInThisFunction() const {
1116f85e193739c953358c865005855253af4f68a497John McCall  return getCurFunction()->ErrorTrap.hasUnrecoverableErrorOccurred();
11179ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
11189ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
11199ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo *Sema::getCurBlock() {
11209ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.empty())
11216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
11226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
11236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  auto CurBSI = dyn_cast<BlockScopeInfo>(FunctionScopes.back());
11246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (CurBSI && CurBSI->TheDecl &&
11256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      !CurBSI->TheDecl->Encloses(CurContext)) {
11266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // We have switched contexts due to template instantiation.
11276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    assert(!ActiveTemplateInstantiations.empty());
11286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
11296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
11300116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
11316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return CurBSI;
11329ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
113376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
1134906a7e1c0f272f7e539c82dda01f4644031ce637Eli FriedmanLambdaScopeInfo *Sema::getCurLambda() {
1135906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  if (FunctionScopes.empty())
11366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
11376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
11386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  auto CurLSI = dyn_cast<LambdaScopeInfo>(FunctionScopes.back());
11396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (CurLSI && CurLSI->Lambda &&
11406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      !CurLSI->Lambda->Encloses(CurContext)) {
11416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // We have switched contexts due to template instantiation.
11426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    assert(!ActiveTemplateInstantiations.empty());
11436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
11446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
11450116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
11466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return CurLSI;
1147906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman}
1148fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali// We have a generic lambda if we parsed auto parameters, or we have
1149fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali// an associated template parameter list.
1150fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal ValiLambdaScopeInfo *Sema::getCurGenericLambda() {
1151fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali  if (LambdaScopeInfo *LSI =  getCurLambda()) {
1152fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali    return (LSI->AutoTemplateParams.size() ||
11536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                    LSI->GLTemplateParameterList) ? LSI : nullptr;
1154fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali  }
11556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
1156fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali}
1157fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Vali
1158906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman
1159aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkovoid Sema::ActOnComment(SourceRange Comment) {
1160127ff2ea6440c3da4b47f9c8b3b190254a97e7b5Ted Kremenek  if (!LangOpts.RetainCommentsFromSystemHeaders &&
1161127ff2ea6440c3da4b47f9c8b3b190254a97e7b5Ted Kremenek      SourceMgr.isInSystemHeader(Comment.getBegin()))
1162127ff2ea6440c3da4b47f9c8b3b190254a97e7b5Ted Kremenek    return;
11636fd7d3067dd06584ef3940e88e31fea1a0e83588Dmitri Gribenko  RawComment RC(SourceMgr, Comment, false,
11646fd7d3067dd06584ef3940e88e31fea1a0e83588Dmitri Gribenko                LangOpts.CommentOpts.ParseAllComments);
11659dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko  if (RC.isAlmostTrailingComment()) {
11669dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    SourceRange MagicMarkerRange(Comment.getBegin(),
11679dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko                                 Comment.getBegin().getLocWithOffset(3));
11689dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    StringRef MagicMarkerText;
11699dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    switch (RC.getKind()) {
1170c50a0e3900f1b44503be48457508af372f4dd05aAbramo Bagnara    case RawComment::RCK_OrdinaryBCPL:
11719dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      MagicMarkerText = "///<";
11729dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      break;
1173c50a0e3900f1b44503be48457508af372f4dd05aAbramo Bagnara    case RawComment::RCK_OrdinaryC:
11749dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      MagicMarkerText = "/**<";
11759dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      break;
11769dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    default:
11779dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      llvm_unreachable("if this is an almost Doxygen comment, "
11789dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko                       "it should be ordinary");
11799dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    }
11809dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    Diag(Comment.getBegin(), diag::warn_not_a_doxygen_trailing_member_comment) <<
11819dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      FixItHint::CreateReplacement(MagicMarkerRange, MagicMarkerText);
11829dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko  }
1183aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  Context.addComment(RC);
1184aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}
1185aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
118676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall// Pin this vtable to this file.
118776bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalSemaSource::~ExternalSemaSource() {}
1188f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
11895ac4b6917aa34fae6da64036539023a6155a3d48Douglas Gregorvoid ExternalSemaSource::ReadMethodPool(Selector Sel) { }
11908c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl
1191d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregorvoid ExternalSemaSource::ReadKnownNamespaces(
11920116a40116616701282e12f3d92cf2949102d76aMichael Gottesman                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
1193d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor}
1194d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor
1195cd0655b17249c4c4908ca91462657f62285017e6Nick Lewyckyvoid ExternalSemaSource::ReadUndefinedButUsed(
1196995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky                       llvm::DenseMap<NamedDecl *, SourceLocation> &Undefined) {
119701a41140cd8ec9475ed0c33384310fbdd3b6de11Nick Lewycky}
119801a41140cd8ec9475ed0c33384310fbdd3b6de11Nick Lewycky
11995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid PrettyDeclStackTraceEntry::print(raw_ostream &OS) const {
1200f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  SourceLocation Loc = this->Loc;
1201f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
1202f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (Loc.isValid()) {
1203f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Loc.print(OS, S.getSourceManager());
1204f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    OS << ": ";
1205f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
1206f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << Message;
1207f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
1208f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (TheDecl && isa<NamedDecl>(TheDecl)) {
1209f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString();
1210f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    if (!Name.empty())
1211f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      OS << " '" << Name << '\'';
1212f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
1213f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
1214f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << '\n';
1215f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall}
1216c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1217c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \brief Figure out if an expression could be turned into a call.
1218c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1219c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// Use this when trying to recover from an error where the programmer may have
1220c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// written just the name of a function instead of actually calling it.
1221c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1222c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param E - The expression to examine.
1223c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param ZeroArgCallReturnTy - If the expression can be turned into a call
1224c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  with no arguments, this parameter is set to the type returned by such a
1225c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  call; otherwise, it is set to an empty QualType.
12266dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// \param OverloadSet - If the expression is an overloaded function
1227c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  name, this parameter is populated with the decls of the various overloads.
1228c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikiebool Sema::tryExprAsCall(Expr &E, QualType &ZeroArgCallReturnTy,
1229c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie                         UnresolvedSetImpl &OverloadSet) {
1230c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  ZeroArgCallReturnTy = QualType();
12316dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  OverloadSet.clear();
12326dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
12336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const OverloadExpr *Overloads = nullptr;
1234c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  bool IsMemExpr = false;
12356dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (E.getType() == Context.OverloadTy) {
12366dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    OverloadExpr::FindResult FR = OverloadExpr::find(const_cast<Expr*>(&E));
12376dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1238c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie    // Ignore overloads that are pointer-to-member constants.
1239c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie    if (FR.HasFormOfMemberPointer)
1240c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie      return false;
1241c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie
1242c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie    Overloads = FR.Expression;
1243c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie  } else if (E.getType() == Context.BoundMemberTy) {
1244c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie    Overloads = dyn_cast<UnresolvedMemberExpr>(E.IgnoreParens());
1245c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    IsMemExpr = true;
1246c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie  }
1247c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie
1248c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  bool Ambiguous = false;
1249c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie
1250c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie  if (Overloads) {
1251c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    for (OverloadExpr::decls_iterator it = Overloads->decls_begin(),
1252c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay         DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) {
12536dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      OverloadSet.addDecl(*it);
12546dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1255c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie      // Check whether the function is a non-template, non-member which takes no
12566dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      // arguments.
1257c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie      if (IsMemExpr)
1258c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie        continue;
12596dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      if (const FunctionDecl *OverloadDecl
12606dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall            = dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl())) {
1261c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie        if (OverloadDecl->getMinRequiredArguments() == 0) {
1262c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie          if (!ZeroArgCallReturnTy.isNull() && !Ambiguous) {
1263c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie            ZeroArgCallReturnTy = QualType();
1264c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie            Ambiguous = true;
1265c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie          } else
1266651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines            ZeroArgCallReturnTy = OverloadDecl->getReturnType();
1267c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie        }
1268c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      }
1269c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
12706dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1271c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    // If it's not a member, use better machinery to try to resolve the call
1272c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    if (!IsMemExpr)
1273c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie      return !ZeroArgCallReturnTy.isNull();
1274c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  }
1275c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie
1276c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  // Attempt to call the member with no arguments - this will correctly handle
1277c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  // member templates with defaults/deduction of template arguments, overloads
1278c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  // with default arguments, etc.
1279d739c4ecf7d7f2222f6f1c8de9e234d1c8a0468cEli Friedman  if (IsMemExpr && !E.isTypeDependent()) {
1280c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
1281c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    getDiagnostics().setSuppressAllDiagnostics(true);
12826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ExprResult R = BuildCallToMemberFunction(nullptr, &E, SourceLocation(),
12836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                             None, SourceLocation());
1284c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    getDiagnostics().setSuppressAllDiagnostics(Suppress);
1285c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    if (R.isUsable()) {
1286c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie      ZeroArgCallReturnTy = R.get()->getType();
1287c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie      return true;
1288c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    }
1289c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    return false;
1290c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
1291c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
12926dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) {
1293c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
1294c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      if (Fun->getMinRequiredArguments() == 0)
1295651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        ZeroArgCallReturnTy = Fun->getReturnType();
1296c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      return true;
1297c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
1298c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
1299c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1300c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  // We don't have an expression that's convenient to get a FunctionDecl from,
1301c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  // but we can at least check if the type is "function of 0 arguments".
1302c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  QualType ExprTy = E.getType();
13036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const FunctionType *FunTy = nullptr;
13049389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay  QualType PointeeTy = ExprTy->getPointeeType();
13059389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay  if (!PointeeTy.isNull())
13069389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay    FunTy = PointeeTy->getAs<FunctionType>();
1307c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (!FunTy)
1308c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    FunTy = ExprTy->getAs<FunctionType>();
1309c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1310c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (const FunctionProtoType *FPT =
1311c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      dyn_cast_or_null<FunctionProtoType>(FunTy)) {
1312651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (FPT->getNumParams() == 0)
1313651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ZeroArgCallReturnTy = FunTy->getReturnType();
1314c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    return true;
1315c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
1316c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  return false;
1317c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay}
1318c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1319c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \brief Give notes for a set of overloads.
1320c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1321c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie/// A companion to tryExprAsCall. In cases when the name that the programmer
1322c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// wrote was an overloaded function, we may be able to make some guesses about
1323c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// plausible overloads based on their return types; such guesses can be handed
1324c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// off to this method to be emitted as notes.
1325c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1326c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param Overloads - The overloads to note.
1327c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param FinalNoteLoc - If we've suppressed printing some overloads due to
1328c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  -fshow-overloads=best, this is the location to attach to the note about too
1329c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  many candidates. Typically this will be the location of the original
1330c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  ill-formed expression.
13316dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads,
13326dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                          const SourceLocation FinalNoteLoc) {
1333c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  int ShownOverloads = 0;
1334c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  int SuppressedOverloads = 0;
1335c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  for (UnresolvedSetImpl::iterator It = Overloads.begin(),
1336c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay       DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
1337c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // FIXME: Magic number for max shown overloads stolen from
1338c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // OverloadCandidateSet::NoteCandidates.
1339dc7b641574a733624489bd87fc7061771edf2113Douglas Gregor    if (ShownOverloads >= 4 && S.Diags.getShowOverloads() == Ovl_Best) {
1340c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      ++SuppressedOverloads;
1341c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      continue;
1342c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
13436dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
13446dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    NamedDecl *Fn = (*It)->getUnderlyingDecl();
13459c0e1ec7b3afd833c1b958ce2aeedff71c7eb4c5Abramo Bagnara    S.Diag(Fn->getLocation(), diag::note_possible_target_of_call);
1346c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    ++ShownOverloads;
1347c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
13486dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1349c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (SuppressedOverloads)
13506dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    S.Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates)
13516dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << SuppressedOverloads;
13526dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
13536dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
13546dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic void notePlausibleOverloads(Sema &S, SourceLocation Loc,
13556dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                   const UnresolvedSetImpl &Overloads,
13566dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                   bool (*IsPlausibleResult)(QualType)) {
13576dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (!IsPlausibleResult)
13586dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return noteOverloads(S, Overloads, Loc);
13596dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
13606dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  UnresolvedSet<2> PlausibleOverloads;
13616dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  for (OverloadExpr::decls_iterator It = Overloads.begin(),
13626dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall         DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
13636dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It);
1364651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    QualType OverloadResultTy = OverloadDecl->getReturnType();
13656dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    if (IsPlausibleResult(OverloadResultTy))
13666dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      PlausibleOverloads.addDecl(It.getDecl());
13676dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  }
13686dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  noteOverloads(S, PlausibleOverloads, Loc);
13696dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
13706dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
13716dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// Determine whether the given expression can be called by just
13726dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// putting parentheses after it.  Notably, expressions with unary
13736dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// operators can't be because the unary operator will start parsing
13746dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// outside the call.
13756dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool IsCallableWithAppend(Expr *E) {
13766dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  E = E->IgnoreImplicit();
13776dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return (!isa<CStyleCastExpr>(E) &&
13786dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<UnaryOperator>(E) &&
13796dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<BinaryOperator>(E) &&
13806dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<CXXOperatorCallExpr>(E));
13816dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
13826dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
13836dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallbool Sema::tryToRecoverWithCall(ExprResult &E, const PartialDiagnostic &PD,
13846dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                bool ForceComplain,
13856dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                bool (*IsPlausibleResult)(QualType)) {
13866dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  SourceLocation Loc = E.get()->getExprLoc();
13876dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  SourceRange Range = E.get()->getSourceRange();
13886dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
13896dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  QualType ZeroArgCallTy;
13906dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  UnresolvedSet<4> Overloads;
1391c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  if (tryExprAsCall(*E.get(), ZeroArgCallTy, Overloads) &&
13926dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      !ZeroArgCallTy.isNull() &&
13936dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      (!IsPlausibleResult || IsPlausibleResult(ZeroArgCallTy))) {
13946dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // At this point, we know E is potentially callable with 0
13956dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // arguments and that it returns something of a reasonable type,
13966dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // so we can emit a fixit and carry on pretending that E was
13976dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // actually a CallExpr.
1398f96df620402f41ce9f1a97c0d286c2b42637d5b9Nick Lewycky    SourceLocation ParenInsertionLoc = PP.getLocForEndOfToken(Range.getEnd());
13990116a40116616701282e12f3d92cf2949102d76aMichael Gottesman    Diag(Loc, PD)
14006dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << /*zero-arg*/ 1 << Range
14016dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << (IsCallableWithAppend(E.get())
14026dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          ? FixItHint::CreateInsertion(ParenInsertionLoc, "()")
14036dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          : FixItHint());
14046dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
14056dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
14066dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // FIXME: Try this before emitting the fixit, and suppress diagnostics
14076dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // while doing so.
1408ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    E = ActOnCallExpr(nullptr, E.get(), Range.getEnd(), None,
1409f96df620402f41ce9f1a97c0d286c2b42637d5b9Nick Lewycky                      Range.getEnd().getLocWithOffset(1));
14106dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return true;
14116dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  }
14126dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
14136dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (!ForceComplain) return false;
14146dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
14156dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  Diag(Loc, PD) << /*not zero-arg*/ 0 << Range;
14166dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
14176dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  E = ExprError();
14186dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return true;
1419c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay}
142057f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis
142157f8da506a0db208a936e26a8cb77267f638b26bArgyrios KyrtzidisIdentifierInfo *Sema::getSuperIdentifier() const {
142257f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis  if (!Ident_super)
142357f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis    Ident_super = &Context.Idents.get("super");
142457f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis  return Ident_super;
142557f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis}
14266afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj
1427cac18add73d095eaab600aefe27ea7174aec4922Nico WeberIdentifierInfo *Sema::getFloat128Identifier() const {
1428cac18add73d095eaab600aefe27ea7174aec4922Nico Weber  if (!Ident___float128)
1429cac18add73d095eaab600aefe27ea7174aec4922Nico Weber    Ident___float128 = &Context.Idents.get("__float128");
1430cac18add73d095eaab600aefe27ea7174aec4922Nico Weber  return Ident___float128;
1431cac18add73d095eaab600aefe27ea7174aec4922Nico Weber}
1432cac18add73d095eaab600aefe27ea7174aec4922Nico Weber
14336afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Sirajvoid Sema::PushCapturedRegionScope(Scope *S, CapturedDecl *CD, RecordDecl *RD,
14343a2f91280a49f4747063f983dc6a3296bd9359d2Ben Langmuir                                   CapturedRegionKind K) {
14358c045ace381972f41d385b0a661ccf172834f459Ben Langmuir  CapturingScopeInfo *CSI = new CapturedRegionScopeInfo(getDiagnostics(), S, CD, RD,
14368c045ace381972f41d385b0a661ccf172834f459Ben Langmuir                                                        CD->getContextParam(), K);
14376afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  CSI->ReturnType = Context.VoidTy;
14386afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  FunctionScopes.push_back(CSI);
14396afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj}
14406afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj
14416afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. SirajCapturedRegionScopeInfo *Sema::getCurCapturedRegion() {
14426afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  if (FunctionScopes.empty())
14436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
14446afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj
14456afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  return dyn_cast<CapturedRegionScopeInfo>(FunctionScopes.back());
14466afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj}
1447