2009-03-18 00:15:40 +03: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-11-18 10:57:50 +03:00
|
|
|
#include "Lookup.h"
|
2009-05-27 00:50:29 +04:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-03-18 00:15:40 +03:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/DeclTemplate.h"
|
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2009-09-01 09:12:24 +04:00
|
|
|
#include "clang/Basic/PrettyStackTrace.h"
|
2009-08-27 01:14:46 +04:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-03-18 00:15:40 +03:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2009-11-28 22:45:26 +03:00
|
|
|
class TemplateDeclInstantiator
|
2009-03-28 22:18:32 +03:00
|
|
|
: public DeclVisitor<TemplateDeclInstantiator, Decl *> {
|
2009-03-18 00:15:40 +03:00
|
|
|
Sema &SemaRef;
|
|
|
|
DeclContext *Owner;
|
2009-08-29 00:31:08 +04:00
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-07 09:07:58 +03:00
|
|
|
void InstantiateAttrs(Decl *Tmpl, Decl *New);
|
|
|
|
|
2009-03-18 00:15:40 +03:00
|
|
|
public:
|
|
|
|
typedef Sema::OwningExprResult OwningExprResult;
|
|
|
|
|
|
|
|
TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
|
2009-08-29 00:31:08 +04:00
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs)
|
2009-05-12 03:53:27 +04:00
|
|
|
: SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-16 11:39:55 +04: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 18:45:12 +03:00
|
|
|
Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
|
|
|
|
Decl *VisitNamespaceDecl(NamespaceDecl *D);
|
2009-03-18 00:15:40 +03:00
|
|
|
Decl *VisitTypedefDecl(TypedefDecl *D);
|
2009-03-26 02:32:15 +03:00
|
|
|
Decl *VisitVarDecl(VarDecl *D);
|
2009-03-18 00:15:40 +03:00
|
|
|
Decl *VisitFieldDecl(FieldDecl *D);
|
|
|
|
Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
|
|
|
|
Decl *VisitEnumDecl(EnumDecl *D);
|
2009-03-25 18:04:13 +03:00
|
|
|
Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
|
2009-08-28 11:59:38 +04:00
|
|
|
Decl *VisitFriendDecl(FriendDecl *D);
|
2009-10-13 18:39:41 +04:00
|
|
|
Decl *VisitFunctionDecl(FunctionDecl *D,
|
|
|
|
TemplateParameterList *TemplateParams = 0);
|
2009-03-26 00:17:03 +03:00
|
|
|
Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
|
2009-08-27 20:57:43 +04:00
|
|
|
Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
|
|
|
|
TemplateParameterList *TemplateParams = 0);
|
2009-03-24 19:43:20 +03:00
|
|
|
Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
|
2009-03-24 03:15:49 +03:00
|
|
|
Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
|
2009-03-25 03:34:44 +03:00
|
|
|
Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
|
2009-03-25 18:04:13 +03:00
|
|
|
ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
|
2009-08-20 05:44:21 +04:00
|
|
|
Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
|
2009-10-07 21:21:34 +04:00
|
|
|
Decl *VisitClassTemplatePartialSpecializationDecl(
|
|
|
|
ClassTemplatePartialSpecializationDecl *D);
|
2009-08-27 20:57:43 +04:00
|
|
|
Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
|
2009-08-20 05:44:21 +04:00
|
|
|
Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
|
2009-10-24 03:25:44 +04:00
|
|
|
Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
|
2009-11-11 19:58:32 +03:00
|
|
|
Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
|
2009-11-17 09:07:40 +03:00
|
|
|
Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
|
2009-11-18 05:36:19 +03:00
|
|
|
Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
|
|
|
|
Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-18 00:15:40 +03:00
|
|
|
// Base case. FIXME: Remove once we can instantiate everything.
|
2009-11-17 09:07:40 +03:00
|
|
|
Decl *VisitDecl(Decl *D) {
|
|
|
|
unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
|
|
|
|
Diagnostic::Error,
|
|
|
|
"cannot instantiate %0 yet");
|
|
|
|
SemaRef.Diag(D->getLocation(), DiagID)
|
|
|
|
<< D->getDeclKindName();
|
|
|
|
|
2009-03-18 00:15:40 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2009-03-24 03:38:23 +03:00
|
|
|
|
2009-08-14 06:03:10 +04:00
|
|
|
const LangOptions &getLangOptions() {
|
|
|
|
return SemaRef.getLangOptions();
|
|
|
|
}
|
|
|
|
|
2009-03-24 03:38:23 +03:00
|
|
|
// Helper functions for instantiating methods.
|
2009-08-26 02:02:44 +04:00
|
|
|
QualType SubstFunctionType(FunctionDecl *D,
|
2009-03-24 03:38:23 +03:00
|
|
|
llvm::SmallVectorImpl<ParmVarDecl *> &Params);
|
2009-06-26 02:08:12 +04:00
|
|
|
bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
|
2009-03-24 03:38:23 +03:00
|
|
|
bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
|
2009-08-20 05:44:21 +04:00
|
|
|
|
|
|
|
TemplateParameterList *
|
2009-08-26 02:02:44 +04:00
|
|
|
SubstTemplateParams(TemplateParameterList *List);
|
2009-10-29 03:04:11 +03:00
|
|
|
|
|
|
|
bool InstantiateClassTemplatePartialSpecialization(
|
|
|
|
ClassTemplateDecl *ClassTemplate,
|
|
|
|
ClassTemplatePartialSpecializationDecl *PartialSpec);
|
2009-03-18 00:15:40 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-11-07 09:07:58 +03:00
|
|
|
// FIXME: Is this too simple?
|
|
|
|
void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
|
|
|
|
for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
|
|
|
|
TmplAttr = TmplAttr->getNext()) {
|
|
|
|
|
|
|
|
// FIXME: Is cloning correct for all attributes?
|
|
|
|
Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
|
|
|
|
|
|
|
|
New->addAttr(NewAttr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-25 18:45:12 +03: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 00:15:40 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
|
|
|
|
bool Invalid = false;
|
2009-10-24 12:00:42 +04:00
|
|
|
DeclaratorInfo *DI = D->getTypeDeclaratorInfo();
|
|
|
|
if (DI->getType()->isDependentType()) {
|
|
|
|
DI = SemaRef.SubstType(DI, TemplateArgs,
|
|
|
|
D->getLocation(), D->getDeclName());
|
|
|
|
if (!DI) {
|
2009-03-18 00:15:40 +03:00
|
|
|
Invalid = true;
|
2009-10-24 12:00:42 +04:00
|
|
|
DI = SemaRef.Context.getTrivialDeclaratorInfo(SemaRef.Context.IntTy);
|
2009-03-18 00:15:40 +03:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-18 00:15:40 +03:00
|
|
|
// Create the new typedef
|
|
|
|
TypedefDecl *Typedef
|
|
|
|
= TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
2009-10-24 12:00:42 +04:00
|
|
|
D->getIdentifier(), DI);
|
2009-03-18 00:15:40 +03:00
|
|
|
if (Invalid)
|
|
|
|
Typedef->setInvalidDecl();
|
|
|
|
|
2009-06-30 06:36:12 +04:00
|
|
|
Owner->addDecl(Typedef);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-18 00:15:40 +03:00
|
|
|
return Typedef;
|
|
|
|
}
|
|
|
|
|
2009-03-26 02:32:15 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
|
2009-08-26 02:02:44 +04:00
|
|
|
// Do substitution on the type of the declaration
|
2009-10-21 06:39:02 +04:00
|
|
|
DeclaratorInfo *DI = SemaRef.SubstType(D->getDeclaratorInfo(),
|
|
|
|
TemplateArgs,
|
|
|
|
D->getTypeSpecStartLoc(),
|
|
|
|
D->getDeclName());
|
|
|
|
if (!DI)
|
2009-03-26 02:32:15 +03:00
|
|
|
return 0;
|
|
|
|
|
2009-05-15 04:01:03 +04:00
|
|
|
// Build the instantiated declaration
|
2009-03-26 02:32:15 +03:00
|
|
|
VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
|
|
|
|
D->getLocation(), D->getIdentifier(),
|
2009-10-21 06:39:02 +04:00
|
|
|
DI->getType(), DI,
|
2009-08-21 04:31:54 +04:00
|
|
|
D->getStorageClass());
|
2009-03-26 02:32:15 +03:00
|
|
|
Var->setThreadSpecified(D->isThreadSpecified());
|
|
|
|
Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
|
|
|
|
Var->setDeclaredInCondition(D->isDeclaredInCondition());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
// If we are instantiating a static data member defined
|
2009-07-25 00:34:43 +04:00
|
|
|
// 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-09-09 19:08:12 +04:00
|
|
|
|
2009-05-16 11:39:55 +04:00
|
|
|
// FIXME: In theory, we could have a previous declaration for variables that
|
|
|
|
// are not static data members.
|
2009-03-26 02:32:15 +03:00
|
|
|
bool Redeclaration = false;
|
2009-11-19 01:49:29 +03:00
|
|
|
// FIXME: having to fake up a LookupResult is dumb.
|
|
|
|
LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
|
|
|
|
Sema::LookupOrdinaryName);
|
|
|
|
SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
if (D->isOutOfLine()) {
|
|
|
|
D->getLexicalDeclContext()->addDecl(Var);
|
|
|
|
Owner->makeDeclVisibleInContext(Var);
|
|
|
|
} else {
|
|
|
|
Owner->addDecl(Var);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-08 11:24:58 +04: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-11-01 23:32:48 +03:00
|
|
|
TSK_ImplicitInstantiation);
|
2009-10-08 11:24:58 +04:00
|
|
|
|
2009-03-26 02:32:15 +03:00
|
|
|
if (D->getInit()) {
|
2009-09-09 19:08:12 +04:00
|
|
|
OwningExprResult Init
|
2009-08-26 02:02:44 +04:00
|
|
|
= SemaRef.SubstExpr(D->getInit(), TemplateArgs);
|
2009-03-26 02:32:15 +03:00
|
|
|
if (Init.isInvalid())
|
|
|
|
Var->setInvalidDecl();
|
2009-11-08 13:16:43 +03:00
|
|
|
else if (!D->getType()->isDependentType() &&
|
|
|
|
!D->getInit()->isTypeDependent() &&
|
2009-11-09 20:16:50 +03:00
|
|
|
!D->getInit()->isValueDependent()) {
|
2009-11-08 13:16:43 +03:00
|
|
|
// If neither the declaration's type nor its initializer are dependent,
|
|
|
|
// we don't want to redo all the checking, especially since the
|
|
|
|
// initializer might have been wrapped by a CXXConstructExpr since we did
|
|
|
|
// it the first time.
|
2009-11-24 19:52:50 +03:00
|
|
|
Var->setType(D->getType());
|
2009-11-08 13:16:43 +03:00
|
|
|
Var->setInit(SemaRef.Context, Init.takeAs<Expr>());
|
|
|
|
}
|
2009-08-27 01:14:46 +04:00
|
|
|
else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
|
2009-09-09 19:08:12 +04:00
|
|
|
// FIXME: We're faking all of the comma locations, which is suboptimal.
|
2009-08-27 01:14:46 +04:00
|
|
|
// Do we even need these comma locations?
|
|
|
|
llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
|
|
|
|
if (PLE->getNumExprs() > 0) {
|
|
|
|
FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
|
|
|
|
for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
|
|
|
|
Expr *E = PLE->getExpr(I)->Retain();
|
|
|
|
FakeCommaLocs.push_back(
|
|
|
|
SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
|
|
|
|
}
|
2009-08-27 03:26:04 +04:00
|
|
|
PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
|
2009-08-27 01:14:46 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-27 01:14:46 +04:00
|
|
|
// Add the direct initializer to the declaration.
|
|
|
|
SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
|
2009-09-09 19:08:12 +04:00
|
|
|
PLE->getLParenLoc(),
|
2009-08-27 01:14:46 +04:00
|
|
|
Sema::MultiExprArg(SemaRef,
|
|
|
|
(void**)PLE->getExprs(),
|
|
|
|
PLE->getNumExprs()),
|
|
|
|
FakeCommaLocs.data(),
|
|
|
|
PLE->getRParenLoc());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-27 01:14:46 +04:00
|
|
|
// When Init is destroyed, it will destroy the instantiated ParenListExpr;
|
|
|
|
// we've explicitly retained all of its subexpressions already.
|
|
|
|
} else
|
2009-03-28 22:18:32 +03:00
|
|
|
SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
|
2009-03-26 02:32:15 +03:00
|
|
|
D->hasCXXDirectInitializer());
|
2009-07-27 21:43:39 +04:00
|
|
|
} else if (!Var->isStaticDataMember() || Var->isOutOfLine())
|
|
|
|
SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
|
2009-03-26 02:32:15 +03:00
|
|
|
|
|
|
|
return Var;
|
|
|
|
}
|
|
|
|
|
2009-03-18 00:15:40 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
|
|
|
|
bool Invalid = false;
|
2009-10-23 03:33:21 +04:00
|
|
|
DeclaratorInfo *DI = D->getDeclaratorInfo();
|
|
|
|
if (DI->getType()->isDependentType()) {
|
|
|
|
DI = SemaRef.SubstType(DI, TemplateArgs,
|
|
|
|
D->getLocation(), D->getDeclName());
|
|
|
|
if (!DI) {
|
|
|
|
DI = D->getDeclaratorInfo();
|
|
|
|
Invalid = true;
|
|
|
|
} else if (DI->getType()->isFunctionType()) {
|
2009-03-18 00:15:40 +03:00
|
|
|
// 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)
|
2009-10-23 03:33:21 +04:00
|
|
|
<< DI->getType();
|
2009-03-18 00:15:40 +03:00
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr *BitWidth = D->getBitWidth();
|
|
|
|
if (Invalid)
|
|
|
|
BitWidth = 0;
|
|
|
|
else if (BitWidth) {
|
2009-06-23 00:57:11 +04:00
|
|
|
// The bit-width expression is not potentially evaluated.
|
|
|
|
EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-18 00:15:40 +03:00
|
|
|
OwningExprResult InstantiatedBitWidth
|
2009-08-26 02:02:44 +04:00
|
|
|
= SemaRef.SubstExpr(BitWidth, TemplateArgs);
|
2009-03-18 00:15:40 +03:00
|
|
|
if (InstantiatedBitWidth.isInvalid()) {
|
|
|
|
Invalid = true;
|
|
|
|
BitWidth = 0;
|
|
|
|
} else
|
2009-05-01 23:49:17 +04:00
|
|
|
BitWidth = InstantiatedBitWidth.takeAs<Expr>();
|
2009-03-18 00:15:40 +03:00
|
|
|
}
|
|
|
|
|
2009-10-23 03:33:21 +04:00
|
|
|
FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
|
|
|
|
DI->getType(), DI,
|
2009-09-09 19:08:12 +04:00
|
|
|
cast<RecordDecl>(Owner),
|
2009-03-18 00:15:40 +03:00
|
|
|
D->getLocation(),
|
|
|
|
D->isMutable(),
|
|
|
|
BitWidth,
|
2009-07-14 18:58:18 +04:00
|
|
|
D->getTypeSpecStartLoc(),
|
2009-03-18 00:15:40 +03:00
|
|
|
D->getAccess(),
|
|
|
|
0);
|
2009-10-15 00:14:33 +04:00
|
|
|
if (!Field) {
|
|
|
|
cast<Decl>(Owner)->setInvalidDecl();
|
2009-09-02 23:17:55 +04:00
|
|
|
return 0;
|
2009-10-15 00:14:33 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-07 09:07:58 +03:00
|
|
|
InstantiateAttrs(D, Field);
|
|
|
|
|
2009-09-02 23:17:55 +04:00
|
|
|
if (Invalid)
|
|
|
|
Field->setInvalidDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-02 23:17:55 +04:00
|
|
|
if (!Field->getDeclName()) {
|
|
|
|
// Keep track of where this decl came from.
|
|
|
|
SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
|
2009-03-18 00:15:40 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-02 23:17:55 +04:00
|
|
|
Field->setImplicit(D->isImplicit());
|
|
|
|
Owner->addDecl(Field);
|
2009-03-18 00:15:40 +03:00
|
|
|
|
|
|
|
return Field;
|
|
|
|
}
|
|
|
|
|
2009-08-28 11:59:38 +04:00
|
|
|
Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
|
|
|
|
FriendDecl::FriendUnion FU;
|
|
|
|
|
|
|
|
// Handle friend type expressions by simply substituting template
|
|
|
|
// parameters into the pattern type.
|
|
|
|
if (Type *Ty = D->getFriendType()) {
|
|
|
|
QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
|
|
|
|
D->getLocation(), DeclarationName());
|
|
|
|
if (T.isNull()) return 0;
|
2009-08-14 06:03:10 +04:00
|
|
|
|
2009-08-28 11:59:38 +04:00
|
|
|
assert(getLangOptions().CPlusPlus0x || T->isRecordType());
|
|
|
|
FU = T.getTypePtr();
|
2009-08-14 06:03:10 +04:00
|
|
|
|
2009-08-28 11:59:38 +04:00
|
|
|
// Handle everything else by appropriate substitution.
|
|
|
|
} else {
|
|
|
|
NamedDecl *ND = D->getFriendDecl();
|
|
|
|
assert(ND && "friend decl must be a decl or a type!");
|
|
|
|
|
2009-10-13 18:39:41 +04:00
|
|
|
// FIXME: We have a problem here, because the nested call to Visit(ND)
|
|
|
|
// will inject the thing that the friend references into the current
|
|
|
|
// owner, which is wrong.
|
2009-08-28 11:59:38 +04:00
|
|
|
Decl *NewND = Visit(ND);
|
|
|
|
if (!NewND) return 0;
|
2009-08-27 22:38:56 +04:00
|
|
|
|
2009-08-28 11:59:38 +04:00
|
|
|
FU = cast<NamedDecl>(NewND);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-28 11:59:38 +04:00
|
|
|
FriendDecl *FD =
|
|
|
|
FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
|
|
|
|
D->getFriendLoc());
|
2009-08-29 07:50:18 +04:00
|
|
|
FD->setAccess(AS_public);
|
2009-08-28 11:59:38 +04:00
|
|
|
Owner->addDecl(FD);
|
|
|
|
return FD;
|
2009-08-14 06:03:10 +04:00
|
|
|
}
|
|
|
|
|
2009-03-18 00:15:40 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
|
|
|
|
Expr *AssertExpr = D->getAssertExpr();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-23 00:57:11 +04:00
|
|
|
// The expression in a static assertion is not potentially evaluated.
|
|
|
|
EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-18 00:15:40 +03:00
|
|
|
OwningExprResult InstantiatedAssertExpr
|
2009-08-26 02:02:44 +04:00
|
|
|
= SemaRef.SubstExpr(AssertExpr, TemplateArgs);
|
2009-03-18 00:15:40 +03:00
|
|
|
if (InstantiatedAssertExpr.isInvalid())
|
|
|
|
return 0;
|
|
|
|
|
2009-08-08 05:41:12 +04:00
|
|
|
OwningExprResult Message(SemaRef, D->getMessage());
|
|
|
|
D->getMessage()->Retain();
|
2009-09-09 19:08:12 +04:00
|
|
|
Decl *StaticAssert
|
|
|
|
= SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
|
2009-03-28 22:18:32 +03:00
|
|
|
move(InstantiatedAssertExpr),
|
|
|
|
move(Message)).getAs<Decl>();
|
2009-03-18 00:15:40 +03:00
|
|
|
return StaticAssert;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
|
2009-09-09 19:08:12 +04:00
|
|
|
EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
|
2009-03-18 00:15:40 +03:00
|
|
|
D->getLocation(), D->getIdentifier(),
|
2009-07-21 18:46:17 +04:00
|
|
|
D->getTagKeywordLoc(),
|
2009-03-18 00:15:40 +03:00
|
|
|
/*PrevDecl=*/0);
|
2009-05-27 21:20:35 +04:00
|
|
|
Enum->setInstantiationOfMemberEnum(D);
|
2009-03-26 01:00:53 +03:00
|
|
|
Enum->setAccess(D->getAccess());
|
2009-06-30 06:36:12 +04:00
|
|
|
Owner->addDecl(Enum);
|
2009-03-18 00:15:40 +03:00
|
|
|
Enum->startDefinition();
|
|
|
|
|
2009-05-29 22:27:38 +04:00
|
|
|
llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
|
2009-03-18 00:15:40 +03:00
|
|
|
|
|
|
|
EnumConstantDecl *LastEnumConst = 0;
|
2009-06-30 06:36:12 +04:00
|
|
|
for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
|
|
|
|
ECEnd = D->enumerator_end();
|
2009-03-18 00:15:40 +03:00
|
|
|
EC != ECEnd; ++EC) {
|
|
|
|
// The specified value for the enumerator.
|
|
|
|
OwningExprResult Value = SemaRef.Owned((Expr *)0);
|
2009-06-23 00:57:11 +04:00
|
|
|
if (Expr *UninstValue = EC->getInitExpr()) {
|
|
|
|
// The enumerator's value expression is not potentially evaluated.
|
2009-09-09 19:08:12 +04:00
|
|
|
EnterExpressionEvaluationContext Unevaluated(SemaRef,
|
2009-06-23 00:57:11 +04:00
|
|
|
Action::Unevaluated);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-26 02:02:44 +04:00
|
|
|
Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
|
2009-06-23 00:57:11 +04:00
|
|
|
}
|
2009-03-18 00:15:40 +03:00
|
|
|
|
|
|
|
// Drop the initial value and continue.
|
|
|
|
bool isInvalid = false;
|
|
|
|
if (Value.isInvalid()) {
|
|
|
|
Value = SemaRef.Owned((Expr *)0);
|
|
|
|
isInvalid = true;
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
EnumConstantDecl *EnumConst
|
2009-03-18 00:15:40 +03:00
|
|
|
= SemaRef.CheckEnumConstant(Enum, LastEnumConst,
|
|
|
|
EC->getLocation(), EC->getIdentifier(),
|
|
|
|
move(Value));
|
|
|
|
|
|
|
|
if (isInvalid) {
|
|
|
|
if (EnumConst)
|
|
|
|
EnumConst->setInvalidDecl();
|
|
|
|
Enum->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EnumConst) {
|
2009-06-30 06:36:12 +04:00
|
|
|
Enum->addDecl(EnumConst);
|
2009-03-28 22:18:32 +03:00
|
|
|
Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
|
2009-03-18 00:15:40 +03:00
|
|
|
LastEnumConst = EnumConst;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-16 11:06:02 +04:00
|
|
|
// FIXME: Fixup LBraceLoc and RBraceLoc
|
2009-08-08 18:36:57 +04:00
|
|
|
// FIXME: Empty Scope and AttributeList (required to handle attribute packed).
|
2009-05-16 11:06:02 +04:00
|
|
|
SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
|
|
|
|
Sema::DeclPtrTy::make(Enum),
|
2009-08-08 18:36:57 +04:00
|
|
|
&Enumerators[0], Enumerators.size(),
|
|
|
|
0, 0);
|
2009-03-18 00:15:40 +03:00
|
|
|
|
|
|
|
return Enum;
|
|
|
|
}
|
|
|
|
|
2009-03-25 18:04:13 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
|
|
|
|
assert(false && "EnumConstantDecls can only occur within EnumDecls.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-29 03:04:11 +03:00
|
|
|
namespace {
|
|
|
|
class SortDeclByLocation {
|
|
|
|
SourceManager &SourceMgr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SortDeclByLocation(SourceManager &SourceMgr)
|
|
|
|
: SourceMgr(SourceMgr) { }
|
|
|
|
|
|
|
|
bool operator()(const Decl *X, const Decl *Y) const {
|
|
|
|
return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
|
|
|
|
Y->getLocation());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:44:21 +04:00
|
|
|
Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
|
2009-10-31 20:21:17 +03:00
|
|
|
// Create a local instantiation scope for this class template, which
|
|
|
|
// will contain the instantiations of the template parameters.
|
|
|
|
Sema::LocalInstantiationScope Scope(SemaRef);
|
2009-08-20 05:44:21 +04:00
|
|
|
TemplateParameterList *TempParams = D->getTemplateParameters();
|
2009-08-26 02:02:44 +04:00
|
|
|
TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
|
2009-09-09 19:08:12 +04:00
|
|
|
if (!InstParams)
|
2009-08-27 20:57:43 +04:00
|
|
|
return NULL;
|
2009-08-20 05:44:21 +04:00
|
|
|
|
|
|
|
CXXRecordDecl *Pattern = D->getTemplatedDecl();
|
|
|
|
CXXRecordDecl *RecordInst
|
|
|
|
= CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
|
|
|
|
Pattern->getLocation(), Pattern->getIdentifier(),
|
2009-10-13 03:11:44 +04:00
|
|
|
Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
|
|
|
|
/*DelayTypeCreation=*/true);
|
2009-08-20 05:44:21 +04:00
|
|
|
|
|
|
|
ClassTemplateDecl *Inst
|
|
|
|
= ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
|
|
|
D->getIdentifier(), InstParams, RecordInst, 0);
|
|
|
|
RecordInst->setDescribedClassTemplate(Inst);
|
2009-10-31 00:07:27 +03:00
|
|
|
if (D->getFriendObjectKind())
|
|
|
|
Inst->setObjectOfFriendDecl(true);
|
|
|
|
else
|
|
|
|
Inst->setAccess(D->getAccess());
|
2009-08-20 05:44:21 +04:00
|
|
|
Inst->setInstantiatedFromMemberTemplate(D);
|
2009-10-13 03:11:44 +04:00
|
|
|
|
|
|
|
// Trigger creation of the type for the instantiation.
|
|
|
|
SemaRef.Context.getTypeDeclType(RecordInst);
|
|
|
|
|
2009-10-31 01:42:42 +03:00
|
|
|
// Finish handling of friends.
|
|
|
|
if (Inst->getFriendObjectKind()) {
|
2009-10-31 00:07:27 +03:00
|
|
|
return Inst;
|
2009-10-31 01:42:42 +03:00
|
|
|
}
|
2009-10-31 00:07:27 +03:00
|
|
|
|
2009-08-20 05:44:21 +04:00
|
|
|
Owner->addDecl(Inst);
|
2009-10-29 03:04:11 +03:00
|
|
|
|
|
|
|
// First, we sort the partial specializations by location, so
|
|
|
|
// that we instantiate them in the order they were declared.
|
|
|
|
llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
|
|
|
|
for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
|
|
|
|
P = D->getPartialSpecializations().begin(),
|
|
|
|
PEnd = D->getPartialSpecializations().end();
|
|
|
|
P != PEnd; ++P)
|
|
|
|
PartialSpecs.push_back(&*P);
|
|
|
|
std::sort(PartialSpecs.begin(), PartialSpecs.end(),
|
|
|
|
SortDeclByLocation(SemaRef.SourceMgr));
|
|
|
|
|
|
|
|
// Instantiate all of the partial specializations of this member class
|
|
|
|
// template.
|
|
|
|
for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
|
|
|
|
InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
|
|
|
|
|
2009-08-20 05:44:21 +04:00
|
|
|
return Inst;
|
|
|
|
}
|
|
|
|
|
2009-10-07 21:21:34 +04:00
|
|
|
Decl *
|
|
|
|
TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
|
|
|
|
ClassTemplatePartialSpecializationDecl *D) {
|
2009-10-29 03:04:11 +03:00
|
|
|
ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
|
|
|
|
|
|
|
|
// Lookup the already-instantiated declaration in the instantiation
|
|
|
|
// of the class template and return that.
|
|
|
|
DeclContext::lookup_result Found
|
|
|
|
= Owner->lookup(ClassTemplate->getDeclName());
|
|
|
|
if (Found.first == Found.second)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ClassTemplateDecl *InstClassTemplate
|
|
|
|
= dyn_cast<ClassTemplateDecl>(*Found.first);
|
|
|
|
if (!InstClassTemplate)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Decl *DCanon = D->getCanonicalDecl();
|
|
|
|
for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
|
|
|
|
P = InstClassTemplate->getPartialSpecializations().begin(),
|
|
|
|
PEnd = InstClassTemplate->getPartialSpecializations().end();
|
|
|
|
P != PEnd; ++P) {
|
|
|
|
if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
|
|
|
|
return &*P;
|
|
|
|
}
|
|
|
|
|
2009-10-07 21:21:34 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-27 20:57:43 +04:00
|
|
|
Decl *
|
|
|
|
TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
|
2009-10-31 20:21:17 +03:00
|
|
|
// Create a local instantiation scope for this function template, which
|
|
|
|
// will contain the instantiations of the template parameters and then get
|
|
|
|
// merged with the local instantiation scope for the function template
|
|
|
|
// itself.
|
|
|
|
Sema::LocalInstantiationScope Scope(SemaRef);
|
|
|
|
|
2009-08-27 20:57:43 +04:00
|
|
|
TemplateParameterList *TempParams = D->getTemplateParameters();
|
|
|
|
TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
|
2009-09-09 19:08:12 +04:00
|
|
|
if (!InstParams)
|
2009-08-27 20:57:43 +04:00
|
|
|
return NULL;
|
2009-10-29 03:04:11 +03:00
|
|
|
|
2009-10-13 18:39:41 +04:00
|
|
|
FunctionDecl *Instantiated = 0;
|
|
|
|
if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
|
|
|
|
Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
|
|
|
|
InstParams));
|
|
|
|
else
|
|
|
|
Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
|
|
|
|
D->getTemplatedDecl(),
|
|
|
|
InstParams));
|
|
|
|
|
|
|
|
if (!Instantiated)
|
2009-08-27 20:57:43 +04:00
|
|
|
return 0;
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
// Link the instantiated function template declaration to the function
|
2009-08-27 20:57:43 +04:00
|
|
|
// template from which it was instantiated.
|
2009-10-13 02:27:17 +04:00
|
|
|
FunctionTemplateDecl *InstTemplate
|
2009-10-13 18:39:41 +04:00
|
|
|
= Instantiated->getDescribedFunctionTemplate();
|
2009-10-13 02:27:17 +04:00
|
|
|
InstTemplate->setAccess(D->getAccess());
|
2009-10-13 18:39:41 +04:00
|
|
|
assert(InstTemplate &&
|
|
|
|
"VisitFunctionDecl/CXXMethodDecl didn't create a template!");
|
|
|
|
if (!InstTemplate->getInstantiatedFromMemberTemplate())
|
|
|
|
InstTemplate->setInstantiatedFromMemberTemplate(D);
|
|
|
|
|
|
|
|
// Add non-friends into the owner.
|
|
|
|
if (!InstTemplate->getFriendObjectKind())
|
|
|
|
Owner->addDecl(InstTemplate);
|
2009-08-27 20:57:43 +04:00
|
|
|
return InstTemplate;
|
|
|
|
}
|
|
|
|
|
2009-03-26 00:17:03 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
|
|
|
|
CXXRecordDecl *PrevDecl = 0;
|
|
|
|
if (D->isInjectedClassName())
|
|
|
|
PrevDecl = cast<CXXRecordDecl>(Owner);
|
|
|
|
|
|
|
|
CXXRecordDecl *Record
|
2009-09-09 19:08:12 +04:00
|
|
|
= CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
|
2009-07-21 18:46:17 +04:00
|
|
|
D->getLocation(), D->getIdentifier(),
|
|
|
|
D->getTagKeywordLoc(), PrevDecl);
|
2009-03-26 00:17:03 +03:00
|
|
|
Record->setImplicit(D->isImplicit());
|
2009-08-27 23:11:42 +04:00
|
|
|
// FIXME: Check against AS_none is an ugly hack to work around the issue that
|
|
|
|
// the tag decls introduced by friend class declarations don't have an access
|
|
|
|
// specifier. Remove once this area of the code gets sorted out.
|
|
|
|
if (D->getAccess() != AS_none)
|
|
|
|
Record->setAccess(D->getAccess());
|
2009-03-26 00:17:03 +03:00
|
|
|
if (!D->isInjectedClassName())
|
2009-10-08 19:14:33 +04:00
|
|
|
Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
|
2009-03-26 00:17:03 +03:00
|
|
|
|
2009-08-28 11:59:38 +04:00
|
|
|
// If the original function was part of a friend declaration,
|
|
|
|
// inherit its namespace state.
|
|
|
|
if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
|
|
|
|
Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
|
|
|
|
|
2009-09-01 08:26:58 +04:00
|
|
|
Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
|
|
|
|
|
2009-06-30 06:36:12 +04:00
|
|
|
Owner->addDecl(Record);
|
2009-03-26 00:17:03 +03:00
|
|
|
return Record;
|
|
|
|
}
|
|
|
|
|
2009-08-28 11:59:38 +04:00
|
|
|
/// Normal class members are of more specific types and therefore
|
|
|
|
/// don't make it here. This function serves two purposes:
|
|
|
|
/// 1) instantiating function templates
|
|
|
|
/// 2) substituting friend declarations
|
|
|
|
/// FIXME: preserve function definitions in case #2
|
2009-10-13 18:39:41 +04:00
|
|
|
Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
|
|
|
|
TemplateParameterList *TemplateParams) {
|
2009-06-30 00:59:39 +04:00
|
|
|
// Check whether there is already a function template specialization for
|
|
|
|
// this declaration.
|
|
|
|
FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
|
|
|
|
void *InsertPos = 0;
|
2009-10-13 18:39:41 +04:00
|
|
|
if (FunctionTemplate && !TemplateParams) {
|
2009-06-30 00:59:39 +04:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2009-09-09 19:08:12 +04:00
|
|
|
FunctionTemplateSpecializationInfo::Profile(ID,
|
2009-08-29 00:31:08 +04:00
|
|
|
TemplateArgs.getInnermost().getFlatArgumentList(),
|
|
|
|
TemplateArgs.getInnermost().flat_size(),
|
2009-07-29 20:09:57 +04:00
|
|
|
SemaRef.Context);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
FunctionTemplateSpecializationInfo *Info
|
|
|
|
= FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
|
2009-06-30 00:59:39 +04:00
|
|
|
InsertPos);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-30 00:59:39 +04:00
|
|
|
// If we already have a function template specialization, return it.
|
|
|
|
if (Info)
|
|
|
|
return Info->Function;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-31 20:21:17 +03:00
|
|
|
Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-26 02:08:12 +04:00
|
|
|
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
2009-08-26 02:02:44 +04:00
|
|
|
QualType T = SubstFunctionType(D, Params);
|
2009-06-26 02:08:12 +04:00
|
|
|
if (T.isNull())
|
2009-03-24 02:06:20 +03:00
|
|
|
return 0;
|
2009-08-14 06:03:10 +04:00
|
|
|
|
2009-06-26 02:08:12 +04:00
|
|
|
// Build the instantiated method declaration.
|
2009-09-16 22:34:49 +04:00
|
|
|
DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
|
|
|
|
TemplateArgs);
|
2009-08-28 11:59:38 +04:00
|
|
|
FunctionDecl *Function =
|
2009-09-09 19:08:12 +04:00
|
|
|
FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
|
2009-08-19 05:27:57 +04:00
|
|
|
D->getDeclName(), T, D->getDeclaratorInfo(),
|
|
|
|
D->getStorageClass(),
|
2009-10-28 00:01:01 +03:00
|
|
|
D->isInlineSpecified(), D->hasWrittenPrototype());
|
2009-08-28 11:59:38 +04:00
|
|
|
Function->setLexicalDeclContext(Owner);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-26 02:08:12 +04:00
|
|
|
// Attach the parameters
|
|
|
|
for (unsigned P = 0; P < Params.size(); ++P)
|
|
|
|
Params[P]->setOwningFunction(Function);
|
|
|
|
Function->setParams(SemaRef.Context, Params.data(), Params.size());
|
2009-08-28 11:59:38 +04:00
|
|
|
|
2009-10-13 18:39:41 +04:00
|
|
|
if (TemplateParams) {
|
|
|
|
// Our resulting instantiation is actually a function template, since we
|
|
|
|
// are substituting only the outer template parameters. For example, given
|
|
|
|
//
|
|
|
|
// template<typename T>
|
|
|
|
// struct X {
|
|
|
|
// template<typename U> friend void f(T, U);
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// X<int> x;
|
|
|
|
//
|
|
|
|
// We are instantiating the friend function template "f" within X<int>,
|
|
|
|
// which means substituting int for T, but leaving "f" as a friend function
|
|
|
|
// template.
|
|
|
|
// Build the function template itself.
|
|
|
|
FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
|
|
|
|
Function->getLocation(),
|
|
|
|
Function->getDeclName(),
|
|
|
|
TemplateParams, Function);
|
|
|
|
Function->setDescribedFunctionTemplate(FunctionTemplate);
|
|
|
|
FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
|
2009-11-14 04:20:54 +03:00
|
|
|
} else if (FunctionTemplate) {
|
|
|
|
// Record this function template specialization.
|
|
|
|
Function->setFunctionTemplateSpecialization(SemaRef.Context,
|
|
|
|
FunctionTemplate,
|
|
|
|
&TemplateArgs.getInnermost(),
|
|
|
|
InsertPos);
|
2009-08-28 11:59:38 +04:00
|
|
|
}
|
2009-10-13 18:39:41 +04:00
|
|
|
|
2009-06-26 02:08:12 +04:00
|
|
|
if (InitFunctionInstantiation(Function, D))
|
|
|
|
Function->setInvalidDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-26 02:08:12 +04:00
|
|
|
bool Redeclaration = false;
|
|
|
|
bool OverloadableAttrRequired = false;
|
2009-10-13 18:39:41 +04:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
|
|
|
|
Sema::LookupOrdinaryName, Sema::ForRedeclaration);
|
|
|
|
|
2009-10-13 18:39:41 +04:00
|
|
|
if (TemplateParams || !FunctionTemplate) {
|
|
|
|
// Look only into the namespace where the friend would be declared to
|
|
|
|
// find a previous declaration. This is the innermost enclosing namespace,
|
|
|
|
// as described in ActOnFriendFunctionDecl.
|
2009-11-19 01:49:29 +03:00
|
|
|
SemaRef.LookupQualifiedName(Previous, DC);
|
2009-10-13 18:39:41 +04:00
|
|
|
|
|
|
|
// 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).
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.isSingleTagDecl())
|
|
|
|
Previous.clear();
|
2009-10-13 18:39:41 +04:00
|
|
|
}
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
SemaRef.CheckFunctionDeclaration(Function, Previous, false, Redeclaration,
|
2009-06-26 02:08:12 +04:00
|
|
|
/*FIXME:*/OverloadableAttrRequired);
|
|
|
|
|
2009-10-13 18:39:41 +04:00
|
|
|
// If the original function was part of a friend declaration,
|
|
|
|
// inherit its namespace state and add it to the owner.
|
|
|
|
NamedDecl *FromFriendD
|
|
|
|
= TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
|
|
|
|
if (FromFriendD->getFriendObjectKind()) {
|
|
|
|
NamedDecl *ToFriendD = 0;
|
2009-11-19 01:49:29 +03:00
|
|
|
NamedDecl *PrevDecl;
|
2009-10-13 18:39:41 +04:00
|
|
|
if (TemplateParams) {
|
|
|
|
ToFriendD = cast<NamedDecl>(FunctionTemplate);
|
|
|
|
PrevDecl = FunctionTemplate->getPreviousDeclaration();
|
|
|
|
} else {
|
|
|
|
ToFriendD = Function;
|
|
|
|
PrevDecl = Function->getPreviousDeclaration();
|
|
|
|
}
|
|
|
|
ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
|
|
|
|
if (!Owner->isDependentContext() && !PrevDecl)
|
|
|
|
DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
|
|
|
|
|
|
|
|
if (!TemplateParams)
|
|
|
|
Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
|
|
|
|
}
|
|
|
|
|
2009-06-26 02:08:12 +04:00
|
|
|
return Function;
|
|
|
|
}
|
2009-03-24 02:06:20 +03:00
|
|
|
|
2009-08-27 20:57:43 +04:00
|
|
|
Decl *
|
|
|
|
TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
|
|
|
|
TemplateParameterList *TemplateParams) {
|
2009-08-21 04:16:32 +04:00
|
|
|
FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
|
|
|
|
void *InsertPos = 0;
|
2009-08-27 20:57:43 +04:00
|
|
|
if (FunctionTemplate && !TemplateParams) {
|
2009-09-09 19:08:12 +04:00
|
|
|
// We are creating a function template specialization from a function
|
|
|
|
// template. Check whether there is already a function template
|
2009-08-27 20:57:43 +04:00
|
|
|
// specialization for this particular set of template arguments.
|
2009-08-21 04:16:32 +04:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2009-09-09 19:08:12 +04:00
|
|
|
FunctionTemplateSpecializationInfo::Profile(ID,
|
2009-08-29 00:31:08 +04:00
|
|
|
TemplateArgs.getInnermost().getFlatArgumentList(),
|
|
|
|
TemplateArgs.getInnermost().flat_size(),
|
2009-08-21 04:16:32 +04:00
|
|
|
SemaRef.Context);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
FunctionTemplateSpecializationInfo *Info
|
|
|
|
= FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
|
2009-08-21 04:16:32 +04:00
|
|
|
InsertPos);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-21 04:16:32 +04:00
|
|
|
// If we already have a function template specialization, return it.
|
|
|
|
if (Info)
|
|
|
|
return Info->Function;
|
|
|
|
}
|
|
|
|
|
2009-10-31 20:21:17 +03:00
|
|
|
Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
|
2009-05-15 01:44:34 +04:00
|
|
|
|
2009-05-29 22:27:38 +04:00
|
|
|
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
2009-08-26 02:02:44 +04:00
|
|
|
QualType T = SubstFunctionType(D, Params);
|
2009-03-24 02:06:20 +03:00
|
|
|
if (T.isNull())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Build the instantiated method declaration.
|
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
|
2009-08-21 22:42:58 +04:00
|
|
|
CXXMethodDecl *Method = 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-21 22:42:58 +04:00
|
|
|
DeclarationName Name = D->getDeclName();
|
2009-08-22 02:43:28 +04:00
|
|
|
if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
|
2009-08-21 22:42:58 +04:00
|
|
|
QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
|
|
|
|
Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
|
|
|
|
SemaRef.Context.getCanonicalType(ClassTy));
|
2009-09-09 19:08:12 +04:00
|
|
|
Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
|
|
|
|
Constructor->getLocation(),
|
|
|
|
Name, T,
|
2009-08-22 02:43:28 +04:00
|
|
|
Constructor->getDeclaratorInfo(),
|
2009-09-09 19:08:12 +04:00
|
|
|
Constructor->isExplicit(),
|
2009-10-28 00:01:01 +03:00
|
|
|
Constructor->isInlineSpecified(), false);
|
2009-08-22 02:43:28 +04:00
|
|
|
} else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
|
|
|
|
QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
|
|
|
|
Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
|
|
|
|
SemaRef.Context.getCanonicalType(ClassTy));
|
|
|
|
Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
|
|
|
|
Destructor->getLocation(), Name,
|
2009-10-28 00:01:01 +03:00
|
|
|
T, Destructor->isInlineSpecified(), false);
|
2009-08-22 03:19:43 +04:00
|
|
|
} else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
CanQualType ConvTy
|
2009-08-22 03:19:43 +04:00
|
|
|
= SemaRef.Context.getCanonicalType(
|
2009-09-22 03:43:11 +04:00
|
|
|
T->getAs<FunctionType>()->getResultType());
|
2009-08-22 03:19:43 +04:00
|
|
|
Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
|
|
|
|
ConvTy);
|
|
|
|
Method = CXXConversionDecl::Create(SemaRef.Context, Record,
|
|
|
|
Conversion->getLocation(), Name,
|
|
|
|
T, Conversion->getDeclaratorInfo(),
|
2009-10-28 00:01:01 +03:00
|
|
|
Conversion->isInlineSpecified(),
|
2009-08-22 03:19:43 +04:00
|
|
|
Conversion->isExplicit());
|
2009-08-21 22:42:58 +04:00
|
|
|
} else {
|
2009-09-09 19:08:12 +04:00
|
|
|
Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
|
2009-08-21 22:42:58 +04:00
|
|
|
D->getDeclName(), T, D->getDeclaratorInfo(),
|
2009-10-28 00:01:01 +03:00
|
|
|
D->isStatic(), D->isInlineSpecified());
|
2009-08-21 22:42:58 +04:00
|
|
|
}
|
2009-08-21 04:16:32 +04:00
|
|
|
|
2009-08-27 20:57:43 +04:00
|
|
|
if (TemplateParams) {
|
|
|
|
// Our resulting instantiation is actually a function template, since we
|
|
|
|
// are substituting only the outer template parameters. For example, given
|
2009-09-09 19:08:12 +04:00
|
|
|
//
|
2009-08-27 20:57:43 +04:00
|
|
|
// template<typename T>
|
|
|
|
// struct X {
|
|
|
|
// template<typename U> void f(T, U);
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// X<int> x;
|
|
|
|
//
|
|
|
|
// We are instantiating the member template "f" within X<int>, which means
|
|
|
|
// substituting int for T, but leaving "f" as a member function template.
|
|
|
|
// Build the function template itself.
|
|
|
|
FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
|
|
|
|
Method->getLocation(),
|
2009-09-09 19:08:12 +04:00
|
|
|
Method->getDeclName(),
|
2009-08-27 20:57:43 +04:00
|
|
|
TemplateParams, Method);
|
|
|
|
if (D->isOutOfLine())
|
2009-09-09 19:08:12 +04:00
|
|
|
FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
|
2009-08-27 20:57:43 +04:00
|
|
|
Method->setDescribedFunctionTemplate(FunctionTemplate);
|
2009-11-14 04:20:54 +03:00
|
|
|
} else if (FunctionTemplate) {
|
|
|
|
// Record this function template specialization.
|
|
|
|
Method->setFunctionTemplateSpecialization(SemaRef.Context,
|
|
|
|
FunctionTemplate,
|
|
|
|
&TemplateArgs.getInnermost(),
|
|
|
|
InsertPos);
|
|
|
|
} else {
|
|
|
|
// Record that this is an instantiation of a member function.
|
2009-10-08 03:56:10 +04:00
|
|
|
Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
|
2009-11-14 04:20:54 +03:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
// If we are instantiating a member function defined
|
2009-07-25 00:34:43 +04:00
|
|
|
// 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-09-09 19:08:12 +04:00
|
|
|
|
2009-03-24 03:38:23 +03:00
|
|
|
// Attach the parameters
|
|
|
|
for (unsigned P = 0; P < Params.size(); ++P)
|
|
|
|
Params[P]->setOwningFunction(Method);
|
2009-05-21 13:52:38 +04:00
|
|
|
Method->setParams(SemaRef.Context, Params.data(), Params.size());
|
2009-03-24 03:38:23 +03:00
|
|
|
|
|
|
|
if (InitMethodInstantiation(Method, D))
|
|
|
|
Method->setInvalidDecl();
|
2009-03-24 02:06:20 +03:00
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
LookupResult Previous(SemaRef, Name, SourceLocation(),
|
|
|
|
Sema::LookupOrdinaryName, Sema::ForRedeclaration);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-27 20:57:43 +04:00
|
|
|
if (!FunctionTemplate || TemplateParams) {
|
2009-11-19 01:49:29 +03:00
|
|
|
SemaRef.LookupQualifiedName(Previous, Owner);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-21 22:42:58 +04:00
|
|
|
// 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).
|
2009-11-19 01:49:29 +03:00
|
|
|
if (Previous.isSingleTagDecl())
|
|
|
|
Previous.clear();
|
2009-08-21 22:42:58 +04:00
|
|
|
}
|
2009-03-24 02:06:20 +03:00
|
|
|
|
2009-08-22 03:19:43 +04:00
|
|
|
bool Redeclaration = false;
|
|
|
|
bool OverloadableAttrRequired = false;
|
2009-11-19 01:49:29 +03:00
|
|
|
SemaRef.CheckFunctionDeclaration(Method, Previous, false, Redeclaration,
|
2009-08-22 03:19:43 +04:00
|
|
|
/*FIXME:*/OverloadableAttrRequired);
|
|
|
|
|
2009-12-01 20:24:26 +03:00
|
|
|
if (D->isPure())
|
|
|
|
SemaRef.CheckPureMethod(Method, SourceRange());
|
|
|
|
|
2009-11-19 01:49:29 +03:00
|
|
|
if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
|
2009-10-13 18:39:41 +04:00
|
|
|
!Method->getFriendObjectKind())
|
2009-08-21 22:42:58 +04:00
|
|
|
Owner->addDecl(Method);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-24 02:06:20 +03:00
|
|
|
return Method;
|
|
|
|
}
|
|
|
|
|
2009-03-24 19:43:20 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
|
2009-08-21 22:42:58 +04:00
|
|
|
return VisitCXXMethodDecl(D);
|
2009-03-24 19:43:20 +03:00
|
|
|
}
|
|
|
|
|
2009-03-24 03:15:49 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
|
2009-08-22 02:43:28 +04:00
|
|
|
return VisitCXXMethodDecl(D);
|
2009-03-24 03:15:49 +03:00
|
|
|
}
|
|
|
|
|
2009-03-25 03:34:44 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
|
2009-08-22 03:19:43 +04:00
|
|
|
return VisitCXXMethodDecl(D);
|
2009-03-25 03:34:44 +03:00
|
|
|
}
|
|
|
|
|
2009-03-25 18:04:13 +03:00
|
|
|
ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
|
2009-10-24 01:48:59 +04:00
|
|
|
QualType T;
|
|
|
|
DeclaratorInfo *DI = D->getDeclaratorInfo();
|
|
|
|
if (DI) {
|
|
|
|
DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
|
|
|
|
D->getDeclName());
|
|
|
|
if (DI) T = DI->getType();
|
|
|
|
} else {
|
|
|
|
T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
|
|
|
|
D->getDeclName());
|
|
|
|
DI = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (T.isNull())
|
2009-03-24 02:06:20 +03:00
|
|
|
return 0;
|
|
|
|
|
2009-10-24 01:48:59 +04:00
|
|
|
T = SemaRef.adjustParameterType(T);
|
2009-03-24 02:06:20 +03:00
|
|
|
|
|
|
|
// Allocate the parameter
|
2009-10-24 01:48:59 +04:00
|
|
|
ParmVarDecl *Param
|
|
|
|
= ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
|
|
|
D->getIdentifier(), T, DI, D->getStorageClass(), 0);
|
2009-03-24 02:06:20 +03:00
|
|
|
|
2009-08-25 07:18:48 +04:00
|
|
|
// Mark the default argument as being uninstantiated.
|
2009-09-25 10:56:31 +04:00
|
|
|
if (D->hasUninstantiatedDefaultArg())
|
|
|
|
Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
|
2009-09-25 11:03:22 +04:00
|
|
|
else if (Expr *Arg = D->getDefaultArg())
|
|
|
|
Param->setUninstantiatedDefaultArg(Arg);
|
|
|
|
|
2009-03-24 02:06:20 +03: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 01:44:34 +04:00
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
|
2009-03-24 02:06:20 +03:00
|
|
|
return Param;
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:44:21 +04: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>();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-20 05:44:21 +04:00
|
|
|
TemplateTypeParmDecl *Inst =
|
|
|
|
TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
2009-10-31 20:21:17 +03:00
|
|
|
TTPT->getDepth() - 1, TTPT->getIndex(),
|
2009-08-20 05:44:21 +04:00
|
|
|
TTPT->getName(),
|
|
|
|
D->wasDeclaredWithTypename(),
|
|
|
|
D->isParameterPack());
|
|
|
|
|
2009-11-09 22:17:50 +03:00
|
|
|
if (D->hasDefaultArgument())
|
|
|
|
Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
|
2009-08-20 05:44:21 +04:00
|
|
|
|
2009-10-31 20:21:17 +03:00
|
|
|
// Introduce this template parameter's instantiation into the instantiation
|
|
|
|
// scope.
|
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
|
|
|
|
|
2009-08-20 05:44:21 +04:00
|
|
|
return Inst;
|
|
|
|
}
|
|
|
|
|
2009-10-24 03:25:44 +04:00
|
|
|
Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
|
|
|
|
NonTypeTemplateParmDecl *D) {
|
|
|
|
// Substitute into the type of the non-type template parameter.
|
|
|
|
QualType T;
|
|
|
|
DeclaratorInfo *DI = D->getDeclaratorInfo();
|
|
|
|
if (DI) {
|
|
|
|
DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
|
|
|
|
D->getDeclName());
|
|
|
|
if (DI) T = DI->getType();
|
|
|
|
} else {
|
|
|
|
T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
|
|
|
|
D->getDeclName());
|
|
|
|
DI = 0;
|
|
|
|
}
|
|
|
|
if (T.isNull())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Check that this type is acceptable for a non-type template parameter.
|
|
|
|
bool Invalid = false;
|
|
|
|
T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
|
|
|
|
if (T.isNull()) {
|
|
|
|
T = SemaRef.Context.IntTy;
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NonTypeTemplateParmDecl *Param
|
|
|
|
= NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
|
|
|
D->getDepth() - 1, D->getPosition(),
|
|
|
|
D->getIdentifier(), T, DI);
|
|
|
|
if (Invalid)
|
|
|
|
Param->setInvalidDecl();
|
|
|
|
|
|
|
|
Param->setDefaultArgument(D->getDefaultArgument());
|
2009-10-31 20:21:17 +03:00
|
|
|
|
|
|
|
// Introduce this template parameter's instantiation into the instantiation
|
|
|
|
// scope.
|
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
|
2009-10-24 03:25:44 +04:00
|
|
|
return Param;
|
|
|
|
}
|
|
|
|
|
2009-11-11 19:58:32 +03:00
|
|
|
Decl *
|
|
|
|
TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
|
|
|
|
TemplateTemplateParmDecl *D) {
|
|
|
|
// Instantiate the template parameter list of the template template parameter.
|
|
|
|
TemplateParameterList *TempParams = D->getTemplateParameters();
|
|
|
|
TemplateParameterList *InstParams;
|
|
|
|
{
|
|
|
|
// Perform the actual substitution of template parameters within a new,
|
|
|
|
// local instantiation scope.
|
|
|
|
Sema::LocalInstantiationScope Scope(SemaRef);
|
|
|
|
InstParams = SubstTemplateParams(TempParams);
|
|
|
|
if (!InstParams)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the template template parameter.
|
|
|
|
TemplateTemplateParmDecl *Param
|
|
|
|
= TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
|
|
|
D->getDepth() - 1, D->getPosition(),
|
|
|
|
D->getIdentifier(), InstParams);
|
|
|
|
Param->setDefaultArgument(D->getDefaultArgument());
|
|
|
|
|
|
|
|
// Introduce this template parameter's instantiation into the instantiation
|
|
|
|
// scope.
|
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
|
|
|
|
|
|
|
|
return Param;
|
|
|
|
}
|
|
|
|
|
2009-11-17 09:07:40 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
|
|
|
|
// Using directives are never dependent, so they require no explicit
|
|
|
|
|
|
|
|
UsingDirectiveDecl *Inst
|
|
|
|
= UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
|
|
|
D->getNamespaceKeyLocation(),
|
|
|
|
D->getQualifierRange(), D->getQualifier(),
|
|
|
|
D->getIdentLocation(),
|
|
|
|
D->getNominatedNamespace(),
|
|
|
|
D->getCommonAncestor());
|
|
|
|
Owner->addDecl(Inst);
|
|
|
|
return Inst;
|
|
|
|
}
|
|
|
|
|
2009-11-18 05:36:19 +03:00
|
|
|
Decl * TemplateDeclInstantiator
|
|
|
|
::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
|
|
|
|
NestedNameSpecifier *NNS =
|
|
|
|
SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
|
|
|
|
D->getTargetNestedNameRange(),
|
|
|
|
TemplateArgs);
|
|
|
|
if (!NNS)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
CXXScopeSpec SS;
|
|
|
|
SS.setRange(D->getTargetNestedNameRange());
|
|
|
|
SS.setScopeRep(NNS);
|
|
|
|
|
|
|
|
NamedDecl *UD =
|
|
|
|
SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
|
|
|
|
D->getUsingLoc(), SS, D->getLocation(),
|
|
|
|
D->getDeclName(), 0,
|
|
|
|
/*instantiation*/ true,
|
|
|
|
/*typename*/ true, D->getTypenameLoc());
|
|
|
|
if (UD)
|
|
|
|
SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
|
|
|
|
D);
|
|
|
|
return UD;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decl * TemplateDeclInstantiator
|
|
|
|
::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
|
2009-09-09 19:08:12 +04:00
|
|
|
NestedNameSpecifier *NNS =
|
|
|
|
SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
|
|
|
|
D->getTargetNestedNameRange(),
|
2009-08-28 19:18:15 +04:00
|
|
|
TemplateArgs);
|
|
|
|
if (!NNS)
|
|
|
|
return 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-28 19:18:15 +04:00
|
|
|
CXXScopeSpec SS;
|
|
|
|
SS.setRange(D->getTargetNestedNameRange());
|
|
|
|
SS.setScopeRep(NNS);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
NamedDecl *UD =
|
2009-11-17 08:59:44 +03:00
|
|
|
SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
|
2009-11-18 05:36:19 +03:00
|
|
|
D->getUsingLoc(), SS, D->getLocation(),
|
|
|
|
D->getDeclName(), 0,
|
|
|
|
/*instantiation*/ true,
|
|
|
|
/*typename*/ false, SourceLocation());
|
2009-08-29 23:37:28 +04:00
|
|
|
if (UD)
|
2009-09-09 19:08:12 +04:00
|
|
|
SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
|
2009-08-29 23:37:28 +04:00
|
|
|
D);
|
|
|
|
return UD;
|
2009-08-28 19:18:15 +04:00
|
|
|
}
|
|
|
|
|
2009-08-26 02:02:44 +04:00
|
|
|
Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
|
2009-08-29 00:31:08 +04:00
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs) {
|
2009-05-12 03:53:27 +04:00
|
|
|
TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
|
2009-03-18 00:15:40 +03:00
|
|
|
return Instantiator.Visit(D);
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:44:21 +04: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 *
|
2009-08-26 02:02:44 +04:00
|
|
|
TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
|
2009-08-20 05:44:21 +04:00
|
|
|
// Get errors for all the parameters before bailing out.
|
|
|
|
bool Invalid = false;
|
|
|
|
|
|
|
|
unsigned N = L->size();
|
2009-09-15 20:23:51 +04:00
|
|
|
typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
|
2009-08-20 05:44:21 +04:00
|
|
|
ParamVector Params;
|
|
|
|
Params.reserve(N);
|
|
|
|
for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
|
|
|
|
PI != PE; ++PI) {
|
2009-09-15 20:23:51 +04:00
|
|
|
NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
|
2009-08-20 05:44:21 +04:00
|
|
|
Params.push_back(D);
|
2009-11-11 22:13:48 +03:00
|
|
|
Invalid = Invalid || !D || D->isInvalidDecl();
|
2009-08-20 05:44:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-09 19:08:12 +04:00
|
|
|
}
|
2009-08-20 05:44:21 +04:00
|
|
|
|
2009-10-29 03:04:11 +03:00
|
|
|
/// \brief Instantiate the declaration of a class template partial
|
|
|
|
/// specialization.
|
|
|
|
///
|
|
|
|
/// \param ClassTemplate the (instantiated) class template that is partially
|
|
|
|
// specialized by the instantiation of \p PartialSpec.
|
|
|
|
///
|
|
|
|
/// \param PartialSpec the (uninstantiated) class template partial
|
|
|
|
/// specialization that we are instantiating.
|
|
|
|
///
|
|
|
|
/// \returns true if there was an error, false otherwise.
|
|
|
|
bool
|
|
|
|
TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
|
|
|
|
ClassTemplateDecl *ClassTemplate,
|
|
|
|
ClassTemplatePartialSpecializationDecl *PartialSpec) {
|
2009-10-31 20:21:17 +03:00
|
|
|
// Create a local instantiation scope for this class template partial
|
|
|
|
// specialization, which will contain the instantiations of the template
|
|
|
|
// parameters.
|
|
|
|
Sema::LocalInstantiationScope Scope(SemaRef);
|
|
|
|
|
2009-10-29 03:04:11 +03:00
|
|
|
// Substitute into the template parameters of the class template partial
|
|
|
|
// specialization.
|
|
|
|
TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
|
|
|
|
TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
|
|
|
|
if (!InstParams)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Substitute into the template arguments of the class template partial
|
|
|
|
// specialization.
|
2009-10-29 11:12:44 +03:00
|
|
|
const TemplateArgumentLoc *PartialSpecTemplateArgs
|
|
|
|
= PartialSpec->getTemplateArgsAsWritten();
|
|
|
|
unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
|
|
|
|
|
2009-11-23 04:53:49 +03:00
|
|
|
TemplateArgumentListInfo InstTemplateArgs; // no angle locations
|
2009-10-29 11:12:44 +03:00
|
|
|
for (unsigned I = 0; I != N; ++I) {
|
2009-11-23 04:53:49 +03:00
|
|
|
TemplateArgumentLoc Loc;
|
|
|
|
if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
|
2009-10-29 03:04:11 +03:00
|
|
|
return true;
|
2009-11-23 04:53:49 +03:00
|
|
|
InstTemplateArgs.addArgument(Loc);
|
2009-10-29 03:04:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check that the template argument list is well-formed for this
|
|
|
|
// class template.
|
|
|
|
TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
|
|
|
|
InstTemplateArgs.size());
|
|
|
|
if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
|
|
|
|
PartialSpec->getLocation(),
|
2009-11-23 04:53:49 +03:00
|
|
|
InstTemplateArgs,
|
2009-10-29 03:04:11 +03:00
|
|
|
false,
|
|
|
|
Converted))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Figure out where to insert this class template partial specialization
|
|
|
|
// in the member template's set of class template partial specializations.
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
ClassTemplatePartialSpecializationDecl::Profile(ID,
|
|
|
|
Converted.getFlatArguments(),
|
|
|
|
Converted.flatSize(),
|
|
|
|
SemaRef.Context);
|
|
|
|
void *InsertPos = 0;
|
|
|
|
ClassTemplateSpecializationDecl *PrevDecl
|
|
|
|
= ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos);
|
|
|
|
|
|
|
|
// Build the canonical type that describes the converted template
|
|
|
|
// arguments of the class template partial specialization.
|
|
|
|
QualType CanonType
|
|
|
|
= SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
|
|
|
|
Converted.getFlatArguments(),
|
|
|
|
Converted.flatSize());
|
|
|
|
|
|
|
|
// Build the fully-sugared type for this class template
|
|
|
|
// specialization as the user wrote in the specialization
|
|
|
|
// itself. This means that we'll pretty-print the type retrieved
|
|
|
|
// from the specialization's declaration the way that the user
|
|
|
|
// actually wrote the specialization, rather than formatting the
|
|
|
|
// name based on the "canonical" representation used to store the
|
|
|
|
// template arguments in the specialization.
|
|
|
|
QualType WrittenTy
|
|
|
|
= SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
|
2009-11-23 04:53:49 +03:00
|
|
|
InstTemplateArgs,
|
2009-10-29 03:04:11 +03:00
|
|
|
CanonType);
|
|
|
|
|
|
|
|
if (PrevDecl) {
|
|
|
|
// We've already seen a partial specialization with the same template
|
|
|
|
// parameters and template arguments. This can happen, for example, when
|
|
|
|
// substituting the outer template arguments ends up causing two
|
|
|
|
// class template partial specializations of a member class template
|
|
|
|
// to have identical forms, e.g.,
|
|
|
|
//
|
|
|
|
// template<typename T, typename U>
|
|
|
|
// struct Outer {
|
|
|
|
// template<typename X, typename Y> struct Inner;
|
|
|
|
// template<typename Y> struct Inner<T, Y>;
|
|
|
|
// template<typename Y> struct Inner<U, Y>;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// Outer<int, int> outer; // error: the partial specializations of Inner
|
|
|
|
// // have the same signature.
|
|
|
|
SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
|
|
|
|
<< WrittenTy;
|
|
|
|
SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
|
|
|
|
<< SemaRef.Context.getTypeDeclType(PrevDecl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create the class template partial specialization declaration.
|
|
|
|
ClassTemplatePartialSpecializationDecl *InstPartialSpec
|
|
|
|
= ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
|
|
|
|
PartialSpec->getLocation(),
|
|
|
|
InstParams,
|
|
|
|
ClassTemplate,
|
|
|
|
Converted,
|
2009-11-23 04:53:49 +03:00
|
|
|
InstTemplateArgs,
|
2009-10-29 03:04:11 +03:00
|
|
|
0);
|
|
|
|
InstPartialSpec->setInstantiatedFromMember(PartialSpec);
|
|
|
|
InstPartialSpec->setTypeAsWritten(WrittenTy);
|
|
|
|
|
|
|
|
// Add this partial specialization to the set of class template partial
|
|
|
|
// specializations.
|
|
|
|
ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
|
|
|
|
InsertPos);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-26 02:02:44 +04:00
|
|
|
/// \brief Does substitution on the type of the given function, including
|
|
|
|
/// all of the function parameters.
|
2009-03-24 03:38:23 +03:00
|
|
|
///
|
2009-08-26 02:02:44 +04:00
|
|
|
/// \param D The function whose type will be the basis of the substitution
|
2009-03-24 03:38:23 +03:00
|
|
|
///
|
|
|
|
/// \param Params the instantiated parameter declarations
|
|
|
|
|
2009-08-26 02:02:44 +04:00
|
|
|
/// \returns the instantiated function's type if successful, a NULL
|
2009-03-24 03:38:23 +03:00
|
|
|
/// type if there was an error.
|
2009-09-09 19:08:12 +04:00
|
|
|
QualType
|
2009-08-26 02:02:44 +04:00
|
|
|
TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
|
2009-03-24 03:38:23 +03:00
|
|
|
llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
|
|
|
|
bool InvalidDecl = false;
|
|
|
|
|
2009-08-26 02:02:44 +04:00
|
|
|
// Substitute all of the function's formal parameter types.
|
2009-05-12 03:53:27 +04:00
|
|
|
TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
|
2009-05-29 22:27:38 +04:00
|
|
|
llvm::SmallVector<QualType, 4> ParamTys;
|
2009-09-09 19:08:12 +04:00
|
|
|
for (FunctionDecl::param_iterator P = D->param_begin(),
|
2009-03-24 03:38:23 +03:00
|
|
|
PEnd = D->param_end();
|
|
|
|
P != PEnd; ++P) {
|
2009-03-25 18:04:13 +03:00
|
|
|
if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
|
2009-03-24 03:38:23 +03:00
|
|
|
if (PInst->getType()->isVoidType()) {
|
|
|
|
SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
|
|
|
|
PInst->setInvalidDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
} else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
|
2009-08-05 01:02:39 +04:00
|
|
|
PInst->getType(),
|
|
|
|
diag::err_abstract_type_in_decl,
|
|
|
|
Sema::AbstractParamType))
|
2009-03-24 03:38:23 +03:00
|
|
|
PInst->setInvalidDecl();
|
|
|
|
|
|
|
|
Params.push_back(PInst);
|
|
|
|
ParamTys.push_back(PInst->getType());
|
|
|
|
|
|
|
|
if (PInst->isInvalidDecl())
|
|
|
|
InvalidDecl = true;
|
2009-09-09 19:08:12 +04:00
|
|
|
} else
|
2009-03-24 03:38:23 +03:00
|
|
|
InvalidDecl = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Deallocate dead declarations.
|
|
|
|
if (InvalidDecl)
|
|
|
|
return QualType();
|
|
|
|
|
2009-09-22 03:43:11 +04:00
|
|
|
const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
|
2009-03-24 03:38:23 +03:00
|
|
|
assert(Proto && "Missing prototype?");
|
2009-09-09 19:08:12 +04:00
|
|
|
QualType ResultType
|
2009-08-26 02:02:44 +04:00
|
|
|
= SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
|
|
|
|
D->getLocation(), D->getDeclName());
|
2009-03-24 03:38:23 +03:00
|
|
|
if (ResultType.isNull())
|
|
|
|
return QualType();
|
|
|
|
|
2009-05-21 13:52:38 +04:00
|
|
|
return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
|
2009-03-24 03:38:23 +03:00
|
|
|
Proto->isVariadic(), Proto->getTypeQuals(),
|
|
|
|
D->getLocation(), D->getDeclName());
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
/// \brief Initializes the common fields of an instantiation function
|
2009-06-26 02:08:12 +04:00
|
|
|
/// declaration (New) from the corresponding fields of its template (Tmpl).
|
|
|
|
///
|
|
|
|
/// \returns true if there was an error
|
2009-09-09 19:08:12 +04:00
|
|
|
bool
|
|
|
|
TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
|
2009-06-26 02:08:12 +04:00
|
|
|
FunctionDecl *Tmpl) {
|
|
|
|
if (Tmpl->isDeleted())
|
|
|
|
New->setDeleted();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-02 02:01:06 +04: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
|
2009-09-09 19:08:12 +04:00
|
|
|
// to keeping the new function template specialization. We therefore
|
|
|
|
// convert the active template instantiation for the function template
|
2009-07-02 02:01:06 +04:00
|
|
|
// 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) {
|
2009-09-09 19:08:12 +04:00
|
|
|
if (FunctionTemplateDecl *FunTmpl
|
2009-07-02 02:01:06 +04:00
|
|
|
= dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
assert(FunTmpl->getTemplatedDecl() == Tmpl &&
|
2009-07-02 02:01:06 +04:00
|
|
|
"Deduction from the wrong function template?");
|
2009-07-17 02:10:11 +04:00
|
|
|
(void) FunTmpl;
|
2009-07-02 02:01:06 +04:00
|
|
|
ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
|
|
|
|
ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
|
2009-11-12 00:54:23 +03:00
|
|
|
--SemaRef.NonInstantiationEntries;
|
2009-07-02 02:01:06 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-26 02:08:12 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-24 03:38:23 +03: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
|
2009-09-09 19:08:12 +04:00
|
|
|
bool
|
|
|
|
TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
|
2009-03-24 03:38:23 +03:00
|
|
|
CXXMethodDecl *Tmpl) {
|
2009-06-26 02:08:12 +04:00
|
|
|
if (InitFunctionInstantiation(New, Tmpl))
|
|
|
|
return true;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-24 03:38:23 +03:00
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
|
|
|
|
New->setAccess(Tmpl->getAccess());
|
2009-05-15 02:15:41 +04:00
|
|
|
if (Tmpl->isVirtualAsWritten()) {
|
|
|
|
New->setVirtualAsWritten(true);
|
2009-03-24 03:38:23 +03:00
|
|
|
Record->setAggregate(false);
|
|
|
|
Record->setPOD(false);
|
2009-08-16 01:55:26 +04:00
|
|
|
Record->setEmpty(false);
|
2009-03-24 03:38:23 +03:00
|
|
|
Record->setPolymorphic(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: attributes
|
|
|
|
// FIXME: New needs a pointer to Tmpl
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-14 00:28:22 +04:00
|
|
|
|
|
|
|
/// \brief Instantiate the definition of the given function from its
|
|
|
|
/// template.
|
|
|
|
///
|
2009-06-30 21:20:14 +04: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 00:28:22 +04:00
|
|
|
/// \param Function the already-instantiated declaration of a
|
2009-06-30 21:20:14 +04: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-10-15 18:05:49 +04:00
|
|
|
///
|
|
|
|
/// \param DefinitionRequired if true, then we are performing an explicit
|
|
|
|
/// instantiation where the body of the function is required. Complain if
|
|
|
|
/// there is no such body.
|
2009-05-18 21:01:57 +04:00
|
|
|
void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
|
2009-06-30 21:20:14 +04:00
|
|
|
FunctionDecl *Function,
|
2009-10-15 18:05:49 +04:00
|
|
|
bool Recursive,
|
|
|
|
bool DefinitionRequired) {
|
2009-05-15 03:26:13 +04:00
|
|
|
if (Function->isInvalidDecl())
|
|
|
|
return;
|
|
|
|
|
2009-06-30 06:35:26 +04:00
|
|
|
assert(!Function->getBody() && "Already instantiated!");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-08 11:24:58 +04:00
|
|
|
// Never instantiate an explicit specialization.
|
|
|
|
if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
|
|
|
|
return;
|
|
|
|
|
2009-05-15 01:06:31 +04:00
|
|
|
// Find the function body that we'll be substituting.
|
2009-10-27 23:53:28 +03:00
|
|
|
const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
|
2009-05-15 01:06:31 +04:00
|
|
|
Stmt *Pattern = 0;
|
|
|
|
if (PatternDecl)
|
2009-06-30 06:35:26 +04:00
|
|
|
Pattern = PatternDecl->getBody(PatternDecl);
|
2009-05-15 01:06:31 +04:00
|
|
|
|
2009-10-15 18:05:49 +04:00
|
|
|
if (!Pattern) {
|
|
|
|
if (DefinitionRequired) {
|
|
|
|
if (Function->getPrimaryTemplate())
|
|
|
|
Diag(PointOfInstantiation,
|
|
|
|
diag::err_explicit_instantiation_undefined_func_template)
|
|
|
|
<< Function->getPrimaryTemplate();
|
|
|
|
else
|
|
|
|
Diag(PointOfInstantiation,
|
|
|
|
diag::err_explicit_instantiation_undefined_member)
|
|
|
|
<< 1 << Function->getDeclName() << Function->getDeclContext();
|
|
|
|
|
|
|
|
if (PatternDecl)
|
|
|
|
Diag(PatternDecl->getLocation(),
|
|
|
|
diag::note_explicit_instantiation_here);
|
|
|
|
}
|
|
|
|
|
2009-05-15 01:06:31 +04:00
|
|
|
return;
|
2009-10-15 18:05:49 +04:00
|
|
|
}
|
2009-05-15 01:06:31 +04:00
|
|
|
|
2009-09-05 02:48:11 +04:00
|
|
|
// C++0x [temp.explicit]p9:
|
|
|
|
// Except for inline functions, other explicit instantiation declarations
|
2009-09-09 19:08:12 +04:00
|
|
|
// have the effect of suppressing the implicit instantiation of the entity
|
2009-09-05 02:48:11 +04:00
|
|
|
// to which they refer.
|
2009-09-09 19:08:12 +04:00
|
|
|
if (Function->getTemplateSpecializationKind()
|
2009-09-05 02:48:11 +04:00
|
|
|
== TSK_ExplicitInstantiationDeclaration &&
|
2009-10-28 00:11:48 +03:00
|
|
|
!PatternDecl->isInlined())
|
2009-09-05 02:48:11 +04:00
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-18 21:01:57 +04:00
|
|
|
InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
|
|
|
|
if (Inst)
|
|
|
|
return;
|
2009-05-15 04:01:03 +04:00
|
|
|
|
2009-06-30 21:20:14 +04: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-09-09 19:08:12 +04:00
|
|
|
|
2009-05-15 21:59:04 +04:00
|
|
|
ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
|
|
|
|
|
2009-05-15 03:26:13 +04:00
|
|
|
// Introduce a new scope where local variable instantiations will be
|
|
|
|
// recorded.
|
|
|
|
LocalInstantiationScope Scope(*this);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-15 03:26:13 +04:00
|
|
|
// 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 04:01:03 +04: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-09-09 19:08:12 +04:00
|
|
|
MultiLevelTemplateArgumentList TemplateArgs =
|
2009-08-29 09:16:22 +04:00
|
|
|
getTemplateInstantiationArgs(Function);
|
|
|
|
|
|
|
|
// If this is a constructor, instantiate the member initializers.
|
2009-09-09 19:08:12 +04:00
|
|
|
if (const CXXConstructorDecl *Ctor =
|
2009-08-29 09:16:22 +04:00
|
|
|
dyn_cast<CXXConstructorDecl>(PatternDecl)) {
|
|
|
|
InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
|
|
|
|
TemplateArgs);
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
|
|
|
|
2009-05-15 03:26:13 +04:00
|
|
|
// Instantiate the function body.
|
2009-08-29 09:16:22 +04:00
|
|
|
OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
|
2009-05-15 21:59:04 +04:00
|
|
|
|
2009-09-12 01:19:12 +04:00
|
|
|
if (Body.isInvalid())
|
|
|
|
Function->setInvalidDecl();
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
|
2009-05-15 21:59:04 +04:00
|
|
|
/*IsInstantiation=*/true);
|
2009-05-15 04:01:03 +04:00
|
|
|
|
|
|
|
CurContext = PreviousContext;
|
2009-05-27 00:50:29 +04:00
|
|
|
|
|
|
|
DeclGroupRef DG(Function);
|
|
|
|
Consumer.HandleTopLevelDecl(DG);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-30 21:20:14 +04:00
|
|
|
if (Recursive) {
|
|
|
|
// Instantiate any pending implicit instantiations found during the
|
2009-09-09 19:08:12 +04:00
|
|
|
// instantiation of this template.
|
2009-06-30 21:20:14 +04:00
|
|
|
PerformPendingImplicitInstantiations();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-30 21:20:14 +04:00
|
|
|
// Restore the set of pending implicit instantiations.
|
|
|
|
PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
|
|
|
|
}
|
2009-05-14 00:28:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Instantiate the definition of the given variable from its
|
|
|
|
/// template.
|
|
|
|
///
|
2009-07-25 00:34:43 +04: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.
|
2009-10-15 18:05:49 +04:00
|
|
|
///
|
|
|
|
/// \param DefinitionRequired if true, then we are performing an explicit
|
|
|
|
/// instantiation where an out-of-line definition of the member variable
|
|
|
|
/// is required. Complain if there is no such definition.
|
2009-07-25 00:34:43 +04:00
|
|
|
void Sema::InstantiateStaticDataMemberDefinition(
|
|
|
|
SourceLocation PointOfInstantiation,
|
|
|
|
VarDecl *Var,
|
2009-10-15 18:05:49 +04:00
|
|
|
bool Recursive,
|
|
|
|
bool DefinitionRequired) {
|
2009-07-25 00:34:43 +04:00
|
|
|
if (Var->isInvalidDecl())
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
// Find the out-of-line definition of this static data member.
|
|
|
|
VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
|
|
|
|
assert(Def && "This data member was not instantiated from a template?");
|
2009-10-27 21:42:08 +03:00
|
|
|
assert(Def->isStaticDataMember() && "Not a static data member?");
|
|
|
|
Def = Def->getOutOfLineDefinition();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-27 21:42:08 +03:00
|
|
|
if (!Def) {
|
2009-07-25 00:34:43 +04:00
|
|
|
// 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
|
2009-09-09 19:08:12 +04:00
|
|
|
// instantiate this definition (or provide a specialization for it) in
|
|
|
|
// another translation unit.
|
2009-10-15 18:05:49 +04:00
|
|
|
if (DefinitionRequired) {
|
2009-10-27 21:42:08 +03:00
|
|
|
Def = Var->getInstantiatedFromStaticDataMember();
|
2009-10-15 18:05:49 +04:00
|
|
|
Diag(PointOfInstantiation,
|
|
|
|
diag::err_explicit_instantiation_undefined_member)
|
|
|
|
<< 2 << Var->getDeclName() << Var->getDeclContext();
|
|
|
|
Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
|
|
|
|
}
|
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-08 11:24:58 +04:00
|
|
|
// Never instantiate an explicit specialization.
|
2009-10-15 01:29:40 +04:00
|
|
|
if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
|
2009-10-08 11:24:58 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
// C++0x [temp.explicit]p9:
|
|
|
|
// Except for inline functions, other explicit instantiation declarations
|
|
|
|
// have the effect of suppressing the implicit instantiation of the entity
|
|
|
|
// to which they refer.
|
2009-10-15 01:29:40 +04:00
|
|
|
if (Var->getTemplateSpecializationKind()
|
2009-10-08 11:24:58 +04:00
|
|
|
== TSK_ExplicitInstantiationDeclaration)
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
|
|
|
|
if (Inst)
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04: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-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
// Enter the scope of this instantiation. We don't use
|
|
|
|
// PushDeclContext because we don't have a scope.
|
|
|
|
DeclContext *PreviousContext = CurContext;
|
|
|
|
CurContext = Var->getDeclContext();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-15 01:29:40 +04:00
|
|
|
VarDecl *OldVar = Var;
|
2009-08-26 02:02:44 +04:00
|
|
|
Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
|
2009-07-25 00:34:43 +04:00
|
|
|
getTemplateInstantiationArgs(Var)));
|
|
|
|
CurContext = PreviousContext;
|
|
|
|
|
|
|
|
if (Var) {
|
2009-10-15 01:29:40 +04:00
|
|
|
Var->setPreviousDeclaration(OldVar);
|
2009-10-15 22:07:02 +04:00
|
|
|
MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
|
|
|
|
assert(MSInfo && "Missing member specialization information?");
|
|
|
|
Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
|
|
|
|
MSInfo->getPointOfInstantiation());
|
2009-07-25 00:34:43 +04:00
|
|
|
DeclGroupRef DG(Var);
|
|
|
|
Consumer.HandleTopLevelDecl(DG);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
if (Recursive) {
|
|
|
|
// Instantiate any pending implicit instantiations found during the
|
2009-09-09 19:08:12 +04:00
|
|
|
// instantiation of this template.
|
2009-07-25 00:34:43 +04:00
|
|
|
PerformPendingImplicitInstantiations();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
// Restore the set of pending implicit instantiations.
|
|
|
|
PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2009-05-14 00:28:22 +04:00
|
|
|
}
|
2009-05-27 09:35:12 +04:00
|
|
|
|
2009-08-29 09:16:22 +04:00
|
|
|
void
|
|
|
|
Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
|
|
|
|
const CXXConstructorDecl *Tmpl,
|
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs) {
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-29 09:16:22 +04:00
|
|
|
llvm::SmallVector<MemInitTy*, 4> NewInits;
|
|
|
|
|
|
|
|
// Instantiate all the initializers.
|
|
|
|
for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
|
2009-09-02 01:04:42 +04:00
|
|
|
InitsEnd = Tmpl->init_end();
|
|
|
|
Inits != InitsEnd; ++Inits) {
|
2009-08-29 09:16:22 +04:00
|
|
|
CXXBaseOrMemberInitializer *Init = *Inits;
|
|
|
|
|
|
|
|
ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-29 09:16:22 +04:00
|
|
|
// Instantiate all the arguments.
|
|
|
|
for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
|
|
|
|
Args != ArgsEnd; ++Args) {
|
|
|
|
OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
|
|
|
|
|
|
|
|
if (NewArg.isInvalid())
|
|
|
|
New->setInvalidDecl();
|
|
|
|
else
|
|
|
|
NewArgs.push_back(NewArg.takeAs<Expr>());
|
|
|
|
}
|
|
|
|
|
|
|
|
MemInitResult NewInit;
|
|
|
|
|
|
|
|
if (Init->isBaseInitializer()) {
|
2009-12-03 01:36:29 +03:00
|
|
|
DeclaratorInfo *BaseDInfo = SubstType(Init->getBaseClassInfo(),
|
|
|
|
TemplateArgs,
|
|
|
|
Init->getSourceLocation(),
|
|
|
|
New->getDeclName());
|
|
|
|
if (!BaseDInfo) {
|
|
|
|
New->setInvalidDecl();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
NewInit = BuildBaseInitializer(BaseDInfo->getType(), BaseDInfo,
|
2009-09-09 19:08:12 +04:00
|
|
|
(Expr **)NewArgs.data(),
|
2009-08-29 09:16:22 +04:00
|
|
|
NewArgs.size(),
|
2009-12-03 01:36:29 +03:00
|
|
|
Init->getLParenLoc(),
|
2009-08-29 09:16:22 +04:00
|
|
|
Init->getRParenLoc(),
|
|
|
|
New->getParent());
|
|
|
|
} else if (Init->isMemberInitializer()) {
|
2009-09-01 08:31:02 +04:00
|
|
|
FieldDecl *Member;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-01 08:31:02 +04:00
|
|
|
// Is this an anonymous union?
|
|
|
|
if (FieldDecl *UnionInit = Init->getAnonUnionMember())
|
2009-09-16 22:34:49 +04:00
|
|
|
Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
|
2009-09-01 08:31:02 +04:00
|
|
|
else
|
2009-09-16 22:34:49 +04:00
|
|
|
Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
|
|
|
|
TemplateArgs));
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
|
2009-08-29 09:16:22 +04:00
|
|
|
NewArgs.size(),
|
|
|
|
Init->getSourceLocation(),
|
2009-12-03 01:36:29 +03:00
|
|
|
Init->getLParenLoc(),
|
2009-08-29 09:16:22 +04:00
|
|
|
Init->getRParenLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NewInit.isInvalid())
|
|
|
|
New->setInvalidDecl();
|
|
|
|
else {
|
|
|
|
// FIXME: It would be nice if ASTOwningVector had a release function.
|
|
|
|
NewArgs.take();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-29 09:16:22 +04:00
|
|
|
NewInits.push_back((MemInitTy *)NewInit.get());
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-29 09:16:22 +04:00
|
|
|
// Assign all the initializers to the new constructor.
|
2009-09-09 19:08:12 +04:00
|
|
|
ActOnMemInitializers(DeclPtrTy::make(New),
|
2009-08-29 09:16:22 +04:00
|
|
|
/*FIXME: ColonLoc */
|
|
|
|
SourceLocation(),
|
2009-09-09 19:08:12 +04:00
|
|
|
NewInits.data(), NewInits.size());
|
2009-08-29 09:16:22 +04:00
|
|
|
}
|
|
|
|
|
2009-08-29 12:11:13 +04:00
|
|
|
// TODO: this could be templated if the various decl types used the
|
|
|
|
// same method name.
|
|
|
|
static bool isInstantiationOf(ClassTemplateDecl *Pattern,
|
|
|
|
ClassTemplateDecl *Instance) {
|
|
|
|
Pattern = Pattern->getCanonicalDecl();
|
|
|
|
|
|
|
|
do {
|
|
|
|
Instance = Instance->getCanonicalDecl();
|
|
|
|
if (Pattern == Instance) return true;
|
|
|
|
Instance = Instance->getInstantiatedFromMemberTemplate();
|
|
|
|
} while (Instance);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-28 10:34:35 +04:00
|
|
|
static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
|
|
|
|
FunctionTemplateDecl *Instance) {
|
|
|
|
Pattern = Pattern->getCanonicalDecl();
|
|
|
|
|
|
|
|
do {
|
|
|
|
Instance = Instance->getCanonicalDecl();
|
|
|
|
if (Pattern == Instance) return true;
|
|
|
|
Instance = Instance->getInstantiatedFromMemberTemplate();
|
|
|
|
} while (Instance);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-29 03:04:11 +03:00
|
|
|
static bool
|
|
|
|
isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
|
|
|
|
ClassTemplatePartialSpecializationDecl *Instance) {
|
|
|
|
Pattern
|
|
|
|
= cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
|
|
|
|
do {
|
|
|
|
Instance = cast<ClassTemplatePartialSpecializationDecl>(
|
|
|
|
Instance->getCanonicalDecl());
|
|
|
|
if (Pattern == Instance)
|
|
|
|
return true;
|
|
|
|
Instance = Instance->getInstantiatedFromMember();
|
|
|
|
} while (Instance);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-29 12:11:13 +04:00
|
|
|
static bool isInstantiationOf(CXXRecordDecl *Pattern,
|
|
|
|
CXXRecordDecl *Instance) {
|
|
|
|
Pattern = Pattern->getCanonicalDecl();
|
|
|
|
|
|
|
|
do {
|
|
|
|
Instance = Instance->getCanonicalDecl();
|
|
|
|
if (Pattern == Instance) return true;
|
|
|
|
Instance = Instance->getInstantiatedFromMemberClass();
|
|
|
|
} while (Instance);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isInstantiationOf(FunctionDecl *Pattern,
|
|
|
|
FunctionDecl *Instance) {
|
|
|
|
Pattern = Pattern->getCanonicalDecl();
|
|
|
|
|
|
|
|
do {
|
|
|
|
Instance = Instance->getCanonicalDecl();
|
|
|
|
if (Pattern == Instance) return true;
|
|
|
|
Instance = Instance->getInstantiatedFromMemberFunction();
|
|
|
|
} while (Instance);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isInstantiationOf(EnumDecl *Pattern,
|
|
|
|
EnumDecl *Instance) {
|
|
|
|
Pattern = Pattern->getCanonicalDecl();
|
|
|
|
|
|
|
|
do {
|
|
|
|
Instance = Instance->getCanonicalDecl();
|
|
|
|
if (Pattern == Instance) return true;
|
|
|
|
Instance = Instance->getInstantiatedFromMemberEnum();
|
|
|
|
} while (Instance);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-18 05:36:19 +03:00
|
|
|
static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
|
|
|
|
UsingDecl *Instance,
|
|
|
|
ASTContext &C) {
|
|
|
|
return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
|
2009-08-29 23:37:28 +04:00
|
|
|
UsingDecl *Instance,
|
|
|
|
ASTContext &C) {
|
|
|
|
return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
|
|
|
|
}
|
|
|
|
|
2009-08-29 12:11:13 +04:00
|
|
|
static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
|
|
|
|
VarDecl *Instance) {
|
|
|
|
assert(Instance->isStaticDataMember());
|
|
|
|
|
|
|
|
Pattern = Pattern->getCanonicalDecl();
|
|
|
|
|
|
|
|
do {
|
|
|
|
Instance = Instance->getCanonicalDecl();
|
|
|
|
if (Pattern == Instance) return true;
|
|
|
|
Instance = Instance->getInstantiatedFromStaticDataMember();
|
|
|
|
} while (Instance);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-27 09:35:12 +04:00
|
|
|
static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
|
2009-08-29 23:37:28 +04:00
|
|
|
if (D->getKind() != Other->getKind()) {
|
2009-11-18 05:36:19 +03:00
|
|
|
if (UnresolvedUsingTypenameDecl *UUD
|
|
|
|
= dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
|
|
|
|
if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
|
|
|
|
return isInstantiationOf(UUD, UD, Ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UnresolvedUsingValueDecl *UUD
|
|
|
|
= dyn_cast<UnresolvedUsingValueDecl>(D)) {
|
2009-08-29 23:37:28 +04:00
|
|
|
if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
|
|
|
|
return isInstantiationOf(UUD, UD, Ctx);
|
|
|
|
}
|
|
|
|
}
|
2009-05-27 09:35:12 +04:00
|
|
|
|
2009-08-29 23:37:28 +04:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-29 12:11:13 +04:00
|
|
|
if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
|
|
|
|
return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-29 12:11:13 +04:00
|
|
|
if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
|
|
|
|
return isInstantiationOf(cast<FunctionDecl>(D), Function);
|
2009-05-27 09:35:12 +04:00
|
|
|
|
2009-08-29 12:11:13 +04:00
|
|
|
if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
|
|
|
|
return isInstantiationOf(cast<EnumDecl>(D), Enum);
|
2009-05-27 09:35:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
if (VarDecl *Var = dyn_cast<VarDecl>(Other))
|
2009-08-29 12:11:13 +04:00
|
|
|
if (Var->isStaticDataMember())
|
|
|
|
return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
|
|
|
|
|
|
|
|
if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
|
|
|
|
return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
|
2009-08-29 02:03:51 +04:00
|
|
|
|
2009-09-28 10:34:35 +04:00
|
|
|
if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
|
|
|
|
return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
|
|
|
|
|
2009-10-29 03:04:11 +03:00
|
|
|
if (ClassTemplatePartialSpecializationDecl *PartialSpec
|
|
|
|
= dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
|
|
|
|
return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
|
|
|
|
PartialSpec);
|
|
|
|
|
2009-09-01 08:26:58 +04:00
|
|
|
if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
|
|
|
|
if (!Field->getDeclName()) {
|
|
|
|
// This is an unnamed field.
|
2009-09-09 19:08:12 +04:00
|
|
|
return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
|
2009-09-01 08:26:58 +04:00
|
|
|
cast<FieldDecl>(D);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-27 09:35:12 +04:00
|
|
|
return D->getDeclName() && isa<NamedDecl>(Other) &&
|
|
|
|
D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ForwardIterator>
|
2009-09-09 19:08:12 +04:00
|
|
|
static NamedDecl *findInstantiationOf(ASTContext &Ctx,
|
2009-05-27 09:35:12 +04:00
|
|
|
NamedDecl *D,
|
|
|
|
ForwardIterator first,
|
|
|
|
ForwardIterator last) {
|
|
|
|
for (; first != last; ++first)
|
|
|
|
if (isInstantiationOf(Ctx, D, *first))
|
|
|
|
return cast<NamedDecl>(*first);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-28 11:59:38 +04:00
|
|
|
/// \brief Finds the instantiation of the given declaration context
|
|
|
|
/// within the current instantiation.
|
|
|
|
///
|
|
|
|
/// \returns NULL if there was an error
|
2009-09-16 22:34:49 +04:00
|
|
|
DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
|
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs) {
|
2009-08-28 11:59:38 +04:00
|
|
|
if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
|
2009-09-16 22:34:49 +04:00
|
|
|
Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
|
2009-08-28 11:59:38 +04:00
|
|
|
return cast_or_null<DeclContext>(ID);
|
|
|
|
} else return DC;
|
|
|
|
}
|
|
|
|
|
2009-05-27 21:54:46 +04:00
|
|
|
/// \brief Find the instantiation of the given declaration within the
|
|
|
|
/// current instantiation.
|
2009-05-27 09:35:12 +04: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-27 21:54:46 +04:00
|
|
|
/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
|
|
|
|
/// this mapping from within the instantiation of X<int>.
|
2009-09-16 22:34:49 +04:00
|
|
|
NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
|
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs) {
|
2009-05-27 09:35:12 +04:00
|
|
|
DeclContext *ParentDC = D->getDeclContext();
|
2009-10-31 20:21:17 +03:00
|
|
|
if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
|
|
|
|
isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
|
|
|
|
ParentDC->isFunctionOrMethod()) {
|
2009-05-27 21:07:49 +04:00
|
|
|
// 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 09:35:12 +04:00
|
|
|
|
2009-09-16 22:34:49 +04:00
|
|
|
if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
|
|
|
|
if (!Record->isDependentContext())
|
|
|
|
return D;
|
|
|
|
|
|
|
|
// If the RecordDecl is actually the injected-class-name or a "templated"
|
|
|
|
// declaration for a class template or class template partial
|
|
|
|
// specialization, substitute into the injected-class-name of the
|
|
|
|
// class template or partial specialization to find the new DeclContext.
|
|
|
|
QualType T;
|
|
|
|
ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
|
|
|
|
|
|
|
|
if (ClassTemplate) {
|
|
|
|
T = ClassTemplate->getInjectedClassNameType(Context);
|
|
|
|
} else if (ClassTemplatePartialSpecializationDecl *PartialSpec
|
|
|
|
= dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
|
|
|
|
T = Context.getTypeDeclType(Record);
|
|
|
|
ClassTemplate = PartialSpec->getSpecializedTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!T.isNull()) {
|
|
|
|
// Substitute into the injected-class-name to get the type corresponding
|
|
|
|
// to the instantiation we want. This substitution should never fail,
|
|
|
|
// since we know we can instantiate the injected-class-name or we wouldn't
|
|
|
|
// have gotten to the injected-class-name!
|
|
|
|
// FIXME: Can we use the CurrentInstantiationScope to avoid this extra
|
|
|
|
// instantiation in the common case?
|
|
|
|
T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
|
|
|
|
assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
|
|
|
|
|
|
|
|
if (!T->isDependentType()) {
|
|
|
|
assert(T->isRecordType() && "Instantiation must produce a record type");
|
|
|
|
return T->getAs<RecordType>()->getDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are performing "partial" template instantiation to create the
|
|
|
|
// member declarations for the members of a class template
|
|
|
|
// specialization. Therefore, D is actually referring to something in
|
|
|
|
// the current instantiation. Look through the current context,
|
|
|
|
// which contains actual instantiations, to find the instantiation of
|
|
|
|
// the "current instantiation" that D refers to.
|
2009-09-09 19:08:12 +04:00
|
|
|
for (DeclContext *DC = CurContext; !DC->isFileContext();
|
2009-08-29 12:11:13 +04:00
|
|
|
DC = DC->getParent()) {
|
2009-09-09 19:08:12 +04:00
|
|
|
if (ClassTemplateSpecializationDecl *Spec
|
2009-08-29 12:11:13 +04:00
|
|
|
= dyn_cast<ClassTemplateSpecializationDecl>(DC))
|
2009-09-16 22:34:49 +04:00
|
|
|
if (isInstantiationOf(ClassTemplate,
|
|
|
|
Spec->getSpecializedTemplate()))
|
2009-08-29 12:11:13 +04:00
|
|
|
return Spec;
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
assert(false &&
|
2009-08-29 12:11:13 +04:00
|
|
|
"Unable to find declaration for the current instantiation");
|
2009-09-16 22:34:49 +04:00
|
|
|
return Record;
|
2009-08-29 12:11:13 +04:00
|
|
|
}
|
2009-09-16 22:34:49 +04:00
|
|
|
|
|
|
|
// Fall through to deal with other dependent record types (e.g.,
|
|
|
|
// anonymous unions in class templates).
|
|
|
|
}
|
2009-08-29 12:11:13 +04:00
|
|
|
|
2009-09-16 22:34:49 +04:00
|
|
|
if (!ParentDC->isDependentContext())
|
|
|
|
return D;
|
|
|
|
|
|
|
|
ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
|
2009-09-09 19:08:12 +04:00
|
|
|
if (!ParentDC)
|
2009-09-01 21:53:10 +04:00
|
|
|
return 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-27 09:35:12 +04:00
|
|
|
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 06:36:12 +04:00
|
|
|
DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
|
2009-05-27 09:35:12 +04: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!
|
2009-09-09 19:08:12 +04:00
|
|
|
Result = findInstantiationOf(Context, D,
|
2009-06-30 06:36:12 +04:00
|
|
|
ParentDC->decls_begin(),
|
|
|
|
ParentDC->decls_end());
|
2009-05-27 09:35:12 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-27 09:35:12 +04:00
|
|
|
assert(Result && "Unable to find instantiation of declaration!");
|
|
|
|
D = Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return D;
|
|
|
|
}
|
2009-06-23 03:06:13 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
/// \brief Performs template instantiation for all implicit template
|
2009-06-23 03:06:13 +04:00
|
|
|
/// instantiations we have seen until this point.
|
|
|
|
void Sema::PerformPendingImplicitInstantiations() {
|
|
|
|
while (!PendingImplicitInstantiations.empty()) {
|
|
|
|
PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
|
2009-06-30 21:20:14 +04:00
|
|
|
PendingImplicitInstantiations.pop_front();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
// Instantiate function definitions
|
|
|
|
if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
|
2009-09-01 09:12:24 +04:00
|
|
|
Function->getLocation(), *this,
|
|
|
|
Context.getSourceManager(),
|
|
|
|
"instantiating function definition");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-30 06:35:26 +04:00
|
|
|
if (!Function->getBody())
|
2009-06-30 21:20:14 +04:00
|
|
|
InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
|
2009-07-25 00:34:43 +04:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
// Instantiate static data member definitions.
|
|
|
|
VarDecl *Var = cast<VarDecl>(Inst.first);
|
|
|
|
assert(Var->isStaticDataMember() && "Not a static data member?");
|
2009-09-01 09:12:24 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
|
2009-09-01 09:12:24 +04:00
|
|
|
Var->getLocation(), *this,
|
|
|
|
Context.getSourceManager(),
|
|
|
|
"instantiating static data member "
|
|
|
|
"definition");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
|
2009-06-23 03:06:13 +04:00
|
|
|
}
|
|
|
|
}
|