2009-03-18 05:15:40 +08:00
|
|
|
//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//===----------------------------------------------------------------------===/
|
|
|
|
//
|
|
|
|
// This file implements C++ template instantiation for declarations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===/
|
|
|
|
#include "Sema.h"
|
2009-05-27 04:50:29 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-03-18 05:15:40 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/DeclTemplate.h"
|
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN TemplateDeclInstantiator
|
2009-03-29 03:18:32 +08:00
|
|
|
: public DeclVisitor<TemplateDeclInstantiator, Decl *> {
|
2009-03-18 05:15:40 +08:00
|
|
|
Sema &SemaRef;
|
|
|
|
DeclContext *Owner;
|
2009-05-12 07:53:27 +08:00
|
|
|
const TemplateArgumentList &TemplateArgs;
|
2009-03-18 05:15:40 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
typedef Sema::OwningExprResult OwningExprResult;
|
|
|
|
|
|
|
|
TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
|
2009-05-12 07:53:27 +08:00
|
|
|
const TemplateArgumentList &TemplateArgs)
|
|
|
|
: SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
|
2009-03-18 05:15:40 +08:00
|
|
|
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: Once we get closer to completion, replace these manually-written
|
|
|
|
// declarations with automatically-generated ones from
|
|
|
|
// clang/AST/DeclNodes.def.
|
2009-03-25 23:45:12 +08:00
|
|
|
Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
|
|
|
|
Decl *VisitNamespaceDecl(NamespaceDecl *D);
|
2009-03-18 05:15:40 +08:00
|
|
|
Decl *VisitTypedefDecl(TypedefDecl *D);
|
2009-03-26 07:32:15 +08:00
|
|
|
Decl *VisitVarDecl(VarDecl *D);
|
2009-03-18 05:15:40 +08:00
|
|
|
Decl *VisitFieldDecl(FieldDecl *D);
|
|
|
|
Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
|
|
|
|
Decl *VisitEnumDecl(EnumDecl *D);
|
2009-03-25 23:04:13 +08:00
|
|
|
Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
|
2009-08-14 10:03:10 +08:00
|
|
|
Decl *VisitFriendClassDecl(FriendClassDecl *D);
|
2009-06-26 06:08:12 +08:00
|
|
|
Decl *VisitFunctionDecl(FunctionDecl *D);
|
2009-03-26 05:17:03 +08:00
|
|
|
Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
|
2009-03-24 07:06:20 +08:00
|
|
|
Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
|
2009-03-25 00:43:20 +08:00
|
|
|
Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
|
2009-03-24 08:15:49 +08:00
|
|
|
Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
|
2009-03-25 08:34:44 +08:00
|
|
|
Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
|
2009-03-25 23:04:13 +08:00
|
|
|
ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
|
2009-03-24 07:06:20 +08:00
|
|
|
Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
|
2009-08-20 09:44:21 +08:00
|
|
|
Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
|
|
|
|
Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
|
2009-03-24 08:38:23 +08:00
|
|
|
|
2009-03-18 05:15:40 +08:00
|
|
|
// Base case. FIXME: Remove once we can instantiate everything.
|
|
|
|
Decl *VisitDecl(Decl *) {
|
2009-03-26 07:32:15 +08:00
|
|
|
assert(false && "Template instantiation of unknown declaration kind!");
|
2009-03-18 05:15:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-03-24 08:38:23 +08:00
|
|
|
|
2009-08-14 10:03:10 +08:00
|
|
|
const LangOptions &getLangOptions() {
|
|
|
|
return SemaRef.getLangOptions();
|
|
|
|
}
|
|
|
|
|
2009-03-24 08:38:23 +08:00
|
|
|
// Helper functions for instantiating methods.
|
|
|
|
QualType InstantiateFunctionType(FunctionDecl *D,
|
|
|
|
llvm::SmallVectorImpl<ParmVarDecl *> &Params);
|
2009-06-26 06:08:12 +08:00
|
|
|
bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
|
2009-03-24 08:38:23 +08:00
|
|
|
bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
|
2009-08-20 09:44:21 +08:00
|
|
|
|
|
|
|
TemplateParameterList *
|
|
|
|
InstantiateTemplateParams(TemplateParameterList *List);
|
2009-03-18 05:15:40 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-03-25 23:45:12 +08:00
|
|
|
Decl *
|
|
|
|
TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
|
|
|
|
assert(false && "Translation units cannot be instantiated");
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decl *
|
|
|
|
TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
|
|
|
|
assert(false && "Namespaces cannot be instantiated");
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2009-03-18 05:15:40 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
|
|
|
|
bool Invalid = false;
|
|
|
|
QualType T = D->getUnderlyingType();
|
|
|
|
if (T->isDependentType()) {
|
2009-05-15 05:06:31 +08:00
|
|
|
T = SemaRef.InstantiateType(T, TemplateArgs,
|
|
|
|
D->getLocation(), D->getDeclName());
|
2009-03-18 05:15:40 +08:00
|
|
|
if (T.isNull()) {
|
|
|
|
Invalid = true;
|
|
|
|
T = SemaRef.Context.IntTy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new typedef
|
|
|
|
TypedefDecl *Typedef
|
|
|
|
= TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
|
|
|
D->getIdentifier(), T);
|
|
|
|
if (Invalid)
|
|
|
|
Typedef->setInvalidDecl();
|
|
|
|
|
2009-06-30 10:36:12 +08:00
|
|
|
Owner->addDecl(Typedef);
|
2009-05-29 00:34:51 +08:00
|
|
|
|
2009-03-18 05:15:40 +08:00
|
|
|
return Typedef;
|
|
|
|
}
|
|
|
|
|
2009-03-26 07:32:15 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
|
|
|
|
// Instantiate the type of the declaration
|
|
|
|
QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs,
|
|
|
|
D->getTypeSpecStartLoc(),
|
|
|
|
D->getDeclName());
|
|
|
|
if (T.isNull())
|
|
|
|
return 0;
|
|
|
|
|
2009-05-15 08:01:03 +08:00
|
|
|
// Build the instantiated declaration
|
2009-03-26 07:32:15 +08:00
|
|
|
VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
|
|
|
|
D->getLocation(), D->getIdentifier(),
|
2009-08-19 09:27:57 +08:00
|
|
|
T, D->getDeclaratorInfo(),
|
2009-08-21 08:31:54 +08:00
|
|
|
D->getStorageClass());
|
2009-03-26 07:32:15 +08:00
|
|
|
Var->setThreadSpecified(D->isThreadSpecified());
|
|
|
|
Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
|
|
|
|
Var->setDeclaredInCondition(D->isDeclaredInCondition());
|
|
|
|
|
2009-07-25 04:34:43 +08:00
|
|
|
// If we are instantiating a static data member defined
|
|
|
|
// out-of-line, the instantiation will have the same lexical
|
|
|
|
// context (which will be a namespace scope) as the template.
|
|
|
|
if (D->isOutOfLine())
|
|
|
|
Var->setLexicalDeclContext(D->getLexicalDeclContext());
|
|
|
|
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: In theory, we could have a previous declaration for variables that
|
|
|
|
// are not static data members.
|
2009-03-26 07:32:15 +08:00
|
|
|
bool Redeclaration = false;
|
2009-04-25 16:06:05 +08:00
|
|
|
SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
|
2009-07-25 04:34:43 +08:00
|
|
|
|
|
|
|
if (D->isOutOfLine()) {
|
|
|
|
D->getLexicalDeclContext()->addDecl(Var);
|
|
|
|
Owner->makeDeclVisibleInContext(Var);
|
|
|
|
} else {
|
|
|
|
Owner->addDecl(Var);
|
|
|
|
}
|
|
|
|
|
2009-03-26 07:32:15 +08:00
|
|
|
if (D->getInit()) {
|
|
|
|
OwningExprResult Init
|
2009-05-12 07:53:27 +08:00
|
|
|
= SemaRef.InstantiateExpr(D->getInit(), TemplateArgs);
|
2009-03-26 07:32:15 +08:00
|
|
|
if (Init.isInvalid())
|
|
|
|
Var->setInvalidDecl();
|
|
|
|
else
|
2009-03-29 03:18:32 +08:00
|
|
|
SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
|
2009-03-26 07:32:15 +08:00
|
|
|
D->hasCXXDirectInitializer());
|
2009-07-28 01:43:39 +08:00
|
|
|
} else if (!Var->isStaticDataMember() || Var->isOutOfLine())
|
|
|
|
SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
|
2009-03-26 07:32:15 +08:00
|
|
|
|
2009-07-25 04:34:43 +08:00
|
|
|
// Link instantiations of static data members back to the template from
|
|
|
|
// which they were instantiated.
|
|
|
|
if (Var->isStaticDataMember())
|
|
|
|
SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D);
|
|
|
|
|
2009-03-26 07:32:15 +08:00
|
|
|
return Var;
|
|
|
|
}
|
|
|
|
|
2009-03-18 05:15:40 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
|
|
|
|
bool Invalid = false;
|
|
|
|
QualType T = D->getType();
|
|
|
|
if (T->isDependentType()) {
|
2009-05-15 05:06:31 +08:00
|
|
|
T = SemaRef.InstantiateType(T, TemplateArgs,
|
|
|
|
D->getLocation(), D->getDeclName());
|
2009-03-18 05:15:40 +08:00
|
|
|
if (!T.isNull() && T->isFunctionType()) {
|
|
|
|
// C++ [temp.arg.type]p3:
|
|
|
|
// If a declaration acquires a function type through a type
|
|
|
|
// dependent on a template-parameter and this causes a
|
|
|
|
// declaration that does not use the syntactic form of a
|
|
|
|
// function declarator to have function type, the program is
|
|
|
|
// ill-formed.
|
|
|
|
SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
|
|
|
|
<< T;
|
|
|
|
T = QualType();
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr *BitWidth = D->getBitWidth();
|
|
|
|
if (Invalid)
|
|
|
|
BitWidth = 0;
|
|
|
|
else if (BitWidth) {
|
2009-06-23 04:57:11 +08:00
|
|
|
// The bit-width expression is not potentially evaluated.
|
|
|
|
EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
|
|
|
|
|
2009-03-18 05:15:40 +08:00
|
|
|
OwningExprResult InstantiatedBitWidth
|
2009-05-12 07:53:27 +08:00
|
|
|
= SemaRef.InstantiateExpr(BitWidth, TemplateArgs);
|
2009-03-18 05:15:40 +08:00
|
|
|
if (InstantiatedBitWidth.isInvalid()) {
|
|
|
|
Invalid = true;
|
|
|
|
BitWidth = 0;
|
|
|
|
} else
|
2009-05-02 03:49:17 +08:00
|
|
|
BitWidth = InstantiatedBitWidth.takeAs<Expr>();
|
2009-03-18 05:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
|
2009-08-19 09:27:57 +08:00
|
|
|
D->getDeclaratorInfo(),
|
2009-03-18 05:15:40 +08:00
|
|
|
cast<RecordDecl>(Owner),
|
|
|
|
D->getLocation(),
|
|
|
|
D->isMutable(),
|
|
|
|
BitWidth,
|
2009-07-14 22:58:18 +08:00
|
|
|
D->getTypeSpecStartLoc(),
|
2009-03-18 05:15:40 +08:00
|
|
|
D->getAccess(),
|
|
|
|
0);
|
|
|
|
if (Field) {
|
|
|
|
if (Invalid)
|
|
|
|
Field->setInvalidDecl();
|
|
|
|
|
2009-06-30 10:36:12 +08:00
|
|
|
Owner->addDecl(Field);
|
2009-03-18 05:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Field;
|
|
|
|
}
|
|
|
|
|
2009-08-14 10:03:10 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitFriendClassDecl(FriendClassDecl *D) {
|
|
|
|
QualType T = D->getFriendType();
|
|
|
|
if (T->isDependentType()) {
|
|
|
|
T = SemaRef.InstantiateType(T, TemplateArgs, D->getLocation(),
|
|
|
|
DeclarationName());
|
|
|
|
assert(T.isNull() || getLangOptions().CPlusPlus0x || T->isRecordType());
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: the target context might be dependent.
|
|
|
|
DeclContext *DC = D->getDeclContext();
|
|
|
|
assert(DC->isFileContext());
|
|
|
|
|
|
|
|
FriendClassDecl *NewD =
|
|
|
|
FriendClassDecl::Create(SemaRef.Context, DC, D->getLocation(), T,
|
|
|
|
D->getFriendLoc());
|
|
|
|
Owner->addDecl(NewD);
|
|
|
|
return NewD;
|
|
|
|
}
|
|
|
|
|
2009-03-18 05:15:40 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
|
|
|
|
Expr *AssertExpr = D->getAssertExpr();
|
|
|
|
|
2009-06-23 04:57:11 +08:00
|
|
|
// The expression in a static assertion is not potentially evaluated.
|
|
|
|
EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
|
|
|
|
|
2009-03-18 05:15:40 +08:00
|
|
|
OwningExprResult InstantiatedAssertExpr
|
2009-05-12 07:53:27 +08:00
|
|
|
= SemaRef.InstantiateExpr(AssertExpr, TemplateArgs);
|
2009-03-18 05:15:40 +08:00
|
|
|
if (InstantiatedAssertExpr.isInvalid())
|
|
|
|
return 0;
|
|
|
|
|
2009-08-08 09:41:12 +08:00
|
|
|
OwningExprResult Message(SemaRef, D->getMessage());
|
|
|
|
D->getMessage()->Retain();
|
2009-03-18 05:15:40 +08:00
|
|
|
Decl *StaticAssert
|
2009-03-29 03:18:32 +08:00
|
|
|
= SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
|
|
|
|
move(InstantiatedAssertExpr),
|
|
|
|
move(Message)).getAs<Decl>();
|
2009-03-18 05:15:40 +08:00
|
|
|
return StaticAssert;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
|
|
|
|
EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
|
|
|
|
D->getLocation(), D->getIdentifier(),
|
2009-07-21 22:46:17 +08:00
|
|
|
D->getTagKeywordLoc(),
|
2009-03-18 05:15:40 +08:00
|
|
|
/*PrevDecl=*/0);
|
2009-05-28 01:20:35 +08:00
|
|
|
Enum->setInstantiationOfMemberEnum(D);
|
2009-03-26 06:00:53 +08:00
|
|
|
Enum->setAccess(D->getAccess());
|
2009-06-30 10:36:12 +08:00
|
|
|
Owner->addDecl(Enum);
|
2009-03-18 05:15:40 +08:00
|
|
|
Enum->startDefinition();
|
|
|
|
|
2009-05-30 02:27:38 +08:00
|
|
|
llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
|
2009-03-18 05:15:40 +08:00
|
|
|
|
|
|
|
EnumConstantDecl *LastEnumConst = 0;
|
2009-06-30 10:36:12 +08:00
|
|
|
for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
|
|
|
|
ECEnd = D->enumerator_end();
|
2009-03-18 05:15:40 +08:00
|
|
|
EC != ECEnd; ++EC) {
|
|
|
|
// The specified value for the enumerator.
|
|
|
|
OwningExprResult Value = SemaRef.Owned((Expr *)0);
|
2009-06-23 04:57:11 +08:00
|
|
|
if (Expr *UninstValue = EC->getInitExpr()) {
|
|
|
|
// The enumerator's value expression is not potentially evaluated.
|
|
|
|
EnterExpressionEvaluationContext Unevaluated(SemaRef,
|
|
|
|
Action::Unevaluated);
|
|
|
|
|
2009-05-12 07:53:27 +08:00
|
|
|
Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs);
|
2009-06-23 04:57:11 +08:00
|
|
|
}
|
2009-03-18 05:15:40 +08:00
|
|
|
|
|
|
|
// Drop the initial value and continue.
|
|
|
|
bool isInvalid = false;
|
|
|
|
if (Value.isInvalid()) {
|
|
|
|
Value = SemaRef.Owned((Expr *)0);
|
|
|
|
isInvalid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnumConstantDecl *EnumConst
|
|
|
|
= SemaRef.CheckEnumConstant(Enum, LastEnumConst,
|
|
|
|
EC->getLocation(), EC->getIdentifier(),
|
|
|
|
move(Value));
|
|
|
|
|
|
|
|
if (isInvalid) {
|
|
|
|
if (EnumConst)
|
|
|
|
EnumConst->setInvalidDecl();
|
|
|
|
Enum->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EnumConst) {
|
2009-06-30 10:36:12 +08:00
|
|
|
Enum->addDecl(EnumConst);
|
2009-03-29 03:18:32 +08:00
|
|
|
Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
|
2009-03-18 05:15:40 +08:00
|
|
|
LastEnumConst = EnumConst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 15:06:02 +08:00
|
|
|
// FIXME: Fixup LBraceLoc and RBraceLoc
|
2009-08-08 22:36:57 +08:00
|
|
|
// FIXME: Empty Scope and AttributeList (required to handle attribute packed).
|
2009-05-16 15:06:02 +08:00
|
|
|
SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
|
|
|
|
Sema::DeclPtrTy::make(Enum),
|
2009-08-08 22:36:57 +08:00
|
|
|
&Enumerators[0], Enumerators.size(),
|
|
|
|
0, 0);
|
2009-03-18 05:15:40 +08:00
|
|
|
|
|
|
|
return Enum;
|
|
|
|
}
|
|
|
|
|
2009-03-25 23:04:13 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
|
|
|
|
assert(false && "EnumConstantDecls can only occur within EnumDecls.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-20 09:44:21 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
|
|
|
|
TemplateParameterList *TempParams = D->getTemplateParameters();
|
|
|
|
TemplateParameterList *InstParams = InstantiateTemplateParams(TempParams);
|
|
|
|
if (!InstParams) return NULL;
|
|
|
|
|
|
|
|
CXXRecordDecl *Pattern = D->getTemplatedDecl();
|
|
|
|
CXXRecordDecl *RecordInst
|
|
|
|
= CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
|
|
|
|
Pattern->getLocation(), Pattern->getIdentifier(),
|
|
|
|
Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL);
|
|
|
|
|
|
|
|
ClassTemplateDecl *Inst
|
|
|
|
= ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
|
|
|
D->getIdentifier(), InstParams, RecordInst, 0);
|
|
|
|
RecordInst->setDescribedClassTemplate(Inst);
|
|
|
|
Inst->setAccess(D->getAccess());
|
|
|
|
Inst->setInstantiatedFromMemberTemplate(D);
|
|
|
|
|
|
|
|
Owner->addDecl(Inst);
|
|
|
|
return Inst;
|
|
|
|
}
|
|
|
|
|
2009-03-26 05:17:03 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
|
|
|
|
CXXRecordDecl *PrevDecl = 0;
|
|
|
|
if (D->isInjectedClassName())
|
|
|
|
PrevDecl = cast<CXXRecordDecl>(Owner);
|
|
|
|
|
|
|
|
CXXRecordDecl *Record
|
|
|
|
= CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
|
2009-07-21 22:46:17 +08:00
|
|
|
D->getLocation(), D->getIdentifier(),
|
|
|
|
D->getTagKeywordLoc(), PrevDecl);
|
2009-03-26 05:17:03 +08:00
|
|
|
Record->setImplicit(D->isImplicit());
|
|
|
|
Record->setAccess(D->getAccess());
|
|
|
|
if (!D->isInjectedClassName())
|
|
|
|
Record->setInstantiationOfMemberClass(D);
|
|
|
|
|
2009-06-30 10:36:12 +08:00
|
|
|
Owner->addDecl(Record);
|
2009-03-26 05:17:03 +08:00
|
|
|
return Record;
|
|
|
|
}
|
|
|
|
|
2009-06-26 06:08:12 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
|
2009-06-30 04:59:39 +08:00
|
|
|
// Check whether there is already a function template specialization for
|
|
|
|
// this declaration.
|
|
|
|
FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
|
|
|
|
void *InsertPos = 0;
|
|
|
|
if (FunctionTemplate) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
FunctionTemplateSpecializationInfo::Profile(ID,
|
|
|
|
TemplateArgs.getFlatArgumentList(),
|
2009-07-30 00:09:57 +08:00
|
|
|
TemplateArgs.flat_size(),
|
|
|
|
SemaRef.Context);
|
2009-06-30 04:59:39 +08:00
|
|
|
|
|
|
|
FunctionTemplateSpecializationInfo *Info
|
|
|
|
= FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos);
|
|
|
|
|
|
|
|
// If we already have a function template specialization, return it.
|
|
|
|
if (Info)
|
|
|
|
return Info->Function;
|
|
|
|
}
|
2009-06-26 06:08:12 +08:00
|
|
|
|
|
|
|
Sema::LocalInstantiationScope Scope(SemaRef);
|
|
|
|
|
|
|
|
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
|
|
|
QualType T = InstantiateFunctionType(D, Params);
|
|
|
|
if (T.isNull())
|
2009-03-24 07:06:20 +08:00
|
|
|
return 0;
|
2009-08-14 10:03:10 +08:00
|
|
|
|
2009-06-26 06:08:12 +08:00
|
|
|
// Build the instantiated method declaration.
|
2009-08-14 10:03:10 +08:00
|
|
|
FunctionDecl *Function;
|
|
|
|
if (FriendFunctionDecl* FFD = dyn_cast<FriendFunctionDecl>(D)) {
|
|
|
|
// The new decl's semantic context. FIXME: this might need
|
|
|
|
// to be instantiated.
|
|
|
|
DeclContext *DC = D->getDeclContext();
|
|
|
|
|
|
|
|
// This assert is bogus and exists only to catch cases we don't
|
|
|
|
// handle yet.
|
|
|
|
assert(!DC->isDependentContext());
|
|
|
|
|
|
|
|
Function =
|
|
|
|
FriendFunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
D->getDeclName(), T, D->getDeclaratorInfo(),
|
|
|
|
D->isInline(), FFD->getFriendLoc());
|
2009-08-14 10:03:10 +08:00
|
|
|
Function->setLexicalDeclContext(Owner);
|
|
|
|
} else {
|
|
|
|
Function =
|
|
|
|
FunctionDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
D->getDeclName(), T, D->getDeclaratorInfo(),
|
|
|
|
D->getStorageClass(),
|
2009-08-21 08:31:54 +08:00
|
|
|
D->isInline(), D->hasWrittenPrototype());
|
2009-08-14 10:03:10 +08:00
|
|
|
}
|
2009-06-26 06:08:12 +08:00
|
|
|
|
|
|
|
// Attach the parameters
|
|
|
|
for (unsigned P = 0; P < Params.size(); ++P)
|
|
|
|
Params[P]->setOwningFunction(Function);
|
|
|
|
Function->setParams(SemaRef.Context, Params.data(), Params.size());
|
|
|
|
|
|
|
|
if (InitFunctionInstantiation(Function, D))
|
|
|
|
Function->setInvalidDecl();
|
|
|
|
|
|
|
|
bool Redeclaration = false;
|
|
|
|
bool OverloadableAttrRequired = false;
|
|
|
|
NamedDecl *PrevDecl = 0;
|
|
|
|
SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
|
|
|
|
/*FIXME:*/OverloadableAttrRequired);
|
|
|
|
|
2009-06-30 04:59:39 +08:00
|
|
|
if (FunctionTemplate) {
|
|
|
|
// Record this function template specialization.
|
|
|
|
Function->setFunctionTemplateSpecialization(SemaRef.Context,
|
|
|
|
FunctionTemplate,
|
|
|
|
&TemplateArgs,
|
|
|
|
InsertPos);
|
2009-08-14 10:03:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this was a friend function decl, it's a member which
|
|
|
|
// needs to be added.
|
|
|
|
if (isa<FriendFunctionDecl>(Function)) {
|
|
|
|
// If the new context is still dependent, this declaration
|
|
|
|
// needs to remain hidden.
|
|
|
|
if (Owner->isDependentContext())
|
|
|
|
Owner->addHiddenDecl(Function);
|
|
|
|
else
|
|
|
|
Owner->addDecl(Function);
|
|
|
|
}
|
2009-06-30 04:59:39 +08:00
|
|
|
|
2009-06-26 06:08:12 +08:00
|
|
|
return Function;
|
|
|
|
}
|
2009-03-24 07:06:20 +08:00
|
|
|
|
2009-06-26 06:08:12 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
|
2009-08-21 08:16:32 +08:00
|
|
|
// Check whether there is already a function template specialization for
|
|
|
|
// this declaration.
|
|
|
|
FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
|
|
|
|
void *InsertPos = 0;
|
|
|
|
if (FunctionTemplate) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
FunctionTemplateSpecializationInfo::Profile(ID,
|
|
|
|
TemplateArgs.getFlatArgumentList(),
|
|
|
|
TemplateArgs.flat_size(),
|
|
|
|
SemaRef.Context);
|
|
|
|
|
|
|
|
FunctionTemplateSpecializationInfo *Info
|
|
|
|
= FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos);
|
|
|
|
|
|
|
|
// If we already have a function template specialization, return it.
|
|
|
|
if (Info)
|
|
|
|
return Info->Function;
|
|
|
|
}
|
|
|
|
|
2009-05-15 05:44:34 +08:00
|
|
|
Sema::LocalInstantiationScope Scope(SemaRef);
|
|
|
|
|
2009-05-30 02:27:38 +08:00
|
|
|
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
2009-03-24 08:38:23 +08:00
|
|
|
QualType T = InstantiateFunctionType(D, Params);
|
2009-03-24 07:06:20 +08:00
|
|
|
if (T.isNull())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Build the instantiated method declaration.
|
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
|
2009-08-22 02:42:58 +08:00
|
|
|
CXXMethodDecl *Method = 0;
|
|
|
|
|
|
|
|
DeclarationName Name = D->getDeclName();
|
|
|
|
CXXConstructorDecl *ConstructorD = dyn_cast<CXXConstructorDecl>(D);
|
|
|
|
if (ConstructorD) {
|
|
|
|
QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
|
|
|
|
Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
|
|
|
|
SemaRef.Context.getCanonicalType(ClassTy));
|
|
|
|
Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
|
|
|
|
ConstructorD->getLocation(),
|
|
|
|
Name, T,
|
|
|
|
ConstructorD->getDeclaratorInfo(),
|
|
|
|
ConstructorD->isExplicit(),
|
|
|
|
ConstructorD->isInline(), false);
|
|
|
|
} else {
|
|
|
|
Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
|
|
|
|
D->getDeclName(), T, D->getDeclaratorInfo(),
|
|
|
|
D->isStatic(), D->isInline());
|
|
|
|
}
|
2009-08-21 08:16:32 +08:00
|
|
|
|
|
|
|
if (!FunctionTemplate)
|
|
|
|
Method->setInstantiationOfMemberFunction(D);
|
2009-03-24 07:06:20 +08:00
|
|
|
|
2009-07-25 04:34:43 +08:00
|
|
|
// If we are instantiating a member function defined
|
|
|
|
// out-of-line, the instantiation will have the same lexical
|
|
|
|
// context (which will be a namespace scope) as the template.
|
|
|
|
if (D->isOutOfLine())
|
|
|
|
Method->setLexicalDeclContext(D->getLexicalDeclContext());
|
|
|
|
|
2009-03-24 08:38:23 +08:00
|
|
|
// Attach the parameters
|
|
|
|
for (unsigned P = 0; P < Params.size(); ++P)
|
|
|
|
Params[P]->setOwningFunction(Method);
|
2009-05-21 17:52:38 +08:00
|
|
|
Method->setParams(SemaRef.Context, Params.data(), Params.size());
|
2009-03-24 08:38:23 +08:00
|
|
|
|
|
|
|
if (InitMethodInstantiation(Method, D))
|
|
|
|
Method->setInvalidDecl();
|
2009-03-24 07:06:20 +08:00
|
|
|
|
2009-08-22 02:42:58 +08:00
|
|
|
NamedDecl *PrevDecl = 0;
|
|
|
|
|
|
|
|
if (!FunctionTemplate) {
|
|
|
|
PrevDecl = SemaRef.LookupQualifiedName(Owner, Name,
|
|
|
|
Sema::LookupOrdinaryName, true);
|
|
|
|
|
|
|
|
// In C++, the previous declaration we find might be a tag type
|
|
|
|
// (class or enum). In this case, the new declaration will hide the
|
|
|
|
// tag type. Note that this does does not apply if we're declaring a
|
|
|
|
// typedef (C++ [dcl.typedef]p4).
|
|
|
|
if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
|
|
|
|
PrevDecl = 0;
|
|
|
|
}
|
|
|
|
|
2009-03-24 07:06:20 +08:00
|
|
|
bool Redeclaration = false;
|
|
|
|
bool OverloadableAttrRequired = false;
|
2009-04-25 16:06:05 +08:00
|
|
|
SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
|
|
|
|
/*FIXME:*/OverloadableAttrRequired);
|
2009-03-24 07:06:20 +08:00
|
|
|
|
2009-08-21 08:16:32 +08:00
|
|
|
if (FunctionTemplate)
|
|
|
|
// Record this function template specialization.
|
|
|
|
Method->setFunctionTemplateSpecialization(SemaRef.Context,
|
|
|
|
FunctionTemplate,
|
|
|
|
&TemplateArgs,
|
|
|
|
InsertPos);
|
|
|
|
else if (!Method->isInvalidDecl() || !PrevDecl)
|
2009-08-22 02:42:58 +08:00
|
|
|
Owner->addDecl(Method);
|
|
|
|
|
2009-03-24 07:06:20 +08:00
|
|
|
return Method;
|
|
|
|
}
|
|
|
|
|
2009-03-25 00:43:20 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
|
2009-08-22 02:42:58 +08:00
|
|
|
return VisitCXXMethodDecl(D);
|
2009-03-25 00:43:20 +08:00
|
|
|
}
|
|
|
|
|
2009-03-24 08:15:49 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
|
2009-05-15 05:44:34 +08:00
|
|
|
Sema::LocalInstantiationScope Scope(SemaRef);
|
|
|
|
|
2009-05-30 02:27:38 +08:00
|
|
|
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
2009-03-24 08:38:23 +08:00
|
|
|
QualType T = InstantiateFunctionType(D, Params);
|
2009-03-24 08:15:49 +08:00
|
|
|
if (T.isNull())
|
|
|
|
return 0;
|
2009-03-24 08:38:23 +08:00
|
|
|
assert(Params.size() == 0 && "Destructor with parameters?");
|
|
|
|
|
2009-03-24 08:15:49 +08:00
|
|
|
// Build the instantiated destructor declaration.
|
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
|
2009-08-05 13:36:45 +08:00
|
|
|
CanQualType ClassTy =
|
2009-05-16 05:18:27 +08:00
|
|
|
SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record));
|
2009-03-24 08:15:49 +08:00
|
|
|
CXXDestructorDecl *Destructor
|
|
|
|
= CXXDestructorDecl::Create(SemaRef.Context, Record,
|
|
|
|
D->getLocation(),
|
|
|
|
SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
|
|
|
|
T, D->isInline(), false);
|
2009-05-15 05:06:31 +08:00
|
|
|
Destructor->setInstantiationOfMemberFunction(D);
|
2009-03-24 08:38:23 +08:00
|
|
|
if (InitMethodInstantiation(Destructor, D))
|
|
|
|
Destructor->setInvalidDecl();
|
2009-03-24 08:15:49 +08:00
|
|
|
|
|
|
|
bool Redeclaration = false;
|
|
|
|
bool OverloadableAttrRequired = false;
|
|
|
|
NamedDecl *PrevDecl = 0;
|
2009-04-25 16:06:05 +08:00
|
|
|
SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
|
|
|
|
/*FIXME:*/OverloadableAttrRequired);
|
2009-06-30 10:36:12 +08:00
|
|
|
Owner->addDecl(Destructor);
|
2009-03-24 08:15:49 +08:00
|
|
|
return Destructor;
|
|
|
|
}
|
|
|
|
|
2009-03-25 08:34:44 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
|
2009-06-26 06:08:12 +08:00
|
|
|
// FIXME: Look for existing, explicit specializations.
|
2009-05-15 05:44:34 +08:00
|
|
|
Sema::LocalInstantiationScope Scope(SemaRef);
|
|
|
|
|
2009-05-30 02:27:38 +08:00
|
|
|
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
2009-03-25 08:34:44 +08:00
|
|
|
QualType T = InstantiateFunctionType(D, Params);
|
|
|
|
if (T.isNull())
|
|
|
|
return 0;
|
|
|
|
assert(Params.size() == 0 && "Destructor with parameters?");
|
|
|
|
|
|
|
|
// Build the instantiated conversion declaration.
|
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
|
|
|
|
QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
|
2009-08-05 13:36:45 +08:00
|
|
|
CanQualType ConvTy
|
2009-03-25 08:34:44 +08:00
|
|
|
= SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
|
|
|
|
CXXConversionDecl *Conversion
|
|
|
|
= CXXConversionDecl::Create(SemaRef.Context, Record,
|
|
|
|
D->getLocation(),
|
|
|
|
SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
|
2009-08-19 09:27:57 +08:00
|
|
|
T, D->getDeclaratorInfo(),
|
|
|
|
D->isInline(), D->isExplicit());
|
2009-05-15 05:06:31 +08:00
|
|
|
Conversion->setInstantiationOfMemberFunction(D);
|
2009-03-25 08:34:44 +08:00
|
|
|
if (InitMethodInstantiation(Conversion, D))
|
|
|
|
Conversion->setInvalidDecl();
|
|
|
|
|
|
|
|
bool Redeclaration = false;
|
|
|
|
bool OverloadableAttrRequired = false;
|
|
|
|
NamedDecl *PrevDecl = 0;
|
2009-04-25 16:06:05 +08:00
|
|
|
SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
|
|
|
|
/*FIXME:*/OverloadableAttrRequired);
|
2009-06-30 10:36:12 +08:00
|
|
|
Owner->addDecl(Conversion);
|
2009-03-25 08:34:44 +08:00
|
|
|
return Conversion;
|
|
|
|
}
|
|
|
|
|
2009-03-25 23:04:13 +08:00
|
|
|
ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
|
2009-03-24 07:06:20 +08:00
|
|
|
QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
|
2009-05-12 07:53:27 +08:00
|
|
|
D->getLocation(), D->getDeclName());
|
2009-03-24 07:06:20 +08:00
|
|
|
if (OrigT.isNull())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
QualType T = SemaRef.adjustParameterType(OrigT);
|
|
|
|
|
|
|
|
if (D->getDefaultArg()) {
|
|
|
|
// FIXME: Leave a marker for "uninstantiated" default
|
|
|
|
// arguments. They only get instantiated on demand at the call
|
|
|
|
// site.
|
|
|
|
unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
|
|
|
|
"sorry, dropping default argument during template instantiation");
|
|
|
|
SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
|
|
|
|
<< D->getDefaultArg()->getSourceRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the parameter
|
|
|
|
ParmVarDecl *Param = 0;
|
|
|
|
if (T == OrigT)
|
|
|
|
Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
D->getIdentifier(), T, D->getDeclaratorInfo(),
|
|
|
|
D->getStorageClass(), 0);
|
2009-03-24 07:06:20 +08:00
|
|
|
else
|
|
|
|
Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
|
|
|
|
D->getLocation(), D->getIdentifier(),
|
2009-08-19 09:27:57 +08:00
|
|
|
T, D->getDeclaratorInfo(), OrigT,
|
|
|
|
D->getStorageClass(), 0);
|
2009-03-24 07:06:20 +08:00
|
|
|
|
|
|
|
// Note: we don't try to instantiate function parameters until after
|
|
|
|
// we've instantiated the function's type. Therefore, we don't have
|
|
|
|
// to check for 'void' parameter types here.
|
2009-05-15 05:44:34 +08:00
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
|
2009-03-24 07:06:20 +08:00
|
|
|
return Param;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decl *
|
|
|
|
TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
|
|
|
|
// Since parameter types can decay either before or after
|
|
|
|
// instantiation, we simply treat OriginalParmVarDecls as
|
|
|
|
// ParmVarDecls the same way, and create one or the other depending
|
|
|
|
// on what happens after template instantiation.
|
|
|
|
return VisitParmVarDecl(D);
|
|
|
|
}
|
|
|
|
|
2009-08-20 09:44:21 +08:00
|
|
|
Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
|
|
|
|
TemplateTypeParmDecl *D) {
|
|
|
|
// TODO: don't always clone when decls are refcounted.
|
|
|
|
const Type* T = D->getTypeForDecl();
|
|
|
|
assert(T->isTemplateTypeParmType());
|
|
|
|
const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
|
|
|
|
|
|
|
|
TemplateTypeParmDecl *Inst =
|
|
|
|
TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
|
|
|
TTPT->getDepth(), TTPT->getIndex(),
|
|
|
|
TTPT->getName(),
|
|
|
|
D->wasDeclaredWithTypename(),
|
|
|
|
D->isParameterPack());
|
|
|
|
|
|
|
|
if (D->hasDefaultArgument()) {
|
|
|
|
QualType DefaultPattern = D->getDefaultArgument();
|
|
|
|
QualType DefaultInst
|
|
|
|
= SemaRef.InstantiateType(DefaultPattern, TemplateArgs,
|
|
|
|
D->getDefaultArgumentLoc(),
|
|
|
|
D->getDeclName());
|
|
|
|
|
|
|
|
Inst->setDefaultArgument(DefaultInst,
|
|
|
|
D->getDefaultArgumentLoc(),
|
|
|
|
D->defaultArgumentWasInherited() /* preserve? */);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Inst;
|
|
|
|
}
|
|
|
|
|
2009-03-18 05:15:40 +08:00
|
|
|
Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
|
2009-05-12 07:53:27 +08:00
|
|
|
const TemplateArgumentList &TemplateArgs) {
|
|
|
|
TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
|
2009-03-18 05:15:40 +08:00
|
|
|
return Instantiator.Visit(D);
|
|
|
|
}
|
|
|
|
|
2009-08-20 09:44:21 +08:00
|
|
|
/// \brief Instantiates a nested template parameter list in the current
|
|
|
|
/// instantiation context.
|
|
|
|
///
|
|
|
|
/// \param L The parameter list to instantiate
|
|
|
|
///
|
|
|
|
/// \returns NULL if there was an error
|
|
|
|
TemplateParameterList *
|
|
|
|
TemplateDeclInstantiator::InstantiateTemplateParams(TemplateParameterList *L) {
|
|
|
|
// Get errors for all the parameters before bailing out.
|
|
|
|
bool Invalid = false;
|
|
|
|
|
|
|
|
unsigned N = L->size();
|
|
|
|
typedef llvm::SmallVector<Decl*,8> ParamVector;
|
|
|
|
ParamVector Params;
|
|
|
|
Params.reserve(N);
|
|
|
|
for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
|
|
|
|
PI != PE; ++PI) {
|
|
|
|
Decl *D = Visit(*PI);
|
|
|
|
Params.push_back(D);
|
|
|
|
Invalid = Invalid || !D;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up if we had an error.
|
|
|
|
if (Invalid) {
|
|
|
|
for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
|
|
|
|
PI != PE; ++PI)
|
|
|
|
if (*PI)
|
|
|
|
(*PI)->Destroy(SemaRef.Context);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TemplateParameterList *InstL
|
|
|
|
= TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
|
|
|
|
L->getLAngleLoc(), &Params.front(), N,
|
|
|
|
L->getRAngleLoc());
|
|
|
|
return InstL;
|
|
|
|
}
|
|
|
|
|
2009-03-24 08:38:23 +08:00
|
|
|
/// \brief Instantiates the type of the given function, including
|
|
|
|
/// instantiating all of the function parameters.
|
|
|
|
///
|
|
|
|
/// \param D The function that we will be instantiated
|
|
|
|
///
|
|
|
|
/// \param Params the instantiated parameter declarations
|
|
|
|
|
|
|
|
/// \returns the instantiated function's type if successfull, a NULL
|
|
|
|
/// type if there was an error.
|
|
|
|
QualType
|
|
|
|
TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D,
|
|
|
|
llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
|
|
|
|
bool InvalidDecl = false;
|
|
|
|
|
|
|
|
// Instantiate the function parameters
|
2009-05-12 07:53:27 +08:00
|
|
|
TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
|
2009-05-30 02:27:38 +08:00
|
|
|
llvm::SmallVector<QualType, 4> ParamTys;
|
2009-03-24 08:38:23 +08:00
|
|
|
for (FunctionDecl::param_iterator P = D->param_begin(),
|
|
|
|
PEnd = D->param_end();
|
|
|
|
P != PEnd; ++P) {
|
2009-03-25 23:04:13 +08:00
|
|
|
if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
|
2009-03-24 08:38:23 +08:00
|
|
|
if (PInst->getType()->isVoidType()) {
|
|
|
|
SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
|
|
|
|
PInst->setInvalidDecl();
|
2009-08-05 05:02:39 +08:00
|
|
|
} else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
|
|
|
|
PInst->getType(),
|
|
|
|
diag::err_abstract_type_in_decl,
|
|
|
|
Sema::AbstractParamType))
|
2009-03-24 08:38:23 +08:00
|
|
|
PInst->setInvalidDecl();
|
|
|
|
|
|
|
|
Params.push_back(PInst);
|
|
|
|
ParamTys.push_back(PInst->getType());
|
|
|
|
|
|
|
|
if (PInst->isInvalidDecl())
|
|
|
|
InvalidDecl = true;
|
|
|
|
} else
|
|
|
|
InvalidDecl = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Deallocate dead declarations.
|
|
|
|
if (InvalidDecl)
|
|
|
|
return QualType();
|
|
|
|
|
|
|
|
const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
|
|
|
|
assert(Proto && "Missing prototype?");
|
|
|
|
QualType ResultType
|
2009-05-15 05:06:31 +08:00
|
|
|
= SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs,
|
2009-03-24 08:38:23 +08:00
|
|
|
D->getLocation(), D->getDeclName());
|
|
|
|
if (ResultType.isNull())
|
|
|
|
return QualType();
|
|
|
|
|
2009-05-21 17:52:38 +08:00
|
|
|
return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
|
2009-03-24 08:38:23 +08:00
|
|
|
Proto->isVariadic(), Proto->getTypeQuals(),
|
|
|
|
D->getLocation(), D->getDeclName());
|
|
|
|
}
|
|
|
|
|
2009-06-26 06:08:12 +08:00
|
|
|
/// \brief Initializes the common fields of an instantiation function
|
|
|
|
/// declaration (New) from the corresponding fields of its template (Tmpl).
|
|
|
|
///
|
|
|
|
/// \returns true if there was an error
|
|
|
|
bool
|
|
|
|
TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
|
|
|
|
FunctionDecl *Tmpl) {
|
|
|
|
if (Tmpl->isDeleted())
|
|
|
|
New->setDeleted();
|
2009-07-02 06:01:06 +08:00
|
|
|
|
|
|
|
// If we are performing substituting explicitly-specified template arguments
|
|
|
|
// or deduced template arguments into a function template and we reach this
|
|
|
|
// point, we are now past the point where SFINAE applies and have committed
|
|
|
|
// to keeping the new function template specialization. We therefore
|
|
|
|
// convert the active template instantiation for the function template
|
|
|
|
// into a template instantiation for this specific function template
|
|
|
|
// specialization, which is not a SFINAE context, so that we diagnose any
|
|
|
|
// further errors in the declaration itself.
|
|
|
|
typedef Sema::ActiveTemplateInstantiation ActiveInstType;
|
|
|
|
ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
|
|
|
|
if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
|
|
|
|
ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
|
|
|
|
if (FunctionTemplateDecl *FunTmpl
|
|
|
|
= dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
|
|
|
|
assert(FunTmpl->getTemplatedDecl() == Tmpl &&
|
|
|
|
"Deduction from the wrong function template?");
|
2009-07-17 06:10:11 +08:00
|
|
|
(void) FunTmpl;
|
2009-07-02 06:01:06 +08:00
|
|
|
ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
|
|
|
|
ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-26 06:08:12 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-24 08:38:23 +08:00
|
|
|
/// \brief Initializes common fields of an instantiated method
|
|
|
|
/// declaration (New) from the corresponding fields of its template
|
|
|
|
/// (Tmpl).
|
|
|
|
///
|
|
|
|
/// \returns true if there was an error
|
|
|
|
bool
|
|
|
|
TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
|
|
|
|
CXXMethodDecl *Tmpl) {
|
2009-06-26 06:08:12 +08:00
|
|
|
if (InitFunctionInstantiation(New, Tmpl))
|
|
|
|
return true;
|
|
|
|
|
2009-03-24 08:38:23 +08:00
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
|
|
|
|
New->setAccess(Tmpl->getAccess());
|
2009-05-15 06:15:41 +08:00
|
|
|
if (Tmpl->isVirtualAsWritten()) {
|
|
|
|
New->setVirtualAsWritten(true);
|
2009-03-24 08:38:23 +08:00
|
|
|
Record->setAggregate(false);
|
|
|
|
Record->setPOD(false);
|
2009-08-16 05:55:26 +08:00
|
|
|
Record->setEmpty(false);
|
2009-03-24 08:38:23 +08:00
|
|
|
Record->setPolymorphic(true);
|
|
|
|
}
|
|
|
|
if (Tmpl->isPure()) {
|
|
|
|
New->setPure();
|
|
|
|
Record->setAbstract(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: attributes
|
|
|
|
// FIXME: New needs a pointer to Tmpl
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-14 04:28:22 +08:00
|
|
|
|
|
|
|
/// \brief Instantiate the definition of the given function from its
|
|
|
|
/// template.
|
|
|
|
///
|
2009-07-01 01:20:14 +08:00
|
|
|
/// \param PointOfInstantiation the point at which the instantiation was
|
|
|
|
/// required. Note that this is not precisely a "point of instantiation"
|
|
|
|
/// for the function, but it's close.
|
|
|
|
///
|
2009-05-14 04:28:22 +08:00
|
|
|
/// \param Function the already-instantiated declaration of a
|
2009-07-01 01:20:14 +08:00
|
|
|
/// function template specialization or member function of a class template
|
|
|
|
/// specialization.
|
|
|
|
///
|
|
|
|
/// \param Recursive if true, recursively instantiates any functions that
|
|
|
|
/// are required by this instantiation.
|
2009-05-19 01:01:57 +08:00
|
|
|
void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
|
2009-07-01 01:20:14 +08:00
|
|
|
FunctionDecl *Function,
|
|
|
|
bool Recursive) {
|
2009-05-15 07:26:13 +08:00
|
|
|
if (Function->isInvalidDecl())
|
|
|
|
return;
|
|
|
|
|
2009-06-30 10:35:26 +08:00
|
|
|
assert(!Function->getBody() && "Already instantiated!");
|
2009-06-23 07:06:13 +08:00
|
|
|
|
2009-05-15 05:06:31 +08:00
|
|
|
// Find the function body that we'll be substituting.
|
2009-06-26 08:10:03 +08:00
|
|
|
const FunctionDecl *PatternDecl = 0;
|
|
|
|
if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate())
|
|
|
|
PatternDecl = Primary->getTemplatedDecl();
|
|
|
|
else
|
|
|
|
PatternDecl = Function->getInstantiatedFromMemberFunction();
|
2009-05-15 05:06:31 +08:00
|
|
|
Stmt *Pattern = 0;
|
|
|
|
if (PatternDecl)
|
2009-06-30 10:35:26 +08:00
|
|
|
Pattern = PatternDecl->getBody(PatternDecl);
|
2009-05-15 05:06:31 +08:00
|
|
|
|
|
|
|
if (!Pattern)
|
|
|
|
return;
|
|
|
|
|
2009-05-19 01:01:57 +08:00
|
|
|
InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
|
|
|
|
if (Inst)
|
|
|
|
return;
|
2009-05-15 08:01:03 +08:00
|
|
|
|
2009-07-01 01:20:14 +08:00
|
|
|
// If we're performing recursive template instantiation, create our own
|
|
|
|
// queue of pending implicit instantiations that we will instantiate later,
|
|
|
|
// while we're still within our own instantiation context.
|
|
|
|
std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
|
|
|
|
if (Recursive)
|
|
|
|
PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
|
|
|
|
|
2009-05-16 01:59:04 +08:00
|
|
|
ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
|
|
|
|
|
2009-05-15 07:26:13 +08:00
|
|
|
// Introduce a new scope where local variable instantiations will be
|
|
|
|
// recorded.
|
|
|
|
LocalInstantiationScope Scope(*this);
|
|
|
|
|
|
|
|
// Introduce the instantiated function parameters into the local
|
|
|
|
// instantiation scope.
|
|
|
|
for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
|
|
|
|
Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
|
|
|
|
Function->getParamDecl(I));
|
|
|
|
|
2009-05-15 08:01:03 +08:00
|
|
|
// Enter the scope of this instantiation. We don't use
|
|
|
|
// PushDeclContext because we don't have a scope.
|
|
|
|
DeclContext *PreviousContext = CurContext;
|
|
|
|
CurContext = Function;
|
|
|
|
|
2009-05-15 07:26:13 +08:00
|
|
|
// Instantiate the function body.
|
|
|
|
OwningStmtResult Body
|
|
|
|
= InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
|
2009-05-16 01:59:04 +08:00
|
|
|
|
|
|
|
ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
|
|
|
|
/*IsInstantiation=*/true);
|
2009-05-15 08:01:03 +08:00
|
|
|
|
|
|
|
CurContext = PreviousContext;
|
2009-05-27 04:50:29 +08:00
|
|
|
|
|
|
|
DeclGroupRef DG(Function);
|
|
|
|
Consumer.HandleTopLevelDecl(DG);
|
2009-07-01 01:20:14 +08:00
|
|
|
|
|
|
|
if (Recursive) {
|
|
|
|
// Instantiate any pending implicit instantiations found during the
|
|
|
|
// instantiation of this template.
|
|
|
|
PerformPendingImplicitInstantiations();
|
|
|
|
|
|
|
|
// Restore the set of pending implicit instantiations.
|
|
|
|
PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
|
|
|
|
}
|
2009-05-14 04:28:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Instantiate the definition of the given variable from its
|
|
|
|
/// template.
|
|
|
|
///
|
2009-07-25 04:34:43 +08:00
|
|
|
/// \param PointOfInstantiation the point at which the instantiation was
|
|
|
|
/// required. Note that this is not precisely a "point of instantiation"
|
|
|
|
/// for the function, but it's close.
|
|
|
|
///
|
|
|
|
/// \param Var the already-instantiated declaration of a static member
|
|
|
|
/// variable of a class template specialization.
|
|
|
|
///
|
|
|
|
/// \param Recursive if true, recursively instantiates any functions that
|
|
|
|
/// are required by this instantiation.
|
|
|
|
void Sema::InstantiateStaticDataMemberDefinition(
|
|
|
|
SourceLocation PointOfInstantiation,
|
|
|
|
VarDecl *Var,
|
|
|
|
bool Recursive) {
|
|
|
|
if (Var->isInvalidDecl())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find the out-of-line definition of this static data member.
|
|
|
|
// FIXME: Do we have to look for specializations separately?
|
|
|
|
VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
|
|
|
|
bool FoundOutOfLineDef = false;
|
|
|
|
assert(Def && "This data member was not instantiated from a template?");
|
|
|
|
assert(Def->isStaticDataMember() && "Not a static data member?");
|
|
|
|
for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
|
|
|
|
RDEnd = Def->redecls_end();
|
|
|
|
RD != RDEnd; ++RD) {
|
|
|
|
if (RD->getLexicalDeclContext()->isFileContext()) {
|
|
|
|
Def = *RD;
|
|
|
|
FoundOutOfLineDef = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FoundOutOfLineDef) {
|
|
|
|
// We did not find an out-of-line definition of this static data member,
|
|
|
|
// so we won't perform any instantiation. Rather, we rely on the user to
|
|
|
|
// instantiate this definition (or provide a specialization for it) in
|
|
|
|
// another translation unit.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
|
|
|
|
if (Inst)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If we're performing recursive template instantiation, create our own
|
|
|
|
// queue of pending implicit instantiations that we will instantiate later,
|
|
|
|
// while we're still within our own instantiation context.
|
|
|
|
std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
|
|
|
|
if (Recursive)
|
|
|
|
PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
|
|
|
|
|
|
|
|
// Enter the scope of this instantiation. We don't use
|
|
|
|
// PushDeclContext because we don't have a scope.
|
|
|
|
DeclContext *PreviousContext = CurContext;
|
|
|
|
CurContext = Var->getDeclContext();
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// Instantiate the initializer of this static data member.
|
|
|
|
OwningExprResult Init
|
|
|
|
= InstantiateExpr(Def->getInit(), getTemplateInstantiationArgs(Var));
|
|
|
|
if (Init.isInvalid()) {
|
|
|
|
// If instantiation of the initializer failed, mark the declaration invalid
|
|
|
|
// and don't instantiate anything else that was triggered by this
|
|
|
|
// instantiation.
|
|
|
|
Var->setInvalidDecl();
|
|
|
|
|
|
|
|
// Restore the set of pending implicit instantiations.
|
|
|
|
PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type-check the initializer.
|
|
|
|
if (Init.get())
|
|
|
|
AddInitializerToDecl(DeclPtrTy::make(Var), move(Init),
|
|
|
|
Def->hasCXXDirectInitializer());
|
|
|
|
else
|
|
|
|
ActOnUninitializedDecl(DeclPtrTy::make(Var), false);
|
|
|
|
#else
|
|
|
|
Var = cast_or_null<VarDecl>(InstantiateDecl(Def, Var->getDeclContext(),
|
|
|
|
getTemplateInstantiationArgs(Var)));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CurContext = PreviousContext;
|
|
|
|
|
|
|
|
if (Var) {
|
|
|
|
DeclGroupRef DG(Var);
|
|
|
|
Consumer.HandleTopLevelDecl(DG);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Recursive) {
|
|
|
|
// Instantiate any pending implicit instantiations found during the
|
|
|
|
// instantiation of this template.
|
|
|
|
PerformPendingImplicitInstantiations();
|
|
|
|
|
|
|
|
// Restore the set of pending implicit instantiations.
|
|
|
|
PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
|
|
|
|
}
|
2009-05-14 04:28:22 +08:00
|
|
|
}
|
2009-05-27 13:35:12 +08:00
|
|
|
|
|
|
|
static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
|
|
|
|
if (D->getKind() != Other->getKind())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
|
2009-07-18 08:34:25 +08:00
|
|
|
return Record->getInstantiatedFromMemberClass()->getCanonicalDecl()
|
|
|
|
== D->getCanonicalDecl();
|
2009-05-27 13:35:12 +08:00
|
|
|
|
|
|
|
if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
|
2009-07-18 08:34:25 +08:00
|
|
|
return Function->getInstantiatedFromMemberFunction()->getCanonicalDecl()
|
|
|
|
== D->getCanonicalDecl();
|
2009-05-27 13:35:12 +08:00
|
|
|
|
2009-05-28 01:20:35 +08:00
|
|
|
if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
|
2009-07-18 08:34:25 +08:00
|
|
|
return Enum->getInstantiatedFromMemberEnum()->getCanonicalDecl()
|
|
|
|
== D->getCanonicalDecl();
|
2009-05-27 13:35:12 +08:00
|
|
|
|
2009-07-25 04:34:43 +08:00
|
|
|
if (VarDecl *Var = dyn_cast<VarDecl>(Other))
|
|
|
|
if (Var->isStaticDataMember())
|
|
|
|
return Var->getInstantiatedFromStaticDataMember()->getCanonicalDecl()
|
|
|
|
== D->getCanonicalDecl();
|
|
|
|
|
2009-05-27 13:35:12 +08:00
|
|
|
// FIXME: How can we find instantiations of anonymous unions?
|
|
|
|
|
|
|
|
return D->getDeclName() && isa<NamedDecl>(Other) &&
|
|
|
|
D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ForwardIterator>
|
|
|
|
static NamedDecl *findInstantiationOf(ASTContext &Ctx,
|
|
|
|
NamedDecl *D,
|
|
|
|
ForwardIterator first,
|
|
|
|
ForwardIterator last) {
|
|
|
|
for (; first != last; ++first)
|
|
|
|
if (isInstantiationOf(Ctx, D, *first))
|
|
|
|
return cast<NamedDecl>(*first);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-28 01:54:46 +08:00
|
|
|
/// \brief Find the instantiation of the given declaration within the
|
|
|
|
/// current instantiation.
|
2009-05-27 13:35:12 +08:00
|
|
|
///
|
|
|
|
/// This routine is intended to be used when \p D is a declaration
|
|
|
|
/// referenced from within a template, that needs to mapped into the
|
|
|
|
/// corresponding declaration within an instantiation. For example,
|
|
|
|
/// given:
|
|
|
|
///
|
|
|
|
/// \code
|
|
|
|
/// template<typename T>
|
|
|
|
/// struct X {
|
|
|
|
/// enum Kind {
|
|
|
|
/// KnownValue = sizeof(T)
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// bool getKind() const { return KnownValue; }
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// template struct X<int>;
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// In the instantiation of X<int>::getKind(), we need to map the
|
|
|
|
/// EnumConstantDecl for KnownValue (which refers to
|
|
|
|
/// X<T>::<Kind>::KnownValue) to its instantiation
|
2009-05-28 01:54:46 +08:00
|
|
|
/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
|
|
|
|
/// this mapping from within the instantiation of X<int>.
|
|
|
|
NamedDecl * Sema::InstantiateCurrentDeclRef(NamedDecl *D) {
|
2009-05-27 13:35:12 +08:00
|
|
|
DeclContext *ParentDC = D->getDeclContext();
|
2009-05-28 01:07:49 +08:00
|
|
|
if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
|
|
|
|
// D is a local of some kind. Look into the map of local
|
|
|
|
// declarations to their instantiations.
|
|
|
|
return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
|
|
|
|
}
|
2009-05-27 13:35:12 +08:00
|
|
|
|
2009-05-28 01:07:49 +08:00
|
|
|
if (NamedDecl *ParentDecl = dyn_cast<NamedDecl>(ParentDC)) {
|
2009-05-28 01:54:46 +08:00
|
|
|
ParentDecl = InstantiateCurrentDeclRef(ParentDecl);
|
2009-05-27 13:35:12 +08:00
|
|
|
if (!ParentDecl)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ParentDC = cast<DeclContext>(ParentDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ParentDC != D->getDeclContext()) {
|
|
|
|
// We performed some kind of instantiation in the parent context,
|
|
|
|
// so now we need to look into the instantiated parent context to
|
|
|
|
// find the instantiation of the declaration D.
|
|
|
|
NamedDecl *Result = 0;
|
|
|
|
if (D->getDeclName()) {
|
2009-06-30 10:36:12 +08:00
|
|
|
DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
|
2009-05-27 13:35:12 +08:00
|
|
|
Result = findInstantiationOf(Context, D, Found.first, Found.second);
|
|
|
|
} else {
|
|
|
|
// Since we don't have a name for the entity we're looking for,
|
|
|
|
// our only option is to walk through all of the declarations to
|
|
|
|
// find that name. This will occur in a few cases:
|
|
|
|
//
|
|
|
|
// - anonymous struct/union within a template
|
|
|
|
// - unnamed class/struct/union/enum within a template
|
|
|
|
//
|
|
|
|
// FIXME: Find a better way to find these instantiations!
|
|
|
|
Result = findInstantiationOf(Context, D,
|
2009-06-30 10:36:12 +08:00
|
|
|
ParentDC->decls_begin(),
|
|
|
|
ParentDC->decls_end());
|
2009-05-27 13:35:12 +08:00
|
|
|
}
|
|
|
|
assert(Result && "Unable to find instantiation of declaration!");
|
|
|
|
D = Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
|
2009-05-28 01:54:46 +08:00
|
|
|
if (ClassTemplateDecl *ClassTemplate
|
|
|
|
= Record->getDescribedClassTemplate()) {
|
|
|
|
// When the declaration D was parsed, it referred to the current
|
|
|
|
// instantiation. Therefore, look through the current context,
|
|
|
|
// which contains actual instantiations, to find the
|
|
|
|
// instantiation of the "current instantiation" that D refers
|
|
|
|
// to. Alternatively, we could just instantiate the
|
|
|
|
// injected-class-name with the current template arguments, but
|
|
|
|
// such an instantiation is far more expensive.
|
|
|
|
for (DeclContext *DC = CurContext; !DC->isFileContext();
|
|
|
|
DC = DC->getParent()) {
|
|
|
|
if (ClassTemplateSpecializationDecl *Spec
|
|
|
|
= dyn_cast<ClassTemplateSpecializationDecl>(DC))
|
2009-07-18 08:34:25 +08:00
|
|
|
if (Spec->getSpecializedTemplate()->getCanonicalDecl()
|
|
|
|
== ClassTemplate->getCanonicalDecl())
|
2009-05-28 01:54:46 +08:00
|
|
|
return Spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false &&
|
|
|
|
"Unable to find declaration for the current instantiation");
|
2009-05-27 13:35:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return D;
|
|
|
|
}
|
2009-06-23 07:06:13 +08:00
|
|
|
|
|
|
|
/// \brief Performs template instantiation for all implicit template
|
|
|
|
/// instantiations we have seen until this point.
|
|
|
|
void Sema::PerformPendingImplicitInstantiations() {
|
|
|
|
while (!PendingImplicitInstantiations.empty()) {
|
|
|
|
PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
|
2009-07-01 01:20:14 +08:00
|
|
|
PendingImplicitInstantiations.pop_front();
|
2009-06-23 07:06:13 +08:00
|
|
|
|
2009-07-25 04:34:43 +08:00
|
|
|
// Instantiate function definitions
|
|
|
|
if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
|
2009-06-30 10:35:26 +08:00
|
|
|
if (!Function->getBody())
|
2009-07-01 01:20:14 +08:00
|
|
|
InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
|
2009-07-25 04:34:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-06-23 07:06:13 +08:00
|
|
|
|
2009-07-25 04:34:43 +08:00
|
|
|
// Instantiate static data member definitions.
|
|
|
|
VarDecl *Var = cast<VarDecl>(Inst.first);
|
|
|
|
assert(Var->isStaticDataMember() && "Not a static data member?");
|
|
|
|
InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
|
2009-06-23 07:06:13 +08:00
|
|
|
}
|
|
|
|
}
|