Sema.cpp revision 384aff8b94bb0d1ad6c5667b90621e5699815bb2
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
15e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Sema.h"
1682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov#include "TargetAttributesSema.h"
17e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn#include "llvm/ADT/DenseMap.h"
18e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl#include "llvm/ADT/SmallSet.h"
19680523a91dd3351389667c8de17121ba7ae82673John McCall#include "llvm/ADT/APFloat.h"
205f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/CXXFieldCollector.h"
2176bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall#include "clang/Sema/ExternalSemaSource.h"
225f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/Scope.h"
2346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor#include "clang/Sema/SemaConsumer.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
2579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/ASTDiagnostic.h"
26384aff8b94bb0d1ad6c5667b90621e5699815bb2John McCall#include "clang/AST/DeclCXX.h"
27c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
28e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Preprocessor.h"
3091a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
314d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner#include "clang/Basic/TargetInfo.h"
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
339ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
349ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorFunctionScopeInfo::~FunctionScopeInfo() { }
359ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
369ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid FunctionScopeInfo::Clear(unsigned NumErrors) {
37b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasBranchProtectedScope = false;
38b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasBranchIntoScope = false;
39b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  HasIndirectGoto = false;
40b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
419ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  LabelMap.clear();
429ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  SwitchStack.clear();
435077c3876beeaed32280af88244e8050078619a8Douglas Gregor  Returns.clear();
449ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  NumErrorsAtStartOfFunction = NumErrors;
459ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
469ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
479ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo::~BlockScopeInfo() { }
489ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
498ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroffvoid Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
508ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroff  TUScope = S;
5144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  PushDeclContext(S, Context.getTranslationUnitDecl());
521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall  VAListTagName = PP.getIdentifierInfo("__va_list_tag");
54c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall
55c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl  if (!Context.isInt128Installed() && // May be set by ASTReader.
56006113841bdae1edb77aef75ba1ffdf2e55a3094Argyrios Kyrtzidis      PP.getTargetInfo().getPointerWidth(0) >= 64) {
57a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *TInfo;
58ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
594d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    // Install [u]int128_t for 64-bit targets.
60a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
614d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
624d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          SourceLocation(),
634d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          &Context.Idents.get("__int128_t"),
64a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                          TInfo), TUScope);
65ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
66a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
674d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
684d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          SourceLocation(),
694d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          &Context.Idents.get("__uint128_t"),
70a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                          TInfo), TUScope);
71006113841bdae1edb77aef75ba1ffdf2e55a3094Argyrios Kyrtzidis    Context.setInt128Installed();
724d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner  }
731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
752ae34ed8caf90ca4044feca07811561053c08666Chris Lattner  if (!PP.getLangOptions().ObjC1) return;
761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl  // Built-in ObjC types may already be set by ASTReader (hence isNull checks).
78319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCSelType().isNull()) {
7913dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian    // Create the built-in typedef for 'SEL'.
8004765ac135e0c4e6b78651c2a287d80a32b2b8b9Fariborz Jahanian    QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
81a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
82ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *SelTypedef
83ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
84ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("SEL"), SelInfo);
85319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    PushOnScopeChains(SelTypedef, TUScope);
86319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
87369a3bd9979cf529eed529aa037de713c213e47dFariborz Jahanian    Context.ObjCSelRedefinitionType = Context.getObjCSelType();
88319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
89319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
906ee1f9c0142e0c374f32fc86312630cd901247cfChris Lattner  // Synthesize "@class Protocol;
91319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCProtoType().isNull()) {
92319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    ObjCInterfaceDecl *ProtocolDecl =
93319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor      ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                &Context.Idents.get("Protocol"),
95deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor                                SourceLocation(), true);
96319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
9710324db994455a9a1520be6cfe5bb29685cde141Fariborz Jahanian    PushOnScopeChains(ProtocolDecl, TUScope, false);
98319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
99de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Create the built-in typedef for 'id'.
100319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCIdType().isNull()) {
101c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0);
102c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = Context.getObjCObjectPointerType(T);
103c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T);
104ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *IdTypedef
105ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
106ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("id"), IdInfo);
107319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    PushOnScopeChains(IdTypedef, TUScope);
108319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
1090f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    Context.ObjCIdRedefinitionType = Context.getObjCIdType();
110319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
111de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Create the built-in typedef for 'Class'.
11214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (Context.getObjCClassType().isNull()) {
113c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0);
114c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = Context.getObjCObjectPointerType(T);
115c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T);
116ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *ClassTypedef
117ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
118ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("Class"), ClassInfo);
11914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    PushOnScopeChains(ClassTypedef, TUScope);
12014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
1210f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    Context.ObjCClassRedefinitionType = Context.getObjCClassType();
12246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  }
123b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
1243b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff
125f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas GregorSema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
1263a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           bool CompleteTranslationUnit,
1273a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           CodeCompleteConsumer *CodeCompleter)
12882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  : TheTargetAttributesSema(0),
12982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
1301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
1313a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar    ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
132aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman    PackContext(0), VisContext(0), TopFunctionScope(0), ParsingDeclDepth(0),
13376c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis    IdResolver(pp.getLangOptions()), GlobalNewDeleteDeclared(false),
13448dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor    CompleteTranslationUnit(CompleteTranslationUnit),
135926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth    NumSFINAEErrors(0), SuppressAccessChecking(false),
136926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth    NonInstantiationEntries(0), CurrentInstantiationScope(0), TyposCorrected(0),
137d064fdc4b7b64ca55b40b70490c79d6f569df78eTed Kremenek    AnalysisWarnings(*this)
138f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor{
1393b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff  TUScope = 0;
14007952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis  if (getLangOptions().CPlusPlus)
14107952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    FieldCollector.reset(new CXXFieldCollector());
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14322caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  // Tell diagnostics how to render things from the AST library.
14479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
14579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                       &Context);
1462afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor
1472afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor  ExprEvalContexts.push_back(
14846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor                  ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
14946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor}
15046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
15146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregorvoid Sema::Initialize() {
15246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the AST consumer about this Sema object.
15346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  Consumer.Initialize(Context);
15446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
15546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // FIXME: Isn't this redundant with the initialization above?
15646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
15746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->InitializeSema(*this);
15846ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
15946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the external Sema source about this Sema object.
16046ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
16146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor      = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
16246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->InitializeSema(*this);
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovSema::~Sema() {
16682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (PackContext) FreePackedContext();
167aa8b0d19244a6e7e8e5798fcc6aef003c274d3e0Eli Friedman  if (VisContext) FreeVisContext();
16882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  delete TheTargetAttributesSema;
1699ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  while (!FunctionScopes.empty())
1709ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    PopFunctionOrBlockScope();
17146ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
17246ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Tell the SemaConsumer to forget about us; we're going out of scope.
17346ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
17446ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    SC->ForgetSema();
17546ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor
17646ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  // Detach from the external Sema source.
17746ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor  if (ExternalSemaSource *ExternalSema
178914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor        = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
17946ea32a4b54481b7575499cb9f8d275f1d4cdd54Douglas Gregor    ExternalSema->ForgetSema();
18082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
18182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
1821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
1831e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner/// If there is already an implicit cast, merge into the existing one.
184906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl/// The result is of the given category.
1851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
18688465d3e996e627bbaa11099b039ddab66d5af2cAnders Carlsson                             CastExpr::CastKind Kind,
187906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl                             ImplicitCastExpr::ResultCategory Category,
188f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                             const CXXCastPath *BasePath) {
1893a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType ExprTy = Context.getCanonicalType(Expr->getType());
1903a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType TypeTy = Context.getCanonicalType(Ty);
1911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1923a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  if (ExprTy == TypeTy)
1933a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    return;
1941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
195680523a91dd3351389667c8de17121ba7ae82673John McCall  if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
196680523a91dd3351389667c8de17121ba7ae82673John McCall    QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
197680523a91dd3351389667c8de17121ba7ae82673John McCall    QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
1983a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
199dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
200dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        << Expr->getSourceRange();
2013a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    }
2023a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  }
2031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2046fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // If this is a derived-to-base cast to a through a virtual base, we
2056fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // need a vtable.
2066fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  if (Kind == CastExpr::CK_DerivedToBase &&
207f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall      BasePathInvolvesVirtualBase(*BasePath)) {
2086fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    QualType T = Expr->getType();
2096fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const PointerType *Pointer = T->getAs<PointerType>())
2106fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      T = Pointer->getPointeeType();
2116fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const RecordType *RecordTy = T->getAs<RecordType>())
2126fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      MarkVTableUsed(Expr->getLocStart(),
2136fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor                     cast<CXXRecordDecl>(RecordTy->getDecl()));
2146fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  }
2156fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor
216eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
217f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
2184c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      ImpCast->setType(Ty);
219906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl      ImpCast->setCategory(Category);
2204c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      return;
2214c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson    }
2224c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson  }
2234c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson
224f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  Expr = ImplicitCastExpr::Create(Context, Ty, Kind, Expr, BasePath, Category);
225906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl}
226906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
227906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian RedlImplicitCastExpr::ResultCategory Sema::CastCategory(Expr *E) {
228906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  Expr::Classification Classification = E->Classify(Context);
229906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  return Classification.isRValue() ?
230906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl      ImplicitCastExpr::RValue :
231906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl      (Classification.isLValue() ?
232906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl          ImplicitCastExpr::LValue :
233906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl          ImplicitCastExpr::XValue);
2341e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner}
2351e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner
236394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattnervoid Sema::DeleteExpr(ExprTy *E) {
237394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattner}
238394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattnervoid Sema::DeleteStmt(StmtTy *S) {
239394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattner}
240394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattner
241bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis/// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
242bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidisstatic bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
243bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (D->isUsed())
244bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    return true;
245bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
246bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
247bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
248bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
249bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const FunctionDecl *DeclToCheck;
250bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (FD->hasBody(DeclToCheck))
251bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
252bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
253bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
254bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
255bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    DeclToCheck = FD->getMostRecentDeclaration();
256bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != FD)
257bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
258bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
259bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
260bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
261bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // UnusedFileScopedDecls stores the first declaration.
262bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // The declaration may have become definition so check again.
263bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    const VarDecl *DeclToCheck = VD->getDefinition();
264bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck)
265bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
266bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
267bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // Later redecls may add new information resulting in not having to warn,
268bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    // so check again.
269bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    DeclToCheck = VD->getMostRecentDeclaration();
270bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (DeclToCheck != VD)
271bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
272bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  }
273bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
274bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  return false;
275bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis}
276bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
2779299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// ActOnEndOfTranslationUnit - This is called at the very end of the
2789299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// translation unit when EOF is reached and all but the top-level scope is
2799299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// popped.
2800e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidisvoid Sema::ActOnEndOfTranslationUnit() {
2810e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  // At PCH writing, implicit instantiations and VTable handling info are
2820e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  // stored and performed when the PCH is included.
2830e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  if (CompleteTranslationUnit)
2840e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis    while (1) {
2850e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      // C++: Perform implicit template instantiations.
2860e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      //
2870e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      // FIXME: When we perform these implicit instantiations, we do not
2880e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      // carefully keep track of the point of instantiation (C++ [temp.point]).
2890e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      // This means that name lookup that occurs within the template
2900e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      // instantiation will always happen at the end of the translation unit,
2910e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      // so it will find some names that should not be found. Although this is
2920e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      // common behavior for C++ compilers, it is technically wrong. In the
2930e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      // future, we either need to be able to filter the results of name lookup
2940e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      // or we need to perform template instantiations earlier.
2950e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      PerformPendingImplicitInstantiations();
2960e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis
2970e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      /// If DefinedUsedVTables ends up marking any virtual member
2980e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      /// functions it might lead to more pending template
2990e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      /// instantiations, which is why we need to loop here.
3000e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis      if (!DefineUsedVTables())
3010e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis        break;
3020e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis    }
303d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson
30449b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Remove file scoped decls that turned out to be used.
305bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(),
306bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                                             UnusedFileScopedDecls.end(),
307bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                              std::bind1st(std::ptr_fun(ShouldRemoveFromUnused),
308bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis                                           this)),
30949b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis                              UnusedFileScopedDecls.end());
31047268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor
31187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  if (!CompleteTranslationUnit) {
31287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    TUScope = 0;
31372b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    return;
31487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  }
31572b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis
31663d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // Check for #pragma weak identifiers that were never declared
31763d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // FIXME: This will cause diagnostics to be emitted in a non-determinstic
31863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // order!  Iterating over a densemap like this is bad.
319e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
32063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       I = WeakUndeclaredIdentifiers.begin(),
32163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
32263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    if (I->second.getUsed()) continue;
3231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32463d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
32563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      << I->first;
326e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  }
327e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn
328275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  // C99 6.9.2p2:
329275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   A declaration of an identifier for an object that has file
330275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   scope without an initializer, and without a storage-class
331275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   specifier or with the storage-class specifier static,
332275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   constitutes a tentative definition. If a translation unit
333275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   contains one or more tentative definitions for an identifier,
334275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   and the translation unit contains no external definition for
335275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   that identifier, then the behavior is exactly as if the
336275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit contains a file scope declaration of that
337275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   identifier, with the composite type as of the end of the
338275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit, with an initializer equal to 0.
339e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  llvm::SmallSet<VarDecl *, 32> Seen;
340e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) {
341e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    VarDecl *VD = TentativeDefinitions[i]->getActingDefinition();
342e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
343e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // If the tentative definition was completed, getActingDefinition() returns
344e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // null. If we've already seen this variable before, insert()'s second
345e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // return value is false.
346e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
347b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      continue;
348b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
3491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const IncompleteArrayType *ArrayT
350b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        = Context.getAsIncompleteArrayType(VD->getType())) {
3511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (RequireCompleteType(VD->getLocation(),
352b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                              ArrayT->getElementType(),
35363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner                              diag::err_tentative_def_incomplete_type_arr)) {
354b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        VD->setInvalidDecl();
35563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner        continue;
356275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor      }
3571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      // Set the length of the array to 1 (C99 6.9.2p5).
35963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
36063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
36146a617a792bfab0d9b1e057371ea3b9540802226John McCall      QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
36246a617a792bfab0d9b1e057371ea3b9540802226John McCall                                                One, ArrayType::Normal, 0);
36363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      VD->setType(T);
3641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
365b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                                   diag::err_tentative_def_incomplete_type))
366b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      VD->setInvalidDecl();
367b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
368b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    // Notify the consumer that we've completed a tentative definition.
369b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    if (!VD->isInvalidDecl())
370b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      Consumer.CompleteTentativeDefinition(VD);
371b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
372275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  }
373e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner
37449b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Output warning for unused file scoped decls.
375aab0132698c7550d0095986fb1fb4887bf18c3e8John McCall  for (llvm::SmallVectorImpl<const DeclaratorDecl*>::iterator
37649b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis         I = UnusedFileScopedDecls.begin(),
37749b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis         E = UnusedFileScopedDecls.end(); I != E; ++I) {
378bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
379bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      const FunctionDecl *DiagD;
380bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      if (!FD->hasBody(DiagD))
381bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis        DiagD = FD;
3823d27b107c515ab1a8fd47b33c41adcf5a65f465bArgyrios Kyrtzidis      Diag(DiagD->getLocation(),
38342cbd782cf17223076e5b6c0607414d9d68aae19Argyrios Kyrtzidis           isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function
3843d27b107c515ab1a8fd47b33c41adcf5a65f465bArgyrios Kyrtzidis                                     : diag::warn_unused_function)
385bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis            << DiagD->getDeclName();
386bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    } else {
387bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
388bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      if (!DiagD)
389bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis        DiagD = cast<VarDecl>(*I);
390bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis      Diag(DiagD->getLocation(), diag::warn_unused_variable)
391bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis            << DiagD->getDeclName();
392bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis    }
39349b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  }
39487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
39587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor  TUScope = 0;
3969299f3fa85796613cc787a2062c9562d07c8613eChris Lattner}
3979299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
3989299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
3995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper functions.
4015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4038517d9b731f065cdfc55ec0f3ddf5d564d988648Anders CarlssonDeclContext *Sema::getFunctionLevelDeclContext() {
404db0ee1da16e9dbec19b144c9cd96ee9f55fe0c53John McCall  DeclContext *DC = CurContext;
4051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
406d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor  while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
4078517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson    DC = DC->getParent();
4081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4098517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  return DC;
4108517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson}
4118517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson
412371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// getCurFunctionDecl - If inside of a function body, this returns a pointer
413371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// to the function decl for the function being parsed.  If we're currently
414371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// in a 'block', this returns the containing context.
415371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerFunctionDecl *Sema::getCurFunctionDecl() {
4168517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
417371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return dyn_cast<FunctionDecl>(DC);
418371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
419371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
420c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel DunbarObjCMethodDecl *Sema::getCurMethodDecl() {
4218517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
422d7612e183bb09d04677d0ddde89ee29130ffb715Steve Naroff  return dyn_cast<ObjCMethodDecl>(DC);
423c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar}
424371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
425371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerNamedDecl *Sema::getCurFunctionOrMethodDecl() {
4268517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
427371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
4284afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return cast<NamedDecl>(DC);
429371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return 0;
430371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
431371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
43225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas GregorSema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
4335e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  if (!this->Emit())
4345e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    return;
4351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43625a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // If this is not a note, and we're in a template instantiation
43725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // that is different from the last template instantiation where
43825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // we emitted an error, print a template instantiation
43925a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // backtrace.
44025a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
44125a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      !SemaRef.ActiveTemplateInstantiations.empty() &&
4421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      SemaRef.ActiveTemplateInstantiations.back()
44325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor        != SemaRef.LastTemplateInstantiationErrorContext) {
44425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    SemaRef.PrintInstantiationStack();
4451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.LastTemplateInstantiationErrorContext
44625a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      = SemaRef.ActiveTemplateInstantiations.back();
44725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  }
44825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor}
4492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
450eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas GregorSema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) {
451eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor  if (isSFINAEContext()) {
452eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor    switch (Diagnostic::getDiagnosticSFINAEResponse(DiagID)) {
453eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor    case Diagnostic::SFINAE_Report:
454eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      // Fall through; we'll report the diagnostic below.
455eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      break;
456eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor
457eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor    case Diagnostic::SFINAE_SubstitutionFailure:
458eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      // Count this failure so that we know that template argument deduction
459eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      // has failed.
460eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      ++NumSFINAEErrors;
461eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      // Fall through
462eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor
463eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor    case Diagnostic::SFINAE_Suppress:
464eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      // Suppress this diagnostic.
465eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      Diags.setLastDiagnosticIgnored();
466eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      return SemaDiagnosticBuilder(*this);
467eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor    }
468eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor  }
469eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor
470eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor  DiagnosticBuilder DB = Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
471eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor  return SemaDiagnosticBuilder(DB, *this, DiagID);
472eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor}
473eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor
47491a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::SemaDiagnosticBuilder
47591a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
47691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
47791a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  PD.Emit(Builder);
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47991a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  return Builder;
48091a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson}
48191a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson
48223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \brief Determines the active Scope associated with the given declaration
48323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// context.
48423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
48523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// This routine maps a declaration context to the active Scope object that
48623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// represents that declaration context in the parser. It is typically used
48723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// from "scope-less" code (e.g., template instantiation, lazy creation of
48823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// declarations) that injects a name for name-lookup purposes and, therefore,
48923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// must update the Scope.
49023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor///
49123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// \returns The scope corresponding to the given declaraion context, or NULL
49223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor/// if no such scope is open.
49323c94dbb6631fecdb55ba401aa93722803d980c6Douglas GregorScope *Sema::getScopeForContext(DeclContext *Ctx) {
49423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
49523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  if (!Ctx)
49623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return 0;
49723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
49823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Ctx = Ctx->getPrimaryContext();
49923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  for (Scope *S = getCurScope(); S; S = S->getParent()) {
500cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // Ignore scopes that cannot have declarations. This is important for
501cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    // out-of-line definitions of static class members.
502cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl    if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
503cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl      if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity()))
504cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl        if (Ctx == Entity->getPrimaryContext())
505cddc69fc3fe17b043a287a41e3706766c3d09a79Sebastian Redl          return S;
50623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  }
50723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor
50823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return 0;
50923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor}
5109ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5119ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Enter a new function scope
5129ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushFunctionScope() {
5139ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.empty()) {
5149ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    // Use the "top" function scope rather than having to allocate memory for
5159ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    // a new scope.
5169ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    TopFunctionScope.Clear(getDiagnostics().getNumErrors());
5179ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    FunctionScopes.push_back(&TopFunctionScope);
5189ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return;
5199ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  }
5209ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5219ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  FunctionScopes.push_back(
5229ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor                      new FunctionScopeInfo(getDiagnostics().getNumErrors()));
5239ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
5249ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5259ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
5269ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics().getNumErrors(),
5279ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor                                              BlockScope, Block));
5289ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
5299ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5309ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PopFunctionOrBlockScope() {
5319ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.back() != &TopFunctionScope)
5329ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    delete FunctionScopes.back();
5339ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  else
5349ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    TopFunctionScope.Clear(getDiagnostics().getNumErrors());
5359ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5369ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  FunctionScopes.pop_back();
5379ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
5389ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5399ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Determine whether any errors occurred within this function/method/
5409ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// block.
5419ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorbool Sema::hasAnyErrorsInThisFunction() const {
5429ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  unsigned NumErrors = TopFunctionScope.NumErrorsAtStartOfFunction;
5439ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (!FunctionScopes.empty())
5449ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    NumErrors = FunctionScopes.back()->NumErrorsAtStartOfFunction;
5459ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  return NumErrors != getDiagnostics().getNumErrors();
5469ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
5479ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5489ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo *Sema::getCurBlock() {
5499ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.empty())
5509ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return 0;
5519ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
5529ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  return dyn_cast<BlockScopeInfo>(FunctionScopes.back());
5539ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
55476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
55576bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall// Pin this vtable to this file.
55676bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalSemaSource::~ExternalSemaSource() {}
557