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"
1682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov#include "TargetAttributesSema.h"
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/ASTDiagnostic.h"
19384aff8b94bb0d1ad6c5667b90621e5699815bb2John McCall#include "clang/AST/DeclCXX.h"
20f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper#include "clang/AST/DeclFriend.h"
21c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
22e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
23c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay#include "clang/AST/ExprCXX.h"
24ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner#include "clang/AST/StmtCXX.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
47ea8c59aaa6bd19976879142296f8fd12f8926738Argyrios KyrtzidisPrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context,
48ea8c59aaa6bd19976879142296f8fd12f8926738Argyrios Kyrtzidis                                       const Preprocessor &PP) {
498987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  PrintingPolicy Policy = Context.getPrintingPolicy();
504e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  Policy.Bool = Context.getLangOpts().Bool;
518987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  if (!Policy.Bool) {
529818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis    if (const MacroInfo *
539818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis          BoolMacro = PP.getMacroInfo(&Context.Idents.get("bool"))) {
540116a40116616701282e12f3d92cf2949102d76aMichael Gottesman      Policy.Bool = BoolMacro->isObjectLike() &&
558987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor        BoolMacro->getNumTokens() == 1 &&
568987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor        BoolMacro->getReplacementToken(0).is(tok::kw__Bool);
578987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor    }
588987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  }
590116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
608987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  return Policy;
618987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor}
628987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor
63c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregorvoid Sema::ActOnTranslationUnitScope(Scope *S) {
648ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroff  TUScope = S;
6544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  PushDeclContext(S, Context.getTranslationUnitDecl());
661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
67c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall  VAListTagName = PP.getIdentifierInfo("__va_list_tag");
68b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
693b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff
70f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas GregorSema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
71467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor           TranslationUnitKind TUKind,
723a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           CodeCompleteConsumer *CodeCompleter)
730116a40116616701282e12f3d92cf2949102d76aMichael Gottesman  : TheTargetAttributesSema(0), ExternalSource(0),
740ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    isMultiplexExternalSource(false), FPFeatures(pp.getLangOpts()),
754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    LangOpts(pp.getLangOpts()), PP(pp), Context(ctxt), Consumer(consumer),
761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
770ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    CollectStats(false), CodeCompleter(CodeCompleter),
783a387441ae339363ee5b254658f295e97bd9e913Argyrios Kyrtzidis    CurContext(0), OriginalLexicalContext(0),
793a387441ae339363ee5b254658f295e97bd9e913Argyrios Kyrtzidis    PackContext(0), MSStructPragmaOn(false), VisContext(0),
80e49ff3ef3459e97fa76502bd9eae4ed9170fd048Richard Smith    IsBuildingRecoveryCallExpr(false),
8180ee6e878a169e6255d4686a91bb696151ff229fJohn McCall    ExprNeedsCleanups(false), LateTemplateParser(0), OpaqueParser(0),
82395e04dbd7294bae4640aef1705cec3d052578b0Sebastian Redl    IdResolver(pp), StdInitializerList(0), CXXTypeInfoDecl(0), MSVCGuidDecl(0),
83471131a05d4f9dca7f9f4f5109f2a9a4ce80bf65Benjamin Kramer    NSNumberDecl(0),
84471131a05d4f9dca7f9f4f5109f2a9a4ce80bf65Benjamin Kramer    NSStringDecl(0), StringWithUTF8StringMethod(0),
85471131a05d4f9dca7f9f4f5109f2a9a4ce80bf65Benjamin Kramer    NSArrayDecl(0), ArrayWithObjectsMethod(0),
86ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    NSDictionaryDecl(0), DictionaryWithObjectsMethod(0),
870116a40116616701282e12f3d92cf2949102d76aMichael Gottesman    GlobalNewDeleteDeclared(false),
88467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    TUKind(TUKind),
8913489673b84fafaaf49cf5ae4e3bb9a945524dcbJohn McCall    NumSFINAEErrors(0), InFunctionDeclarator(0),
901eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor    AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false),
918491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor    NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1),
928491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor    CurrentInstantiationScope(0), TyposCorrected(0),
93cac18add73d095eaab600aefe27ea7174aec4922Nico Weber    AnalysisWarnings(*this), CurScope(0), Ident_super(0), Ident___float128(0)
94f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor{
953b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff  TUScope = 0;
960116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
97d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor  LoadedExternalKnownNamespaces = false;
98ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  for (unsigned I = 0; I != NSAPI::NumNSNumberLiteralMethods; ++I)
99ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    NSNumberLiteralMethods[I] = 0;
100ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
1014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC1)
102ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    NSAPIObj.reset(new NSAPI(Context));
103ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
1044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
10507952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    FieldCollector.reset(new CXXFieldCollector());
1061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10722caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  // Tell diagnostics how to render things from the AST library.
1080116a40116616701282e12f3d92cf2949102d76aMichael Gottesman  PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
10979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                       &Context);
1102afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor
1112afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor  ExprEvalContexts.push_back(
11276f3f69db1416425070177243e9f390122c553e0Richard Smith        ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0,
11376f3f69db1416425070177243e9f390122c553e0Richard Smith                                          false, 0, false));
114781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
1158fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(Diags));
11646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor}
11746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
11846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregorvoid Sema::Initialize() {
11946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the AST consumer about this Sema object.
12046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  Consumer.Initialize(Context);
1210116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
12246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // FIXME: Isn't this redundant with the initialization above?
12346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
12446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->InitializeSema(*this);
1250116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
12646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the external Sema source about this Sema object.
12746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
12846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor      = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
12946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->InitializeSema(*this);
1304dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor
131772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  // Initialize predefined 128-bit integer types, if needed.
13284268904947ada7e251932a6f5b0f4364df7a2c7Richard Smith  if (PP.getTargetInfo().hasInt128Type()) {
133772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    // If either of the 128-bit integer types are unavailable to name lookup,
134772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    // define them now.
135772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    DeclarationName Int128 = &Context.Idents.get("__int128_t");
136eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Int128) == IdResolver.end())
137772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor      PushOnScopeChains(Context.getInt128Decl(), TUScope);
138772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
139772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    DeclarationName UInt128 = &Context.Idents.get("__uint128_t");
140eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(UInt128) == IdResolver.end())
141772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor      PushOnScopeChains(Context.getUInt128Decl(), TUScope);
142772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
1430116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
144772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
1454dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  // Initialize predefined Objective-C types:
1464e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (PP.getLangOpts().ObjC1) {
1477a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    // If 'SEL' does not yet refer to any declarations, make it refer to the
1487a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    // predefined 'SEL'.
1497a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    DeclarationName SEL = &Context.Idents.get("SEL");
150eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(SEL) == IdResolver.end())
1517a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor      PushOnScopeChains(Context.getObjCSelDecl(), TUScope);
1527a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor
1534dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    // If 'id' does not yet refer to any declarations, make it refer to the
1544dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    // predefined 'id'.
1554dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    DeclarationName Id = &Context.Idents.get("id");
156eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Id) == IdResolver.end())
1574dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor      PushOnScopeChains(Context.getObjCIdDecl(), TUScope);
1580116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
15979d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    // Create the built-in typedef for 'Class'.
16079d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    DeclarationName Class = &Context.Idents.get("Class");
161eee242ff426bf79149f221798966e58688383c1eDouglas Gregor    if (IdResolver.begin(Class) == IdResolver.end())
16279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor      PushOnScopeChains(Context.getObjCClassDecl(), TUScope);
163a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
164a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    // Create the built-in forward declaratino for 'Protocol'.
165a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    DeclarationName Protocol = &Context.Idents.get("Protocol");
166a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    if (IdResolver.begin(Protocol) == IdResolver.end())
167a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor      PushOnScopeChains(Context.getObjCProtocolDecl(), TUScope);
1684dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  }
169c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
170c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  DeclarationName BuiltinVaList = &Context.Idents.get("__builtin_va_list");
171c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  if (IdResolver.begin(BuiltinVaList) == IdResolver.end())
172c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    PushOnScopeChains(Context.getBuiltinVaListDecl(), TUScope);
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovSema::~Sema() {
17682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (PackContext) FreePackedContext();
177aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  if (VisContext) FreeVisContext();
17882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  delete TheTargetAttributesSema;
17962c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  MSStructPragmaOn = false;
180781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  // Kill all the active scopes.
181781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I)
182781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[I];
183781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1)
184781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[0];
1850116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
18646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the SemaConsumer to forget about us; we're going out of scope.
18746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
18846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->ForgetSema();
18946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
19046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Detach from the external Sema source.
19146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
192914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor        = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
19346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->ForgetSema();
1940ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann
1950ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  // If Sema's ExternalSource is the multiplexer - we own it.
1960ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  if (isMultiplexExternalSource)
1970ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    delete ExternalSource;
19882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
19982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
200f85e193739c953358c865005855253af4f68a497John McCall/// makeUnavailableInSystemHeader - There is an error in the current
201f85e193739c953358c865005855253af4f68a497John McCall/// context.  If we're still in a system header, and we can plausibly
202f85e193739c953358c865005855253af4f68a497John McCall/// make the relevant declaration unavailable instead of erroring, do
203f85e193739c953358c865005855253af4f68a497John McCall/// so and return true.
204f85e193739c953358c865005855253af4f68a497John McCallbool Sema::makeUnavailableInSystemHeader(SourceLocation loc,
2055f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                         StringRef msg) {
206f85e193739c953358c865005855253af4f68a497John McCall  // If we're not in a function, it's an error.
207f85e193739c953358c865005855253af4f68a497John McCall  FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext);
208f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) return false;
209f85e193739c953358c865005855253af4f68a497John McCall
210f85e193739c953358c865005855253af4f68a497John McCall  // If we're in template instantiation, it's an error.
211f85e193739c953358c865005855253af4f68a497John McCall  if (!ActiveTemplateInstantiations.empty())
212f85e193739c953358c865005855253af4f68a497John McCall    return false;
2130116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
214f85e193739c953358c865005855253af4f68a497John McCall  // If that function's not in a system header, it's an error.
215f85e193739c953358c865005855253af4f68a497John McCall  if (!Context.getSourceManager().isInSystemHeader(loc))
216f85e193739c953358c865005855253af4f68a497John McCall    return false;
217f85e193739c953358c865005855253af4f68a497John McCall
218f85e193739c953358c865005855253af4f68a497John McCall  // If the function is already unavailable, it's not an error.
219f85e193739c953358c865005855253af4f68a497John McCall  if (fn->hasAttr<UnavailableAttr>()) return true;
220f85e193739c953358c865005855253af4f68a497John McCall
221f85e193739c953358c865005855253af4f68a497John McCall  fn->addAttr(new (Context) UnavailableAttr(loc, Context, msg));
222f85e193739c953358c865005855253af4f68a497John McCall  return true;
223f85e193739c953358c865005855253af4f68a497John McCall}
224f85e193739c953358c865005855253af4f68a497John McCall
22558a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian RedlASTMutationListener *Sema::getASTMutationListener() const {
22658a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl  return getASTConsumer().GetASTMutationListener();
22758a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl}
22858a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl
2290ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann///\brief Registers an external source. If an external source already exists,
2300ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann/// creates a multiplex external source and appends to it.
2310ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann///
2320ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann///\param[in] E - A non-null external sema source.
2330ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann///
2340ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumannvoid Sema::addExternalSource(ExternalSemaSource *E) {
2350ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  assert(E && "Cannot use with NULL ptr");
2360ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann
2370ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  if (!ExternalSource) {
2380ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    ExternalSource = E;
2390ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    return;
2400ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  }
2410ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann
2420ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  if (isMultiplexExternalSource)
2430ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    static_cast<MultiplexExternalSemaSource*>(ExternalSource)->addSource(*E);
2440ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  else {
2450ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    ExternalSource = new MultiplexExternalSemaSource(*ExternalSource, *E);
2460ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann    isMultiplexExternalSource = true;
2470ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann  }
2480ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann}
2490ec56b7add7be643f490ea9b430823570f01b4e2Axel Naumann
2505d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth/// \brief Print out statistics about the semantic analysis.
2515d98994c7749312a43ce6adf45537979a98e7afdChandler Carruthvoid Sema::PrintStats() const {
2525d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  llvm::errs() << "\n*** Semantic Analysis Stats:\n";
2535d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n";
2545d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth
2555d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  BumpAlloc.PrintStats();
2565d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth  AnalysisWarnings.PrintStats();
2575d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth}
2585d98994c7749312a43ce6adf45537979a98e7afdChandler Carruth
259c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
260c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith/// If there is already an implicit cast, merge into the existing one.
261c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith/// The result is of the given category.
262c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard SmithExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
263c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                   CastKind Kind, ExprValueKind VK,
264c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                   const CXXCastPath *BasePath,
265c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith                                   CheckedConversionKind CCK) {
2669c129f818038e0269ba6b095722aa70176dc321dRichard Smith#ifndef NDEBUG
2679c129f818038e0269ba6b095722aa70176dc321dRichard Smith  if (VK == VK_RValue && !E->isRValue()) {
2689c129f818038e0269ba6b095722aa70176dc321dRichard Smith    switch (Kind) {
2699c129f818038e0269ba6b095722aa70176dc321dRichard Smith    default:
2709c129f818038e0269ba6b095722aa70176dc321dRichard Smith      assert(0 && "can't implicitly cast lvalue to rvalue with this cast kind");
2719c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_LValueToRValue:
2729c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_ArrayToPointerDecay:
2739c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_FunctionToPointerDecay:
2749c129f818038e0269ba6b095722aa70176dc321dRichard Smith    case CK_ToVoid:
2759c129f818038e0269ba6b095722aa70176dc321dRichard Smith      break;
2769c129f818038e0269ba6b095722aa70176dc321dRichard Smith    }
2779c129f818038e0269ba6b095722aa70176dc321dRichard Smith  }
278acdfa4d504a8f2514d60569f9ce55d45f11795b9Richard Smith  assert((VK == VK_RValue || !E->isRValue()) && "can't cast rvalue to lvalue");
2799c129f818038e0269ba6b095722aa70176dc321dRichard Smith#endif
2809c129f818038e0269ba6b095722aa70176dc321dRichard Smith
281429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  QualType ExprTy = Context.getCanonicalType(E->getType());
2823a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType TypeTy = Context.getCanonicalType(Ty);
2831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2843a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  if (ExprTy == TypeTy)
285429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return Owned(E);
2861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2876fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // If this is a derived-to-base cast to a through a virtual base, we
2886fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // need a vtable.
2890116a40116616701282e12f3d92cf2949102d76aMichael Gottesman  if (Kind == CK_DerivedToBase &&
290f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall      BasePathInvolvesVirtualBase(*BasePath)) {
291429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    QualType T = E->getType();
2926fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const PointerType *Pointer = T->getAs<PointerType>())
2936fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      T = Pointer->getPointeeType();
2946fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const RecordType *RecordTy = T->getAs<RecordType>())
2950116a40116616701282e12f3d92cf2949102d76aMichael Gottesman      MarkVTableUsed(E->getLocStart(),
2966fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor                     cast<CXXRecordDecl>(RecordTy->getDecl()));
2976fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  }
298c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith
299c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
300c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
301c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      ImpCast->setType(Ty);
302c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      ImpCast->setValueKind(VK);
303c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith      return Owned(E);
304c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith    }
3054c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson  }
3064c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson
307c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith  return Owned(ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK));
308906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl}
309906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
310737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara/// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
311737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara/// to the conversion from scalar type ScalarTy to the Boolean type.
312737d5447b5d20633992ee5388eca5270c28c8ae7Abramo BagnaraCastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) {
313737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  switch (ScalarTy->getScalarTypeKind()) {
314737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Bool: return CK_NoOp;
3151d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_CPointer: return CK_PointerToBoolean;
3161d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_BlockPointer: return CK_PointerToBoolean;
3171d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case Type::STK_ObjCObjectPointer: return CK_PointerToBoolean;
318737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_MemberPointer: return CK_MemberPointerToBoolean;
319737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Integral: return CK_IntegralToBoolean;
320737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Floating: return CK_FloatingToBoolean;
321737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean;
322737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean;
323737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  }
324737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  return CK_Invalid;
325737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara}
326737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara
327bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis/// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
328bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidisstatic bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
329b9725cfb0a50731930a6331beb70f361b4d52a29Rafael Espindola  if (D->getMostRecentDecl()->isUsed())
330bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    return true;
331bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
332181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  if (D->isExternallyVisible())
3332d1b09641ecf2e754bf3fd244dc45dbf3e460c1bRafael Espindola    return true;
3342d1b09641ecf2e754bf3fd244dc45dbf3e460c1bRafael Espindola
335bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
336bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
337bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
338bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const FunctionDecl *DeclToCheck;
339bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (FD->hasBody(DeclToCheck))
340bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
341bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
342bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
343bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
344ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    DeclToCheck = FD->getMostRecentDecl();
345bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != FD)
346bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
347bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
348bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
349bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
350bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
351bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
3520116a40116616701282e12f3d92cf2949102d76aMichael Gottesman    const VarDecl *DeclToCheck = VD->getDefinition();
353bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck)
354bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
355bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
356bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
357bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
358ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    DeclToCheck = VD->getMostRecentDecl();
359bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != VD)
360bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
361bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
362bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
363bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  return false;
364bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis}
365bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
36615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallnamespace {
367cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky  struct SortUndefinedButUsed {
368995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    const SourceManager &SM;
369cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky    explicit SortUndefinedButUsed(SourceManager &SM) : SM(SM) {}
370995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky
371995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    bool operator()(const std::pair<NamedDecl *, SourceLocation> &l,
372995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky                    const std::pair<NamedDecl *, SourceLocation> &r) const {
373cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      if (l.second.isValid() && !r.second.isValid())
374cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky        return true;
375cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      if (!l.second.isValid() && r.second.isValid())
376cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky        return false;
377995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky      if (l.second != r.second)
378995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky        return SM.isBeforeInTranslationUnit(l.second, r.second);
379995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky      return SM.isBeforeInTranslationUnit(l.first->getLocation(),
380995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky                                          r.first->getLocation());
381995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    }
38215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  };
38315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
38415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
385995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky/// Obtains a sorted list of functions that are undefined but ODR-used.
386cd0655b17249c4c4908ca91462657f62285017e6Nick Lewyckyvoid Sema::getUndefinedButUsed(
387995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined) {
388995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  for (llvm::DenseMap<NamedDecl *, SourceLocation>::iterator
389cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky         I = UndefinedButUsed.begin(), E = UndefinedButUsed.end();
390995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky       I != E; ++I) {
391995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    NamedDecl *ND = I->first;
39215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
39315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // Ignore attributes that have become invalid.
394995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    if (ND->isInvalidDecl()) continue;
39515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
39615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // __attribute__((weakref)) is basically a definition.
397995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    if (ND->hasAttr<WeakRefAttr>()) continue;
39815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
399995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
400995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky      if (FD->isDefined())
40115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        continue;
402181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola      if (FD->isExternallyVisible() &&
403cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky          !FD->getMostRecentDecl()->isInlined())
404cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky        continue;
40515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    } else {
406995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky      if (cast<VarDecl>(ND)->hasDefinition() != VarDecl::DeclarationOnly)
40715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        continue;
408181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola      if (ND->isExternallyVisible())
409cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky        continue;
41015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    }
41115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
412995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    Undefined.push_back(std::make_pair(ND, I->second));
413995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  }
414995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky
415cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky  // Sort (in order of use site) so that we're not dependent on the iteration
416cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky  // order through an llvm::DenseMap.
417995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  std::sort(Undefined.begin(), Undefined.end(),
418cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky            SortUndefinedButUsed(Context.getSourceManager()));
419995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky}
420995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky
421cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky/// checkUndefinedButUsed - Check for undefined objects with internal linkage
422cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky/// or that are inline.
423cd0655b17249c4c4908ca91462657f62285017e6Nick Lewyckystatic void checkUndefinedButUsed(Sema &S) {
424cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky  if (S.UndefinedButUsed.empty()) return;
425995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky
426995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  // Collect all the still-undefined entities with internal linkage.
427995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
428cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky  S.getUndefinedButUsed(Undefined);
429995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  if (Undefined.empty()) return;
430995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky
431995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky  for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
432995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky         I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
433995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky    NamedDecl *ND = I->first;
434995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky
435181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    if (!ND->isExternallyVisible()) {
436cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      S.Diag(ND->getLocation(), diag::warn_undefined_internal)
437cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky        << isa<VarDecl>(ND) << ND;
438cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky    } else {
439cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      assert(cast<FunctionDecl>(ND)->getMostRecentDecl()->isInlined() &&
440cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky             "used object requires definition but isn't inline or internal?");
441cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      S.Diag(ND->getLocation(), diag::warn_undefined_inline) << ND;
442cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky    }
443cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky    if (I->second.isValid())
444cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      S.Diag(I->second, diag::note_used_here);
44515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
44615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
44715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
44831e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregorvoid Sema::LoadExternalWeakUndeclaredIdentifiers() {
44931e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  if (!ExternalSource)
45031e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    return;
4510116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
45231e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs;
45331e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs);
45431e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  for (unsigned I = 0, N = WeakIDs.size(); I != N; ++I) {
45531e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator Pos
45631e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor      = WeakUndeclaredIdentifiers.find(WeakIDs[I].first);
45731e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    if (Pos != WeakUndeclaredIdentifiers.end())
45831e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor      continue;
4590116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
46031e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor    WeakUndeclaredIdentifiers.insert(WeakIDs[I]);
46131e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  }
46231e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor}
46331e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor
464f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
465f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jaspertypedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap;
466f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
467f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// \brief Returns true, if all methods and nested classes of the given
468f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// CXXRecordDecl are defined in this translation unit.
469f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper///
470f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// Should only be called from ActOnEndOfTranslationUnit so that all
471f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// definitions are actually read.
472f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasperstatic bool MethodsAndNestedClassesComplete(const CXXRecordDecl *RD,
473f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                            RecordCompleteMap &MNCComplete) {
474f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  RecordCompleteMap::iterator Cache = MNCComplete.find(RD);
475f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  if (Cache != MNCComplete.end())
476f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    return Cache->second;
477f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  if (!RD->isCompleteDefinition())
478f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    return false;
479f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  bool Complete = true;
480f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  for (DeclContext::decl_iterator I = RD->decls_begin(),
481f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                  E = RD->decls_end();
482f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper       I != E && Complete; ++I) {
483f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    if (const CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(*I))
484f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      Complete = M->isDefined() || (M->isPure() && !isa<CXXDestructorDecl>(M));
4850e9e9f8d17e38b3c44f6e1323be3a812a793bdd1Daniel Jasper    else if (const FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(*I))
4860e9e9f8d17e38b3c44f6e1323be3a812a793bdd1Daniel Jasper      Complete = F->getTemplatedDecl()->isDefined();
487f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    else if (const CXXRecordDecl *R = dyn_cast<CXXRecordDecl>(*I)) {
488f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (R->isInjectedClassName())
489f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        continue;
490f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (R->hasDefinition())
491f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = MethodsAndNestedClassesComplete(R->getDefinition(),
492f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                                   MNCComplete);
493f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      else
494f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = false;
495f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    }
496f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  }
497f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  MNCComplete[RD] = Complete;
498f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  return Complete;
499f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper}
500f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
501f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// \brief Returns true, if the given CXXRecordDecl is fully defined in this
502f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// translation unit, i.e. all methods are defined or pure virtual and all
503f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// friends, friend functions and nested classes are fully defined in this
504f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// translation unit.
505f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper///
506f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// Should only be called from ActOnEndOfTranslationUnit so that all
507f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper/// definitions are actually read.
508f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasperstatic bool IsRecordFullyDefined(const CXXRecordDecl *RD,
509f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                 RecordCompleteMap &RecordsComplete,
510f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                 RecordCompleteMap &MNCComplete) {
511f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  RecordCompleteMap::iterator Cache = RecordsComplete.find(RD);
512f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  if (Cache != RecordsComplete.end())
513f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    return Cache->second;
514f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  bool Complete = MethodsAndNestedClassesComplete(RD, MNCComplete);
515f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  for (CXXRecordDecl::friend_iterator I = RD->friend_begin(),
516f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                                      E = RD->friend_end();
517f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper       I != E && Complete; ++I) {
518f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    // Check if friend classes and methods are complete.
519f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    if (TypeSourceInfo *TSI = (*I)->getFriendType()) {
520f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      // Friend classes are available as the TypeSourceInfo of the FriendDecl.
521f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (CXXRecordDecl *FriendD = TSI->getType()->getAsCXXRecordDecl())
522f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = MethodsAndNestedClassesComplete(FriendD, MNCComplete);
523f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      else
524f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = false;
525f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    } else {
526f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      // Friend functions are available through the NamedDecl of FriendDecl.
527f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (const FunctionDecl *FD =
528f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper          dyn_cast<FunctionDecl>((*I)->getFriendDecl()))
529f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = FD->isDefined();
530f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      else
531f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        // This is a template friend, give up.
532f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Complete = false;
533f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    }
534f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  }
535f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  RecordsComplete[RD] = Complete;
536f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  return Complete;
537f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper}
538f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
5399299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// ActOnEndOfTranslationUnit - This is called at the very end of the
5409299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// translation unit when EOF is reached and all but the top-level scope is
5419299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// popped.
5420e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidisvoid Sema::ActOnEndOfTranslationUnit() {
5439257664568bf375b7790131a84d9a4fa30a5b7e3John McCall  assert(DelayedDiagnostics.getCurrentPool() == NULL
5449257664568bf375b7790131a84d9a4fa30a5b7e3John McCall         && "reached end of translation unit with a pool attached?");
5459257664568bf375b7790131a84d9a4fa30a5b7e3John McCall
546d82f5eb1f98825ad888fb0ab45e5d8ef2e0ec6a2Douglas Gregor  // If code completion is enabled, don't perform any end-of-translation-unit
547d82f5eb1f98825ad888fb0ab45e5d8ef2e0ec6a2Douglas Gregor  // work.
548d82f5eb1f98825ad888fb0ab45e5d8ef2e0ec6a2Douglas Gregor  if (PP.isCodeCompletionEnabled())
549d82f5eb1f98825ad888fb0ab45e5d8ef2e0ec6a2Douglas Gregor    return;
550d82f5eb1f98825ad888fb0ab45e5d8ef2e0ec6a2Douglas Gregor
551b775100fea6d8955149897dae1adca50ca471d17Richard Smith  // Complete translation units and modules define vtables and perform implicit
552b775100fea6d8955149897dae1adca50ca471d17Richard Smith  // instantiations. PCH files do not.
553b775100fea6d8955149897dae1adca50ca471d17Richard Smith  if (TUKind != TU_Prefix) {
554849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    DiagnoseUseOfUnimplementedSelectors();
555849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
556aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // If any dynamic classes have their key function defined within
557aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // this translation unit, then those vtables are considered "used" and must
558aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // be emitted.
559a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor    for (DynamicClassesType::iterator I = DynamicClasses.begin(ExternalSource),
560a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor                                      E = DynamicClasses.end();
561a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor         I != E; ++I) {
562a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor      assert(!(*I)->isDependentType() &&
563a5c6c2a84cde5c9b8f8ec0610a9f89ffd54f44eeAnders Carlsson             "Should not see dependent types here!");
564d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall      if (const CXXMethodDecl *KeyFunction = Context.getCurrentKeyFunction(*I)) {
565aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth        const FunctionDecl *Definition = 0;
566aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth        if (KeyFunction->hasBody(Definition))
567a126f17ca83b985300c1f65cee647bea108db657Douglas Gregor          MarkVTableUsed(Definition->getLocation(), *I, true);
568aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth      }
569aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    }
570aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth
5718155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // If DefinedUsedVTables ends up marking any virtual member functions it
5728155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // might lead to more pending template instantiations, which we then need
5738155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // to instantiate.
5748155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
5758155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
5768155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // C++: Perform implicit template instantiations.
5778155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    //
5788155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // FIXME: When we perform these implicit instantiations, we do not
5798155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // carefully keep track of the point of instantiation (C++ [temp.point]).
5808155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // This means that name lookup that occurs within the template
5818155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // instantiation will always happen at the end of the translation unit,
582b775100fea6d8955149897dae1adca50ca471d17Richard Smith    // so it will find some names that are not required to be found. This is
583b775100fea6d8955149897dae1adca50ca471d17Richard Smith    // valid, but we could do better by diagnosing if an instantiation uses a
584b775100fea6d8955149897dae1adca50ca471d17Richard Smith    // name that was not visible at its first point of instantiation.
5858155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    PerformPendingInstantiations();
5862a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
5870116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
58849b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Remove file scoped decls that turned out to be used.
589676ea9dce44ed2d1dc3d7f431824045764d8a3e9Daniel Jasper  UnusedFileScopedDecls.erase(
590676ea9dce44ed2d1dc3d7f431824045764d8a3e9Daniel Jasper      std::remove_if(UnusedFileScopedDecls.begin(0, true),
591676ea9dce44ed2d1dc3d7f431824045764d8a3e9Daniel Jasper                     UnusedFileScopedDecls.end(),
592676ea9dce44ed2d1dc3d7f431824045764d8a3e9Daniel Jasper                     std::bind1st(std::ptr_fun(ShouldRemoveFromUnused), this)),
593676ea9dce44ed2d1dc3d7f431824045764d8a3e9Daniel Jasper      UnusedFileScopedDecls.end());
59447268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor
595467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  if (TUKind == TU_Prefix) {
596467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    // Translation unit prefixes don't need any of the checking below.
59787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    TUScope = 0;
59872b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    return;
59987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
60072b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis
60163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // Check for #pragma weak identifiers that were never declared
60263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // FIXME: This will cause diagnostics to be emitted in a non-determinstic
60363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // order!  Iterating over a densemap like this is bad.
60431e37b2d7b4815fdea6a35d49f33005562f0d494Douglas Gregor  LoadExternalWeakUndeclaredIdentifiers();
605e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
60663d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       I = WeakUndeclaredIdentifiers.begin(),
60763d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
60863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    if (I->second.getUsed()) continue;
6091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
61163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      << I->first;
612e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  }
613e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn
614975d353997a13f7dfdaf4ec7a547fe1adb15f35eArgyrios Kyrtzidis  if (LangOpts.CPlusPlus11 &&
615975d353997a13f7dfdaf4ec7a547fe1adb15f35eArgyrios Kyrtzidis      Diags.getDiagnosticLevel(diag::warn_delegating_ctor_cycle,
616975d353997a13f7dfdaf4ec7a547fe1adb15f35eArgyrios Kyrtzidis                               SourceLocation())
617975d353997a13f7dfdaf4ec7a547fe1adb15f35eArgyrios Kyrtzidis        != DiagnosticsEngine::Ignored)
618975d353997a13f7dfdaf4ec7a547fe1adb15f35eArgyrios Kyrtzidis    CheckDelegatingCtorCycles();
619975d353997a13f7dfdaf4ec7a547fe1adb15f35eArgyrios Kyrtzidis
620467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  if (TUKind == TU_Module) {
62190db26000aefe9335370013eec64c85232d80227Douglas Gregor    // If we are building a module, resolve all of the exported declarations
62290db26000aefe9335370013eec64c85232d80227Douglas Gregor    // now.
62390db26000aefe9335370013eec64c85232d80227Douglas Gregor    if (Module *CurrentModule = PP.getCurrentModule()) {
62490db26000aefe9335370013eec64c85232d80227Douglas Gregor      ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6250116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
626cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko      SmallVector<Module *, 2> Stack;
62790db26000aefe9335370013eec64c85232d80227Douglas Gregor      Stack.push_back(CurrentModule);
62890db26000aefe9335370013eec64c85232d80227Douglas Gregor      while (!Stack.empty()) {
62990db26000aefe9335370013eec64c85232d80227Douglas Gregor        Module *Mod = Stack.back();
63090db26000aefe9335370013eec64c85232d80227Douglas Gregor        Stack.pop_back();
6310116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
632906d66acc5cf2679453e10a4f0a67feedd765b21Douglas Gregor        // Resolve the exported declarations and conflicts.
63390db26000aefe9335370013eec64c85232d80227Douglas Gregor        // FIXME: Actually complain, once we figure out how to teach the
634906d66acc5cf2679453e10a4f0a67feedd765b21Douglas Gregor        // diagnostic client to deal with complaints in the module map at this
63590db26000aefe9335370013eec64c85232d80227Douglas Gregor        // point.
63690db26000aefe9335370013eec64c85232d80227Douglas Gregor        ModMap.resolveExports(Mod, /*Complain=*/false);
637906d66acc5cf2679453e10a4f0a67feedd765b21Douglas Gregor        ModMap.resolveConflicts(Mod, /*Complain=*/false);
6380116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
63990db26000aefe9335370013eec64c85232d80227Douglas Gregor        // Queue the submodules, so their exports will also be resolved.
640b7a7819473709c01ea024a2dc15e99d38f0f8760Douglas Gregor        for (Module::submodule_iterator Sub = Mod->submodule_begin(),
641b7a7819473709c01ea024a2dc15e99d38f0f8760Douglas Gregor                                     SubEnd = Mod->submodule_end();
64290db26000aefe9335370013eec64c85232d80227Douglas Gregor             Sub != SubEnd; ++Sub) {
643b7a7819473709c01ea024a2dc15e99d38f0f8760Douglas Gregor          Stack.push_back(*Sub);
64490db26000aefe9335370013eec64c85232d80227Douglas Gregor        }
64590db26000aefe9335370013eec64c85232d80227Douglas Gregor      }
64690db26000aefe9335370013eec64c85232d80227Douglas Gregor    }
6470116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
648467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    // Modules don't need any of the checking below.
649467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    TUScope = 0;
650467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor    return;
651467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor  }
6520116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
653275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  // C99 6.9.2p2:
654275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   A declaration of an identifier for an object that has file
655275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   scope without an initializer, and without a storage-class
656275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   specifier or with the storage-class specifier static,
657275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   constitutes a tentative definition. If a translation unit
658275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   contains one or more tentative definitions for an identifier,
659275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   and the translation unit contains no external definition for
660275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   that identifier, then the behavior is exactly as if the
661275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit contains a file scope declaration of that
662275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   identifier, with the composite type as of the end of the
663275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit, with an initializer equal to 0.
664e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  llvm::SmallSet<VarDecl *, 32> Seen;
6650116a40116616701282e12f3d92cf2949102d76aMichael Gottesman  for (TentativeDefinitionsType::iterator
666a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor            T = TentativeDefinitions.begin(ExternalSource),
667a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor         TEnd = TentativeDefinitions.end();
6680116a40116616701282e12f3d92cf2949102d76aMichael Gottesman       T != TEnd; ++T)
669a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor  {
670a862320972e63349524dc9aa744dec1b95f54ba1Douglas Gregor    VarDecl *VD = (*T)->getActingDefinition();
671e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
672e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // If the tentative definition was completed, getActingDefinition() returns
673e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // null. If we've already seen this variable before, insert()'s second
674e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // return value is false.
675e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
676b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      continue;
677b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const IncompleteArrayType *ArrayT
679b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        = Context.getAsIncompleteArrayType(VD->getType())) {
68063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      // Set the length of the array to 1 (C99 6.9.2p5).
68163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
68263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
68346a617a792bfab0d9b1e057371ea3b9540802226John McCall      QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
68446a617a792bfab0d9b1e057371ea3b9540802226John McCall                                                One, ArrayType::Normal, 0);
68563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      VD->setType(T);
6861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
687b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                                   diag::err_tentative_def_incomplete_type))
688b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      VD->setInvalidDecl();
689b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
690e4851f26eb7be1f71f919bb5890da7e3583f727dEli Friedman    CheckCompleteVariableDeclaration(VD);
691e4851f26eb7be1f71f919bb5890da7e3583f727dEli Friedman
692b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    // Notify the consumer that we've completed a tentative definition.
693b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    if (!VD->isInvalidDecl())
694b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      Consumer.CompleteTentativeDefinition(VD);
695b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
696275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  }
69743f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis
69843f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  // If there were errors, disable 'unused' warnings since they will mostly be
69943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  // noise.
70043f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  if (!Diags.hasErrorOccurred()) {
70143f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis    // Output warning for unused file scoped decls.
702a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor    for (UnusedFileScopedDeclsType::iterator
703a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor           I = UnusedFileScopedDecls.begin(ExternalSource),
70443f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis           E = UnusedFileScopedDecls.end(); I != E; ++I) {
705a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor      if (ShouldRemoveFromUnused(this, *I))
706a2ee20aa9660851080135219cac5b31fbac08b78Douglas Gregor        continue;
7070116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
70843f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
70943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        const FunctionDecl *DiagD;
71043f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        if (!FD->hasBody(DiagD))
71143f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis          DiagD = FD;
71248b89590f61575cbf365ba996a2bd1ba1561a4abArgyrios Kyrtzidis        if (DiagD->isDeleted())
71348b89590f61575cbf365ba996a2bd1ba1561a4abArgyrios Kyrtzidis          continue; // Deleted functions are supposed to be unused.
7146b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        if (DiagD->isReferenced()) {
7156b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          if (isa<CXXMethodDecl>(DiagD))
7166b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis            Diag(DiagD->getLocation(), diag::warn_unneeded_member_function)
7176b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                  << DiagD->getDeclName();
71812d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian          else {
719d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola            if (FD->getStorageClass() == SC_Static &&
72012d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian                !FD->isInlineSpecified() &&
72112d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian                !SourceMgr.isFromMainFile(
72212d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian                   SourceMgr.getExpansionLoc(FD->getLocation())))
72312d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian              Diag(DiagD->getLocation(), diag::warn_unneeded_static_internal_decl)
72412d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian                << DiagD->getDeclName();
72512d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian            else
72612d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian              Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
72712d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian                   << /*function*/0 << DiagD->getDeclName();
72812d2cc71bfeb1e7be9ce00fc52feab50941cac24Fariborz Jahanian          }
7296b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        } else {
7306b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(),
7316b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis               isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function
7326b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                                         : diag::warn_unused_function)
7336b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << DiagD->getDeclName();
7346b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        }
73543f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      } else {
73643f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
73743f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        if (!DiagD)
73843f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis          DiagD = cast<VarDecl>(*I);
7396b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        if (DiagD->isReferenced()) {
7406b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
7416b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << /*variable*/1 << DiagD->getDeclName();
74219b6a707a86302adc80d64464cbc3cb8a7a7f3a6Matt Beaumont-Gay        } else if (getSourceManager().isFromMainFile(DiagD->getLocation())) {
74319b6a707a86302adc80d64464cbc3cb8a7a7f3a6Matt Beaumont-Gay          // If the declaration is in a header which is included into multiple
74419b6a707a86302adc80d64464cbc3cb8a7a7f3a6Matt Beaumont-Gay          // TUs, it will declare one variable per TU, and one of the other
74519b6a707a86302adc80d64464cbc3cb8a7a7f3a6Matt Beaumont-Gay          // variables may be used. So, only warn if the declaration is in the
74619b6a707a86302adc80d64464cbc3cb8a7a7f3a6Matt Beaumont-Gay          // main file.
7476b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(), diag::warn_unused_variable)
74819b6a707a86302adc80d64464cbc3cb8a7a7f3a6Matt Beaumont-Gay              << DiagD->getDeclName();
7496b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        }
75043f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      }
751bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    }
75215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
75301a41140cd8ec9475ed0c33384310fbdd3b6de11Nick Lewycky    if (ExternalSource)
754cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky      ExternalSource->ReadUndefinedButUsed(UndefinedButUsed);
755cd0655b17249c4c4908ca91462657f62285017e6Nick Lewycky    checkUndefinedButUsed(*this);
75649b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  }
75787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
758f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  if (Diags.getDiagnosticLevel(diag::warn_unused_private_field,
759f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper                               SourceLocation())
760f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        != DiagnosticsEngine::Ignored) {
761f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    RecordCompleteMap RecordsComplete;
762f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    RecordCompleteMap MNCComplete;
763f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    for (NamedDeclSetType::iterator I = UnusedPrivateFields.begin(),
764f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper         E = UnusedPrivateFields.end(); I != E; ++I) {
765f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      const NamedDecl *D = *I;
766f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
767f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      if (RD && !RD->isUnion() &&
768f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper          IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) {
769f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper        Diag(D->getLocation(), diag::warn_unused_private_field)
770f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper              << D->getDeclName();
771f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper      }
772f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper    }
773f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  }
774f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
775483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // Check we've noticed that we're no longer parsing the initializer for every
776483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // variable. If we miss cases, then at best we have a performance issue and
777483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // at worst a rejects-valid bug.
778483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  assert(ParsingInitForAutoVars.empty() &&
779483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith         "Didn't unmark var as having its initializer parsed");
780483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
78187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  TUScope = 0;
7829299f3fa85796613cc787a2062c9562d07c8613eChris Lattner}
7839299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
7849299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
7865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper functions.
7875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
7885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7898517d9b731f065cdfc55ec0f3ddf5d564d988648Anders CarlssonDeclContext *Sema::getFunctionLevelDeclContext() {
790db0ee1da16e9dbec19b144c9cd96ee9f55fe0c53John McCall  DeclContext *DC = CurContext;
7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
79272899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  while (true) {
7936afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj    if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC) || isa<CapturedDecl>(DC)) {
79472899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman      DC = DC->getParent();
79572899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    } else if (isa<CXXMethodDecl>(DC) &&
796215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor               cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call &&
79772899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman               cast<CXXRecordDecl>(DC->getParent())->isLambda()) {
79872899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman      DC = DC->getParent()->getParent();
79972899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    }
80072899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    else break;
80172899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman  }
8021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8038517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  return DC;
8048517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson}
8058517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson
806371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// getCurFunctionDecl - If inside of a function body, this returns a pointer
807371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// to the function decl for the function being parsed.  If we're currently
808371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// in a 'block', this returns the containing context.
809371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerFunctionDecl *Sema::getCurFunctionDecl() {
8108517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
811371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return dyn_cast<FunctionDecl>(DC);
812371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
813371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
814c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel DunbarObjCMethodDecl *Sema::getCurMethodDecl() {
8158517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
81669f86d92ae940c5c8d979951d9c55fe9111627a0Fariborz Jahanian  while (isa<RecordDecl>(DC))
81769f86d92ae940c5c8d979951d9c55fe9111627a0Fariborz Jahanian    DC = DC->getParent();
818d7612e183bb09d04677d0ddde89ee29130ffb715Steve Naroff  return dyn_cast<ObjCMethodDecl>(DC);
819c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar}
820371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
821371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerNamedDecl *Sema::getCurFunctionOrMethodDecl() {
8228517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
823371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
8244afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return cast<NamedDecl>(DC);
825371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return 0;
826371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
827371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
828393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbarvoid Sema::EmitCurrentDiagnostic(unsigned DiagID) {
829393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // FIXME: It doesn't make sense to me that DiagID is an incoming argument here
830393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // and yet we also use the current diag ID on the DiagnosticsEngine. This has
831393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // been made more painfully obvious by the refactor that introduced this
832393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // function, but it is possible that the incoming argument can be
833393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // eliminnated. If it truly cannot be (for example, there is some reentrancy
834393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // issue I am not seeing yet), then there should at least be a clarifying
835393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  // comment somewhere.
836dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie  if (Optional<TemplateDeductionInfo*> Info = isSFINAEContext()) {
837393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar    switch (DiagnosticIDs::getDiagnosticSFINAEResponse(
838393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar              Diags.getCurrentDiagID())) {
83933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Report:
84077faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // We'll report the diagnostic below.
8419b623639378d53a675921ddfa7316034d571881eDouglas Gregor      break;
8420116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
84377faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    case DiagnosticIDs::SFINAE_SubstitutionFailure:
84477faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Count this failure so that we know that template argument deduction
84577faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // has failed.
846393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      ++NumSFINAEErrors;
847b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
848b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // Make a copy of this suppressed diagnostic and store it with the
849b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // template-deduction information.
850b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
851b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        Diagnostic DiagInfo(&Diags);
852b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
853b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
854b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      }
855b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
856393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
857393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.Clear();
85877faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      return;
8590116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
86077faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    case DiagnosticIDs::SFINAE_AccessControl: {
8617822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // Per C++ Core Issue 1170, access control is part of SFINAE.
86200b43848e4a320b0845c1004694fa50920096c75Daniel Dunbar      // Additionally, the AccessCheckingSFINAE flag can be used to temporarily
8637822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // make access control a part of SFINAE for the purposes of checking
8647822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // type traits.
86580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      if (!AccessCheckingSFINAE && !getLangOpts().CPlusPlus11)
8661eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor        break;
86777faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
868393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      SourceLocation Loc = Diags.getCurrentDiagLoc();
86977faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
87077faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Suppress this diagnostic.
871393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      ++NumSFINAEErrors;
872b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
873b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // Make a copy of this suppressed diagnostic and store it with the
874b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // template-deduction information.
875b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
876b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        Diagnostic DiagInfo(&Diags);
877b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
878b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
879b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      }
880b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
881393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
882393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.Clear();
88377faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
88477faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // Now the diagnostic state is clear, produce a C++98 compatibility
88577faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // warning.
886393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diag(Loc, diag::warn_cxx98_compat_sfinae_access_control);
88777faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
88877faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // The last diagnostic which Sema produced was ignored. Suppress any
88977faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith      // notes attached to it.
890393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
8919b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
89277faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith    }
89377faa365cb2322cfc8edf58a4f5d68f2370cc39aRichard Smith
89433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Suppress:
8959b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Make a copy of this suppressed diagnostic and store it with the
8969b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // template-deduction information;
897b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      if (*Info) {
898b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith        Diagnostic DiagInfo(&Diags);
8991eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor        (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(),
900b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
901b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      }
902b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith
903b8590f3572158bde97f14037c4cc8f4a57c8d810Richard Smith      // Suppress this diagnostic.
904393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.setLastDiagnosticIgnored();
905393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      Diags.Clear();
9069b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
9079b623639378d53a675921ddfa7316034d571881eDouglas Gregor    }
9089b623639378d53a675921ddfa7316034d571881eDouglas Gregor  }
9090116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
9108987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor  // Set up the context's printing policy based on our current state.
911393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  Context.setPrintingPolicy(getPrintingPolicy());
9120116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
9139b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // Emit the diagnostic.
914393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar  if (!Diags.EmitCurrentDiagnostic())
9155e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    return;
9161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
91725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // If this is not a note, and we're in a template instantiation
91825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // that is different from the last template instantiation where
91925a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // we emitted an error, print a template instantiation
92025a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // backtrace.
92133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  if (!DiagnosticIDs::isBuiltinNote(DiagID) &&
922393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      !ActiveTemplateInstantiations.empty() &&
923393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar      ActiveTemplateInstantiations.back()
924393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar        != LastTemplateInstantiationErrorContext) {
925393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar    PrintInstantiationStack();
926393eed7fb901e49085c8583ff0439d1273b6f2feDaniel Dunbar    LastTemplateInstantiationErrorContext = ActiveTemplateInstantiations.back();
92725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  }
92825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor}
9292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
93091a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::SemaDiagnosticBuilder
93191a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
93291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
93391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  PD.Emit(Builder);
9341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  return Builder;
93691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson}
93791a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson
938108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// \brief Looks through the macro-expansion chain for the given
939108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// location, looking for a macro expansion with the given name.
940834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// If one is found, returns true and sets the location to that
941108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth/// expansion loc.
9425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerbool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) {
943834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  SourceLocation loc = locref;
944834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (!loc.isMacroID()) return false;
945834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
946834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // There's no good way right now to look at the intermediate
947108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth  // expansions, so just jump to the expansion location.
948402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  loc = getSourceManager().getExpansionLoc(loc);
949834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
950834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // If that's written with the name, stop here.
9515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<char, 16> buffer;
952834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (getPreprocessor().getSpelling(loc, buffer) == name) {
953834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    locref = loc;
954834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    return true;
955834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  }
956834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  return false;
957834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall}
958834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
95923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \brief Determines the active Scope associated with the given declaration
96023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// context.
96123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
96223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// This routine maps a declaration context to the active Scope object that
96323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// represents that declaration context in the parser. It is typically used
96423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// from "scope-less" code (e.g., template instantiation, lazy creation of
96523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// declarations) that injects a name for name-lookup purposes and, therefore,
96623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// must update the Scope.
96723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
96823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \returns The scope corresponding to the given declaraion context, or NULL
96923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// if no such scope is open.
97023c94dbb6631fecdb55ba401aa93722803d980c6Douglas GregorScope *Sema::getScopeForContext(DeclContext *Ctx) {
9710116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
97223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!Ctx)
97323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return 0;
9740116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
97523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Ctx = Ctx->getPrimaryContext();
97623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
977cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // Ignore scopes that cannot have declarations. This is important for
978cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // out-of-line definitions of static class members.
979cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
980cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl      if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity()))
981cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl        if (Ctx == Entity->getPrimaryContext())
982cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl          return S;
98323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  }
9840116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
98523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return 0;
98623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor}
9879ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
9889ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Enter a new function scope
9899ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushFunctionScope() {
990781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1) {
991781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // Use the "top" function scope rather than having to allocate
992781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // memory for a new scope.
9938fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis    FunctionScopes.back()->Clear();
994781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    FunctionScopes.push_back(FunctionScopes.back());
9959ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return;
9969ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  }
9970116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
9988fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics()));
9999ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
10009ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
10019ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
10028fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(),
10039ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor                                              BlockScope, Block));
10049ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
10059ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
10060116a40116616701282e12f3d92cf2949102d76aMichael Gottesmanvoid Sema::PushLambdaScope(CXXRecordDecl *Lambda,
100776e3da57b0e8cf72d221f44d54566ef206341668Douglas Gregor                           CXXMethodDecl *CallOperator) {
100876e3da57b0e8cf72d221f44d54566ef206341668Douglas Gregor  FunctionScopes.push_back(new LambdaScopeInfo(getDiagnostics(), Lambda,
100976e3da57b0e8cf72d221f44d54566ef206341668Douglas Gregor                                               CallOperator));
1010ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman}
1011ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman
1012ec9ea7200718478e8a976529defbe21942a11c9cEli Friedmanvoid Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP,
1013ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman                                const Decl *D, const BlockExpr *blkExpr) {
10140116a40116616701282e12f3d92cf2949102d76aMichael Gottesman  FunctionScopeInfo *Scope = FunctionScopes.pop_back_val();
1015781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  assert(!FunctionScopes.empty() && "mismatched push/pop!");
10160116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
10173ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  // Issue any analysis-based warnings.
10183ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  if (WP && D)
1019283a358aecb75e30fcd486f2206f6c03c5e7f11dTed Kremenek    AnalysisWarnings.IssueWarnings(*WP, Scope, D, blkExpr);
1020351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  else {
10215f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator
1022351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         i = Scope->PossiblyUnreachableDiags.begin(),
1023351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         e = Scope->PossiblyUnreachableDiags.end();
1024351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         i != e; ++i) {
1025351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek      const sema::PossiblyUnreachableDiag &D = *i;
1026351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek      Diag(D.Loc, D.PD);
1027351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek    }
1028351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  }
10293ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek
1030351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  if (FunctionScopes.back() != Scope) {
1031781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete Scope;
1032351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  }
10339ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
10349ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
1035625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::PushCompoundScope() {
1036625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  getCurFunction()->CompoundScopes.push_back(CompoundScopeInfo());
1037625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko}
1038625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
1039625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::PopCompoundScope() {
1040625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  FunctionScopeInfo *CurFunction = getCurFunction();
1041625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  assert(!CurFunction->CompoundScopes.empty() && "mismatched push/pop");
1042625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
1043625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  CurFunction->CompoundScopes.pop_back();
1044625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko}
1045625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
10469ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Determine whether any errors occurred within this function/method/
10479ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// block.
1048f85e193739c953358c865005855253af4f68a497John McCallbool Sema::hasAnyUnrecoverableErrorsInThisFunction() const {
1049f85e193739c953358c865005855253af4f68a497John McCall  return getCurFunction()->ErrorTrap.hasUnrecoverableErrorOccurred();
10509ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
10519ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
10529ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo *Sema::getCurBlock() {
10539ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.empty())
10549ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return 0;
10550116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
10560116a40116616701282e12f3d92cf2949102d76aMichael Gottesman  return dyn_cast<BlockScopeInfo>(FunctionScopes.back());
10579ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
105876bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
1059906a7e1c0f272f7e539c82dda01f4644031ce637Eli FriedmanLambdaScopeInfo *Sema::getCurLambda() {
1060906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman  if (FunctionScopes.empty())
1061906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman    return 0;
10620116a40116616701282e12f3d92cf2949102d76aMichael Gottesman
10630116a40116616701282e12f3d92cf2949102d76aMichael Gottesman  return dyn_cast<LambdaScopeInfo>(FunctionScopes.back());
1064906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman}
1065906a7e1c0f272f7e539c82dda01f4644031ce637Eli Friedman
1066aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkovoid Sema::ActOnComment(SourceRange Comment) {
1067127ff2ea6440c3da4b47f9c8b3b190254a97e7b5Ted Kremenek  if (!LangOpts.RetainCommentsFromSystemHeaders &&
1068127ff2ea6440c3da4b47f9c8b3b190254a97e7b5Ted Kremenek      SourceMgr.isInSystemHeader(Comment.getBegin()))
1069127ff2ea6440c3da4b47f9c8b3b190254a97e7b5Ted Kremenek    return;
10706fd7d3067dd06584ef3940e88e31fea1a0e83588Dmitri Gribenko  RawComment RC(SourceMgr, Comment, false,
10716fd7d3067dd06584ef3940e88e31fea1a0e83588Dmitri Gribenko                LangOpts.CommentOpts.ParseAllComments);
10729dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko  if (RC.isAlmostTrailingComment()) {
10739dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    SourceRange MagicMarkerRange(Comment.getBegin(),
10749dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko                                 Comment.getBegin().getLocWithOffset(3));
10759dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    StringRef MagicMarkerText;
10769dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    switch (RC.getKind()) {
1077c50a0e3900f1b44503be48457508af372f4dd05aAbramo Bagnara    case RawComment::RCK_OrdinaryBCPL:
10789dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      MagicMarkerText = "///<";
10799dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      break;
1080c50a0e3900f1b44503be48457508af372f4dd05aAbramo Bagnara    case RawComment::RCK_OrdinaryC:
10819dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      MagicMarkerText = "/**<";
10829dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      break;
10839dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    default:
10849dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      llvm_unreachable("if this is an almost Doxygen comment, "
10859dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko                       "it should be ordinary");
10869dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    }
10879dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko    Diag(Comment.getBegin(), diag::warn_not_a_doxygen_trailing_member_comment) <<
10889dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko      FixItHint::CreateReplacement(MagicMarkerRange, MagicMarkerText);
10899dda4746867a747c1c3421d8a04a1b666aeb5809Dmitri Gribenko  }
1090aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  Context.addComment(RC);
1091aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}
1092aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
109376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall// Pin this vtable to this file.
109476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalSemaSource::~ExternalSemaSource() {}
1095f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
10965ac4b6917aa34fae6da64036539023a6155a3d48Douglas Gregorvoid ExternalSemaSource::ReadMethodPool(Selector Sel) { }
10978c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl
1098d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregorvoid ExternalSemaSource::ReadKnownNamespaces(
10990116a40116616701282e12f3d92cf2949102d76aMichael Gottesman                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
1100d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor}
1101d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor
1102cd0655b17249c4c4908ca91462657f62285017e6Nick Lewyckyvoid ExternalSemaSource::ReadUndefinedButUsed(
1103995e26b0523ac8e3b6199a509b871b81fa5df6eeNick Lewycky                       llvm::DenseMap<NamedDecl *, SourceLocation> &Undefined) {
110401a41140cd8ec9475ed0c33384310fbdd3b6de11Nick Lewycky}
110501a41140cd8ec9475ed0c33384310fbdd3b6de11Nick Lewycky
11065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid PrettyDeclStackTraceEntry::print(raw_ostream &OS) const {
1107f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  SourceLocation Loc = this->Loc;
1108f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
1109f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (Loc.isValid()) {
1110f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Loc.print(OS, S.getSourceManager());
1111f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    OS << ": ";
1112f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
1113f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << Message;
1114f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
1115f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (TheDecl && isa<NamedDecl>(TheDecl)) {
1116f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString();
1117f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    if (!Name.empty())
1118f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      OS << " '" << Name << '\'';
1119f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
1120f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
1121f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << '\n';
1122f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall}
1123c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1124c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \brief Figure out if an expression could be turned into a call.
1125c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1126c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// Use this when trying to recover from an error where the programmer may have
1127c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// written just the name of a function instead of actually calling it.
1128c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1129c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param E - The expression to examine.
1130c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param ZeroArgCallReturnTy - If the expression can be turned into a call
1131c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  with no arguments, this parameter is set to the type returned by such a
1132c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  call; otherwise, it is set to an empty QualType.
11336dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// \param OverloadSet - If the expression is an overloaded function
1134c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  name, this parameter is populated with the decls of the various overloads.
1135c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikiebool Sema::tryExprAsCall(Expr &E, QualType &ZeroArgCallReturnTy,
1136c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie                         UnresolvedSetImpl &OverloadSet) {
1137c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  ZeroArgCallReturnTy = QualType();
11386dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  OverloadSet.clear();
11396dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1140c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie  const OverloadExpr *Overloads = NULL;
1141c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  bool IsMemExpr = false;
11426dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (E.getType() == Context.OverloadTy) {
11436dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    OverloadExpr::FindResult FR = OverloadExpr::find(const_cast<Expr*>(&E));
11446dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1145c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie    // Ignore overloads that are pointer-to-member constants.
1146c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie    if (FR.HasFormOfMemberPointer)
1147c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie      return false;
1148c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie
1149c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie    Overloads = FR.Expression;
1150c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie  } else if (E.getType() == Context.BoundMemberTy) {
1151c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie    Overloads = dyn_cast<UnresolvedMemberExpr>(E.IgnoreParens());
1152c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    IsMemExpr = true;
1153c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie  }
1154c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie
1155c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  bool Ambiguous = false;
1156c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie
1157c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie  if (Overloads) {
1158c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    for (OverloadExpr::decls_iterator it = Overloads->decls_begin(),
1159c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay         DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) {
11606dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      OverloadSet.addDecl(*it);
11616dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1162c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie      // Check whether the function is a non-template, non-member which takes no
11636dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      // arguments.
1164c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie      if (IsMemExpr)
1165c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie        continue;
11666dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      if (const FunctionDecl *OverloadDecl
11676dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall            = dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl())) {
1168c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie        if (OverloadDecl->getMinRequiredArguments() == 0) {
1169c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie          if (!ZeroArgCallReturnTy.isNull() && !Ambiguous) {
1170c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie            ZeroArgCallReturnTy = QualType();
1171c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie            Ambiguous = true;
1172c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie          } else
1173c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie            ZeroArgCallReturnTy = OverloadDecl->getResultType();
1174c2fe81898b1b3b948791ca4ababd3d495601f22aDavid Blaikie        }
1175c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      }
1176c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
11776dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1178c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    // If it's not a member, use better machinery to try to resolve the call
1179c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    if (!IsMemExpr)
1180c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie      return !ZeroArgCallReturnTy.isNull();
1181c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  }
1182c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie
1183c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  // Attempt to call the member with no arguments - this will correctly handle
1184c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  // member templates with defaults/deduction of template arguments, overloads
1185c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  // with default arguments, etc.
1186d739c4ecf7d7f2222f6f1c8de9e234d1c8a0468cEli Friedman  if (IsMemExpr && !E.isTypeDependent()) {
1187c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
1188c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    getDiagnostics().setSuppressAllDiagnostics(true);
1189c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    ExprResult R = BuildCallToMemberFunction(NULL, &E, SourceLocation(), None,
1190c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie                                             SourceLocation());
1191c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    getDiagnostics().setSuppressAllDiagnostics(Suppress);
1192c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    if (R.isUsable()) {
1193c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie      ZeroArgCallReturnTy = R.get()->getType();
1194c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie      return true;
1195c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    }
1196c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie    return false;
1197c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
1198c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
11996dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) {
1200c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
1201c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      if (Fun->getMinRequiredArguments() == 0)
1202c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay        ZeroArgCallReturnTy = Fun->getResultType();
1203c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      return true;
1204c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
1205c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
1206c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1207c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  // We don't have an expression that's convenient to get a FunctionDecl from,
1208c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  // but we can at least check if the type is "function of 0 arguments".
1209c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  QualType ExprTy = E.getType();
1210c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  const FunctionType *FunTy = NULL;
12119389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay  QualType PointeeTy = ExprTy->getPointeeType();
12129389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay  if (!PointeeTy.isNull())
12139389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay    FunTy = PointeeTy->getAs<FunctionType>();
1214c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (!FunTy)
1215c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    FunTy = ExprTy->getAs<FunctionType>();
1216c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1217c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (const FunctionProtoType *FPT =
1218c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      dyn_cast_or_null<FunctionProtoType>(FunTy)) {
1219c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (FPT->getNumArgs() == 0)
1220c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      ZeroArgCallReturnTy = FunTy->getResultType();
1221c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    return true;
1222c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
1223c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  return false;
1224c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay}
1225c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
1226c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \brief Give notes for a set of overloads.
1227c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1228c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie/// A companion to tryExprAsCall. In cases when the name that the programmer
1229c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// wrote was an overloaded function, we may be able to make some guesses about
1230c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// plausible overloads based on their return types; such guesses can be handed
1231c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// off to this method to be emitted as notes.
1232c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
1233c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param Overloads - The overloads to note.
1234c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param FinalNoteLoc - If we've suppressed printing some overloads due to
1235c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  -fshow-overloads=best, this is the location to attach to the note about too
1236c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  many candidates. Typically this will be the location of the original
1237c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  ill-formed expression.
12386dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads,
12396dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                          const SourceLocation FinalNoteLoc) {
1240c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  int ShownOverloads = 0;
1241c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  int SuppressedOverloads = 0;
1242c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  for (UnresolvedSetImpl::iterator It = Overloads.begin(),
1243c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay       DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
1244c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // FIXME: Magic number for max shown overloads stolen from
1245c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // OverloadCandidateSet::NoteCandidates.
1246dc7b641574a733624489bd87fc7061771edf2113Douglas Gregor    if (ShownOverloads >= 4 && S.Diags.getShowOverloads() == Ovl_Best) {
1247c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      ++SuppressedOverloads;
1248c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      continue;
1249c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
12506dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
12516dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    NamedDecl *Fn = (*It)->getUnderlyingDecl();
12529c0e1ec7b3afd833c1b958ce2aeedff71c7eb4c5Abramo Bagnara    S.Diag(Fn->getLocation(), diag::note_possible_target_of_call);
1253c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    ++ShownOverloads;
1254c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
12556dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1256c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (SuppressedOverloads)
12576dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    S.Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates)
12586dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << SuppressedOverloads;
12596dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
12606dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
12616dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic void notePlausibleOverloads(Sema &S, SourceLocation Loc,
12626dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                   const UnresolvedSetImpl &Overloads,
12636dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                   bool (*IsPlausibleResult)(QualType)) {
12646dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (!IsPlausibleResult)
12656dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return noteOverloads(S, Overloads, Loc);
12666dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
12676dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  UnresolvedSet<2> PlausibleOverloads;
12686dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  for (OverloadExpr::decls_iterator It = Overloads.begin(),
12696dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall         DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
12706dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It);
12716dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    QualType OverloadResultTy = OverloadDecl->getResultType();
12726dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    if (IsPlausibleResult(OverloadResultTy))
12736dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      PlausibleOverloads.addDecl(It.getDecl());
12746dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  }
12756dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  noteOverloads(S, PlausibleOverloads, Loc);
12766dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
12776dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
12786dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// Determine whether the given expression can be called by just
12796dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// putting parentheses after it.  Notably, expressions with unary
12806dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// operators can't be because the unary operator will start parsing
12816dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall/// outside the call.
12826dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool IsCallableWithAppend(Expr *E) {
12836dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  E = E->IgnoreImplicit();
12846dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return (!isa<CStyleCastExpr>(E) &&
12856dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<UnaryOperator>(E) &&
12866dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<BinaryOperator>(E) &&
12876dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          !isa<CXXOperatorCallExpr>(E));
12886dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
12896dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
12906dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallbool Sema::tryToRecoverWithCall(ExprResult &E, const PartialDiagnostic &PD,
12916dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                bool ForceComplain,
12926dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                                bool (*IsPlausibleResult)(QualType)) {
12936dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  SourceLocation Loc = E.get()->getExprLoc();
12946dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  SourceRange Range = E.get()->getSourceRange();
12956dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
12966dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  QualType ZeroArgCallTy;
12976dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  UnresolvedSet<4> Overloads;
1298c8fa525b5b81eeb7a62294dd3218ca2a6ccf0a6aDavid Blaikie  if (tryExprAsCall(*E.get(), ZeroArgCallTy, Overloads) &&
12996dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      !ZeroArgCallTy.isNull() &&
13006dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      (!IsPlausibleResult || IsPlausibleResult(ZeroArgCallTy))) {
13016dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // At this point, we know E is potentially callable with 0
13026dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // arguments and that it returns something of a reasonable type,
13036dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // so we can emit a fixit and carry on pretending that E was
13046dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // actually a CallExpr.
13056dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    SourceLocation ParenInsertionLoc =
13066dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      PP.getLocForEndOfToken(Range.getEnd());
13070116a40116616701282e12f3d92cf2949102d76aMichael Gottesman    Diag(Loc, PD)
13086dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << /*zero-arg*/ 1 << Range
13096dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall      << (IsCallableWithAppend(E.get())
13106dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          ? FixItHint::CreateInsertion(ParenInsertionLoc, "()")
13116dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall          : FixItHint());
13126dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
13136dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
13146dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // FIXME: Try this before emitting the fixit, and suppress diagnostics
13156dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    // while doing so.
13166dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    E = ActOnCallExpr(0, E.take(), ParenInsertionLoc,
131762ed889272d7e9da8e367d8682fdcdeeec0d83b5Dmitri Gribenko                      None, ParenInsertionLoc.getLocWithOffset(1));
13186dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return true;
13196dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  }
13206dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
13216dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (!ForceComplain) return false;
13226dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
13236dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  Diag(Loc, PD) << /*not zero-arg*/ 0 << Range;
13246dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
13256dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  E = ExprError();
13266dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return true;
1327c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay}
132857f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis
132957f8da506a0db208a936e26a8cb77267f638b26bArgyrios KyrtzidisIdentifierInfo *Sema::getSuperIdentifier() const {
133057f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis  if (!Ident_super)
133157f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis    Ident_super = &Context.Idents.get("super");
133257f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis  return Ident_super;
133357f8da506a0db208a936e26a8cb77267f638b26bArgyrios Kyrtzidis}
13346afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj
1335cac18add73d095eaab600aefe27ea7174aec4922Nico WeberIdentifierInfo *Sema::getFloat128Identifier() const {
1336cac18add73d095eaab600aefe27ea7174aec4922Nico Weber  if (!Ident___float128)
1337cac18add73d095eaab600aefe27ea7174aec4922Nico Weber    Ident___float128 = &Context.Idents.get("__float128");
1338cac18add73d095eaab600aefe27ea7174aec4922Nico Weber  return Ident___float128;
1339cac18add73d095eaab600aefe27ea7174aec4922Nico Weber}
1340cac18add73d095eaab600aefe27ea7174aec4922Nico Weber
13416afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Sirajvoid Sema::PushCapturedRegionScope(Scope *S, CapturedDecl *CD, RecordDecl *RD,
13423a2f91280a49f4747063f983dc6a3296bd9359d2Ben Langmuir                                   CapturedRegionKind K) {
13438c045ace381972f41d385b0a661ccf172834f459Ben Langmuir  CapturingScopeInfo *CSI = new CapturedRegionScopeInfo(getDiagnostics(), S, CD, RD,
13448c045ace381972f41d385b0a661ccf172834f459Ben Langmuir                                                        CD->getContextParam(), K);
13456afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  CSI->ReturnType = Context.VoidTy;
13466afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  FunctionScopes.push_back(CSI);
13476afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj}
13486afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj
13496afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. SirajCapturedRegionScopeInfo *Sema::getCurCapturedRegion() {
13506afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  if (FunctionScopes.empty())
13516afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj    return 0;
13526afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj
13536afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  return dyn_cast<CapturedRegionScopeInfo>(FunctionScopes.back());
13546afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj}
1355