Sema.cpp revision f85e193739c953358c865005855253af4f68a497
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements the actions class which performs semantic analysis and
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// builds an AST out of a parse stream.
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
159c3087b0b0bea2fd782205c1274ebfc4290265e0John McCall#include "clang/Sema/SemaInternal.h"
169c3087b0b0bea2fd782205c1274ebfc4290265e0John McCall#include "clang/Sema/DelayedDiagnostic.h"
1782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov#include "TargetAttributesSema.h"
18e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn#include "llvm/ADT/DenseMap.h"
19e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl#include "llvm/ADT/SmallSet.h"
20680523a91dd3351389667c8de17121ba7ae82673John McCall#include "llvm/ADT/APFloat.h"
215f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/CXXFieldCollector.h"
229b623639378d53a675921ddfa7316034d571881eDouglas Gregor#include "clang/Sema/TemplateDeduction.h"
2376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall#include "clang/Sema/ExternalSemaSource.h"
248c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl#include "clang/Sema/ObjCMethodList.h"
25f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
265f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/Scope.h"
27781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "clang/Sema/ScopeInfo.h"
2846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor#include "clang/Sema/SemaConsumer.h"
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
3079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/ASTDiagnostic.h"
31384aff8b94bb0d1ad6c5667b90621e5699815bb2John McCall#include "clang/AST/DeclCXX.h"
32c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
33e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
34c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay#include "clang/AST/ExprCXX.h"
35ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner#include "clang/AST/StmtCXX.h"
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Preprocessor.h"
3791a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
384d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner#include "clang/Basic/TargetInfo.h"
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
40781472fe99a120098c631b0cbe33c89f8cef5e70John McCallusing namespace sema;
419ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
429ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorFunctionScopeInfo::~FunctionScopeInfo() { }
439ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
448fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidisvoid FunctionScopeInfo::Clear() {
45b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasBranchProtectedScope = false;
46b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasBranchIntoScope = false;
47b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasIndirectGoto = false;
48b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
499ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  SwitchStack.clear();
505077c3876beeaed32280af88244e8050078619a8Douglas Gregor  Returns.clear();
518fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  ErrorTrap.reset();
52351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  PossiblyUnreachableDiags.clear();
539ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
549ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
559ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo::~BlockScopeInfo() { }
569ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
57c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregorvoid Sema::ActOnTranslationUnitScope(Scope *S) {
588ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroff  TUScope = S;
5944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  PushDeclContext(S, Context.getTranslationUnitDecl());
601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall  VAListTagName = PP.getIdentifierInfo("__va_list_tag");
62c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall
63c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl  if (!Context.isInt128Installed() && // May be set by ASTReader.
64006113841bdae1edb77aef75ba1ffdf2e55a3094Argyrios Kyrtzidis      PP.getTargetInfo().getPointerWidth(0) >= 64) {
65a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *TInfo;
66ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
674d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    // Install [u]int128_t for 64-bit targets.
68a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
694d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
704d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          SourceLocation(),
71344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                          SourceLocation(),
724d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          &Context.Idents.get("__int128_t"),
73a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                          TInfo), TUScope);
74ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
75a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
764d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
774d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          SourceLocation(),
78344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                          SourceLocation(),
794d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          &Context.Idents.get("__uint128_t"),
80a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                          TInfo), TUScope);
81006113841bdae1edb77aef75ba1ffdf2e55a3094Argyrios Kyrtzidis    Context.setInt128Installed();
824d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner  }
831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
852ae34ed8caf90ca4044feca07811561053c08666Chris Lattner  if (!PP.getLangOptions().ObjC1) return;
861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl  // Built-in ObjC types may already be set by ASTReader (hence isNull checks).
88319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCSelType().isNull()) {
8913dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian    // Create the built-in typedef for 'SEL'.
9004765ac135e0c4e6b78651c2a287d80a32b2b8b9Fariborz Jahanian    QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
91a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
92ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *SelTypedef
93344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara      = TypedefDecl::Create(Context, CurContext,
94344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                            SourceLocation(), SourceLocation(),
95ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("SEL"), SelInfo);
96319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    PushOnScopeChains(SelTypedef, TUScope);
97319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
98369a3bd9979cf529eed529aa037de713c213e47dFariborz Jahanian    Context.ObjCSelRedefinitionType = Context.getObjCSelType();
99319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
100319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
1016ee1f9c0142e0c374f32fc86312630cd901247cfChris Lattner  // Synthesize "@class Protocol;
102319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCProtoType().isNull()) {
103319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    ObjCInterfaceDecl *ProtocolDecl =
104319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor      ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
1051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                &Context.Idents.get("Protocol"),
106deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor                                SourceLocation(), true);
107319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
10810324db994455a9a1520be6cfe5bb29685cde141Fariborz Jahanian    PushOnScopeChains(ProtocolDecl, TUScope, false);
109319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
110de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Create the built-in typedef for 'id'.
111319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCIdType().isNull()) {
112c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0);
113c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = Context.getObjCObjectPointerType(T);
114c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T);
115ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *IdTypedef
116344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara      = TypedefDecl::Create(Context, CurContext,
117344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                            SourceLocation(), SourceLocation(),
118ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("id"), IdInfo);
119319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    PushOnScopeChains(IdTypedef, TUScope);
120319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
1210f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    Context.ObjCIdRedefinitionType = Context.getObjCIdType();
122319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
123de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Create the built-in typedef for 'Class'.
12414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (Context.getObjCClassType().isNull()) {
125c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0);
126c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = Context.getObjCObjectPointerType(T);
127c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T);
128ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *ClassTypedef
129344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara      = TypedefDecl::Create(Context, CurContext,
130344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                            SourceLocation(), SourceLocation(),
131ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("Class"), ClassInfo);
13214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    PushOnScopeChains(ClassTypedef, TUScope);
13314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
1340f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    Context.ObjCClassRedefinitionType = Context.getObjCClassType();
13546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  }
136b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
1373b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff
138f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas GregorSema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
1393a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           bool CompleteTranslationUnit,
1403a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           CodeCompleteConsumer *CodeCompleter)
141321b8179afaf803dcc56b2a19f7b0891a03c92c8Peter Collingbourne  : TheTargetAttributesSema(0), FPFeatures(pp.getLangOptions()),
14282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
1443a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar    ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
14562c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian    PackContext(0), MSStructPragmaOn(false), VisContext(0),
146f85e193739c953358c865005855253af4f68a497John McCall    ExprNeedsCleanups(0), LateTemplateParser(0), OpaqueParser(0),
1474eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    IdResolver(pp.getLangOptions()), CXXTypeInfoDecl(0), MSVCGuidDecl(0),
148b7566d8346ed8040a5cb7cefe96bd6ccbf015595Bill Wendling    GlobalNewDeleteDeclared(false),
14948dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor    CompleteTranslationUnit(CompleteTranslationUnit),
1501eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor    NumSFINAEErrors(0), SuppressAccessChecking(false),
1511eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor    AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false),
1528491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor    NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1),
1538491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor    CurrentInstantiationScope(0), TyposCorrected(0),
154b7566d8346ed8040a5cb7cefe96bd6ccbf015595Bill Wendling    AnalysisWarnings(*this)
155f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor{
1563b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff  TUScope = 0;
15707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis  if (getLangOptions().CPlusPlus)
15807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    FieldCollector.reset(new CXXFieldCollector());
1591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16022caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  // Tell diagnostics how to render things from the AST library.
16179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
16279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                       &Context);
1632afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor
1642afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor  ExprEvalContexts.push_back(
165f85e193739c953358c865005855253af4f68a497John McCall        ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0, false));
166781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
1678fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(Diags));
16846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor}
16946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
17046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregorvoid Sema::Initialize() {
17146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the AST consumer about this Sema object.
17246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  Consumer.Initialize(Context);
17346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
17446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // FIXME: Isn't this redundant with the initialization above?
17546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
17646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->InitializeSema(*this);
17746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
17846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the external Sema source about this Sema object.
17946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
18046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor      = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
18146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->InitializeSema(*this);
1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovSema::~Sema() {
18582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (PackContext) FreePackedContext();
186aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  if (VisContext) FreeVisContext();
18782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  delete TheTargetAttributesSema;
18862c9258f4a71569a66d805fc7776526a2c76b34eFariborz Jahanian  MSStructPragmaOn = false;
189781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  // Kill all the active scopes.
190781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I)
191781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[I];
192781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1)
193781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[0];
19446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
19546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the SemaConsumer to forget about us; we're going out of scope.
19646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
19746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->ForgetSema();
19846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
19946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Detach from the external Sema source.
20046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
201914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor        = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
20246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->ForgetSema();
20382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
20482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
205f85e193739c953358c865005855253af4f68a497John McCall
206f85e193739c953358c865005855253af4f68a497John McCall/// makeUnavailableInSystemHeader - There is an error in the current
207f85e193739c953358c865005855253af4f68a497John McCall/// context.  If we're still in a system header, and we can plausibly
208f85e193739c953358c865005855253af4f68a497John McCall/// make the relevant declaration unavailable instead of erroring, do
209f85e193739c953358c865005855253af4f68a497John McCall/// so and return true.
210f85e193739c953358c865005855253af4f68a497John McCallbool Sema::makeUnavailableInSystemHeader(SourceLocation loc,
211f85e193739c953358c865005855253af4f68a497John McCall                                         llvm::StringRef msg) {
212f85e193739c953358c865005855253af4f68a497John McCall  // If we're not in a function, it's an error.
213f85e193739c953358c865005855253af4f68a497John McCall  FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext);
214f85e193739c953358c865005855253af4f68a497John McCall  if (!fn) return false;
215f85e193739c953358c865005855253af4f68a497John McCall
216f85e193739c953358c865005855253af4f68a497John McCall  // If we're in template instantiation, it's an error.
217f85e193739c953358c865005855253af4f68a497John McCall  if (!ActiveTemplateInstantiations.empty())
218f85e193739c953358c865005855253af4f68a497John McCall    return false;
219f85e193739c953358c865005855253af4f68a497John McCall
220f85e193739c953358c865005855253af4f68a497John McCall  // If that function's not in a system header, it's an error.
221f85e193739c953358c865005855253af4f68a497John McCall  if (!Context.getSourceManager().isInSystemHeader(loc))
222f85e193739c953358c865005855253af4f68a497John McCall    return false;
223f85e193739c953358c865005855253af4f68a497John McCall
224f85e193739c953358c865005855253af4f68a497John McCall  // If the function is already unavailable, it's not an error.
225f85e193739c953358c865005855253af4f68a497John McCall  if (fn->hasAttr<UnavailableAttr>()) return true;
226f85e193739c953358c865005855253af4f68a497John McCall
227f85e193739c953358c865005855253af4f68a497John McCall  fn->addAttr(new (Context) UnavailableAttr(loc, Context, msg));
228f85e193739c953358c865005855253af4f68a497John McCall  return true;
229f85e193739c953358c865005855253af4f68a497John McCall}
230f85e193739c953358c865005855253af4f68a497John McCall
23158a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian RedlASTMutationListener *Sema::getASTMutationListener() const {
23258a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl  return getASTConsumer().GetASTMutationListener();
23358a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl}
23458a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl
2351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
2361e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner/// If there is already an implicit cast, merge into the existing one.
237906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl/// The result is of the given category.
238429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn WiegleyExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
239429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                   CastKind Kind, ExprValueKind VK,
240f85e193739c953358c865005855253af4f68a497John McCall                                   const CXXCastPath *BasePath,
241f85e193739c953358c865005855253af4f68a497John McCall                                   CheckedConversionKind CCK) {
242429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  QualType ExprTy = Context.getCanonicalType(E->getType());
2433a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType TypeTy = Context.getCanonicalType(Ty);
2441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2453a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  if (ExprTy == TypeTy)
246429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    return Owned(E);
2471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
248f85e193739c953358c865005855253af4f68a497John McCall  if (getLangOptions().ObjCAutoRefCount)
249f85e193739c953358c865005855253af4f68a497John McCall    CheckObjCARCConversion(SourceRange(), Ty, E, CCK);
250f85e193739c953358c865005855253af4f68a497John McCall
2516fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // If this is a derived-to-base cast to a through a virtual base, we
2526fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // need a vtable.
2532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  if (Kind == CK_DerivedToBase &&
254f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall      BasePathInvolvesVirtualBase(*BasePath)) {
255429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    QualType T = E->getType();
2566fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const PointerType *Pointer = T->getAs<PointerType>())
2576fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      T = Pointer->getPointeeType();
2586fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const RecordType *RecordTy = T->getAs<RecordType>())
259429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      MarkVTableUsed(E->getLocStart(),
2606fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor                     cast<CXXRecordDecl>(RecordTy->getDecl()));
2616fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  }
2626fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor
263429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
264f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
2654c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      ImpCast->setType(Ty);
2665baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall      ImpCast->setValueKind(VK);
267429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return Owned(E);
2684c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson    }
2694c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson  }
2704c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson
271429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  return Owned(ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK));
272906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl}
273906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
274737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara/// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
275737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara/// to the conversion from scalar type ScalarTy to the Boolean type.
276737d5447b5d20633992ee5388eca5270c28c8ae7Abramo BagnaraCastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) {
277737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  switch (ScalarTy->getScalarTypeKind()) {
278737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Bool: return CK_NoOp;
279737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Pointer: return CK_PointerToBoolean;
280737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_MemberPointer: return CK_MemberPointerToBoolean;
281737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Integral: return CK_IntegralToBoolean;
282737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_Floating: return CK_FloatingToBoolean;
283737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean;
284737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean;
285737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  }
286737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara  return CK_Invalid;
287737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara}
288737d5447b5d20633992ee5388eca5270c28c8ae7Abramo Bagnara
2895baba9d98364a3525d6afa15a04cdad82fd6dd30John McCallExprValueKind Sema::CastCategory(Expr *E) {
290906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  Expr::Classification Classification = E->Classify(Context);
2915baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  return Classification.isRValue() ? VK_RValue :
2925baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall      (Classification.isLValue() ? VK_LValue : VK_XValue);
2931e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner}
2941e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner
295bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis/// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
296bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidisstatic bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
297bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (D->isUsed())
298bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    return true;
299bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
300bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
301bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
302bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
303bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const FunctionDecl *DeclToCheck;
304bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (FD->hasBody(DeclToCheck))
305bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
306bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
307bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
308bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
309bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    DeclToCheck = FD->getMostRecentDeclaration();
310bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != FD)
311bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
312bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
313bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
314bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
315bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
316bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
317bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const VarDecl *DeclToCheck = VD->getDefinition();
318bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck)
319bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
320bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
321bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
322bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
323bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    DeclToCheck = VD->getMostRecentDeclaration();
324bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != VD)
325bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
326bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
327bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
328bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  return false;
329bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis}
330bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
33115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallnamespace {
33215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  struct UndefinedInternal {
33315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    NamedDecl *decl;
33415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    FullSourceLoc useLoc;
33515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
33615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    UndefinedInternal(NamedDecl *decl, FullSourceLoc useLoc)
33715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      : decl(decl), useLoc(useLoc) {}
33815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  };
33915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
34015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  bool operator<(const UndefinedInternal &l, const UndefinedInternal &r) {
34115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    return l.useLoc.isBeforeInTranslationUnitThan(r.useLoc);
34215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
34315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
34415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
34515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall/// checkUndefinedInternals - Check for undefined objects with internal linkage.
34615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallstatic void checkUndefinedInternals(Sema &S) {
34715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (S.UndefinedInternals.empty()) return;
34815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
34915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Collect all the still-undefined entities with internal linkage.
35015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  llvm::SmallVector<UndefinedInternal, 16> undefined;
35115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  for (llvm::DenseMap<NamedDecl*,SourceLocation>::iterator
35215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall         i = S.UndefinedInternals.begin(), e = S.UndefinedInternals.end();
35315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall       i != e; ++i) {
35415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    NamedDecl *decl = i->first;
35515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
35615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // Ignore attributes that have become invalid.
35715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    if (decl->isInvalidDecl()) continue;
35815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
35915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // __attribute__((weakref)) is basically a definition.
36015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    if (decl->hasAttr<WeakRefAttr>()) continue;
36115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
36215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) {
36315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      if (fn->isPure() || fn->hasBody())
36415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        continue;
36515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    } else {
36615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      if (cast<VarDecl>(decl)->hasDefinition() != VarDecl::DeclarationOnly)
36715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        continue;
36815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    }
36915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
37015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // We build a FullSourceLoc so that we can sort with array_pod_sort.
37115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    FullSourceLoc loc(i->second, S.Context.getSourceManager());
37215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    undefined.push_back(UndefinedInternal(decl, loc));
37315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
37415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
37515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (undefined.empty()) return;
37615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
37715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Sort (in order of use site) so that we're not (as) dependent on
37815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // the iteration order through an llvm::DenseMap.
37915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  llvm::array_pod_sort(undefined.begin(), undefined.end());
38015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
38115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  for (llvm::SmallVectorImpl<UndefinedInternal>::iterator
38215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall         i = undefined.begin(), e = undefined.end(); i != e; ++i) {
38315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    NamedDecl *decl = i->decl;
38415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    S.Diag(decl->getLocation(), diag::warn_undefined_internal)
38515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      << isa<VarDecl>(decl) << decl;
38615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    S.Diag(i->useLoc, diag::note_used_here);
38715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
38815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
38915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
3909299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// ActOnEndOfTranslationUnit - This is called at the very end of the
3919299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// translation unit when EOF is reached and all but the top-level scope is
3929299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// popped.
3930e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidisvoid Sema::ActOnEndOfTranslationUnit() {
3940e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  // At PCH writing, implicit instantiations and VTable handling info are
3950e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  // stored and performed when the PCH is included.
3962a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (CompleteTranslationUnit) {
397aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // If any dynamic classes have their key function defined within
398aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // this translation unit, then those vtables are considered "used" and must
399aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // be emitted.
400aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
401a5c6c2a84cde5c9b8f8ec0610a9f89ffd54f44eeAnders Carlsson      assert(!DynamicClasses[I]->isDependentType() &&
402a5c6c2a84cde5c9b8f8ec0610a9f89ffd54f44eeAnders Carlsson             "Should not see dependent types here!");
403aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth      if (const CXXMethodDecl *KeyFunction
404aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth          = Context.getKeyFunction(DynamicClasses[I])) {
405aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth        const FunctionDecl *Definition = 0;
406aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth        if (KeyFunction->hasBody(Definition))
407aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth          MarkVTableUsed(Definition->getLocation(), DynamicClasses[I], true);
408aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth      }
409aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    }
410aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth
4118155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // If DefinedUsedVTables ends up marking any virtual member functions it
4128155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // might lead to more pending template instantiations, which we then need
4138155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // to instantiate.
4148155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
4158155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
4168155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // C++: Perform implicit template instantiations.
4178155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    //
4188155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // FIXME: When we perform these implicit instantiations, we do not
4198155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // carefully keep track of the point of instantiation (C++ [temp.point]).
4208155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // This means that name lookup that occurs within the template
4218155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // instantiation will always happen at the end of the translation unit,
4228155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // so it will find some names that should not be found. Although this is
4238155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // common behavior for C++ compilers, it is technically wrong. In the
4248155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // future, we either need to be able to filter the results of name lookup
4258155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // or we need to perform template instantiations earlier.
4268155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    PerformPendingInstantiations();
4272a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
428d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson
42949b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Remove file scoped decls that turned out to be used.
430bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(),
431bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                                             UnusedFileScopedDecls.end(),
432bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                              std::bind1st(std::ptr_fun(ShouldRemoveFromUnused),
433bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                                           this)),
43449b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis                              UnusedFileScopedDecls.end());
43547268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor
43687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  if (!CompleteTranslationUnit) {
43787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    TUScope = 0;
43872b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    return;
43987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
44072b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis
44163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // Check for #pragma weak identifiers that were never declared
44263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // FIXME: This will cause diagnostics to be emitted in a non-determinstic
44363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // order!  Iterating over a densemap like this is bad.
444e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
44563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       I = WeakUndeclaredIdentifiers.begin(),
44663d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
44763d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    if (I->second.getUsed()) continue;
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
45063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      << I->first;
451e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  }
452e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn
453275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  // C99 6.9.2p2:
454275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   A declaration of an identifier for an object that has file
455275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   scope without an initializer, and without a storage-class
456275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   specifier or with the storage-class specifier static,
457275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   constitutes a tentative definition. If a translation unit
458275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   contains one or more tentative definitions for an identifier,
459275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   and the translation unit contains no external definition for
460275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   that identifier, then the behavior is exactly as if the
461275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit contains a file scope declaration of that
462275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   identifier, with the composite type as of the end of the
463275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit, with an initializer equal to 0.
464e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  llvm::SmallSet<VarDecl *, 32> Seen;
465e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) {
466e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    VarDecl *VD = TentativeDefinitions[i]->getActingDefinition();
467e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
468e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // If the tentative definition was completed, getActingDefinition() returns
469e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // null. If we've already seen this variable before, insert()'s second
470e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // return value is false.
471e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
472b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      continue;
473b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const IncompleteArrayType *ArrayT
475b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        = Context.getAsIncompleteArrayType(VD->getType())) {
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (RequireCompleteType(VD->getLocation(),
477b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                              ArrayT->getElementType(),
47863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner                              diag::err_tentative_def_incomplete_type_arr)) {
479b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        VD->setInvalidDecl();
48063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner        continue;
481275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor      }
4821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      // Set the length of the array to 1 (C99 6.9.2p5).
48463d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
48563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
48646a617a792bfab0d9b1e057371ea3b9540802226John McCall      QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
48746a617a792bfab0d9b1e057371ea3b9540802226John McCall                                                One, ArrayType::Normal, 0);
48863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      VD->setType(T);
4891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
490b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                                   diag::err_tentative_def_incomplete_type))
491b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      VD->setInvalidDecl();
492b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
493b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    // Notify the consumer that we've completed a tentative definition.
494b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    if (!VD->isInvalidDecl())
495b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      Consumer.CompleteTentativeDefinition(VD);
496b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
497275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  }
49843f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis
499c1598700010cea9364a58a65e967b0b56361b6aaSean Hunt  if (LangOpts.CPlusPlus0x &&
500c1598700010cea9364a58a65e967b0b56361b6aaSean Hunt      Diags.getDiagnosticLevel(diag::warn_delegating_ctor_cycle,
501c1598700010cea9364a58a65e967b0b56361b6aaSean Hunt                               SourceLocation())
502c1598700010cea9364a58a65e967b0b56361b6aaSean Hunt        != Diagnostic::Ignored)
503fe57eef44cc80e5bb51e4f484835be08b8d84256Sean Hunt    CheckDelegatingCtorCycles();
504fe57eef44cc80e5bb51e4f484835be08b8d84256Sean Hunt
50543f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  // If there were errors, disable 'unused' warnings since they will mostly be
50643f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  // noise.
50743f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis  if (!Diags.hasErrorOccurred()) {
50843f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis    // Output warning for unused file scoped decls.
50943f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis    for (llvm::SmallVectorImpl<const DeclaratorDecl*>::iterator
51043f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis           I = UnusedFileScopedDecls.begin(),
51143f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis           E = UnusedFileScopedDecls.end(); I != E; ++I) {
51243f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
51343f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        const FunctionDecl *DiagD;
51443f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        if (!FD->hasBody(DiagD))
51543f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis          DiagD = FD;
51648b89590f61575cbf365ba996a2bd1ba1561a4abArgyrios Kyrtzidis        if (DiagD->isDeleted())
51748b89590f61575cbf365ba996a2bd1ba1561a4abArgyrios Kyrtzidis          continue; // Deleted functions are supposed to be unused.
5186b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        if (DiagD->isReferenced()) {
5196b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          if (isa<CXXMethodDecl>(DiagD))
5206b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis            Diag(DiagD->getLocation(), diag::warn_unneeded_member_function)
5216b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                  << DiagD->getDeclName();
5226b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          else
5236b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis            Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
5246b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                  << /*function*/0 << DiagD->getDeclName();
5256b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        } else {
5266b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(),
5276b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis               isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function
5286b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                                         : diag::warn_unused_function)
5296b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << DiagD->getDeclName();
5306b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        }
53143f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      } else {
53243f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
53343f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis        if (!DiagD)
53443f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis          DiagD = cast<VarDecl>(*I);
5356b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        if (DiagD->isReferenced()) {
5366b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
5376b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << /*variable*/1 << DiagD->getDeclName();
5386b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        } else {
5396b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis          Diag(DiagD->getLocation(), diag::warn_unused_variable)
5406b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis                << DiagD->getDeclName();
5416b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis        }
54243f0a7c8e06e55092b43d4dd46fe09a4d57298e9Argyrios Kyrtzidis      }
543bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    }
54415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
54515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    checkUndefinedInternals(*this);
54649b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  }
54787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
548483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // Check we've noticed that we're no longer parsing the initializer for every
549483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // variable. If we miss cases, then at best we have a performance issue and
550483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  // at worst a rejects-valid bug.
551483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  assert(ParsingInitForAutoVars.empty() &&
552483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith         "Didn't unmark var as having its initializer parsed");
553483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
55487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  TUScope = 0;
5559299f3fa85796613cc787a2062c9562d07c8613eChris Lattner}
5569299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
5579299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper functions.
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5628517d9b731f065cdfc55ec0f3ddf5d564d988648Anders CarlssonDeclContext *Sema::getFunctionLevelDeclContext() {
563db0ee1da16e9dbec19b144c9cd96ee9f55fe0c53John McCall  DeclContext *DC = CurContext;
5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
565d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor  while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
5668517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson    DC = DC->getParent();
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5688517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  return DC;
5698517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson}
5708517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson
571371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// getCurFunctionDecl - If inside of a function body, this returns a pointer
572371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// to the function decl for the function being parsed.  If we're currently
573371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// in a 'block', this returns the containing context.
574371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerFunctionDecl *Sema::getCurFunctionDecl() {
5758517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
576371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return dyn_cast<FunctionDecl>(DC);
577371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
578371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
579c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel DunbarObjCMethodDecl *Sema::getCurMethodDecl() {
5808517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
581d7612e183bb09d04677d0ddde89ee29130ffb715Steve Naroff  return dyn_cast<ObjCMethodDecl>(DC);
582c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar}
583371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
584371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerNamedDecl *Sema::getCurFunctionOrMethodDecl() {
5858517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
586371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
5874afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return cast<NamedDecl>(DC);
588371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return 0;
589371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
590371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
59125a88bbf042317976f0d9cbfa87dfe89426e8393Douglas GregorSema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
5929b623639378d53a675921ddfa7316034d571881eDouglas Gregor  if (!isActive())
5939b623639378d53a675921ddfa7316034d571881eDouglas Gregor    return;
5949b623639378d53a675921ddfa7316034d571881eDouglas Gregor
5951eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor  if (llvm::Optional<TemplateDeductionInfo*> Info = SemaRef.isSFINAEContext()) {
59633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    switch (DiagnosticIDs::getDiagnosticSFINAEResponse(getDiagID())) {
59733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Report:
5989b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Fall through; we'll report the diagnostic below.
5999b623639378d53a675921ddfa7316034d571881eDouglas Gregor      break;
6009b623639378d53a675921ddfa7316034d571881eDouglas Gregor
6011eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor    case DiagnosticIDs::SFINAE_AccessControl:
6027822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // Per C++ Core Issue 1170, access control is part of SFINAE.
6037822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // Additionally, the AccessCheckingSFINAE flag can be used to temporary
6047822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // make access control a part of SFINAE for the purposes of checking
6057822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      // type traits.
6067822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor      if (!SemaRef.AccessCheckingSFINAE &&
6077822ee3ef9f0d5cfd289258614ac31be70097449Douglas Gregor          !SemaRef.getLangOptions().CPlusPlus0x)
6081eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor        break;
6091eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor
61033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_SubstitutionFailure:
6119b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Count this failure so that we know that template argument deduction
6129b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // has failed.
6139b623639378d53a675921ddfa7316034d571881eDouglas Gregor      ++SemaRef.NumSFINAEErrors;
6149b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.setLastDiagnosticIgnored();
6159b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.Clear();
6169b623639378d53a675921ddfa7316034d571881eDouglas Gregor      Clear();
6179b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
6189b623639378d53a675921ddfa7316034d571881eDouglas Gregor
61933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Suppress:
6209b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Make a copy of this suppressed diagnostic and store it with the
6219b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // template-deduction information;
622b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor      FlushCounts();
6239b623639378d53a675921ddfa7316034d571881eDouglas Gregor      DiagnosticInfo DiagInfo(&SemaRef.Diags);
624b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor
6251eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor      if (*Info)
6261eee5dc0465c0ab4810e21d365e881152d7f53c0Douglas Gregor        (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(),
6279b623639378d53a675921ddfa7316034d571881eDouglas Gregor                        PartialDiagnostic(DiagInfo,
6289b623639378d53a675921ddfa7316034d571881eDouglas Gregor                                          SemaRef.Context.getDiagAllocator()));
6299b623639378d53a675921ddfa7316034d571881eDouglas Gregor
6309b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Suppress this diagnostic.
6319b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.setLastDiagnosticIgnored();
6329b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.Clear();
6339b623639378d53a675921ddfa7316034d571881eDouglas Gregor      Clear();
6349b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
6359b623639378d53a675921ddfa7316034d571881eDouglas Gregor    }
6369b623639378d53a675921ddfa7316034d571881eDouglas Gregor  }
6379b623639378d53a675921ddfa7316034d571881eDouglas Gregor
6389b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // Emit the diagnostic.
6395e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  if (!this->Emit())
6405e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    return;
6411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // If this is not a note, and we're in a template instantiation
64325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // that is different from the last template instantiation where
64425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // we emitted an error, print a template instantiation
64525a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // backtrace.
64633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  if (!DiagnosticIDs::isBuiltinNote(DiagID) &&
64725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      !SemaRef.ActiveTemplateInstantiations.empty() &&
6481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      SemaRef.ActiveTemplateInstantiations.back()
64925a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor        != SemaRef.LastTemplateInstantiationErrorContext) {
65025a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    SemaRef.PrintInstantiationStack();
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.LastTemplateInstantiationErrorContext
65225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      = SemaRef.ActiveTemplateInstantiations.back();
65325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  }
65425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor}
6552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
656eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas GregorSema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) {
65733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  DiagnosticBuilder DB = Diags.Report(Loc, DiagID);
658eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor  return SemaDiagnosticBuilder(DB, *this, DiagID);
659eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor}
660eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor
66191a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::SemaDiagnosticBuilder
66291a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
66391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
66491a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  PD.Emit(Builder);
6651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  return Builder;
66791a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson}
66891a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson
669834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// \brief Looks through the macro-instantiation chain for the given
670834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// location, looking for a macro instantiation with the given name.
671834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// If one is found, returns true and sets the location to that
672834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// instantiation loc.
673834e3f6c77d9ac03997a3f0c56934edcf406a355John McCallbool Sema::findMacroSpelling(SourceLocation &locref, llvm::StringRef name) {
674834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  SourceLocation loc = locref;
675834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (!loc.isMacroID()) return false;
676834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
677834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // There's no good way right now to look at the intermediate
678834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // instantiations, so just jump to the instantiation location.
679834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  loc = getSourceManager().getInstantiationLoc(loc);
680834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
681834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // If that's written with the name, stop here.
682834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  llvm::SmallVector<char, 16> buffer;
683834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (getPreprocessor().getSpelling(loc, buffer) == name) {
684834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    locref = loc;
685834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    return true;
686834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  }
687834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  return false;
688834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall}
689834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
69023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \brief Determines the active Scope associated with the given declaration
69123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// context.
69223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
69323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// This routine maps a declaration context to the active Scope object that
69423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// represents that declaration context in the parser. It is typically used
69523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// from "scope-less" code (e.g., template instantiation, lazy creation of
69623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// declarations) that injects a name for name-lookup purposes and, therefore,
69723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// must update the Scope.
69823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
69923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \returns The scope corresponding to the given declaraion context, or NULL
70023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// if no such scope is open.
70123c94dbb6631fecdb55ba401aa93722803d980c6Douglas GregorScope *Sema::getScopeForContext(DeclContext *Ctx) {
70223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
70323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!Ctx)
70423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return 0;
70523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
70623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Ctx = Ctx->getPrimaryContext();
70723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
708cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // Ignore scopes that cannot have declarations. This is important for
709cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // out-of-line definitions of static class members.
710cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
711cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl      if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity()))
712cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl        if (Ctx == Entity->getPrimaryContext())
713cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl          return S;
71423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  }
71523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
71623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return 0;
71723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor}
7189ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
7199ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Enter a new function scope
7209ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushFunctionScope() {
721781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1) {
722781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // Use the "top" function scope rather than having to allocate
723781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // memory for a new scope.
7248fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis    FunctionScopes.back()->Clear();
725781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    FunctionScopes.push_back(FunctionScopes.back());
7269ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return;
7279ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  }
7289ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
7298fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics()));
7309ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
7319ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
7329ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
7338fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(),
7349ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor                                              BlockScope, Block));
7359ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
7369ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
7373ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenekvoid Sema::PopFunctionOrBlockScope(const AnalysisBasedWarnings::Policy *WP,
7383ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek                                   const Decl *D, const BlockExpr *blkExpr) {
7393ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  FunctionScopeInfo *Scope = FunctionScopes.pop_back_val();
740781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  assert(!FunctionScopes.empty() && "mismatched push/pop!");
7413ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek
7423ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  // Issue any analysis-based warnings.
7433ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek  if (WP && D)
744283a358aecb75e30fcd486f2206f6c03c5e7f11dTed Kremenek    AnalysisWarnings.IssueWarnings(*WP, Scope, D, blkExpr);
745351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  else {
746351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek    for (llvm::SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator
747351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         i = Scope->PossiblyUnreachableDiags.begin(),
748351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         e = Scope->PossiblyUnreachableDiags.end();
749351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek         i != e; ++i) {
750351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek      const sema::PossiblyUnreachableDiag &D = *i;
751351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek      Diag(D.Loc, D.PD);
752351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek    }
753351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  }
7543ed6fc08a9cd293d012fa49ab2a615e618d7c3faTed Kremenek
755351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  if (FunctionScopes.back() != Scope) {
756781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete Scope;
757351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  }
7589ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
7599ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
7609ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Determine whether any errors occurred within this function/method/
7619ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// block.
762f85e193739c953358c865005855253af4f68a497John McCallbool Sema::hasAnyUnrecoverableErrorsInThisFunction() const {
763f85e193739c953358c865005855253af4f68a497John McCall  return getCurFunction()->ErrorTrap.hasUnrecoverableErrorOccurred();
7649ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
7659ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
7669ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo *Sema::getCurBlock() {
7679ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.empty())
7689ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return 0;
7699ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
7709ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  return dyn_cast<BlockScopeInfo>(FunctionScopes.back());
7719ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
77276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
77376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall// Pin this vtable to this file.
77476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalSemaSource::~ExternalSemaSource() {}
775f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
7768c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redlstd::pair<ObjCMethodList, ObjCMethodList>
7778c84571f3e262569ba51d107db7ab31a23de79b3Sebastian RedlExternalSemaSource::ReadMethodPool(Selector Sel) {
7788c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl  return std::pair<ObjCMethodList, ObjCMethodList>();
7798c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl}
7808c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl
781f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallvoid PrettyDeclStackTraceEntry::print(llvm::raw_ostream &OS) const {
782f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  SourceLocation Loc = this->Loc;
783f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
784f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (Loc.isValid()) {
785f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Loc.print(OS, S.getSourceManager());
786f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    OS << ": ";
787f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
788f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << Message;
789f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
790f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (TheDecl && isa<NamedDecl>(TheDecl)) {
791f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString();
792f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    if (!Name.empty())
793f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      OS << " '" << Name << '\'';
794f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
795f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
796f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << '\n';
797f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall}
798c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
799c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \brief Figure out if an expression could be turned into a call.
800c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
801c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// Use this when trying to recover from an error where the programmer may have
802c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// written just the name of a function instead of actually calling it.
803c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
804c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param E - The expression to examine.
805c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param ZeroArgCallReturnTy - If the expression can be turned into a call
806c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  with no arguments, this parameter is set to the type returned by such a
807c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  call; otherwise, it is set to an empty QualType.
808c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param NonTemplateOverloads - If the expression is an overloaded function
809c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  name, this parameter is populated with the decls of the various overloads.
810c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gaybool Sema::isExprCallable(const Expr &E, QualType &ZeroArgCallReturnTy,
811c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay                          UnresolvedSetImpl &NonTemplateOverloads) {
812c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  ZeroArgCallReturnTy = QualType();
813c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  NonTemplateOverloads.clear();
814c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (const OverloadExpr *Overloads = dyn_cast<OverloadExpr>(&E)) {
815c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    for (OverloadExpr::decls_iterator it = Overloads->decls_begin(),
816c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay         DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) {
817c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      // Our overload set may include TemplateDecls, which we'll ignore for our
818c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      // present purpose.
819c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      if (const FunctionDecl *OverloadDecl = dyn_cast<FunctionDecl>(*it)) {
820c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay        NonTemplateOverloads.addDecl(*it);
821c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay        if (OverloadDecl->getMinRequiredArguments() == 0)
822c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay          ZeroArgCallReturnTy = OverloadDecl->getResultType();
823c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      }
824c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
825c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    return true;
826c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
827c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
828c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(&E)) {
829c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
830c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      if (Fun->getMinRequiredArguments() == 0)
831c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay        ZeroArgCallReturnTy = Fun->getResultType();
832c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      return true;
833c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
834c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
835c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
836c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  // We don't have an expression that's convenient to get a FunctionDecl from,
837c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  // but we can at least check if the type is "function of 0 arguments".
838c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  QualType ExprTy = E.getType();
839c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  const FunctionType *FunTy = NULL;
8409389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay  QualType PointeeTy = ExprTy->getPointeeType();
8419389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay  if (!PointeeTy.isNull())
8429389ddc29ec60931e4dc418541ba3470b6b9fbe0Matt Beaumont-Gay    FunTy = PointeeTy->getAs<FunctionType>();
843c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (!FunTy)
844c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    FunTy = ExprTy->getAs<FunctionType>();
845c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (!FunTy && ExprTy == Context.BoundMemberTy) {
846c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // Look for the bound-member type.  If it's still overloaded, give up,
847c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // although we probably should have fallen into the OverloadExpr case above
848c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // if we actually have an overloaded bound member.
849c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    QualType BoundMemberTy = Expr::findBoundMemberType(&E);
850c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (!BoundMemberTy.isNull())
851c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      FunTy = BoundMemberTy->castAs<FunctionType>();
852c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
853c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
854c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (const FunctionProtoType *FPT =
855c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      dyn_cast_or_null<FunctionProtoType>(FunTy)) {
856c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (FPT->getNumArgs() == 0)
857c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      ZeroArgCallReturnTy = FunTy->getResultType();
858c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    return true;
859c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
860c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  return false;
861c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay}
862c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay
863c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \brief Give notes for a set of overloads.
864c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
865c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// A companion to isExprCallable. In cases when the name that the programmer
866c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// wrote was an overloaded function, we may be able to make some guesses about
867c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// plausible overloads based on their return types; such guesses can be handed
868c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// off to this method to be emitted as notes.
869c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///
870c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param Overloads - The overloads to note.
871c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay/// \param FinalNoteLoc - If we've suppressed printing some overloads due to
872c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  -fshow-overloads=best, this is the location to attach to the note about too
873c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  many candidates. Typically this will be the location of the original
874c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay///  ill-formed expression.
875c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gayvoid Sema::NoteOverloads(const UnresolvedSetImpl &Overloads,
876c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay                         const SourceLocation FinalNoteLoc) {
877c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  int ShownOverloads = 0;
878c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  int SuppressedOverloads = 0;
879c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  for (UnresolvedSetImpl::iterator It = Overloads.begin(),
880c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay       DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
881c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // FIXME: Magic number for max shown overloads stolen from
882c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    // OverloadCandidateSet::NoteCandidates.
883c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    if (ShownOverloads >= 4 &&
884c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay        Diags.getShowOverloads() == Diagnostic::Ovl_Best) {
885c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      ++SuppressedOverloads;
886c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay      continue;
887c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    }
888c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    Diag(cast<FunctionDecl>(*It)->getSourceRange().getBegin(),
889c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay         diag::note_member_ref_possible_intended_overload);
890c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    ++ShownOverloads;
891c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  }
892c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay  if (SuppressedOverloads)
893c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay    Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates)
894c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay        << SuppressedOverloads;
895c9366ba8fff6461a5b7f0fd2626d1bce3e98e629Matt Beaumont-Gay}
896