Sema.cpp revision e6bbc01d1c4ec5241df36042e0a4a12a6711934b
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"
20b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor#include "clang/AST/ASTConsumer.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
2279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/ASTDiagnostic.h"
23c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
24e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Preprocessor.h"
2691a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson#include "clang/Basic/PartialDiagnostic.h"
274d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner#include "clang/Basic/TargetInfo.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
293f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor
300a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattnerstatic inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
31c303606a8e2c1b7f8c985d0e7adf9fb9bb818070Anders Carlsson  if (C.getLangOptions().CPlusPlus)
321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return CXXRecordDecl::Create(C, TagDecl::TK_struct,
33c303606a8e2c1b7f8c985d0e7adf9fb9bb818070Anders Carlsson                                 C.getTranslationUnitDecl(),
34df042e6c2bf06b2d9ed53c52469599ac1bd93a3fTed Kremenek                                 SourceLocation(), &C.Idents.get(Name));
35fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner
361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return RecordDecl::Create(C, TagDecl::TK_struct,
37fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner                            C.getTranslationUnitDecl(),
38fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner                            SourceLocation(), &C.Idents.get(Name));
39c303606a8e2c1b7f8c985d0e7adf9fb9bb818070Anders Carlsson}
40c303606a8e2c1b7f8c985d0e7adf9fb9bb818070Anders Carlsson
418ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroffvoid Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
428ee529b5671295ea38c249df8b9d3766c905cfa7Steve Naroff  TUScope = S;
4344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  PushDeclContext(S, Context.getTranslationUnitDecl());
441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
454d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner  if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
46a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *TInfo;
47ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
484d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    // Install [u]int128_t for 64-bit targets.
49a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
504d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
514d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          SourceLocation(),
524d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          &Context.Idents.get("__int128_t"),
53a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                          TInfo), TUScope);
54ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
55a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
564d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
574d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          SourceLocation(),
584d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner                                          &Context.Idents.get("__uint128_t"),
59a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                          TInfo), TUScope);
604d150c84514dbf15975960a3ea46bdf6b7f16a5bChris Lattner  }
611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
632ae34ed8caf90ca4044feca07811561053c08666Chris Lattner  if (!PP.getLangOptions().ObjC1) return;
641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
65cb83c530ee82c0107720129473fa78336b6f4879Steve Naroff  // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
66319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCSelType().isNull()) {
6713dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian    // Create the built-in typedef for 'SEL'.
6804765ac135e0c4e6b78651c2a287d80a32b2b8b9Fariborz Jahanian    QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
69a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
70ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *SelTypedef
71ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
72ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("SEL"), SelInfo);
73319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    PushOnScopeChains(SelTypedef, TUScope);
74319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
75369a3bd9979cf529eed529aa037de713c213e47dFariborz Jahanian    Context.ObjCSelRedefinitionType = Context.getObjCSelType();
76319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
77319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
786ee1f9c0142e0c374f32fc86312630cd901247cfChris Lattner  // Synthesize "@class Protocol;
79319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCProtoType().isNull()) {
80319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    ObjCInterfaceDecl *ProtocolDecl =
81319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor      ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                &Context.Idents.get("Protocol"),
83319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor                                SourceLocation(), true);
84319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
8510324db994455a9a1520be6cfe5bb29685cde141Fariborz Jahanian    PushOnScopeChains(ProtocolDecl, TUScope, false);
86319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
87de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Create the built-in typedef for 'id'.
88319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (Context.getObjCIdType().isNull()) {
89ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    QualType IdT = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy);
90a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(IdT);
91ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *IdTypedef
92ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
93ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("id"), IdInfo);
94319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    PushOnScopeChains(IdTypedef, TUScope);
95319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
960f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    Context.ObjCIdRedefinitionType = Context.getObjCIdType();
97319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  }
98de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Create the built-in typedef for 'Class'.
9914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (Context.getObjCClassType().isNull()) {
100ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    QualType ClassType
101ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy);
102a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(ClassType);
103ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    TypedefDecl *ClassTypedef
104ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
105ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                            &Context.Idents.get("Class"), ClassInfo);
10614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    PushOnScopeChains(ClassTypedef, TUScope);
10714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
1080f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    Context.ObjCClassRedefinitionType = Context.getObjCClassType();
10914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
110b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
1113b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff
112f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas GregorSema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
1133a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           bool CompleteTranslationUnit,
1143a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar           CodeCompleteConsumer *CodeCompleter)
11582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  : TheTargetAttributesSema(0),
11682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
1171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
1183a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar    ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
119db0ee1da16e9dbec19b144c9cd96ee9f55fe0c53John McCall    CurBlock(0), PackContext(0), ParsingDeclDepth(0),
12081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor    IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
1212afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor    GlobalNewDeleteDeclared(false),
12248dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor    CompleteTranslationUnit(CompleteTranslationUnit),
123f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor    NumSFINAEErrors(0), NonInstantiationEntries(0),
124d0ed44812057f1adf0ebe38f3fab55973c4efaebTed Kremenek    CurrentInstantiationScope(0), TyposCorrected(0)
125f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor{
1263b950178e23b1fe65552996d7bfb7542d03f89daSteve Naroff  TUScope = 0;
12707952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis  if (getLangOptions().CPlusPlus)
12807952324dda0e758c17f8bc3015793c65c51c48cArgyrios Kyrtzidis    FieldCollector.reset(new CXXFieldCollector());
1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13022caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  // Tell diagnostics how to render things from the AST library.
13179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
13279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                       &Context);
1332afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor
1342afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor  ExprEvalContexts.push_back(
1352afce7248b7a362f1e322ad18e43484d575b9c9dDouglas Gregor                  ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovSema::~Sema() {
13982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (PackContext) FreePackedContext();
14082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  delete TheTargetAttributesSema;
14182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
14282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
1441e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner/// If there is already an implicit cast, merge into the existing one.
1456fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman/// If isLvalue, the result of the cast is an lvalue.
1461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
147c0a2fd8f092722c8f78b566ac4506a21437040e9Anders Carlsson                             CastExpr::CastKind Kind, bool isLvalue) {
1483a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType ExprTy = Context.getCanonicalType(Expr->getType());
1493a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  QualType TypeTy = Context.getCanonicalType(Ty);
1501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1513a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  if (ExprTy == TypeTy)
1523a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    return;
1531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
154680523a91dd3351389667c8de17121ba7ae82673John McCall  if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
155680523a91dd3351389667c8de17121ba7ae82673John McCall    QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
156680523a91dd3351389667c8de17121ba7ae82673John McCall    QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
1573a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
158dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
159dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        << Expr->getSourceRange();
1603a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang    }
1613a2c7449e356ed74552450bc1dd50691c8202770Mon P Wang  }
1621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16351313c39c84407dd6a323be99a8c322bf8d052a9John McCall  CheckImplicitConversion(Expr, Ty);
164680523a91dd3351389667c8de17121ba7ae82673John McCall
165eb8f3063257a392f15aea48d42fb73ec51afc548Douglas Gregor  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
1664c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson    if (ImpCast->getCastKind() == Kind) {
1674c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      ImpCast->setType(Ty);
1684c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      ImpCast->setLvalueCast(isLvalue);
1694c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson      return;
1704c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson    }
1714c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson  }
1724c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson
1734c5fad3f73957420b0410f7370cbd63b09f32a1cAnders Carlsson  Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue);
1741e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner}
1751e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner
176394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattnervoid Sema::DeleteExpr(ExprTy *E) {
17705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (E) static_cast<Expr*>(E)->Destroy(Context);
178394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattner}
179394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattnervoid Sema::DeleteStmt(StmtTy *S) {
18005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (S) static_cast<Stmt*>(S)->Destroy(Context);
181394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattner}
182394a3fd57d2430f26dd1872cc7226437f5698e63Chris Lattner
1839299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// ActOnEndOfTranslationUnit - This is called at the very end of the
1849299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// translation unit when EOF is reached and all but the top-level scope is
1859299f3fa85796613cc787a2062c9562d07c8613eChris Lattner/// popped.
1869299f3fa85796613cc787a2062c9562d07c8613eChris Lattnervoid Sema::ActOnEndOfTranslationUnit() {
187d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson
188e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner  // Remove functions that turned out to be used.
189e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner  UnusedStaticFuncs.erase(std::remove_if(UnusedStaticFuncs.begin(),
190e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner                                         UnusedStaticFuncs.end(),
191e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner                                         std::mem_fun(&FunctionDecl::isUsed)),
192e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner                          UnusedStaticFuncs.end());
193e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner
194d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson  while (1) {
195d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // C++: Perform implicit template instantiations.
196d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    //
197d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // FIXME: When we perform these implicit instantiations, we do not carefully
198d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // keep track of the point of instantiation (C++ [temp.point]). This means
199d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // that name lookup that occurs within the template instantiation will
200d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // always happen at the end of the translation unit, so it will find
201d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // some names that should not be found. Although this is common behavior
202d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // for C++ compilers, it is technically wrong. In the future, we either need
203d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // to be able to filter the results of name lookup or we need to perform
204d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    // template instantiations earlier.
205d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    PerformPendingImplicitInstantiations();
206d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson
207d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    /// If ProcessPendingClassesWithUnmarkedVirtualMembers ends up marking
208d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    /// any virtual member functions it might lead to more pending template
209d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    /// instantiations, which is why we need to loop here.
210d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson    if (!ProcessPendingClassesWithUnmarkedVirtualMembers())
211d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson      break;
212d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson  }
213d6a637f8c8a93863509fc1bc555513ff6504957dAnders Carlsson
21463d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // Check for #pragma weak identifiers that were never declared
21563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // FIXME: This will cause diagnostics to be emitted in a non-determinstic
21663d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // order!  Iterating over a densemap like this is bad.
217e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
21863d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       I = WeakUndeclaredIdentifiers.begin(),
21963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner       E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
22063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    if (I->second.getUsed()) continue;
2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22263d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner    Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
22363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      << I->first;
224e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  }
225e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn
226f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas Gregor  if (!CompleteTranslationUnit)
227f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas Gregor    return;
228f807fe0d1a865f4c6ba7e494cf4ae360c4173521Douglas Gregor
229275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  // C99 6.9.2p2:
230275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   A declaration of an identifier for an object that has file
231275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   scope without an initializer, and without a storage-class
232275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   specifier or with the storage-class specifier static,
233275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   constitutes a tentative definition. If a translation unit
234275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   contains one or more tentative definitions for an identifier,
235275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   and the translation unit contains no external definition for
236275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   that identifier, then the behavior is exactly as if the
237275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit contains a file scope declaration of that
238275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   identifier, with the composite type as of the end of the
239275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  //   translation unit, with an initializer equal to 0.
240e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  llvm::SmallSet<VarDecl *, 32> Seen;
241e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) {
242e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    VarDecl *VD = TentativeDefinitions[i]->getActingDefinition();
243e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
244e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // If the tentative definition was completed, getActingDefinition() returns
245e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // null. If we've already seen this variable before, insert()'s second
246e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    // return value is false.
247e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
248b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      continue;
249b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
2501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const IncompleteArrayType *ArrayT
251b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        = Context.getAsIncompleteArrayType(VD->getType())) {
2521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (RequireCompleteType(VD->getLocation(),
253b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                              ArrayT->getElementType(),
25463d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner                              diag::err_tentative_def_incomplete_type_arr)) {
255b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        VD->setInvalidDecl();
25663d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner        continue;
257275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor      }
2581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      // Set the length of the array to 1 (C99 6.9.2p5).
26063d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
26163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
26246a617a792bfab0d9b1e057371ea3b9540802226John McCall      QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
26346a617a792bfab0d9b1e057371ea3b9540802226John McCall                                                One, ArrayType::Normal, 0);
26463d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner      VD->setType(T);
2651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
266b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                                   diag::err_tentative_def_incomplete_type))
267b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      VD->setInvalidDecl();
268b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
269b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    // Notify the consumer that we've completed a tentative definition.
270b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor    if (!VD->isInvalidDecl())
271b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      Consumer.CompleteTentativeDefinition(VD);
272b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor
273275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor  }
274e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner
275e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner  // Output warning for unused functions.
276e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner  for (std::vector<FunctionDecl*>::iterator
277e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner       F = UnusedStaticFuncs.begin(),
278e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner       FEnd = UnusedStaticFuncs.end();
279e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner       F != FEnd;
280e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner       ++F)
281e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner    Diag((*F)->getLocation(), diag::warn_unused_function) << (*F)->getDeclName();
282e6bbc01d1c4ec5241df36042e0a4a12a6711934bTanya Lattner
2839299f3fa85796613cc787a2062c9562d07c8613eChris Lattner}
2849299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
2859299f3fa85796613cc787a2062c9562d07c8613eChris Lattner
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper functions.
2885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2908517d9b731f065cdfc55ec0f3ddf5d564d988648Anders CarlssonDeclContext *Sema::getFunctionLevelDeclContext() {
291db0ee1da16e9dbec19b144c9cd96ee9f55fe0c53John McCall  DeclContext *DC = CurContext;
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2938517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  while (isa<BlockDecl>(DC))
2948517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson    DC = DC->getParent();
2951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2968517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  return DC;
2978517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson}
2988517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson
299371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// getCurFunctionDecl - If inside of a function body, this returns a pointer
300371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// to the function decl for the function being parsed.  If we're currently
301371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner/// in a 'block', this returns the containing context.
302371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerFunctionDecl *Sema::getCurFunctionDecl() {
3038517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
304371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return dyn_cast<FunctionDecl>(DC);
305371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
306371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
307c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel DunbarObjCMethodDecl *Sema::getCurMethodDecl() {
3088517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
309d7612e183bb09d04677d0ddde89ee29130ffb715Steve Naroff  return dyn_cast<ObjCMethodDecl>(DC);
310c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar}
311371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
312371f258e61e1365b951b17931a3c5ac1530fd1a0Chris LattnerNamedDecl *Sema::getCurFunctionOrMethodDecl() {
3138517d9b731f065cdfc55ec0f3ddf5d564d988648Anders Carlsson  DeclContext *DC = getFunctionLevelDeclContext();
314371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
3154afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return cast<NamedDecl>(DC);
316371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  return 0;
317371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner}
318371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner
31925a88bbf042317976f0d9cbfa87dfe89426e8393Douglas GregorSema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
3205e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor  if (!this->Emit())
3215e9f35c7cb61aea46f56d46c77cbcf47c0cf28baDouglas Gregor    return;
3221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // If this is not a note, and we're in a template instantiation
32425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // that is different from the last template instantiation where
32525a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // we emitted an error, print a template instantiation
32625a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  // backtrace.
32725a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
32825a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      !SemaRef.ActiveTemplateInstantiations.empty() &&
3291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      SemaRef.ActiveTemplateInstantiations.back()
33025a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor        != SemaRef.LastTemplateInstantiationErrorContext) {
33125a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor    SemaRef.PrintInstantiationStack();
3321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.LastTemplateInstantiationErrorContext
33325a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor      = SemaRef.ActiveTemplateInstantiations.back();
33425a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor  }
33525a88bbf042317976f0d9cbfa87dfe89426e8393Douglas Gregor}
3362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
33791a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::SemaDiagnosticBuilder
33891a0cc913ecc5619b76d2e40742fd09725be8c56Anders CarlssonSema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
33991a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
34091a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  PD.Emit(Builder);
3411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34291a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson  return Builder;
34391a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson}
34491a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson
3452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorvoid Sema::ActOnComment(SourceRange Comment) {
3462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Context.Comments.push_back(Comment);
3472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
34891a0cc913ecc5619b76d2e40742fd09725be8c56Anders Carlsson
349