Sema.cpp revision aee543a1a3d70de38cd2607fd2f3179551febc93
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"
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Preprocessor.h"
3591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
364d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner#include "clang/Basic/TargetInfo.h"
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
38781472fe99a120098c631b0cbe33c89f8cef5e70John McCallusing namespace sema;
399ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
409ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorFunctionScopeInfo::~FunctionScopeInfo() { }
419ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
428fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidisvoid FunctionScopeInfo::Clear() {
43b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasBranchProtectedScope = false;
44b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasBranchIntoScope = false;
45b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasIndirectGoto = false;
46b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
479ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  LabelMap.clear();
489ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  SwitchStack.clear();
495077c3876beeaed32280af88244e8050078619a8Douglas Gregor  Returns.clear();
508fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  ErrorTrap.reset();
519ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
529ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
539ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo::~BlockScopeInfo() { }
549ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
55c1a3e5e73859ece9f106ae9d84c78bef4111956aDouglas Gregorvoid Sema::ActOnTranslationUnitScope(Scope *S) {
568ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroff  TUScope = S;
5744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  PushDeclContext(S, Context.getTranslationUnitDecl());
581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall  VAListTagName = PP.getIdentifierInfo("__va_list_tag");
60c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall
61c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl  if (!Context.isInt128Installed() && // May be set by ASTReader.
62006113841bdae1edb77aef75ba1ffdf2e55a3094Argyrios Kyrtzidis      PP.getTargetInfo().getPointerWidth(0) >= 64) {
63a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *TInfo;
64ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
654d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    // Install [u]int128_t for 64-bit targets.
66a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
674d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
684d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          SourceLocation(),
694d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          &Context.Idents.get("__int128_t"),
70a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                          TInfo), TUScope);
71ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
72a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
734d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
744d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          SourceLocation(),
754d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          &Context.Idents.get("__uint128_t"),
76a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                          TInfo), TUScope);
77006113841bdae1edb77aef75ba1ffdf2e55a3094Argyrios Kyrtzidis    Context.setInt128Installed();
784d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner  }
791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
812ae34ed8caf90ca4044feca07811561053c08666Chris Lattner  if (!PP.getLangOptions().ObjC1) return;
821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl  // Built-in ObjC types may already be set by ASTReader (hence isNull checks).
84319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCSelType().isNull()) {
8513dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian    // Create the built-in typedef for 'SEL'.
8604765ac135e0c4e6b78651c2a287d80a32b2b8b9Fariborz Jahanian    QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
87a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
88ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *SelTypedef
89ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
90ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("SEL"), SelInfo);
91319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    PushOnScopeChains(SelTypedef, TUScope);
92319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
93369a3bd9979cf529eed529aa037de713c213e47dFariborz Jahanian    Context.ObjCSelRedefinitionType = Context.getObjCSelType();
94319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
95319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
966ee1f9c0142e0c374f32fc86312630cd901247cfChris Lattner  // Synthesize "@class Protocol;
97319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCProtoType().isNull()) {
98319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    ObjCInterfaceDecl *ProtocolDecl =
99319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor      ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
1001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                &Context.Idents.get("Protocol"),
101deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor                                SourceLocation(), true);
102319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
10310324db994455a9a1520be6cfe5bb29685cde141Fariborz Jahanian    PushOnScopeChains(ProtocolDecl, TUScope, false);
104319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
105de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Create the built-in typedef for 'id'.
106319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCIdType().isNull()) {
107c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0);
108c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = Context.getObjCObjectPointerType(T);
109c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T);
110ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *IdTypedef
111ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
112ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("id"), IdInfo);
113319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    PushOnScopeChains(IdTypedef, TUScope);
114319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
1150f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    Context.ObjCIdRedefinitionType = Context.getObjCIdType();
116319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
117de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Create the built-in typedef for 'Class'.
11814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (Context.getObjCClassType().isNull()) {
119c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0);
120c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = Context.getObjCObjectPointerType(T);
121c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T);
122ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *ClassTypedef
123ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
124ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("Class"), ClassInfo);
12514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    PushOnScopeChains(ClassTypedef, TUScope);
12614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
1270f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    Context.ObjCClassRedefinitionType = Context.getObjCClassType();
12846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  }
129b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
1303b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff
131f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas GregorSema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
1323a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           bool CompleteTranslationUnit,
1333a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           CodeCompleteConsumer *CodeCompleter)
13482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  : TheTargetAttributesSema(0),
13582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
1361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
1373a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar    ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
138781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    PackContext(0), VisContext(0), ParsingDeclDepth(0),
1394eb4f0f96289cbece50c1270e02af3caf8779705Douglas Gregor    IdResolver(pp.getLangOptions()), CXXTypeInfoDecl(0), MSVCGuidDecl(0),
140b7566d8346ed8040a5cb7cefe96bd6ccbf015595Bill Wendling    GlobalNewDeleteDeclared(false),
14148dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor    CompleteTranslationUnit(CompleteTranslationUnit),
142926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth    NumSFINAEErrors(0), SuppressAccessChecking(false),
143926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth    NonInstantiationEntries(0), CurrentInstantiationScope(0), TyposCorrected(0),
144b7566d8346ed8040a5cb7cefe96bd6ccbf015595Bill Wendling    AnalysisWarnings(*this)
145f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor{
1463b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff  TUScope = 0;
14707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis  if (getLangOptions().CPlusPlus)
14807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    FieldCollector.reset(new CXXFieldCollector());
1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15022caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  // Tell diagnostics how to render things from the AST library.
15179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
15279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                       &Context);
1532afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor
1542afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor  ExprEvalContexts.push_back(
15546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor                  ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
156781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
1578fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(Diags));
15846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor}
15946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
16046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregorvoid Sema::Initialize() {
16146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the AST consumer about this Sema object.
16246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  Consumer.Initialize(Context);
16346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
16446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // FIXME: Isn't this redundant with the initialization above?
16546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
16646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->InitializeSema(*this);
16746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
16846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the external Sema source about this Sema object.
16946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
17046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor      = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
17146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->InitializeSema(*this);
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovSema::~Sema() {
17582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (PackContext) FreePackedContext();
176aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  if (VisContext) FreeVisContext();
17782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  delete TheTargetAttributesSema;
178781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
179781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  // Kill all the active scopes.
180781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I)
181781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[I];
182781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1)
183781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete FunctionScopes[0];
18446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
18546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the SemaConsumer to forget about us; we're going out of scope.
18646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
18746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->ForgetSema();
18846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
18946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Detach from the external Sema source.
19046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
191914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor        = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
19246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->ForgetSema();
19382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
19482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
1961e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner/// If there is already an implicit cast, merge into the existing one.
197906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl/// The result is of the given category.
1981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
1995baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                             CastKind Kind, ExprValueKind VK,
200f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                             const CXXCastPath *BasePath) {
2013a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType ExprTy = Context.getCanonicalType(Expr->getType());
2023a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType TypeTy = Context.getCanonicalType(Ty);
2031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2043a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  if (ExprTy == TypeTy)
2053a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    return;
2061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
207680523a91dd3351389667c8de17121ba7ae82673John McCall  if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
208680523a91dd3351389667c8de17121ba7ae82673John McCall    QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
209680523a91dd3351389667c8de17121ba7ae82673John McCall    QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
2103a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
211dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
212dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        << Expr->getSourceRange();
2133a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    }
2143a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  }
2151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2166fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // If this is a derived-to-base cast to a through a virtual base, we
2176fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // need a vtable.
2182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  if (Kind == CK_DerivedToBase &&
219f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall      BasePathInvolvesVirtualBase(*BasePath)) {
2206fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    QualType T = Expr->getType();
2216fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const PointerType *Pointer = T->getAs<PointerType>())
2226fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      T = Pointer->getPointeeType();
2236fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const RecordType *RecordTy = T->getAs<RecordType>())
2246fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      MarkVTableUsed(Expr->getLocStart(),
2256fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor                     cast<CXXRecordDecl>(RecordTy->getDecl()));
2266fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  }
2276fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor
228eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
229f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
2304c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      ImpCast->setType(Ty);
2315baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall      ImpCast->setValueKind(VK);
2324c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      return;
2334c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson    }
2344c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson  }
2354c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson
2365baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  Expr = ImplicitCastExpr::Create(Context, Ty, Kind, Expr, BasePath, VK);
237906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl}
238906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
2395baba9d98364a3525d6afa15a04cdad82fd6dd30John McCallExprValueKind Sema::CastCategory(Expr *E) {
240906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  Expr::Classification Classification = E->Classify(Context);
2415baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  return Classification.isRValue() ? VK_RValue :
2425baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall      (Classification.isLValue() ? VK_LValue : VK_XValue);
2431e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner}
2441e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner
245bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis/// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
246bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidisstatic bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
247bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (D->isUsed())
248bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    return true;
249bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
250bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
251bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
252bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
253bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const FunctionDecl *DeclToCheck;
254bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (FD->hasBody(DeclToCheck))
255bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
256bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
257bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
258bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
259bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    DeclToCheck = FD->getMostRecentDeclaration();
260bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != FD)
261bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
262bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
263bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
264bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
265bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
266bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
267bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const VarDecl *DeclToCheck = VD->getDefinition();
268bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck)
269bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
270bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
271bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
272bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
273bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    DeclToCheck = VD->getMostRecentDeclaration();
274bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != VD)
275bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
276bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
277bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
278bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  return false;
279bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis}
280bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
2819299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// ActOnEndOfTranslationUnit - This is called at the very end of the
2829299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// translation unit when EOF is reached and all but the top-level scope is
2839299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// popped.
2840e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidisvoid Sema::ActOnEndOfTranslationUnit() {
2850e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  // At PCH writing, implicit instantiations and VTable handling info are
2860e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  // stored and performed when the PCH is included.
2872a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (CompleteTranslationUnit) {
288aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // If any dynamic classes have their key function defined within
289aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // this translation unit, then those vtables are considered "used" and must
290aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    // be emitted.
291aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
292aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth      if (const CXXMethodDecl *KeyFunction
293aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth          = Context.getKeyFunction(DynamicClasses[I])) {
294aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth        const FunctionDecl *Definition = 0;
295aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth        if (KeyFunction->hasBody(Definition))
296aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth          MarkVTableUsed(Definition->getLocation(), DynamicClasses[I], true);
297aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth      }
298aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth    }
299aee543a1a3d70de38cd2607fd2f3179551febc93Chandler Carruth
3002a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // If DefinedUsedVTables ends up marking any virtual member functions it
3012a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // might lead to more pending template instantiations, which we then need
3022a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // to instantiate.
3032a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
3042a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
3052a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // C++: Perform implicit template instantiations.
3062a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    //
3072a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // FIXME: When we perform these implicit instantiations, we do not
3082a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // carefully keep track of the point of instantiation (C++ [temp.point]).
3092a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // This means that name lookup that occurs within the template
3102a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // instantiation will always happen at the end of the translation unit,
3112a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // so it will find some names that should not be found. Although this is
3122a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // common behavior for C++ compilers, it is technically wrong. In the
3132a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // future, we either need to be able to filter the results of name lookup
3142a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // or we need to perform template instantiations earlier.
3152a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    PerformPendingInstantiations();
3162a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
317d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson
31849b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Remove file scoped decls that turned out to be used.
319bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(),
320bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                                             UnusedFileScopedDecls.end(),
321bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                              std::bind1st(std::ptr_fun(ShouldRemoveFromUnused),
322bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                                           this)),
32349b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis                              UnusedFileScopedDecls.end());
32447268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor
32587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  if (!CompleteTranslationUnit) {
32687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    TUScope = 0;
32772b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    return;
32887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
32972b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis
33063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // Check for #pragma weak identifiers that were never declared
33163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // FIXME: This will cause diagnostics to be emitted in a non-determinstic
33263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // order!  Iterating over a densemap like this is bad.
333e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
33463d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       I = WeakUndeclaredIdentifiers.begin(),
33563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
33663d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    if (I->second.getUsed()) continue;
3371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
33963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      << I->first;
340e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  }
341e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn
342275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  // C99 6.9.2p2:
343275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   A declaration of an identifier for an object that has file
344275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   scope without an initializer, and without a storage-class
345275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   specifier or with the storage-class specifier static,
346275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   constitutes a tentative definition. If a translation unit
347275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   contains one or more tentative definitions for an identifier,
348275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   and the translation unit contains no external definition for
349275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   that identifier, then the behavior is exactly as if the
350275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit contains a file scope declaration of that
351275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   identifier, with the composite type as of the end of the
352275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit, with an initializer equal to 0.
353e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  llvm::SmallSet<VarDecl *, 32> Seen;
354e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) {
355e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    VarDecl *VD = TentativeDefinitions[i]->getActingDefinition();
356e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
357e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // If the tentative definition was completed, getActingDefinition() returns
358e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // null. If we've already seen this variable before, insert()'s second
359e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // return value is false.
360e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
361b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      continue;
362b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const IncompleteArrayType *ArrayT
364b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        = Context.getAsIncompleteArrayType(VD->getType())) {
3651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (RequireCompleteType(VD->getLocation(),
366b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                              ArrayT->getElementType(),
36763d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner                              diag::err_tentative_def_incomplete_type_arr)) {
368b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        VD->setInvalidDecl();
36963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner        continue;
370275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor      }
3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      // Set the length of the array to 1 (C99 6.9.2p5).
37363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
37463d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
37546a617a792bfab0d9b1e057371ea3b9540802226John McCall      QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
37646a617a792bfab0d9b1e057371ea3b9540802226John McCall                                                One, ArrayType::Normal, 0);
37763d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      VD->setType(T);
3781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
379b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                                   diag::err_tentative_def_incomplete_type))
380b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      VD->setInvalidDecl();
381b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
382b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    // Notify the consumer that we've completed a tentative definition.
383b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    if (!VD->isInvalidDecl())
384b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      Consumer.CompleteTentativeDefinition(VD);
385b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
386275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  }
387e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner
38849b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Output warning for unused file scoped decls.
389aab0132698c7550d0095986fb1fb4887bf18c3e8John McCall  for (llvm::SmallVectorImpl<const DeclaratorDecl*>::iterator
39049b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis         I = UnusedFileScopedDecls.begin(),
39149b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis         E = UnusedFileScopedDecls.end(); I != E; ++I) {
392bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
393bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      const FunctionDecl *DiagD;
394bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      if (!FD->hasBody(DiagD))
395bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis        DiagD = FD;
3963d27b107c515ab1a8fd47b33c41adcf5a65f465bArgyrios Kyrtzidis      Diag(DiagD->getLocation(),
39742cbd782cf17223076e5b6c0607414d9d68aae19Argyrios Kyrtzidis           isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function
3983d27b107c515ab1a8fd47b33c41adcf5a65f465bArgyrios Kyrtzidis                                     : diag::warn_unused_function)
399bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis            << DiagD->getDeclName();
400bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    } else {
401bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
402bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      if (!DiagD)
403bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis        DiagD = cast<VarDecl>(*I);
404bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      Diag(DiagD->getLocation(), diag::warn_unused_variable)
405bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis            << DiagD->getDeclName();
406bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    }
40749b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  }
40887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
40987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  TUScope = 0;
4109299f3fa85796613cc787a2062c9562d07c8613eChris Lattner}
4119299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
4129299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper functions.
4155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
4165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4178517d9b731f065cdfc55ec0f3ddf5d564d988648Anders CarlssonDeclContext *Sema::getFunctionLevelDeclContext() {
418db0ee1da16e9dbec19b144c9cd96ee9f55fe0c53John McCall  DeclContext *DC = CurContext;
4191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
420d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor  while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
4218517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson    DC = DC->getParent();
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4238517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  return DC;
4248517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson}
4258517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson
426371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// getCurFunctionDecl - If inside of a function body, this returns a pointer
427371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// to the function decl for the function being parsed.  If we're currently
428371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// in a 'block', this returns the containing context.
429371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerFunctionDecl *Sema::getCurFunctionDecl() {
4308517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
431371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return dyn_cast<FunctionDecl>(DC);
432371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
433371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
434c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel DunbarObjCMethodDecl *Sema::getCurMethodDecl() {
4358517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
436d7612e183bb09d04677d0ddde89ee29130ffb715Steve Naroff  return dyn_cast<ObjCMethodDecl>(DC);
437c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar}
438371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
439371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerNamedDecl *Sema::getCurFunctionOrMethodDecl() {
4408517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
441371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
4424afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return cast<NamedDecl>(DC);
443371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return 0;
444371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
445371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
44625a88bbf042317976f0d9cbfa87dfe89426e8393Douglas GregorSema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
4479b623639378d53a675921ddfa7316034d571881eDouglas Gregor  if (!isActive())
4489b623639378d53a675921ddfa7316034d571881eDouglas Gregor    return;
4499b623639378d53a675921ddfa7316034d571881eDouglas Gregor
4509b623639378d53a675921ddfa7316034d571881eDouglas Gregor  if (TemplateDeductionInfo *Info = SemaRef.isSFINAEContext()) {
45133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    switch (DiagnosticIDs::getDiagnosticSFINAEResponse(getDiagID())) {
45233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Report:
4539b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Fall through; we'll report the diagnostic below.
4549b623639378d53a675921ddfa7316034d571881eDouglas Gregor      break;
4559b623639378d53a675921ddfa7316034d571881eDouglas Gregor
45633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_SubstitutionFailure:
4579b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Count this failure so that we know that template argument deduction
4589b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // has failed.
4599b623639378d53a675921ddfa7316034d571881eDouglas Gregor      ++SemaRef.NumSFINAEErrors;
4609b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.setLastDiagnosticIgnored();
4619b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.Clear();
4629b623639378d53a675921ddfa7316034d571881eDouglas Gregor      Clear();
4639b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
4649b623639378d53a675921ddfa7316034d571881eDouglas Gregor
46533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    case DiagnosticIDs::SFINAE_Suppress:
4669b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Make a copy of this suppressed diagnostic and store it with the
4679b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // template-deduction information;
468b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor      FlushCounts();
4699b623639378d53a675921ddfa7316034d571881eDouglas Gregor      DiagnosticInfo DiagInfo(&SemaRef.Diags);
470b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor
4719b623639378d53a675921ddfa7316034d571881eDouglas Gregor      Info->addSuppressedDiagnostic(DiagInfo.getLocation(),
4729b623639378d53a675921ddfa7316034d571881eDouglas Gregor                        PartialDiagnostic(DiagInfo,
4739b623639378d53a675921ddfa7316034d571881eDouglas Gregor                                          SemaRef.Context.getDiagAllocator()));
4749b623639378d53a675921ddfa7316034d571881eDouglas Gregor
4759b623639378d53a675921ddfa7316034d571881eDouglas Gregor      // Suppress this diagnostic.
4769b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.setLastDiagnosticIgnored();
4779b623639378d53a675921ddfa7316034d571881eDouglas Gregor      SemaRef.Diags.Clear();
4789b623639378d53a675921ddfa7316034d571881eDouglas Gregor      Clear();
4799b623639378d53a675921ddfa7316034d571881eDouglas Gregor      return;
4809b623639378d53a675921ddfa7316034d571881eDouglas Gregor    }
4819b623639378d53a675921ddfa7316034d571881eDouglas Gregor  }
4829b623639378d53a675921ddfa7316034d571881eDouglas Gregor
4839b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // Emit the diagnostic.
4845e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  if (!this->Emit())
4855e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    return;
4861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // If this is not a note, and we're in a template instantiation
48825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // that is different from the last template instantiation where
48925a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // we emitted an error, print a template instantiation
49025a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // backtrace.
49133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  if (!DiagnosticIDs::isBuiltinNote(DiagID) &&
49225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      !SemaRef.ActiveTemplateInstantiations.empty() &&
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      SemaRef.ActiveTemplateInstantiations.back()
49425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor        != SemaRef.LastTemplateInstantiationErrorContext) {
49525a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    SemaRef.PrintInstantiationStack();
4961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.LastTemplateInstantiationErrorContext
49725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      = SemaRef.ActiveTemplateInstantiations.back();
49825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  }
49925a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor}
5002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
501eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas GregorSema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) {
50233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  DiagnosticBuilder DB = Diags.Report(Loc, DiagID);
503eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor  return SemaDiagnosticBuilder(DB, *this, DiagID);
504eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor}
505eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor
50691a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::SemaDiagnosticBuilder
50791a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
50891a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
50991a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  PD.Emit(Builder);
5101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51191a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  return Builder;
51291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson}
51391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson
51423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \brief Determines the active Scope associated with the given declaration
51523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// context.
51623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
51723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// This routine maps a declaration context to the active Scope object that
51823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// represents that declaration context in the parser. It is typically used
51923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// from "scope-less" code (e.g., template instantiation, lazy creation of
52023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// declarations) that injects a name for name-lookup purposes and, therefore,
52123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// must update the Scope.
52223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
52323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \returns The scope corresponding to the given declaraion context, or NULL
52423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// if no such scope is open.
52523c94dbb6631fecdb55ba401aa93722803d980c6Douglas GregorScope *Sema::getScopeForContext(DeclContext *Ctx) {
52623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
52723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!Ctx)
52823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return 0;
52923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
53023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Ctx = Ctx->getPrimaryContext();
53123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
532cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // Ignore scopes that cannot have declarations. This is important for
533cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // out-of-line definitions of static class members.
534cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
535cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl      if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity()))
536cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl        if (Ctx == Entity->getPrimaryContext())
537cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl          return S;
53823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  }
53923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
54023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return 0;
54123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor}
5429ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5439ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Enter a new function scope
5449ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushFunctionScope() {
545781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.size() == 1) {
546781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // Use the "top" function scope rather than having to allocate
547781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    // memory for a new scope.
5488fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis    FunctionScopes.back()->Clear();
549781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    FunctionScopes.push_back(FunctionScopes.back());
5509ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return;
5519ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  }
5529ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5538fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics()));
5549ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
5559ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5569ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
5578fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(),
5589ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor                                              BlockScope, Block));
5599ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
5609ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5619ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PopFunctionOrBlockScope() {
562781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  FunctionScopeInfo *Scope = FunctionScopes.pop_back_val();
563781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  assert(!FunctionScopes.empty() && "mismatched push/pop!");
564781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (FunctionScopes.back() != Scope)
565781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    delete Scope;
5669ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
5679ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5689ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Determine whether any errors occurred within this function/method/
5699ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// block.
5709ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorbool Sema::hasAnyErrorsInThisFunction() const {
5718fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  return getCurFunction()->ErrorTrap.hasErrorOccurred();
5729ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
5739ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5749ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo *Sema::getCurBlock() {
5759ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.empty())
5769ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return 0;
5779ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5789ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  return dyn_cast<BlockScopeInfo>(FunctionScopes.back());
5799ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
58076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
58176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall// Pin this vtable to this file.
58276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalSemaSource::~ExternalSemaSource() {}
583f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
5848c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redlstd::pair<ObjCMethodList, ObjCMethodList>
5858c84571f3e262569ba51d107db7ab31a23de79b3Sebastian RedlExternalSemaSource::ReadMethodPool(Selector Sel) {
5868c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl  return std::pair<ObjCMethodList, ObjCMethodList>();
5878c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl}
5888c84571f3e262569ba51d107db7ab31a23de79b3Sebastian Redl
589f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallvoid PrettyDeclStackTraceEntry::print(llvm::raw_ostream &OS) const {
590f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  SourceLocation Loc = this->Loc;
591f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
592f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (Loc.isValid()) {
593f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Loc.print(OS, S.getSourceManager());
594f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    OS << ": ";
595f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
596f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << Message;
597f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
598f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (TheDecl && isa<NamedDecl>(TheDecl)) {
599f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString();
600f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    if (!Name.empty())
601f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      OS << " '" << Name << '\'';
602f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  }
603f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
604f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  OS << '\n';
605f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall}
606