2009-10-24 06:13:42 +08:00
|
|
|
//===--- DeclTemplate.cpp - Template Declaration AST Node Implementation --===//
|
2009-02-05 03:02:06 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the C++ related Decl classes for templates.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2009-02-10 02:46:07 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2009-02-05 03:02:06 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-10-29 16:12:44 +08:00
|
|
|
#include "clang/AST/TypeLoc.h"
|
2009-02-05 03:02:06 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateParameterList Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-02-07 06:42:48 +08:00
|
|
|
TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc,
|
|
|
|
SourceLocation LAngleLoc,
|
2009-09-16 00:23:51 +08:00
|
|
|
NamedDecl **Params, unsigned NumParams,
|
2009-02-07 06:42:48 +08:00
|
|
|
SourceLocation RAngleLoc)
|
|
|
|
: TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
|
|
|
|
NumParams(NumParams) {
|
2009-02-05 03:02:06 +08:00
|
|
|
for (unsigned Idx = 0; Idx < NumParams; ++Idx)
|
|
|
|
begin()[Idx] = Params[Idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
TemplateParameterList *
|
2009-02-07 06:42:48 +08:00
|
|
|
TemplateParameterList::Create(ASTContext &C, SourceLocation TemplateLoc,
|
2009-09-16 00:23:51 +08:00
|
|
|
SourceLocation LAngleLoc, NamedDecl **Params,
|
2009-02-07 06:42:48 +08:00
|
|
|
unsigned NumParams, SourceLocation RAngleLoc) {
|
2009-09-16 00:23:51 +08:00
|
|
|
unsigned Size = sizeof(TemplateParameterList)
|
|
|
|
+ sizeof(NamedDecl *) * NumParams;
|
2009-02-05 03:02:06 +08:00
|
|
|
unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment;
|
|
|
|
void *Mem = C.Allocate(Size, Align);
|
2009-09-09 23:08:12 +08:00
|
|
|
return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
|
2009-02-07 06:42:48 +08:00
|
|
|
NumParams, RAngleLoc);
|
2009-02-05 03:02:06 +08:00
|
|
|
}
|
|
|
|
|
2009-02-12 02:16:40 +08:00
|
|
|
unsigned TemplateParameterList::getMinRequiredArguments() const {
|
|
|
|
unsigned NumRequiredArgs = size();
|
2009-09-09 23:08:12 +08:00
|
|
|
iterator Param = const_cast<TemplateParameterList *>(this)->end(),
|
2009-02-12 02:16:40 +08:00
|
|
|
ParamBegin = const_cast<TemplateParameterList *>(this)->begin();
|
|
|
|
while (Param != ParamBegin) {
|
|
|
|
--Param;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 10:08:00 +08:00
|
|
|
if (!(*Param)->isTemplateParameterPack() &&
|
2009-09-09 23:08:12 +08:00
|
|
|
!(isa<TemplateTypeParmDecl>(*Param) &&
|
2009-02-12 02:16:40 +08:00
|
|
|
cast<TemplateTypeParmDecl>(*Param)->hasDefaultArgument()) &&
|
|
|
|
!(isa<NonTypeTemplateParmDecl>(*Param) &&
|
|
|
|
cast<NonTypeTemplateParmDecl>(*Param)->hasDefaultArgument()) &&
|
|
|
|
!(isa<TemplateTemplateParmDecl>(*Param) &&
|
|
|
|
cast<TemplateTemplateParmDecl>(*Param)->hasDefaultArgument()))
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 02:16:40 +08:00
|
|
|
--NumRequiredArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NumRequiredArgs;
|
|
|
|
}
|
|
|
|
|
2009-10-29 08:04:11 +08:00
|
|
|
unsigned TemplateParameterList::getDepth() const {
|
|
|
|
if (size() == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const NamedDecl *FirstParm = getParam(0);
|
|
|
|
if (const TemplateTypeParmDecl *TTP
|
|
|
|
= dyn_cast<TemplateTypeParmDecl>(FirstParm))
|
|
|
|
return TTP->getDepth();
|
|
|
|
else if (const NonTypeTemplateParmDecl *NTTP
|
|
|
|
= dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
|
|
|
|
return NTTP->getDepth();
|
|
|
|
else
|
|
|
|
return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
|
|
|
|
}
|
|
|
|
|
2010-07-30 00:11:51 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RedeclarableTemplateDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() {
|
|
|
|
// Find the first declaration of this function template.
|
|
|
|
RedeclarableTemplateDecl *First = getCanonicalDecl();
|
|
|
|
|
|
|
|
if (First->CommonOrPrev.isNull()) {
|
|
|
|
CommonBase *CommonPtr = First->newCommon();
|
|
|
|
First->CommonOrPrev = CommonPtr;
|
2010-07-30 00:12:01 +08:00
|
|
|
CommonPtr->Latest = First;
|
2010-07-30 00:11:51 +08:00
|
|
|
}
|
|
|
|
return First->CommonOrPrev.get<CommonBase*>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RedeclarableTemplateDecl *RedeclarableTemplateDecl::getCanonicalDeclImpl() {
|
|
|
|
RedeclarableTemplateDecl *Tmpl = this;
|
|
|
|
while (Tmpl->getPreviousDeclaration())
|
|
|
|
Tmpl = Tmpl->getPreviousDeclaration();
|
|
|
|
return Tmpl;
|
|
|
|
}
|
|
|
|
|
2010-07-30 00:12:01 +08:00
|
|
|
void RedeclarableTemplateDecl::setPreviousDeclarationImpl(
|
|
|
|
RedeclarableTemplateDecl *Prev) {
|
|
|
|
if (Prev) {
|
|
|
|
CommonBase *Common = Prev->getCommonPtr();
|
|
|
|
Prev = Common->Latest;
|
|
|
|
Common->Latest = this;
|
|
|
|
CommonOrPrev = Prev;
|
|
|
|
} else {
|
|
|
|
assert(CommonOrPrev.is<CommonBase*>() && "Cannot reset TemplateDecl Prev");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:02:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FunctionTemplateDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-05-24 02:26:36 +08:00
|
|
|
void FunctionTemplateDecl::DeallocateCommon(void *Ptr) {
|
|
|
|
static_cast<Common *>(Ptr)->~Common();
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:02:06 +08:00
|
|
|
FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
|
|
|
|
DeclContext *DC,
|
|
|
|
SourceLocation L,
|
|
|
|
DeclarationName Name,
|
2009-06-30 04:59:39 +08:00
|
|
|
TemplateParameterList *Params,
|
2009-02-05 03:02:06 +08:00
|
|
|
NamedDecl *Decl) {
|
|
|
|
return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl);
|
|
|
|
}
|
|
|
|
|
2010-07-30 00:11:51 +08:00
|
|
|
RedeclarableTemplateDecl::CommonBase *FunctionTemplateDecl::newCommon() {
|
|
|
|
Common *CommonPtr = new (getASTContext()) Common;
|
|
|
|
getASTContext().AddDeallocation(DeallocateCommon, CommonPtr);
|
|
|
|
return CommonPtr;
|
|
|
|
}
|
|
|
|
|
2010-07-20 21:59:58 +08:00
|
|
|
FunctionDecl *
|
|
|
|
FunctionTemplateDecl::findSpecialization(const TemplateArgument *Args,
|
|
|
|
unsigned NumArgs, void *&InsertPos) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
FunctionTemplateSpecializationInfo::Profile(ID,Args,NumArgs, getASTContext());
|
|
|
|
FunctionTemplateSpecializationInfo *Info
|
|
|
|
= getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
return Info ? Info->Function->getMostRecentDeclaration() : 0;
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:02:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ClassTemplateDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-05-24 02:26:36 +08:00
|
|
|
void ClassTemplateDecl::DeallocateCommon(void *Ptr) {
|
|
|
|
static_cast<Common *>(Ptr)->~Common();
|
|
|
|
}
|
|
|
|
|
2010-06-20 03:29:09 +08:00
|
|
|
ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
|
|
|
|
DeclContext *DC,
|
|
|
|
SourceLocation L,
|
|
|
|
DeclarationName Name,
|
|
|
|
TemplateParameterList *Params,
|
|
|
|
NamedDecl *Decl,
|
|
|
|
ClassTemplateDecl *PrevDecl) {
|
|
|
|
ClassTemplateDecl *New = new (C) ClassTemplateDecl(DC, L, Name, Params, Decl);
|
2010-06-21 18:57:41 +08:00
|
|
|
New->setPreviousDeclaration(PrevDecl);
|
2010-06-20 03:29:09 +08:00
|
|
|
return New;
|
Introduce a new expression type, UnresolvedDeclRefExpr, that describes
dependent qualified-ids such as
Fibonacci<N - 1>::value
where N is a template parameter. These references are "unresolved"
because the name is dependent and, therefore, cannot be resolved to a
declaration node (as we would do for a DeclRefExpr or
QualifiedDeclRefExpr). UnresolvedDeclRefExprs instantiate to
DeclRefExprs, QualifiedDeclRefExprs, etc.
Also, be a bit more careful about keeping only a single set of
specializations for a class template, and instantiating from the
definition of that template rather than a previous declaration. In
general, we need a better solution for this for all TagDecls, because
it's too easy to accidentally look at a declaration that isn't the
definition.
We can now process a simple Fibonacci computation described as a
template metaprogram.
llvm-svn: 67308
2009-03-20 01:26:29 +08:00
|
|
|
}
|
|
|
|
|
2010-07-30 00:11:51 +08:00
|
|
|
RedeclarableTemplateDecl::CommonBase *ClassTemplateDecl::newCommon() {
|
|
|
|
Common *CommonPtr = new (getASTContext()) Common;
|
|
|
|
getASTContext().AddDeallocation(DeallocateCommon, CommonPtr);
|
|
|
|
return CommonPtr;
|
|
|
|
}
|
|
|
|
|
2010-07-20 21:59:28 +08:00
|
|
|
ClassTemplateSpecializationDecl *
|
|
|
|
ClassTemplateDecl::findSpecialization(const TemplateArgument *Args,
|
|
|
|
unsigned NumArgs, void *&InsertPos) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
ClassTemplateSpecializationDecl::Profile(ID, Args, NumArgs, getASTContext());
|
|
|
|
ClassTemplateSpecializationDecl *D
|
|
|
|
= getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
return D ? D->getMostRecentDeclaration() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClassTemplatePartialSpecializationDecl *
|
|
|
|
ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args,
|
|
|
|
unsigned NumArgs,
|
|
|
|
void *&InsertPos) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
ClassTemplatePartialSpecializationDecl::Profile(ID, Args, NumArgs,
|
|
|
|
getASTContext());
|
|
|
|
ClassTemplatePartialSpecializationDecl *D
|
|
|
|
= getPartialSpecializations().FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
return D ? D->getMostRecentDeclaration() : 0;
|
|
|
|
}
|
|
|
|
|
2010-04-30 13:56:50 +08:00
|
|
|
void ClassTemplateDecl::getPartialSpecializations(
|
|
|
|
llvm::SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
|
|
|
|
llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs
|
2010-06-21 18:57:41 +08:00
|
|
|
= getPartialSpecializations();
|
2010-04-30 13:56:50 +08:00
|
|
|
PS.clear();
|
|
|
|
PS.resize(PartialSpecs.size());
|
|
|
|
for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
|
|
|
|
P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
|
|
|
|
P != PEnd; ++P) {
|
|
|
|
assert(!PS[P->getSequenceNumber()]);
|
2010-07-20 21:59:28 +08:00
|
|
|
PS[P->getSequenceNumber()] = P->getMostRecentDeclaration();
|
2010-04-30 13:56:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-31 01:40:51 +08:00
|
|
|
ClassTemplatePartialSpecializationDecl *
|
|
|
|
ClassTemplateDecl::findPartialSpecialization(QualType T) {
|
|
|
|
ASTContext &Context = getASTContext();
|
|
|
|
typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
|
|
|
|
partial_spec_iterator;
|
|
|
|
for (partial_spec_iterator P = getPartialSpecializations().begin(),
|
|
|
|
PEnd = getPartialSpecializations().end();
|
|
|
|
P != PEnd; ++P) {
|
2010-04-27 08:57:59 +08:00
|
|
|
if (Context.hasSameType(P->getInjectedSpecializationType(), T))
|
2010-07-20 21:59:28 +08:00
|
|
|
return P->getMostRecentDeclaration();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClassTemplatePartialSpecializationDecl *
|
|
|
|
ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
|
|
|
|
ClassTemplatePartialSpecializationDecl *D) {
|
|
|
|
Decl *DCanon = D->getCanonicalDecl();
|
|
|
|
for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
|
|
|
|
P = getPartialSpecializations().begin(),
|
|
|
|
PEnd = getPartialSpecializations().end();
|
|
|
|
P != PEnd; ++P) {
|
|
|
|
if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
|
|
|
|
return P->getMostRecentDeclaration();
|
2009-07-31 01:40:51 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-31 01:40:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-10 11:28:59 +08:00
|
|
|
QualType
|
2010-07-09 02:37:38 +08:00
|
|
|
ClassTemplateDecl::getInjectedClassNameSpecialization() {
|
2010-06-21 18:57:41 +08:00
|
|
|
Common *CommonPtr = getCommonPtr();
|
2009-05-11 06:57:19 +08:00
|
|
|
if (!CommonPtr->InjectedClassNameType.isNull())
|
|
|
|
return CommonPtr->InjectedClassNameType;
|
|
|
|
|
|
|
|
// FIXME: n2800 14.6.1p1 should say how the template arguments
|
|
|
|
// corresponding to template parameter packs should be pack
|
|
|
|
// expansions. We already say that in 14.6.2.1p2, so it would be
|
|
|
|
// better to fix that redundancy.
|
2010-07-09 02:37:38 +08:00
|
|
|
ASTContext &Context = getASTContext();
|
2009-05-11 06:57:19 +08:00
|
|
|
TemplateParameterList *Params = getTemplateParameters();
|
|
|
|
llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
|
|
|
|
TemplateArgs.reserve(Params->size());
|
2009-09-09 23:08:12 +08:00
|
|
|
for (TemplateParameterList::iterator Param = Params->begin(),
|
|
|
|
ParamEnd = Params->end();
|
2009-05-11 06:57:19 +08:00
|
|
|
Param != ParamEnd; ++Param) {
|
|
|
|
if (isa<TemplateTypeParmDecl>(*Param)) {
|
|
|
|
QualType ParamType = Context.getTypeDeclType(cast<TypeDecl>(*Param));
|
2009-10-29 16:12:44 +08:00
|
|
|
TemplateArgs.push_back(TemplateArgument(ParamType));
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (NonTypeTemplateParmDecl *NTTP =
|
2009-05-11 06:57:19 +08:00
|
|
|
dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
|
2010-01-31 15:24:03 +08:00
|
|
|
Expr *E = new (Context) DeclRefExpr(NTTP,
|
2010-07-14 02:40:04 +08:00
|
|
|
NTTP->getType().getNonLValueExprType(Context),
|
2009-11-23 19:41:28 +08:00
|
|
|
NTTP->getLocation());
|
2009-05-11 06:57:19 +08:00
|
|
|
TemplateArgs.push_back(TemplateArgument(E));
|
2009-09-09 23:08:12 +08:00
|
|
|
} else {
|
2009-05-11 06:57:19 +08:00
|
|
|
TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
|
2009-11-11 09:00:40 +08:00
|
|
|
TemplateArgs.push_back(TemplateArgument(TemplateName(TTP)));
|
2009-05-11 06:57:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CommonPtr->InjectedClassNameType
|
2009-07-29 07:00:59 +08:00
|
|
|
= Context.getTemplateSpecializationType(TemplateName(this),
|
2009-05-11 06:57:19 +08:00
|
|
|
&TemplateArgs[0],
|
2009-07-29 07:00:59 +08:00
|
|
|
TemplateArgs.size());
|
2009-05-11 06:57:19 +08:00
|
|
|
return CommonPtr->InjectedClassNameType;
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:02:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateTypeParm Allocation/Deallocation Method Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
TemplateTypeParmDecl *
|
|
|
|
TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC,
|
|
|
|
SourceLocation L, unsigned D, unsigned P,
|
2009-06-13 06:23:22 +08:00
|
|
|
IdentifierInfo *Id, bool Typename,
|
|
|
|
bool ParameterPack) {
|
2010-06-16 23:23:05 +08:00
|
|
|
QualType Type = C.getTemplateTypeParmType(D, P, ParameterPack, Id);
|
|
|
|
return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type, ParameterPack);
|
2009-02-05 03:02:06 +08:00
|
|
|
}
|
|
|
|
|
2010-07-02 19:54:55 +08:00
|
|
|
TemplateTypeParmDecl *
|
|
|
|
TemplateTypeParmDecl::Create(ASTContext &C, EmptyShell Empty) {
|
|
|
|
return new (C) TemplateTypeParmDecl(0, SourceLocation(), 0, false,
|
|
|
|
QualType(), false);
|
|
|
|
}
|
|
|
|
|
2009-10-29 16:12:44 +08:00
|
|
|
SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
|
2010-05-20 18:00:11 +08:00
|
|
|
return DefaultArgument->getTypeLoc().getSourceRange().getBegin();
|
2009-10-29 16:12:44 +08:00
|
|
|
}
|
|
|
|
|
2009-10-29 08:04:11 +08:00
|
|
|
unsigned TemplateTypeParmDecl::getDepth() const {
|
|
|
|
return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned TemplateTypeParmDecl::getIndex() const {
|
|
|
|
return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:02:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NonTypeTemplateParmDecl Method Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
NonTypeTemplateParmDecl *
|
|
|
|
NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
|
|
|
|
SourceLocation L, unsigned D, unsigned P,
|
|
|
|
IdentifierInfo *Id, QualType T,
|
2009-12-07 10:54:59 +08:00
|
|
|
TypeSourceInfo *TInfo) {
|
|
|
|
return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, TInfo);
|
2009-02-05 03:02:06 +08:00
|
|
|
}
|
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
|
2010-06-09 17:26:05 +08:00
|
|
|
return hasDefaultArgument()
|
|
|
|
? getDefaultArgument()->getSourceRange().getBegin()
|
|
|
|
: SourceLocation();
|
2009-02-11 03:49:53 +08:00
|
|
|
}
|
|
|
|
|
2009-02-05 03:02:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateTemplateParmDecl Method Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
TemplateTemplateParmDecl *
|
|
|
|
TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
|
|
|
|
SourceLocation L, unsigned D, unsigned P,
|
|
|
|
IdentifierInfo *Id,
|
|
|
|
TemplateParameterList *Params) {
|
|
|
|
return new (C) TemplateTemplateParmDecl(DC, L, D, P, Id, Params);
|
|
|
|
}
|
|
|
|
|
2009-06-05 13:31:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateArgumentListBuilder Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-06-23 09:26:57 +08:00
|
|
|
|
2010-05-20 08:25:36 +08:00
|
|
|
void TemplateArgumentListBuilder::Append(const TemplateArgument &Arg) {
|
|
|
|
assert((Arg.getKind() != TemplateArgument::Type ||
|
|
|
|
Arg.getAsType().isCanonical()) && "Type must be canonical!");
|
|
|
|
assert(FlatArgs.size() < MaxFlatArgs && "Argument list builder is full!");
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(!StructuredArgs &&
|
2009-06-23 09:26:57 +08:00
|
|
|
"Can't append arguments when an argument pack has been added!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-20 08:25:36 +08:00
|
|
|
FlatArgs.push_back(Arg);
|
2009-06-05 13:31:27 +08:00
|
|
|
}
|
|
|
|
|
2009-06-23 09:26:57 +08:00
|
|
|
void TemplateArgumentListBuilder::BeginPack() {
|
|
|
|
assert(!AddingToPack && "Already adding to pack!");
|
|
|
|
assert(!StructuredArgs && "Argument list already contains a pack!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-23 09:26:57 +08:00
|
|
|
AddingToPack = true;
|
2010-05-20 08:25:36 +08:00
|
|
|
PackBeginIndex = FlatArgs.size();
|
2009-06-13 08:08:58 +08:00
|
|
|
}
|
|
|
|
|
2009-06-23 09:26:57 +08:00
|
|
|
void TemplateArgumentListBuilder::EndPack() {
|
|
|
|
assert(AddingToPack && "Not adding to pack!");
|
|
|
|
assert(!StructuredArgs && "Argument list already contains a pack!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-23 09:26:57 +08:00
|
|
|
AddingToPack = false;
|
2009-06-16 01:56:45 +08:00
|
|
|
|
2010-05-20 08:26:28 +08:00
|
|
|
// FIXME: This is a memory leak!
|
2009-06-23 09:26:57 +08:00
|
|
|
StructuredArgs = new TemplateArgument[MaxStructuredArgs];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-23 09:26:57 +08:00
|
|
|
// First copy the flat entries over to the list (if any)
|
|
|
|
for (unsigned I = 0; I != PackBeginIndex; ++I) {
|
|
|
|
NumStructuredArgs++;
|
|
|
|
StructuredArgs[I] = FlatArgs[I];
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-23 09:26:57 +08:00
|
|
|
// Next, set the pack.
|
|
|
|
TemplateArgument *PackArgs = 0;
|
|
|
|
unsigned NumPackArgs = NumFlatArgs - PackBeginIndex;
|
2010-05-20 08:25:36 +08:00
|
|
|
// FIXME: NumPackArgs shouldn't be negative here???
|
2009-06-23 09:26:57 +08:00
|
|
|
if (NumPackArgs)
|
2010-05-20 08:25:36 +08:00
|
|
|
PackArgs = FlatArgs.data()+PackBeginIndex;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
StructuredArgs[NumStructuredArgs++].setArgumentPack(PackArgs, NumPackArgs,
|
2009-06-23 09:26:57 +08:00
|
|
|
/*CopyArgs=*/false);
|
|
|
|
}
|
|
|
|
|
2009-05-12 07:53:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateArgumentList Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
|
2009-06-05 12:47:51 +08:00
|
|
|
TemplateArgumentListBuilder &Builder,
|
2009-06-23 09:26:57 +08:00
|
|
|
bool TakeArgs)
|
2009-09-09 23:08:12 +08:00
|
|
|
: FlatArguments(Builder.getFlatArguments(), TakeArgs),
|
|
|
|
NumFlatArguments(Builder.flatSize()),
|
2009-06-23 09:26:57 +08:00
|
|
|
StructuredArguments(Builder.getStructuredArguments(), TakeArgs),
|
|
|
|
NumStructuredArguments(Builder.structuredSize()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-23 09:26:57 +08:00
|
|
|
if (!TakeArgs)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-20 08:11:47 +08:00
|
|
|
// If this does take ownership of the arguments, then we have to new them
|
|
|
|
// and copy over.
|
2010-05-26 04:43:29 +08:00
|
|
|
TemplateArgument *NewArgs =
|
|
|
|
new (Context) TemplateArgument[Builder.flatSize()];
|
2010-05-20 08:11:47 +08:00
|
|
|
std::copy(Builder.getFlatArguments(),
|
|
|
|
Builder.getFlatArguments()+Builder.flatSize(), NewArgs);
|
|
|
|
FlatArguments.setPointer(NewArgs);
|
|
|
|
|
|
|
|
// Just reuse the structured and flat arguments array if possible.
|
|
|
|
if (Builder.getStructuredArguments() == Builder.getFlatArguments()) {
|
|
|
|
StructuredArguments.setPointer(NewArgs);
|
2009-06-23 09:26:57 +08:00
|
|
|
StructuredArguments.setInt(0);
|
2010-05-20 08:11:47 +08:00
|
|
|
} else {
|
2010-05-26 04:43:29 +08:00
|
|
|
TemplateArgument *NewSArgs =
|
|
|
|
new (Context) TemplateArgument[Builder.flatSize()];
|
2010-05-20 08:11:47 +08:00
|
|
|
std::copy(Builder.getFlatArguments(),
|
|
|
|
Builder.getFlatArguments()+Builder.flatSize(), NewSArgs);
|
|
|
|
StructuredArguments.setPointer(NewSArgs);
|
|
|
|
}
|
2009-05-12 07:53:27 +08:00
|
|
|
}
|
|
|
|
|
2010-06-22 17:54:51 +08:00
|
|
|
TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
|
2010-06-23 21:48:23 +08:00
|
|
|
const TemplateArgument *Args,
|
2010-06-28 17:31:34 +08:00
|
|
|
unsigned NumArgs)
|
|
|
|
: NumFlatArguments(0), NumStructuredArguments(0) {
|
2010-06-23 21:48:23 +08:00
|
|
|
init(Context, Args, NumArgs);
|
2010-06-22 17:54:51 +08:00
|
|
|
}
|
|
|
|
|
2010-05-20 08:19:09 +08:00
|
|
|
/// Produces a shallow copy of the given template argument list. This
|
|
|
|
/// assumes that the input argument list outlives it. This takes the list as
|
|
|
|
/// a pointer to avoid looking like a copy constructor, since this really
|
|
|
|
/// really isn't safe to use that way.
|
|
|
|
TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList *Other)
|
|
|
|
: FlatArguments(Other->FlatArguments.getPointer(), false),
|
|
|
|
NumFlatArguments(Other->flat_size()),
|
|
|
|
StructuredArguments(Other->StructuredArguments.getPointer(), false),
|
|
|
|
NumStructuredArguments(Other->NumStructuredArguments) { }
|
2009-09-25 07:14:47 +08:00
|
|
|
|
2010-06-23 21:48:23 +08:00
|
|
|
void TemplateArgumentList::init(ASTContext &Context,
|
|
|
|
const TemplateArgument *Args,
|
|
|
|
unsigned NumArgs) {
|
|
|
|
assert(NumFlatArguments == 0 && NumStructuredArguments == 0 &&
|
|
|
|
"Already initialized!");
|
|
|
|
|
|
|
|
NumFlatArguments = NumStructuredArguments = NumArgs;
|
|
|
|
TemplateArgument *NewArgs = new (Context) TemplateArgument[NumArgs];
|
|
|
|
std::copy(Args, Args+NumArgs, NewArgs);
|
|
|
|
FlatArguments.setPointer(NewArgs);
|
|
|
|
FlatArguments.setInt(1); // Owns the pointer.
|
|
|
|
|
|
|
|
// Just reuse the flat arguments array.
|
|
|
|
StructuredArguments.setPointer(NewArgs);
|
|
|
|
StructuredArguments.setInt(0); // Doesn't own the pointer.
|
|
|
|
}
|
|
|
|
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ClassTemplateSpecializationDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
ClassTemplateSpecializationDecl::
|
2010-05-06 08:28:52 +08:00
|
|
|
ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
|
2009-05-12 07:53:27 +08:00
|
|
|
DeclContext *DC, SourceLocation L,
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
ClassTemplateDecl *SpecializedTemplate,
|
2009-07-30 07:36:44 +08:00
|
|
|
TemplateArgumentListBuilder &Builder,
|
|
|
|
ClassTemplateSpecializationDecl *PrevDecl)
|
2010-05-06 08:28:52 +08:00
|
|
|
: CXXRecordDecl(DK, TK, DC, L,
|
2009-07-30 07:36:44 +08:00
|
|
|
SpecializedTemplate->getIdentifier(),
|
|
|
|
PrevDecl),
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
SpecializedTemplate(SpecializedTemplate),
|
2010-06-12 15:44:57 +08:00
|
|
|
ExplicitInfo(0),
|
2009-06-23 09:26:57 +08:00
|
|
|
TemplateArgs(Context, Builder, /*TakeArgs=*/true),
|
2009-05-12 07:53:27 +08:00
|
|
|
SpecializationKind(TSK_Undeclared) {
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-06-23 21:48:23 +08:00
|
|
|
ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(Kind DK)
|
|
|
|
: CXXRecordDecl(DK, TTK_Struct, 0, SourceLocation(), 0, 0),
|
|
|
|
ExplicitInfo(0),
|
|
|
|
SpecializationKind(TSK_Undeclared) {
|
|
|
|
}
|
|
|
|
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
ClassTemplateSpecializationDecl *
|
2010-05-06 08:28:52 +08:00
|
|
|
ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
DeclContext *DC, SourceLocation L,
|
|
|
|
ClassTemplateDecl *SpecializedTemplate,
|
2009-06-05 12:06:48 +08:00
|
|
|
TemplateArgumentListBuilder &Builder,
|
2009-02-18 07:15:12 +08:00
|
|
|
ClassTemplateSpecializationDecl *PrevDecl) {
|
|
|
|
ClassTemplateSpecializationDecl *Result
|
2009-09-09 23:08:12 +08:00
|
|
|
= new (Context)ClassTemplateSpecializationDecl(Context,
|
2009-05-31 17:31:02 +08:00
|
|
|
ClassTemplateSpecialization,
|
2010-05-06 08:28:52 +08:00
|
|
|
TK, DC, L,
|
2009-05-12 07:53:27 +08:00
|
|
|
SpecializedTemplate,
|
2009-07-30 07:36:44 +08:00
|
|
|
Builder,
|
|
|
|
PrevDecl);
|
2009-02-18 07:15:12 +08:00
|
|
|
Context.getTypeDeclType(Result, PrevDecl);
|
|
|
|
return Result;
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
}
|
2009-05-31 17:31:02 +08:00
|
|
|
|
2010-06-23 21:48:23 +08:00
|
|
|
ClassTemplateSpecializationDecl *
|
2010-07-02 19:54:55 +08:00
|
|
|
ClassTemplateSpecializationDecl::Create(ASTContext &Context, EmptyShell Empty) {
|
2010-06-23 21:48:23 +08:00
|
|
|
return
|
|
|
|
new (Context)ClassTemplateSpecializationDecl(ClassTemplateSpecialization);
|
|
|
|
}
|
|
|
|
|
2009-09-11 14:45:03 +08:00
|
|
|
void
|
|
|
|
ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
|
|
|
|
const PrintingPolicy &Policy,
|
|
|
|
bool Qualified) const {
|
|
|
|
NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
|
|
|
|
|
|
|
|
const TemplateArgumentList &TemplateArgs = getTemplateArgs();
|
|
|
|
S += TemplateSpecializationType::PrintTemplateArgumentList(
|
|
|
|
TemplateArgs.getFlatArgumentList(),
|
|
|
|
TemplateArgs.flat_size(),
|
|
|
|
Policy);
|
|
|
|
}
|
|
|
|
|
2009-08-03 07:24:31 +08:00
|
|
|
ClassTemplateDecl *
|
2009-09-09 23:08:12 +08:00
|
|
|
ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
|
|
|
|
if (SpecializedPartialSpecialization *PartialSpec
|
2009-08-03 07:24:31 +08:00
|
|
|
= SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
|
|
|
|
return PartialSpec->PartialSpecialization->getSpecializedTemplate();
|
|
|
|
return SpecializedTemplate.get<ClassTemplateDecl*>();
|
|
|
|
}
|
|
|
|
|
2009-05-31 17:31:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ClassTemplatePartialSpecializationDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
ClassTemplatePartialSpecializationDecl *
|
|
|
|
ClassTemplatePartialSpecializationDecl::
|
2010-05-06 08:28:52 +08:00
|
|
|
Create(ASTContext &Context, TagKind TK,DeclContext *DC, SourceLocation L,
|
2009-05-31 17:31:02 +08:00
|
|
|
TemplateParameterList *Params,
|
|
|
|
ClassTemplateDecl *SpecializedTemplate,
|
2009-06-05 12:06:48 +08:00
|
|
|
TemplateArgumentListBuilder &Builder,
|
2009-11-23 09:53:49 +08:00
|
|
|
const TemplateArgumentListInfo &ArgInfos,
|
2010-03-10 11:28:59 +08:00
|
|
|
QualType CanonInjectedType,
|
2010-04-30 13:56:50 +08:00
|
|
|
ClassTemplatePartialSpecializationDecl *PrevDecl,
|
|
|
|
unsigned SequenceNumber) {
|
2009-11-23 09:53:49 +08:00
|
|
|
unsigned N = ArgInfos.size();
|
2009-10-29 16:12:44 +08:00
|
|
|
TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
|
|
|
|
for (unsigned I = 0; I != N; ++I)
|
|
|
|
ClonedArgs[I] = ArgInfos[I];
|
|
|
|
|
2009-05-31 17:31:02 +08:00
|
|
|
ClassTemplatePartialSpecializationDecl *Result
|
2010-05-06 08:28:52 +08:00
|
|
|
= new (Context)ClassTemplatePartialSpecializationDecl(Context, TK,
|
2009-05-31 17:31:02 +08:00
|
|
|
DC, L, Params,
|
|
|
|
SpecializedTemplate,
|
2009-10-29 16:12:44 +08:00
|
|
|
Builder,
|
|
|
|
ClonedArgs, N,
|
2010-04-30 13:56:50 +08:00
|
|
|
PrevDecl,
|
|
|
|
SequenceNumber);
|
2009-05-31 17:31:02 +08:00
|
|
|
Result->setSpecializationKind(TSK_ExplicitSpecialization);
|
2010-03-10 11:28:59 +08:00
|
|
|
|
|
|
|
Context.getInjectedClassNameType(Result, CanonInjectedType);
|
2009-05-31 17:31:02 +08:00
|
|
|
return Result;
|
|
|
|
}
|
2009-09-17 06:47:08 +08:00
|
|
|
|
2010-06-23 21:48:23 +08:00
|
|
|
ClassTemplatePartialSpecializationDecl *
|
2010-07-02 19:54:55 +08:00
|
|
|
ClassTemplatePartialSpecializationDecl::Create(ASTContext &Context,
|
|
|
|
EmptyShell Empty) {
|
2010-06-23 21:48:23 +08:00
|
|
|
return new (Context)ClassTemplatePartialSpecializationDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClassTemplatePartialSpecializationDecl::
|
|
|
|
initTemplateArgsAsWritten(const TemplateArgumentListInfo &ArgInfos) {
|
|
|
|
assert(ArgsAsWritten == 0 && "ArgsAsWritten already set");
|
|
|
|
unsigned N = ArgInfos.size();
|
|
|
|
TemplateArgumentLoc *ClonedArgs
|
|
|
|
= new (getASTContext()) TemplateArgumentLoc[N];
|
|
|
|
for (unsigned I = 0; I != N; ++I)
|
|
|
|
ClonedArgs[I] = ArgInfos[I];
|
|
|
|
|
|
|
|
ArgsAsWritten = ClonedArgs;
|
|
|
|
NumArgsAsWritten = N;
|
|
|
|
}
|
|
|
|
|
2009-09-17 06:47:08 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FriendTemplateDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
|
|
|
|
DeclContext *DC,
|
|
|
|
SourceLocation L,
|
|
|
|
unsigned NParams,
|
|
|
|
TemplateParameterList **Params,
|
|
|
|
FriendUnion Friend,
|
|
|
|
SourceLocation FLoc) {
|
|
|
|
FriendTemplateDecl *Result
|
|
|
|
= new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
|
|
|
|
return Result;
|
|
|
|
}
|
2010-07-23 00:04:10 +08:00
|
|
|
|
|
|
|
FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
|
|
|
|
EmptyShell Empty) {
|
|
|
|
return new (Context) FriendTemplateDecl(Empty);
|
|
|
|
}
|