Sema.cpp revision f71b595f9eb237f28563d8247ce603a4c435d595
150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho//
359d709d503bab6e2b61931737e662dd293b40578ccornelius//                     The LLVM Compiler Infrastructure
48393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius//
554dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius// This file is distributed under the University of Illinois Open Source
654dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius// License. See LICENSE.TXT for details.
750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho//
850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho//===----------------------------------------------------------------------===//
950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho//
1059d709d503bab6e2b61931737e662dd293b40578ccornelius// This file implements the actions class which performs semantic analysis and
1150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho// builds an AST out of a parse stream.
1250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho//
1350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho//===----------------------------------------------------------------------===//
1459d709d503bab6e2b61931737e662dd293b40578ccornelius
1559d709d503bab6e2b61931737e662dd293b40578ccornelius#include "Sema.h"
1659d709d503bab6e2b61931737e662dd293b40578ccornelius#include "TargetAttributesSema.h"
1759d709d503bab6e2b61931737e662dd293b40578ccornelius#include "llvm/ADT/DenseMap.h"
1859d709d503bab6e2b61931737e662dd293b40578ccornelius#include "llvm/ADT/SmallSet.h"
1959d709d503bab6e2b61931737e662dd293b40578ccornelius#include "llvm/ADT/APFloat.h"
2059d709d503bab6e2b61931737e662dd293b40578ccornelius#include "clang/AST/ASTConsumer.h"
2159d709d503bab6e2b61931737e662dd293b40578ccornelius#include "clang/AST/ASTContext.h"
2259d709d503bab6e2b61931737e662dd293b40578ccornelius#include "clang/AST/ASTDiagnostic.h"
2359d709d503bab6e2b61931737e662dd293b40578ccornelius#include "clang/AST/DeclObjC.h"
2459d709d503bab6e2b61931737e662dd293b40578ccornelius#include "clang/AST/Expr.h"
2559d709d503bab6e2b61931737e662dd293b40578ccornelius#include "clang/Lex/Preprocessor.h"
2659d709d503bab6e2b61931737e662dd293b40578ccornelius#include "clang/Basic/PartialDiagnostic.h"
2759d709d503bab6e2b61931737e662dd293b40578ccornelius#include "clang/Basic/TargetInfo.h"
2859d709d503bab6e2b61931737e662dd293b40578ccorneliususing namespace clang;
2959d709d503bab6e2b61931737e662dd293b40578ccornelius
3059d709d503bab6e2b61931737e662dd293b40578ccorneliusFunctionScopeInfo::~FunctionScopeInfo() { }
3159d709d503bab6e2b61931737e662dd293b40578ccornelius
3259d709d503bab6e2b61931737e662dd293b40578ccorneliusvoid FunctionScopeInfo::Clear(unsigned NumErrors) {
3359d709d503bab6e2b61931737e662dd293b40578ccornelius  NeedsScopeChecking = false;
3459d709d503bab6e2b61931737e662dd293b40578ccornelius  LabelMap.clear();
3559d709d503bab6e2b61931737e662dd293b40578ccornelius  SwitchStack.clear();
3659d709d503bab6e2b61931737e662dd293b40578ccornelius  Returns.clear();
3759d709d503bab6e2b61931737e662dd293b40578ccornelius  NumErrorsAtStartOfFunction = NumErrors;
3859d709d503bab6e2b61931737e662dd293b40578ccornelius}
3959d709d503bab6e2b61931737e662dd293b40578ccornelius
4059d709d503bab6e2b61931737e662dd293b40578ccorneliusBlockScopeInfo::~BlockScopeInfo() { }
4159d709d503bab6e2b61931737e662dd293b40578ccornelius
4259d709d503bab6e2b61931737e662dd293b40578ccorneliusvoid Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
4359d709d503bab6e2b61931737e662dd293b40578ccornelius  TUScope = S;
4459d709d503bab6e2b61931737e662dd293b40578ccornelius  PushDeclContext(S, Context.getTranslationUnitDecl());
4559d709d503bab6e2b61931737e662dd293b40578ccornelius
4659d709d503bab6e2b61931737e662dd293b40578ccornelius  VAListTagName = PP.getIdentifierInfo("__va_list_tag");
4759d709d503bab6e2b61931737e662dd293b40578ccornelius
4859d709d503bab6e2b61931737e662dd293b40578ccornelius  if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
4959d709d503bab6e2b61931737e662dd293b40578ccornelius    TypeSourceInfo *TInfo;
5059d709d503bab6e2b61931737e662dd293b40578ccornelius
5159d709d503bab6e2b61931737e662dd293b40578ccornelius    // Install [u]int128_t for 64-bit targets.
5259d709d503bab6e2b61931737e662dd293b40578ccornelius    TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
5359d709d503bab6e2b61931737e662dd293b40578ccornelius    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
5459d709d503bab6e2b61931737e662dd293b40578ccornelius                                          SourceLocation(),
5559d709d503bab6e2b61931737e662dd293b40578ccornelius                                          &Context.Idents.get("__int128_t"),
5659d709d503bab6e2b61931737e662dd293b40578ccornelius                                          TInfo), TUScope);
5759d709d503bab6e2b61931737e662dd293b40578ccornelius
5859d709d503bab6e2b61931737e662dd293b40578ccornelius    TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
5959d709d503bab6e2b61931737e662dd293b40578ccornelius    PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
6059d709d503bab6e2b61931737e662dd293b40578ccornelius                                          SourceLocation(),
6159d709d503bab6e2b61931737e662dd293b40578ccornelius                                          &Context.Idents.get("__uint128_t"),
6259d709d503bab6e2b61931737e662dd293b40578ccornelius                                          TInfo), TUScope);
6359d709d503bab6e2b61931737e662dd293b40578ccornelius  }
6459d709d503bab6e2b61931737e662dd293b40578ccornelius
6559d709d503bab6e2b61931737e662dd293b40578ccornelius
6659d709d503bab6e2b61931737e662dd293b40578ccornelius  if (!PP.getLangOptions().ObjC1) return;
6759d709d503bab6e2b61931737e662dd293b40578ccornelius
6859d709d503bab6e2b61931737e662dd293b40578ccornelius  // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
6959d709d503bab6e2b61931737e662dd293b40578ccornelius  if (Context.getObjCSelType().isNull()) {
7059d709d503bab6e2b61931737e662dd293b40578ccornelius    // Create the built-in typedef for 'SEL'.
7159d709d503bab6e2b61931737e662dd293b40578ccornelius    QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
7259d709d503bab6e2b61931737e662dd293b40578ccornelius    TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
7359d709d503bab6e2b61931737e662dd293b40578ccornelius    TypedefDecl *SelTypedef
7459d709d503bab6e2b61931737e662dd293b40578ccornelius      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
7559d709d503bab6e2b61931737e662dd293b40578ccornelius                            &Context.Idents.get("SEL"), SelInfo);
7659d709d503bab6e2b61931737e662dd293b40578ccornelius    PushOnScopeChains(SelTypedef, TUScope);
7759d709d503bab6e2b61931737e662dd293b40578ccornelius    Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
7859d709d503bab6e2b61931737e662dd293b40578ccornelius    Context.ObjCSelRedefinitionType = Context.getObjCSelType();
7959d709d503bab6e2b61931737e662dd293b40578ccornelius  }
8059d709d503bab6e2b61931737e662dd293b40578ccornelius
8159d709d503bab6e2b61931737e662dd293b40578ccornelius  // Synthesize "@class Protocol;
8259d709d503bab6e2b61931737e662dd293b40578ccornelius  if (Context.getObjCProtoType().isNull()) {
8359d709d503bab6e2b61931737e662dd293b40578ccornelius    ObjCInterfaceDecl *ProtocolDecl =
8459d709d503bab6e2b61931737e662dd293b40578ccornelius      ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
8559d709d503bab6e2b61931737e662dd293b40578ccornelius                                &Context.Idents.get("Protocol"),
8659d709d503bab6e2b61931737e662dd293b40578ccornelius                                SourceLocation(), true);
8759d709d503bab6e2b61931737e662dd293b40578ccornelius    Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
8859d709d503bab6e2b61931737e662dd293b40578ccornelius    PushOnScopeChains(ProtocolDecl, TUScope, false);
8959d709d503bab6e2b61931737e662dd293b40578ccornelius  }
9059d709d503bab6e2b61931737e662dd293b40578ccornelius  // Create the built-in typedef for 'id'.
9159d709d503bab6e2b61931737e662dd293b40578ccornelius  if (Context.getObjCIdType().isNull()) {
9259d709d503bab6e2b61931737e662dd293b40578ccornelius    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0);
9359d709d503bab6e2b61931737e662dd293b40578ccornelius    T = Context.getObjCObjectPointerType(T);
9459d709d503bab6e2b61931737e662dd293b40578ccornelius    TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T);
9559d709d503bab6e2b61931737e662dd293b40578ccornelius    TypedefDecl *IdTypedef
9659d709d503bab6e2b61931737e662dd293b40578ccornelius      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
9759d709d503bab6e2b61931737e662dd293b40578ccornelius                            &Context.Idents.get("id"), IdInfo);
9859d709d503bab6e2b61931737e662dd293b40578ccornelius    PushOnScopeChains(IdTypedef, TUScope);
9959d709d503bab6e2b61931737e662dd293b40578ccornelius    Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
10059d709d503bab6e2b61931737e662dd293b40578ccornelius    Context.ObjCIdRedefinitionType = Context.getObjCIdType();
10159d709d503bab6e2b61931737e662dd293b40578ccornelius  }
10259d709d503bab6e2b61931737e662dd293b40578ccornelius  // Create the built-in typedef for 'Class'.
10359d709d503bab6e2b61931737e662dd293b40578ccornelius  if (Context.getObjCClassType().isNull()) {
10459d709d503bab6e2b61931737e662dd293b40578ccornelius    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0);
10559d709d503bab6e2b61931737e662dd293b40578ccornelius    T = Context.getObjCObjectPointerType(T);
10659d709d503bab6e2b61931737e662dd293b40578ccornelius    TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T);
10759d709d503bab6e2b61931737e662dd293b40578ccornelius    TypedefDecl *ClassTypedef
10859d709d503bab6e2b61931737e662dd293b40578ccornelius      = TypedefDecl::Create(Context, CurContext, SourceLocation(),
10959d709d503bab6e2b61931737e662dd293b40578ccornelius                            &Context.Idents.get("Class"), ClassInfo);
11059d709d503bab6e2b61931737e662dd293b40578ccornelius    PushOnScopeChains(ClassTypedef, TUScope);
11159d709d503bab6e2b61931737e662dd293b40578ccornelius    Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
11259d709d503bab6e2b61931737e662dd293b40578ccornelius    Context.ObjCClassRedefinitionType = Context.getObjCClassType();
11359d709d503bab6e2b61931737e662dd293b40578ccornelius  }
11459d709d503bab6e2b61931737e662dd293b40578ccornelius}
11559d709d503bab6e2b61931737e662dd293b40578ccornelius
11659d709d503bab6e2b61931737e662dd293b40578ccorneliusSema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
11759d709d503bab6e2b61931737e662dd293b40578ccornelius           bool CompleteTranslationUnit,
11859d709d503bab6e2b61931737e662dd293b40578ccornelius           CodeCompleteConsumer *CodeCompleter)
11959d709d503bab6e2b61931737e662dd293b40578ccornelius  : TheTargetAttributesSema(0),
12059d709d503bab6e2b61931737e662dd293b40578ccornelius    LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
12159d709d503bab6e2b61931737e662dd293b40578ccornelius    Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
12259d709d503bab6e2b61931737e662dd293b40578ccornelius    ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
12359d709d503bab6e2b61931737e662dd293b40578ccornelius    PackContext(0), TopFunctionScope(0), ParsingDeclDepth(0),
12459d709d503bab6e2b61931737e662dd293b40578ccornelius    IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
12559d709d503bab6e2b61931737e662dd293b40578ccornelius    GlobalNewDeleteDeclared(false),
12659d709d503bab6e2b61931737e662dd293b40578ccornelius    CompleteTranslationUnit(CompleteTranslationUnit),
12759d709d503bab6e2b61931737e662dd293b40578ccornelius    NumSFINAEErrors(0), NonInstantiationEntries(0),
12859d709d503bab6e2b61931737e662dd293b40578ccornelius    CurrentInstantiationScope(0), TyposCorrected(0),
12959d709d503bab6e2b61931737e662dd293b40578ccornelius    AnalysisWarnings(*this)
13059d709d503bab6e2b61931737e662dd293b40578ccornelius{
13159d709d503bab6e2b61931737e662dd293b40578ccornelius  TUScope = 0;
13259d709d503bab6e2b61931737e662dd293b40578ccornelius  if (getLangOptions().CPlusPlus)
13359d709d503bab6e2b61931737e662dd293b40578ccornelius    FieldCollector.reset(new CXXFieldCollector());
13459d709d503bab6e2b61931737e662dd293b40578ccornelius
13559d709d503bab6e2b61931737e662dd293b40578ccornelius  // Tell diagnostics how to render things from the AST library.
13659d709d503bab6e2b61931737e662dd293b40578ccornelius  PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
13759d709d503bab6e2b61931737e662dd293b40578ccornelius                                       &Context);
13859d709d503bab6e2b61931737e662dd293b40578ccornelius
13959d709d503bab6e2b61931737e662dd293b40578ccornelius  ExprEvalContexts.push_back(
14059d709d503bab6e2b61931737e662dd293b40578ccornelius                  ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
14159d709d503bab6e2b61931737e662dd293b40578ccornelius}
14259d709d503bab6e2b61931737e662dd293b40578ccornelius
14359d709d503bab6e2b61931737e662dd293b40578ccorneliusSema::~Sema() {
14459d709d503bab6e2b61931737e662dd293b40578ccornelius  if (PackContext) FreePackedContext();
14559d709d503bab6e2b61931737e662dd293b40578ccornelius  delete TheTargetAttributesSema;
14659d709d503bab6e2b61931737e662dd293b40578ccornelius  while (!FunctionScopes.empty())
14759d709d503bab6e2b61931737e662dd293b40578ccornelius    PopFunctionOrBlockScope();
14859d709d503bab6e2b61931737e662dd293b40578ccornelius}
14959d709d503bab6e2b61931737e662dd293b40578ccornelius
15059d709d503bab6e2b61931737e662dd293b40578ccornelius/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
15159d709d503bab6e2b61931737e662dd293b40578ccornelius/// If there is already an implicit cast, merge into the existing one.
15259d709d503bab6e2b61931737e662dd293b40578ccornelius/// If isLvalue, the result of the cast is an lvalue.
15359d709d503bab6e2b61931737e662dd293b40578ccorneliusvoid Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
15450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho                             CastExpr::CastKind Kind,
15559d709d503bab6e2b61931737e662dd293b40578ccornelius                             bool isLvalue, CXXBaseSpecifierArray BasePath) {
15659d709d503bab6e2b61931737e662dd293b40578ccornelius  QualType ExprTy = Context.getCanonicalType(Expr->getType());
15759d709d503bab6e2b61931737e662dd293b40578ccornelius  QualType TypeTy = Context.getCanonicalType(Ty);
15859d709d503bab6e2b61931737e662dd293b40578ccornelius
15959d709d503bab6e2b61931737e662dd293b40578ccornelius  if (ExprTy == TypeTy)
16059d709d503bab6e2b61931737e662dd293b40578ccornelius    return;
16159d709d503bab6e2b61931737e662dd293b40578ccornelius
16259d709d503bab6e2b61931737e662dd293b40578ccornelius  if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
16359d709d503bab6e2b61931737e662dd293b40578ccornelius    QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
16459d709d503bab6e2b61931737e662dd293b40578ccornelius    QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
16559d709d503bab6e2b61931737e662dd293b40578ccornelius    if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
16659d709d503bab6e2b61931737e662dd293b40578ccornelius      Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
16759d709d503bab6e2b61931737e662dd293b40578ccornelius        << Expr->getSourceRange();
16859d709d503bab6e2b61931737e662dd293b40578ccornelius    }
16959d709d503bab6e2b61931737e662dd293b40578ccornelius  }
17059d709d503bab6e2b61931737e662dd293b40578ccornelius
17159d709d503bab6e2b61931737e662dd293b40578ccornelius  // If this is a derived-to-base cast to a through a virtual base, we
17259d709d503bab6e2b61931737e662dd293b40578ccornelius  // need a vtable.
17359d709d503bab6e2b61931737e662dd293b40578ccornelius  if (Kind == CastExpr::CK_DerivedToBase &&
17459d709d503bab6e2b61931737e662dd293b40578ccornelius      BasePathInvolvesVirtualBase(BasePath)) {
17559d709d503bab6e2b61931737e662dd293b40578ccornelius    QualType T = Expr->getType();
17659d709d503bab6e2b61931737e662dd293b40578ccornelius    if (const PointerType *Pointer = T->getAs<PointerType>())
17759d709d503bab6e2b61931737e662dd293b40578ccornelius      T = Pointer->getPointeeType();
17859d709d503bab6e2b61931737e662dd293b40578ccornelius    if (const RecordType *RecordTy = T->getAs<RecordType>())
17959d709d503bab6e2b61931737e662dd293b40578ccornelius      MarkVTableUsed(Expr->getLocStart(),
18059d709d503bab6e2b61931737e662dd293b40578ccornelius                     cast<CXXRecordDecl>(RecordTy->getDecl()));
18159d709d503bab6e2b61931737e662dd293b40578ccornelius  }
18259d709d503bab6e2b61931737e662dd293b40578ccornelius
18359d709d503bab6e2b61931737e662dd293b40578ccornelius  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
18459d709d503bab6e2b61931737e662dd293b40578ccornelius    if (ImpCast->getCastKind() == Kind && BasePath.empty()) {
18559d709d503bab6e2b61931737e662dd293b40578ccornelius      ImpCast->setType(Ty);
18659d709d503bab6e2b61931737e662dd293b40578ccornelius      ImpCast->setLvalueCast(isLvalue);
18759d709d503bab6e2b61931737e662dd293b40578ccornelius      return;
18859d709d503bab6e2b61931737e662dd293b40578ccornelius    }
18959d709d503bab6e2b61931737e662dd293b40578ccornelius  }
19059d709d503bab6e2b61931737e662dd293b40578ccornelius
19159d709d503bab6e2b61931737e662dd293b40578ccornelius  Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, BasePath, isLvalue);
19259d709d503bab6e2b61931737e662dd293b40578ccornelius}
19359d709d503bab6e2b61931737e662dd293b40578ccornelius
19459d709d503bab6e2b61931737e662dd293b40578ccorneliusvoid Sema::DeleteExpr(ExprTy *E) {
19559d709d503bab6e2b61931737e662dd293b40578ccornelius  if (E) static_cast<Expr*>(E)->Destroy(Context);
19659d709d503bab6e2b61931737e662dd293b40578ccornelius}
19759d709d503bab6e2b61931737e662dd293b40578ccorneliusvoid Sema::DeleteStmt(StmtTy *S) {
19859d709d503bab6e2b61931737e662dd293b40578ccornelius  if (S) static_cast<Stmt*>(S)->Destroy(Context);
19959d709d503bab6e2b61931737e662dd293b40578ccornelius}
20059d709d503bab6e2b61931737e662dd293b40578ccornelius
20159d709d503bab6e2b61931737e662dd293b40578ccornelius/// ActOnEndOfTranslationUnit - This is called at the very end of the
20259d709d503bab6e2b61931737e662dd293b40578ccornelius/// translation unit when EOF is reached and all but the top-level scope is
20359d709d503bab6e2b61931737e662dd293b40578ccornelius/// popped.
20459d709d503bab6e2b61931737e662dd293b40578ccorneliusvoid Sema::ActOnEndOfTranslationUnit() {
20559d709d503bab6e2b61931737e662dd293b40578ccornelius  while (1) {
20659d709d503bab6e2b61931737e662dd293b40578ccornelius    // C++: Perform implicit template instantiations.
20759d709d503bab6e2b61931737e662dd293b40578ccornelius    //
20859d709d503bab6e2b61931737e662dd293b40578ccornelius    // FIXME: When we perform these implicit instantiations, we do not carefully
20959d709d503bab6e2b61931737e662dd293b40578ccornelius    // keep track of the point of instantiation (C++ [temp.point]). This means
21059d709d503bab6e2b61931737e662dd293b40578ccornelius    // that name lookup that occurs within the template instantiation will
21159d709d503bab6e2b61931737e662dd293b40578ccornelius    // always happen at the end of the translation unit, so it will find
21259d709d503bab6e2b61931737e662dd293b40578ccornelius    // some names that should not be found. Although this is common behavior
21359d709d503bab6e2b61931737e662dd293b40578ccornelius    // for C++ compilers, it is technically wrong. In the future, we either need
21459d709d503bab6e2b61931737e662dd293b40578ccornelius    // to be able to filter the results of name lookup or we need to perform
21559d709d503bab6e2b61931737e662dd293b40578ccornelius    // template instantiations earlier.
21659d709d503bab6e2b61931737e662dd293b40578ccornelius    PerformPendingImplicitInstantiations();
21759d709d503bab6e2b61931737e662dd293b40578ccornelius
21859d709d503bab6e2b61931737e662dd293b40578ccornelius    /// If DefinedUsedVTables ends up marking any virtual member
21959d709d503bab6e2b61931737e662dd293b40578ccornelius    /// functions it might lead to more pending template
22059d709d503bab6e2b61931737e662dd293b40578ccornelius    /// instantiations, which is why we need to loop here.
22159d709d503bab6e2b61931737e662dd293b40578ccornelius    if (!DefineUsedVTables())
22259d709d503bab6e2b61931737e662dd293b40578ccornelius      break;
22359d709d503bab6e2b61931737e662dd293b40578ccornelius  }
22459d709d503bab6e2b61931737e662dd293b40578ccornelius
22559d709d503bab6e2b61931737e662dd293b40578ccornelius  // Remove functions that turned out to be used.
22659d709d503bab6e2b61931737e662dd293b40578ccornelius  UnusedStaticFuncs.erase(std::remove_if(UnusedStaticFuncs.begin(),
22759d709d503bab6e2b61931737e662dd293b40578ccornelius                                         UnusedStaticFuncs.end(),
22859d709d503bab6e2b61931737e662dd293b40578ccornelius                                         std::mem_fun(&FunctionDecl::isUsed)),
22959d709d503bab6e2b61931737e662dd293b40578ccornelius                          UnusedStaticFuncs.end());
23059d709d503bab6e2b61931737e662dd293b40578ccornelius
23159d709d503bab6e2b61931737e662dd293b40578ccornelius  // Check for #pragma weak identifiers that were never declared
23259d709d503bab6e2b61931737e662dd293b40578ccornelius  // FIXME: This will cause diagnostics to be emitted in a non-determinstic
23359d709d503bab6e2b61931737e662dd293b40578ccornelius  // order!  Iterating over a densemap like this is bad.
23459d709d503bab6e2b61931737e662dd293b40578ccornelius  for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
23559d709d503bab6e2b61931737e662dd293b40578ccornelius       I = WeakUndeclaredIdentifiers.begin(),
23659d709d503bab6e2b61931737e662dd293b40578ccornelius       E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
23759d709d503bab6e2b61931737e662dd293b40578ccornelius    if (I->second.getUsed()) continue;
23859d709d503bab6e2b61931737e662dd293b40578ccornelius
23959d709d503bab6e2b61931737e662dd293b40578ccornelius    Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
24059d709d503bab6e2b61931737e662dd293b40578ccornelius      << I->first;
24159d709d503bab6e2b61931737e662dd293b40578ccornelius  }
24259d709d503bab6e2b61931737e662dd293b40578ccornelius
24359d709d503bab6e2b61931737e662dd293b40578ccornelius  if (!CompleteTranslationUnit)
24459d709d503bab6e2b61931737e662dd293b40578ccornelius    return;
24559d709d503bab6e2b61931737e662dd293b40578ccornelius
24659d709d503bab6e2b61931737e662dd293b40578ccornelius  // C99 6.9.2p2:
24759d709d503bab6e2b61931737e662dd293b40578ccornelius  //   A declaration of an identifier for an object that has file
24859d709d503bab6e2b61931737e662dd293b40578ccornelius  //   scope without an initializer, and without a storage-class
24959d709d503bab6e2b61931737e662dd293b40578ccornelius  //   specifier or with the storage-class specifier static,
25059d709d503bab6e2b61931737e662dd293b40578ccornelius  //   constitutes a tentative definition. If a translation unit
25159d709d503bab6e2b61931737e662dd293b40578ccornelius  //   contains one or more tentative definitions for an identifier,
25259d709d503bab6e2b61931737e662dd293b40578ccornelius  //   and the translation unit contains no external definition for
25359d709d503bab6e2b61931737e662dd293b40578ccornelius  //   that identifier, then the behavior is exactly as if the
25459d709d503bab6e2b61931737e662dd293b40578ccornelius  //   translation unit contains a file scope declaration of that
25559d709d503bab6e2b61931737e662dd293b40578ccornelius  //   identifier, with the composite type as of the end of the
25659d709d503bab6e2b61931737e662dd293b40578ccornelius  //   translation unit, with an initializer equal to 0.
25759d709d503bab6e2b61931737e662dd293b40578ccornelius  llvm::SmallSet<VarDecl *, 32> Seen;
25859d709d503bab6e2b61931737e662dd293b40578ccornelius  for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) {
25959d709d503bab6e2b61931737e662dd293b40578ccornelius    VarDecl *VD = TentativeDefinitions[i]->getActingDefinition();
26059d709d503bab6e2b61931737e662dd293b40578ccornelius
26159d709d503bab6e2b61931737e662dd293b40578ccornelius    // If the tentative definition was completed, getActingDefinition() returns
26259d709d503bab6e2b61931737e662dd293b40578ccornelius    // null. If we've already seen this variable before, insert()'s second
26359d709d503bab6e2b61931737e662dd293b40578ccornelius    // return value is false.
26459d709d503bab6e2b61931737e662dd293b40578ccornelius    if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
26559d709d503bab6e2b61931737e662dd293b40578ccornelius      continue;
26659d709d503bab6e2b61931737e662dd293b40578ccornelius
26759d709d503bab6e2b61931737e662dd293b40578ccornelius    if (const IncompleteArrayType *ArrayT
26859d709d503bab6e2b61931737e662dd293b40578ccornelius        = Context.getAsIncompleteArrayType(VD->getType())) {
26959d709d503bab6e2b61931737e662dd293b40578ccornelius      if (RequireCompleteType(VD->getLocation(),
27059d709d503bab6e2b61931737e662dd293b40578ccornelius                              ArrayT->getElementType(),
27159d709d503bab6e2b61931737e662dd293b40578ccornelius                              diag::err_tentative_def_incomplete_type_arr)) {
27259d709d503bab6e2b61931737e662dd293b40578ccornelius        VD->setInvalidDecl();
27359d709d503bab6e2b61931737e662dd293b40578ccornelius        continue;
27459d709d503bab6e2b61931737e662dd293b40578ccornelius      }
27559d709d503bab6e2b61931737e662dd293b40578ccornelius
27659d709d503bab6e2b61931737e662dd293b40578ccornelius      // Set the length of the array to 1 (C99 6.9.2p5).
27759d709d503bab6e2b61931737e662dd293b40578ccornelius      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
27859d709d503bab6e2b61931737e662dd293b40578ccornelius      llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
27959d709d503bab6e2b61931737e662dd293b40578ccornelius      QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
28059d709d503bab6e2b61931737e662dd293b40578ccornelius                                                One, ArrayType::Normal, 0);
28159d709d503bab6e2b61931737e662dd293b40578ccornelius      VD->setType(T);
28259d709d503bab6e2b61931737e662dd293b40578ccornelius    } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
28359d709d503bab6e2b61931737e662dd293b40578ccornelius                                   diag::err_tentative_def_incomplete_type))
28459d709d503bab6e2b61931737e662dd293b40578ccornelius      VD->setInvalidDecl();
28559d709d503bab6e2b61931737e662dd293b40578ccornelius
28659d709d503bab6e2b61931737e662dd293b40578ccornelius    // Notify the consumer that we've completed a tentative definition.
28759d709d503bab6e2b61931737e662dd293b40578ccornelius    if (!VD->isInvalidDecl())
28859d709d503bab6e2b61931737e662dd293b40578ccornelius      Consumer.CompleteTentativeDefinition(VD);
28959d709d503bab6e2b61931737e662dd293b40578ccornelius
29059d709d503bab6e2b61931737e662dd293b40578ccornelius  }
29159d709d503bab6e2b61931737e662dd293b40578ccornelius
29259d709d503bab6e2b61931737e662dd293b40578ccornelius  // Output warning for unused functions.
29359d709d503bab6e2b61931737e662dd293b40578ccornelius  for (std::vector<FunctionDecl*>::iterator
29459d709d503bab6e2b61931737e662dd293b40578ccornelius       F = UnusedStaticFuncs.begin(),
29559d709d503bab6e2b61931737e662dd293b40578ccornelius       FEnd = UnusedStaticFuncs.end();
29659d709d503bab6e2b61931737e662dd293b40578ccornelius       F != FEnd;
29759d709d503bab6e2b61931737e662dd293b40578ccornelius       ++F)
29859d709d503bab6e2b61931737e662dd293b40578ccornelius    Diag((*F)->getLocation(), diag::warn_unused_function) << (*F)->getDeclName();
29959d709d503bab6e2b61931737e662dd293b40578ccornelius
30059d709d503bab6e2b61931737e662dd293b40578ccornelius}
30159d709d503bab6e2b61931737e662dd293b40578ccornelius
30259d709d503bab6e2b61931737e662dd293b40578ccornelius
30350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho//===----------------------------------------------------------------------===//
30459d709d503bab6e2b61931737e662dd293b40578ccornelius// Helper functions.
30559d709d503bab6e2b61931737e662dd293b40578ccornelius//===----------------------------------------------------------------------===//
30659d709d503bab6e2b61931737e662dd293b40578ccornelius
30759d709d503bab6e2b61931737e662dd293b40578ccorneliusDeclContext *Sema::getFunctionLevelDeclContext() {
308fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius  DeclContext *DC = CurContext;
30950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
310  while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
311    DC = DC->getParent();
312
313  return DC;
314}
315
316/// getCurFunctionDecl - If inside of a function body, this returns a pointer
317/// to the function decl for the function being parsed.  If we're currently
318/// in a 'block', this returns the containing context.
319FunctionDecl *Sema::getCurFunctionDecl() {
320  DeclContext *DC = getFunctionLevelDeclContext();
321  return dyn_cast<FunctionDecl>(DC);
322}
323
324ObjCMethodDecl *Sema::getCurMethodDecl() {
325  DeclContext *DC = getFunctionLevelDeclContext();
326  return dyn_cast<ObjCMethodDecl>(DC);
327}
328
329NamedDecl *Sema::getCurFunctionOrMethodDecl() {
330  DeclContext *DC = getFunctionLevelDeclContext();
331  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
332    return cast<NamedDecl>(DC);
333  return 0;
334}
335
336Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
337  if (!this->Emit())
338    return;
339
340  // If this is not a note, and we're in a template instantiation
341  // that is different from the last template instantiation where
342  // we emitted an error, print a template instantiation
343  // backtrace.
344  if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
345      !SemaRef.ActiveTemplateInstantiations.empty() &&
346      SemaRef.ActiveTemplateInstantiations.back()
347        != SemaRef.LastTemplateInstantiationErrorContext) {
348    SemaRef.PrintInstantiationStack();
349    SemaRef.LastTemplateInstantiationErrorContext
350      = SemaRef.ActiveTemplateInstantiations.back();
351  }
352}
353
354Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) {
355  if (isSFINAEContext()) {
356    switch (Diagnostic::getDiagnosticSFINAEResponse(DiagID)) {
357    case Diagnostic::SFINAE_Report:
358      // Fall through; we'll report the diagnostic below.
359      break;
360
361    case Diagnostic::SFINAE_SubstitutionFailure:
362      // Count this failure so that we know that template argument deduction
363      // has failed.
364      ++NumSFINAEErrors;
365      // Fall through
366
367    case Diagnostic::SFINAE_Suppress:
368      // Suppress this diagnostic.
369      Diags.setLastDiagnosticIgnored();
370      return SemaDiagnosticBuilder(*this);
371    }
372  }
373
374  DiagnosticBuilder DB = Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
375  return SemaDiagnosticBuilder(DB, *this, DiagID);
376}
377
378Sema::SemaDiagnosticBuilder
379Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
380  SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
381  PD.Emit(Builder);
382
383  return Builder;
384}
385
386
387/// \brief Enter a new function scope
388void Sema::PushFunctionScope() {
389  if (FunctionScopes.empty()) {
390    // Use the "top" function scope rather than having to allocate memory for
391    // a new scope.
392    TopFunctionScope.Clear(getDiagnostics().getNumErrors());
393    FunctionScopes.push_back(&TopFunctionScope);
394    return;
395  }
396
397  FunctionScopes.push_back(
398                      new FunctionScopeInfo(getDiagnostics().getNumErrors()));
399}
400
401void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
402  FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics().getNumErrors(),
403                                              BlockScope, Block));
404}
405
406void Sema::PopFunctionOrBlockScope() {
407  if (FunctionScopes.back() != &TopFunctionScope)
408    delete FunctionScopes.back();
409  else
410    TopFunctionScope.Clear(getDiagnostics().getNumErrors());
411
412  FunctionScopes.pop_back();
413}
414
415/// \brief Determine whether any errors occurred within this function/method/
416/// block.
417bool Sema::hasAnyErrorsInThisFunction() const {
418  unsigned NumErrors = TopFunctionScope.NumErrorsAtStartOfFunction;
419  if (!FunctionScopes.empty())
420    NumErrors = FunctionScopes.back()->NumErrorsAtStartOfFunction;
421  return NumErrors != getDiagnostics().getNumErrors();
422}
423
424BlockScopeInfo *Sema::getCurBlock() {
425  if (FunctionScopes.empty())
426    return 0;
427
428  return dyn_cast<BlockScopeInfo>(FunctionScopes.back());
429}
430