Sema.cpp revision 926c4b486a08f698cd3a367fd6f1a3a07604358d
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
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "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"
2076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall#include "clang/Sema/ExternalSemaSource.h"
21b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor#include "clang/AST/ASTConsumer.h"
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
2379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/ASTDiagnostic.h"
24c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
25e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Preprocessor.h"
2791a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
284d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner#include "clang/Basic/TargetInfo.h"
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
309ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
319ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorFunctionScopeInfo::~FunctionScopeInfo() { }
329ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
339ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid FunctionScopeInfo::Clear(unsigned NumErrors) {
349ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  NeedsScopeChecking = false;
359ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  LabelMap.clear();
369ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  SwitchStack.clear();
375077c3876beeaed32280af88244e8050078619a8Douglas Gregor  Returns.clear();
389ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  NumErrorsAtStartOfFunction = NumErrors;
399ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
409ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
419ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo::~BlockScopeInfo() { }
429ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
438ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroffvoid Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
448ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroff  TUScope = S;
4544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  PushDeclContext(S, Context.getTranslationUnitDecl());
461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall  VAListTagName = PP.getIdentifierInfo("__va_list_tag");
48c7e04dad588a30c94648b9bd70cdbe25688d7629John McCall
494d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner  if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
50a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *TInfo;
51ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
524d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    // Install [u]int128_t for 64-bit targets.
53a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
544d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
554d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          SourceLocation(),
564d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          &Context.Idents.get("__int128_t"),
57a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                          TInfo), TUScope);
58ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
59a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
604d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
614d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          SourceLocation(),
624d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          &Context.Idents.get("__uint128_t"),
63a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                          TInfo), TUScope);
644d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner  }
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
672ae34ed8caf90ca4044feca07811561053c08666Chris Lattner  if (!PP.getLangOptions().ObjC1) return;
681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69cb83c530ee82c0107720129473fa78336b6f4879Steve Naroff  // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
70319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCSelType().isNull()) {
7113dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian    // Create the built-in typedef for 'SEL'.
7204765ac135e0c4e6b78651c2a287d80a32b2b8b9Fariborz Jahanian    QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
73a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
74ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *SelTypedef
75ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
76ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("SEL"), SelInfo);
77319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    PushOnScopeChains(SelTypedef, TUScope);
78319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
79369a3bd9979cf529eed529aa037de713c213e47dFariborz Jahanian    Context.ObjCSelRedefinitionType = Context.getObjCSelType();
80319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
81319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
826ee1f9c0142e0c374f32fc86312630cd901247cfChris Lattner  // Synthesize "@class Protocol;
83319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCProtoType().isNull()) {
84319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    ObjCInterfaceDecl *ProtocolDecl =
85319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor      ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                &Context.Idents.get("Protocol"),
87319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor                                SourceLocation(), true);
88319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
8910324db994455a9a1520be6cfe5bb29685cde141Fariborz Jahanian    PushOnScopeChains(ProtocolDecl, TUScope, false);
90319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
91de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Create the built-in typedef for 'id'.
92319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCIdType().isNull()) {
93c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0);
94c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = Context.getObjCObjectPointerType(T);
95c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T);
96ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *IdTypedef
97ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
98ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("id"), IdInfo);
99319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    PushOnScopeChains(IdTypedef, TUScope);
100319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
1010f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    Context.ObjCIdRedefinitionType = Context.getObjCIdType();
102319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
103de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Create the built-in typedef for 'Class'.
10414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (Context.getObjCClassType().isNull()) {
105c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0);
106c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = Context.getObjCObjectPointerType(T);
107c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T);
108ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *ClassTypedef
109ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
110ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("Class"), ClassInfo);
11114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    PushOnScopeChains(ClassTypedef, TUScope);
11214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
1130f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    Context.ObjCClassRedefinitionType = Context.getObjCClassType();
11414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
115b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
1163b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff
117f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas GregorSema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
1183a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           bool CompleteTranslationUnit,
1193a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           CodeCompleteConsumer *CodeCompleter)
12082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  : TheTargetAttributesSema(0),
12182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
1221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
1233a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar    ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
1249ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    PackContext(0), TopFunctionScope(0), ParsingDeclDepth(0),
12581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor    IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
1262afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor    GlobalNewDeleteDeclared(false),
12748dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor    CompleteTranslationUnit(CompleteTranslationUnit),
128926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth    NumSFINAEErrors(0), SuppressAccessChecking(false),
129926c4b486a08f698cd3a367fd6f1a3a07604358dChandler Carruth    NonInstantiationEntries(0), CurrentInstantiationScope(0), TyposCorrected(0),
130d064fdc4b7b64ca55b40b70490c79d6f569df78eTed Kremenek    AnalysisWarnings(*this)
131f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor{
1323b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff  TUScope = 0;
13307952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis  if (getLangOptions().CPlusPlus)
13407952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    FieldCollector.reset(new CXXFieldCollector());
1351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13622caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  // Tell diagnostics how to render things from the AST library.
13779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
13879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                       &Context);
1392afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor
1402afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor  ExprEvalContexts.push_back(
1412afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor                  ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovSema::~Sema() {
14582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (PackContext) FreePackedContext();
14682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  delete TheTargetAttributesSema;
1479ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  while (!FunctionScopes.empty())
1489ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    PopFunctionOrBlockScope();
14982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
15082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
1511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
1521e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner/// If there is already an implicit cast, merge into the existing one.
1536fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman/// If isLvalue, the result of the cast is an lvalue.
1541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
15588465d3e996e627bbaa11099b039ddab66d5af2cAnders Carlsson                             CastExpr::CastKind Kind,
156f1b48b7014992155286d58bb1676f9f51031d18bAnders Carlsson                             bool isLvalue, CXXBaseSpecifierArray BasePath) {
1573a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType ExprTy = Context.getCanonicalType(Expr->getType());
1583a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType TypeTy = Context.getCanonicalType(Ty);
1591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1603a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  if (ExprTy == TypeTy)
1613a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    return;
1621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
163680523a91dd3351389667c8de17121ba7ae82673John McCall  if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
164680523a91dd3351389667c8de17121ba7ae82673John McCall    QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
165680523a91dd3351389667c8de17121ba7ae82673John McCall    QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
1663a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
167dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
168dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        << Expr->getSourceRange();
1693a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    }
1703a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  }
1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1726fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // If this is a derived-to-base cast to a through a virtual base, we
1736fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  // need a vtable.
1746fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  if (Kind == CastExpr::CK_DerivedToBase &&
1756fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      BasePathInvolvesVirtualBase(BasePath)) {
1766fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    QualType T = Expr->getType();
1776fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const PointerType *Pointer = T->getAs<PointerType>())
1786fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      T = Pointer->getPointeeType();
1796fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (const RecordType *RecordTy = T->getAs<RecordType>())
1806fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      MarkVTableUsed(Expr->getLocStart(),
1816fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor                     cast<CXXRecordDecl>(RecordTy->getDecl()));
1826fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor  }
1836fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor
184eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
185cee22421929c91b481f4d1bb85cd48c0f6b7510bAnders Carlsson    if (ImpCast->getCastKind() == Kind && BasePath.empty()) {
1864c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      ImpCast->setType(Ty);
1874c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      ImpCast->setLvalueCast(isLvalue);
1884c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      return;
1894c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson    }
1904c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson  }
1914c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson
1927ab9d574d27ecee1f130e5755aa403e5ab529b6bAnders Carlsson  Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, BasePath, isLvalue);
1931e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner}
1941e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner
195394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattnervoid Sema::DeleteExpr(ExprTy *E) {
19605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (E) static_cast<Expr*>(E)->Destroy(Context);
197394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattner}
198394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattnervoid Sema::DeleteStmt(StmtTy *S) {
19905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (S) static_cast<Stmt*>(S)->Destroy(Context);
200394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattner}
201394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattner
2029299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// ActOnEndOfTranslationUnit - This is called at the very end of the
2039299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// translation unit when EOF is reached and all but the top-level scope is
2049299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// popped.
20547268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregorvoid Sema::ActOnEndOfTranslationUnit() {
206d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson  while (1) {
207d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // C++: Perform implicit template instantiations.
208d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    //
209d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // FIXME: When we perform these implicit instantiations, we do not carefully
210d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // keep track of the point of instantiation (C++ [temp.point]). This means
211d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // that name lookup that occurs within the template instantiation will
212d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // always happen at the end of the translation unit, so it will find
213d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // some names that should not be found. Although this is common behavior
214d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // for C++ compilers, it is technically wrong. In the future, we either need
215d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // to be able to filter the results of name lookup or we need to perform
216d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // template instantiations earlier.
217d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    PerformPendingImplicitInstantiations();
218d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson
2196fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    /// If DefinedUsedVTables ends up marking any virtual member
2206fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    /// functions it might lead to more pending template
221d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    /// instantiations, which is why we need to loop here.
2226fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor    if (!DefineUsedVTables())
223d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson      break;
224d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson  }
225d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson
22647268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor  // Remove functions that turned out to be used.
22747268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor  UnusedStaticFuncs.erase(std::remove_if(UnusedStaticFuncs.begin(),
22847268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor                                         UnusedStaticFuncs.end(),
229c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor                             std::bind2nd(std::mem_fun(&FunctionDecl::isUsed),
230c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor                                          true)),
23147268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor                          UnusedStaticFuncs.end());
23247268a3f2843a8d64f3a6fef1e9a9dde1feb4a8cDouglas Gregor
23363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // Check for #pragma weak identifiers that were never declared
23463d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // FIXME: This will cause diagnostics to be emitted in a non-determinstic
23563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // order!  Iterating over a densemap like this is bad.
236e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
23763d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       I = WeakUndeclaredIdentifiers.begin(),
23863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
23963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    if (I->second.getUsed()) continue;
2401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
24263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      << I->first;
243e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  }
244e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn
245f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas Gregor  if (!CompleteTranslationUnit)
246f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas Gregor    return;
247f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas Gregor
248275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  // C99 6.9.2p2:
249275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   A declaration of an identifier for an object that has file
250275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   scope without an initializer, and without a storage-class
251275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   specifier or with the storage-class specifier static,
252275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   constitutes a tentative definition. If a translation unit
253275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   contains one or more tentative definitions for an identifier,
254275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   and the translation unit contains no external definition for
255275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   that identifier, then the behavior is exactly as if the
256275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit contains a file scope declaration of that
257275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   identifier, with the composite type as of the end of the
258275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit, with an initializer equal to 0.
259e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  llvm::SmallSet<VarDecl *, 32> Seen;
260e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) {
261e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    VarDecl *VD = TentativeDefinitions[i]->getActingDefinition();
262e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
263e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // If the tentative definition was completed, getActingDefinition() returns
264e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // null. If we've already seen this variable before, insert()'s second
265e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // return value is false.
266e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
267b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      continue;
268b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
2691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const IncompleteArrayType *ArrayT
270b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        = Context.getAsIncompleteArrayType(VD->getType())) {
2711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (RequireCompleteType(VD->getLocation(),
272b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                              ArrayT->getElementType(),
27363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner                              diag::err_tentative_def_incomplete_type_arr)) {
274b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        VD->setInvalidDecl();
27563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner        continue;
276275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor      }
2771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      // Set the length of the array to 1 (C99 6.9.2p5).
27963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
28063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
28146a617a792bfab0d9b1e057371ea3b9540802226John McCall      QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
28246a617a792bfab0d9b1e057371ea3b9540802226John McCall                                                One, ArrayType::Normal, 0);
28363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      VD->setType(T);
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
285b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                                   diag::err_tentative_def_incomplete_type))
286b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      VD->setInvalidDecl();
287b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
288b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    // Notify the consumer that we've completed a tentative definition.
289b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    if (!VD->isInvalidDecl())
290b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      Consumer.CompleteTentativeDefinition(VD);
291b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
292275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  }
293e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner
294e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner  // Output warning for unused functions.
295e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner  for (std::vector<FunctionDecl*>::iterator
296e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner       F = UnusedStaticFuncs.begin(),
297e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner       FEnd = UnusedStaticFuncs.end();
298e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner       F != FEnd;
299e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner       ++F)
300e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner    Diag((*F)->getLocation(), diag::warn_unused_function) << (*F)->getDeclName();
301e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner
3029299f3fa85796613cc787a2062c9562d07c8613eChris Lattner}
3039299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
3049299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper functions.
3075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3098517d9b731f065cdfc55ec0f3ddf5d564d988648Anders CarlssonDeclContext *Sema::getFunctionLevelDeclContext() {
310db0ee1da16e9dbec19b144c9cd96ee9f55fe0c53John McCall  DeclContext *DC = CurContext;
3111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
312d9008318fe395dcbb9049cfb4f2b87cfb5a75f3aDouglas Gregor  while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
3138517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson    DC = DC->getParent();
3141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3158517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  return DC;
3168517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson}
3178517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson
318371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// getCurFunctionDecl - If inside of a function body, this returns a pointer
319371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// to the function decl for the function being parsed.  If we're currently
320371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// in a 'block', this returns the containing context.
321371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerFunctionDecl *Sema::getCurFunctionDecl() {
3228517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
323371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return dyn_cast<FunctionDecl>(DC);
324371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
325371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
326c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel DunbarObjCMethodDecl *Sema::getCurMethodDecl() {
3278517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
328d7612e183bb09d04677d0ddde89ee29130ffb715Steve Naroff  return dyn_cast<ObjCMethodDecl>(DC);
329c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar}
330371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
331371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerNamedDecl *Sema::getCurFunctionOrMethodDecl() {
3328517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
333371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
3344afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return cast<NamedDecl>(DC);
335371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return 0;
336371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
337371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
33825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas GregorSema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
3395e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  if (!this->Emit())
3405e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    return;
3411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // If this is not a note, and we're in a template instantiation
34325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // that is different from the last template instantiation where
34425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // we emitted an error, print a template instantiation
34525a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // backtrace.
34625a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
34725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      !SemaRef.ActiveTemplateInstantiations.empty() &&
3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      SemaRef.ActiveTemplateInstantiations.back()
34925a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor        != SemaRef.LastTemplateInstantiationErrorContext) {
35025a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    SemaRef.PrintInstantiationStack();
3511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.LastTemplateInstantiationErrorContext
35225a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      = SemaRef.ActiveTemplateInstantiations.back();
35325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  }
35425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor}
3552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
356eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas GregorSema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) {
357eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor  if (isSFINAEContext()) {
358eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor    switch (Diagnostic::getDiagnosticSFINAEResponse(DiagID)) {
359eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor    case Diagnostic::SFINAE_Report:
360eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      // Fall through; we'll report the diagnostic below.
361eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      break;
362eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor
363eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor    case Diagnostic::SFINAE_SubstitutionFailure:
364eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      // Count this failure so that we know that template argument deduction
365eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      // has failed.
366eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      ++NumSFINAEErrors;
367eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      // Fall through
368eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor
369eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor    case Diagnostic::SFINAE_Suppress:
370eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      // Suppress this diagnostic.
371eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      Diags.setLastDiagnosticIgnored();
372eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor      return SemaDiagnosticBuilder(*this);
373eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor    }
374eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor  }
375eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor
376eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor  DiagnosticBuilder DB = Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
377eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor  return SemaDiagnosticBuilder(DB, *this, DiagID);
378eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor}
379eab5d1eaaa662c849f1f9920dc8c6a31d7c32d47Douglas Gregor
38091a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::SemaDiagnosticBuilder
38191a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
38291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
38391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  PD.Emit(Builder);
3841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38591a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  return Builder;
38691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson}
38791a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson
3889ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
3899ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Enter a new function scope
3909ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushFunctionScope() {
3919ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.empty()) {
3929ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    // Use the "top" function scope rather than having to allocate memory for
3939ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    // a new scope.
3949ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    TopFunctionScope.Clear(getDiagnostics().getNumErrors());
3959ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    FunctionScopes.push_back(&TopFunctionScope);
3969ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return;
3979ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  }
3989ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
3999ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  FunctionScopes.push_back(
4009ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor                      new FunctionScopeInfo(getDiagnostics().getNumErrors()));
4019ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
4029ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
4039ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
4049ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics().getNumErrors(),
4059ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor                                              BlockScope, Block));
4069ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
4079ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
4089ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorvoid Sema::PopFunctionOrBlockScope() {
4099ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.back() != &TopFunctionScope)
4109ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    delete FunctionScopes.back();
4119ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  else
4129ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    TopFunctionScope.Clear(getDiagnostics().getNumErrors());
4139ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
4149ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  FunctionScopes.pop_back();
4159ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
4169ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
4179ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// \brief Determine whether any errors occurred within this function/method/
4189ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor/// block.
4199ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregorbool Sema::hasAnyErrorsInThisFunction() const {
4209ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  unsigned NumErrors = TopFunctionScope.NumErrorsAtStartOfFunction;
4219ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (!FunctionScopes.empty())
4229ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    NumErrors = FunctionScopes.back()->NumErrorsAtStartOfFunction;
4239ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  return NumErrors != getDiagnostics().getNumErrors();
4249ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
4259ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
4269ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas GregorBlockScopeInfo *Sema::getCurBlock() {
4279ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (FunctionScopes.empty())
4289ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor    return 0;
4299ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor
4309ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  return dyn_cast<BlockScopeInfo>(FunctionScopes.back());
4319ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor}
43276bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
43376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall// Pin this vtable to this file.
43476bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCallExternalSemaSource::~ExternalSemaSource() {}
435