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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===/
|
2010-08-26 02:03:47 +04:00
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2010-08-13 00:07:10 +04:00
|
|
|
#include "clang/Sema/Lookup.h"
|
2010-08-27 03:41:50 +04:00
|
|
|
#include "clang/Sema/PrettyDeclStackTrace.h"
|
2010-08-24 11:21:54 +04:00
|
|
|
#include "clang/Sema/Template.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"
|
2010-03-24 08:22:00 +03:00
|
|
|
#include "clang/AST/DependentDiagnostic.h"
|
2009-03-18 00:15:40 +03:00
|
|
|
#include "clang/AST/Expr.h"
|
2009-12-12 21:16:41 +03:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2010-03-11 12:03:00 +03:00
|
|
|
#include "clang/AST/TypeLoc.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;
|
|
|
|
|
2010-03-15 13:12:16 +03:00
|
|
|
bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
|
|
|
|
DeclaratorDecl *NewDecl) {
|
2011-02-25 05:25:35 +03:00
|
|
|
if (!OldDecl->getQualifierLoc())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NestedNameSpecifierLoc NewQualifierLoc
|
|
|
|
= SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
|
|
|
|
TemplateArgs);
|
|
|
|
|
|
|
|
if (!NewQualifierLoc)
|
2010-03-15 13:12:16 +03:00
|
|
|
return true;
|
2011-02-25 05:25:35 +03:00
|
|
|
|
|
|
|
NewDecl->setQualifierInfo(NewQualifierLoc);
|
2010-03-15 13:12:16 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
|
|
|
|
TagDecl *NewDecl) {
|
2011-02-25 05:25:35 +03:00
|
|
|
if (!OldDecl->getQualifierLoc())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NestedNameSpecifierLoc NewQualifierLoc
|
|
|
|
= SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
|
|
|
|
TemplateArgs);
|
|
|
|
|
|
|
|
if (!NewQualifierLoc)
|
2010-03-15 13:12:16 +03:00
|
|
|
return true;
|
2011-02-25 05:25:35 +03:00
|
|
|
|
|
|
|
NewDecl->setQualifierInfo(NewQualifierLoc);
|
2010-03-15 13:12:16 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-25 07:22:07 +04:00
|
|
|
// FIXME: Is this still too simple?
|
2010-08-01 06:01:53 +04:00
|
|
|
void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
|
|
|
|
Decl *Tmpl, Decl *New) {
|
2010-08-19 03:23:40 +04:00
|
|
|
for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
|
|
|
|
i != e; ++i) {
|
|
|
|
const Attr *TmplAttr = *i;
|
2010-06-25 07:22:07 +04:00
|
|
|
// FIXME: This should be generalized to more than just the AlignedAttr.
|
|
|
|
if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
|
2010-08-19 03:23:40 +04:00
|
|
|
if (Aligned->isAlignmentDependent()) {
|
2010-06-25 07:22:07 +04:00
|
|
|
// The alignment expression is not potentially evaluated.
|
2010-08-01 06:01:53 +04:00
|
|
|
EnterExpressionEvaluationContext Unevaluated(*this,
|
2010-08-27 03:41:50 +04:00
|
|
|
Sema::Unevaluated);
|
2010-06-25 07:22:07 +04:00
|
|
|
|
2010-08-19 03:23:40 +04:00
|
|
|
if (Aligned->isAlignmentExpr()) {
|
2010-08-24 10:29:42 +04:00
|
|
|
ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
|
2010-11-20 04:29:55 +03:00
|
|
|
TemplateArgs);
|
2010-08-19 03:23:40 +04:00
|
|
|
if (!Result.isInvalid())
|
|
|
|
AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
|
2010-11-20 04:29:55 +03:00
|
|
|
TemplateArgs,
|
|
|
|
Aligned->getLocation(),
|
|
|
|
DeclarationName());
|
2010-08-19 03:23:40 +04:00
|
|
|
if (Result)
|
|
|
|
AddAlignedAttr(Aligned->getLocation(), New, Result);
|
|
|
|
}
|
2010-06-25 07:22:07 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-07 09:07:58 +03:00
|
|
|
// FIXME: Is cloning correct for all attributes?
|
2010-08-01 06:01:53 +04:00
|
|
|
Attr *NewAttr = TmplAttr->clone(Context);
|
2009-11-07 09:07:58 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-02-17 23:34:02 +03:00
|
|
|
Decl *
|
|
|
|
TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
|
|
|
|
LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
|
|
|
D->getIdentifier());
|
|
|
|
Owner->addDecl(Inst);
|
|
|
|
return Inst;
|
|
|
|
}
|
|
|
|
|
2009-03-25 18:45:12 +03:00
|
|
|
Decl *
|
|
|
|
TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
|
|
|
|
assert(false && "Namespaces cannot be instantiated");
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2010-02-16 09:53:13 +03:00
|
|
|
Decl *
|
|
|
|
TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
|
|
|
|
NamespaceAliasDecl *Inst
|
|
|
|
= NamespaceAliasDecl::Create(SemaRef.Context, Owner,
|
|
|
|
D->getNamespaceLoc(),
|
|
|
|
D->getAliasLoc(),
|
2011-02-25 20:08:07 +03:00
|
|
|
D->getIdentifier(),
|
|
|
|
D->getQualifierLoc(),
|
2010-02-16 09:53:13 +03:00
|
|
|
D->getTargetNameLoc(),
|
|
|
|
D->getNamespace());
|
|
|
|
Owner->addDecl(Inst);
|
|
|
|
return Inst;
|
|
|
|
}
|
|
|
|
|
2009-03-18 00:15:40 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
|
|
|
|
bool Invalid = false;
|
2009-12-07 05:54:59 +03:00
|
|
|
TypeSourceInfo *DI = D->getTypeSourceInfo();
|
2010-05-24 21:22:01 +04:00
|
|
|
if (DI->getType()->isDependentType() ||
|
|
|
|
DI->getType()->isVariablyModifiedType()) {
|
2009-10-24 12:00:42 +04:00
|
|
|
DI = SemaRef.SubstType(DI, TemplateArgs,
|
|
|
|
D->getLocation(), D->getDeclName());
|
|
|
|
if (!DI) {
|
2009-03-18 00:15:40 +03:00
|
|
|
Invalid = true;
|
2009-12-07 05:54:59 +03:00
|
|
|
DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
|
2009-03-18 00:15:40 +03:00
|
|
|
}
|
2010-05-08 03:12:07 +04:00
|
|
|
} else {
|
|
|
|
SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
|
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();
|
|
|
|
|
2011-02-01 11:20:08 +03:00
|
|
|
// If the old typedef was the name for linkage purposes of an anonymous
|
|
|
|
// tag decl, re-establish that relationship for the new typedef.
|
|
|
|
if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
|
|
|
|
TagDecl *oldTag = oldTagType->getDecl();
|
|
|
|
if (oldTag->getTypedefForAnonDecl() == D) {
|
|
|
|
TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
|
|
|
|
assert(!newTag->getIdentifier() && !newTag->getTypedefForAnonDecl());
|
|
|
|
newTag->setTypedefForAnonDecl(Typedef);
|
|
|
|
}
|
2010-04-23 20:25:07 +04:00
|
|
|
}
|
|
|
|
|
2009-12-30 03:31:22 +03:00
|
|
|
if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
|
2010-03-01 18:56:25 +03:00
|
|
|
NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
|
|
|
|
TemplateArgs);
|
2011-03-04 22:46:35 +03:00
|
|
|
if (!InstPrev)
|
|
|
|
return 0;
|
|
|
|
|
2009-12-30 03:31:22 +03:00
|
|
|
Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
|
|
|
|
}
|
|
|
|
|
2010-08-01 06:01:53 +04:00
|
|
|
SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
|
2010-04-23 20:25:07 +04:00
|
|
|
|
2010-01-21 00:53:11 +03:00
|
|
|
Typedef->setAccess(D->getAccess());
|
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;
|
|
|
|
}
|
|
|
|
|
2010-03-02 10:38:39 +03:00
|
|
|
/// \brief Instantiate an initializer, breaking it into separate
|
|
|
|
/// initialization arguments.
|
|
|
|
///
|
|
|
|
/// \param S The semantic analysis object.
|
|
|
|
///
|
|
|
|
/// \param Init The initializer to instantiate.
|
|
|
|
///
|
|
|
|
/// \param TemplateArgs Template arguments to be substituted into the
|
|
|
|
/// initializer.
|
|
|
|
///
|
|
|
|
/// \param NewArgs Will be filled in with the instantiation arguments.
|
|
|
|
///
|
|
|
|
/// \returns true if an error occurred, false otherwise
|
|
|
|
static bool InstantiateInitializer(Sema &S, Expr *Init,
|
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs,
|
|
|
|
SourceLocation &LParenLoc,
|
2010-11-20 04:29:55 +03:00
|
|
|
ASTOwningVector<Expr*> &NewArgs,
|
2010-03-02 10:38:39 +03:00
|
|
|
SourceLocation &RParenLoc) {
|
|
|
|
NewArgs.clear();
|
|
|
|
LParenLoc = SourceLocation();
|
|
|
|
RParenLoc = SourceLocation();
|
|
|
|
|
|
|
|
if (!Init)
|
|
|
|
return false;
|
|
|
|
|
2010-12-06 11:20:24 +03:00
|
|
|
if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
|
2010-03-02 10:38:39 +03:00
|
|
|
Init = ExprTemp->getSubExpr();
|
|
|
|
|
|
|
|
while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
|
|
|
|
Init = Binder->getSubExpr();
|
|
|
|
|
|
|
|
if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
|
|
|
|
Init = ICE->getSubExprAsWritten();
|
|
|
|
|
|
|
|
if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
|
|
|
|
LParenLoc = ParenList->getLParenLoc();
|
|
|
|
RParenLoc = ParenList->getRParenLoc();
|
2011-01-07 22:35:17 +03:00
|
|
|
return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
|
|
|
|
true, TemplateArgs, NewArgs);
|
2010-03-02 10:38:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
|
2010-03-25 00:22:47 +03:00
|
|
|
if (!isa<CXXTemporaryObjectExpr>(Construct)) {
|
2011-01-07 22:35:17 +03:00
|
|
|
if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
|
|
|
|
TemplateArgs, NewArgs))
|
2010-03-25 00:22:47 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// FIXME: Fake locations!
|
|
|
|
LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
|
2010-09-09 20:33:13 +04:00
|
|
|
RParenLoc = LParenLoc;
|
2010-03-25 00:22:47 +03:00
|
|
|
return false;
|
|
|
|
}
|
2010-03-02 10:38:39 +03:00
|
|
|
}
|
|
|
|
|
2010-08-24 10:29:42 +04:00
|
|
|
ExprResult Result = S.SubstExpr(Init, TemplateArgs);
|
2010-03-02 10:38:39 +03:00
|
|
|
if (Result.isInvalid())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
NewArgs.push_back(Result.takeAs<Expr>());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-26 02:32:15 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
|
2010-05-21 04:31:19 +04:00
|
|
|
// If this is the variable for an anonymous struct or union,
|
|
|
|
// instantiate the anonymous struct/union type first.
|
|
|
|
if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
|
|
|
|
if (RecordTy->getDecl()->isAnonymousStructOrUnion())
|
|
|
|
if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
|
|
|
|
return 0;
|
|
|
|
|
2009-08-26 02:02:44 +04:00
|
|
|
// Do substitution on the type of the declaration
|
2009-12-07 05:54:59 +03:00
|
|
|
TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
|
2009-10-21 06:39:02 +04:00
|
|
|
TemplateArgs,
|
|
|
|
D->getTypeSpecStartLoc(),
|
|
|
|
D->getDeclName());
|
|
|
|
if (!DI)
|
2009-03-26 02:32:15 +03:00
|
|
|
return 0;
|
|
|
|
|
2010-09-12 11:37:24 +04:00
|
|
|
if (DI->getType()->isFunctionType()) {
|
|
|
|
SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
|
|
|
|
<< D->isStaticDataMember() << DI->getType();
|
|
|
|
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,
|
2010-04-20 02:54:31 +04:00
|
|
|
D->getStorageClass(),
|
|
|
|
D->getStorageClassAsWritten());
|
2009-03-26 02:32:15 +03:00
|
|
|
Var->setThreadSpecified(D->isThreadSpecified());
|
|
|
|
Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-03-15 13:12:16 +03:00
|
|
|
// Substitute the nested name specifier, if any.
|
|
|
|
if (SubstQualifier(D, Var))
|
|
|
|
return 0;
|
|
|
|
|
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
|
|
|
|
2010-01-21 00:53:11 +03:00
|
|
|
Var->setAccess(D->getAccess());
|
2010-06-18 03:14:26 +04:00
|
|
|
|
|
|
|
if (!D->isStaticDataMember())
|
|
|
|
Var->setUsed(D->isUsed(false));
|
2010-05-19 21:02:24 +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(),
|
2010-03-01 22:11:54 +03:00
|
|
|
Sema::LookupOrdinaryName, Sema::ForRedeclaration);
|
2010-02-09 10:26:29 +03:00
|
|
|
if (D->isStaticDataMember())
|
|
|
|
SemaRef.LookupQualifiedName(Previous, Owner, false);
|
2009-11-19 01:49:29 +03:00
|
|
|
SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
if (D->isOutOfLine()) {
|
2010-06-04 13:35:39 +04:00
|
|
|
if (!D->isStaticDataMember())
|
|
|
|
D->getLexicalDeclContext()->addDecl(Var);
|
2009-07-25 00:34:43 +04:00
|
|
|
Owner->makeDeclVisibleInContext(Var);
|
|
|
|
} else {
|
|
|
|
Owner->addDecl(Var);
|
2010-05-04 00:22:41 +04:00
|
|
|
if (Owner->isFunctionOrMethod())
|
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
|
2009-07-25 00:34:43 +04:00
|
|
|
}
|
2010-08-01 06:01:53 +04:00
|
|
|
SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
|
2010-07-13 04:16:40 +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
|
|
|
|
2010-02-09 10:26:29 +03:00
|
|
|
if (Var->getAnyInitializer()) {
|
|
|
|
// We already have an initializer in the class.
|
|
|
|
} else if (D->getInit()) {
|
2009-12-03 20:10:37 +03:00
|
|
|
if (Var->isStaticDataMember() && !D->isOutOfLine())
|
|
|
|
SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
|
|
|
|
else
|
|
|
|
SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
|
|
|
|
|
2010-03-02 10:38:39 +03:00
|
|
|
// Instantiate the initializer.
|
|
|
|
SourceLocation LParenLoc, RParenLoc;
|
2010-08-23 10:44:23 +04:00
|
|
|
ASTOwningVector<Expr*> InitArgs(SemaRef);
|
2010-03-02 10:38:39 +03:00
|
|
|
if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
|
2010-09-09 20:33:13 +04:00
|
|
|
InitArgs, RParenLoc)) {
|
2011-02-20 06:19:35 +03:00
|
|
|
bool TypeMayContainAuto = true;
|
2011-01-14 20:12:22 +03:00
|
|
|
// Attach the initializer to the declaration, if we have one.
|
|
|
|
if (InitArgs.size() == 0)
|
2011-02-20 06:19:35 +03:00
|
|
|
SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
|
2011-01-14 20:12:22 +03:00
|
|
|
else if (D->hasCXXDirectInitializer()) {
|
2009-12-14 22:27:10 +03:00
|
|
|
// Add the direct initializer to the declaration.
|
2010-08-21 13:40:31 +04:00
|
|
|
SemaRef.AddCXXDirectInitializerToDecl(Var,
|
2010-03-02 10:38:39 +03:00
|
|
|
LParenLoc,
|
2009-12-14 22:27:10 +03:00
|
|
|
move_arg(InitArgs),
|
2011-02-20 06:19:35 +03:00
|
|
|
RParenLoc,
|
|
|
|
TypeMayContainAuto);
|
2011-01-14 20:12:22 +03:00
|
|
|
} else {
|
|
|
|
assert(InitArgs.size() == 1);
|
2010-08-24 03:25:46 +04:00
|
|
|
Expr *Init = InitArgs.take()[0];
|
2011-02-20 06:19:35 +03:00
|
|
|
SemaRef.AddInitializerToDecl(Var, Init, false, TypeMayContainAuto);
|
2009-08-27 01:14:46 +04:00
|
|
|
}
|
2009-12-14 22:27:10 +03:00
|
|
|
} else {
|
2010-03-02 10:38:39 +03:00
|
|
|
// FIXME: Not too happy about invalidating the declaration
|
|
|
|
// because of a bogus initializer.
|
|
|
|
Var->setInvalidDecl();
|
2009-12-14 22:27:10 +03:00
|
|
|
}
|
|
|
|
|
2009-12-03 20:10:37 +03:00
|
|
|
SemaRef.PopExpressionEvaluationContext();
|
2009-07-27 21:43:39 +04:00
|
|
|
} else if (!Var->isStaticDataMember() || Var->isOutOfLine())
|
2010-08-21 13:40:31 +04:00
|
|
|
SemaRef.ActOnUninitializedDecl(Var, false);
|
2009-03-26 02:32:15 +03:00
|
|
|
|
2010-05-09 03:05:03 +04:00
|
|
|
// Diagnose unused local variables.
|
|
|
|
if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
|
|
|
|
SemaRef.DiagnoseUnusedDecl(Var);
|
2010-08-15 05:15:20 +04:00
|
|
|
|
2009-03-26 02:32:15 +03:00
|
|
|
return Var;
|
|
|
|
}
|
|
|
|
|
2010-06-05 09:09:32 +04:00
|
|
|
Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
|
|
|
|
AccessSpecDecl* AD
|
|
|
|
= AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
|
|
|
|
D->getAccessSpecifierLoc(), D->getColonLoc());
|
|
|
|
Owner->addHiddenDecl(AD);
|
|
|
|
return AD;
|
|
|
|
}
|
|
|
|
|
2009-03-18 00:15:40 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
|
|
|
|
bool Invalid = false;
|
2009-12-07 05:54:59 +03:00
|
|
|
TypeSourceInfo *DI = D->getTypeSourceInfo();
|
2010-05-24 21:22:01 +04:00
|
|
|
if (DI->getType()->isDependentType() ||
|
|
|
|
DI->getType()->isVariablyModifiedType()) {
|
2009-10-23 03:33:21 +04:00
|
|
|
DI = SemaRef.SubstType(DI, TemplateArgs,
|
|
|
|
D->getLocation(), D->getDeclName());
|
|
|
|
if (!DI) {
|
2009-12-07 05:54:59 +03:00
|
|
|
DI = D->getTypeSourceInfo();
|
2009-10-23 03:33:21 +04:00
|
|
|
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;
|
|
|
|
}
|
2010-05-08 03:12:07 +04:00
|
|
|
} else {
|
|
|
|
SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
|
2009-03-18 00:15:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2010-08-27 03:41:50 +04:00
|
|
|
EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-24 10:29:42 +04:00
|
|
|
ExprResult 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
|
|
|
|
2010-08-01 06:01:53 +04:00
|
|
|
SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
|
2009-11-07 09:07:58 +03:00
|
|
|
|
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);
|
2010-05-21 04:31:19 +04:00
|
|
|
}
|
|
|
|
if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
|
|
|
|
if (Parent->isAnonymousStructOrUnion() &&
|
2010-08-31 04:36:30 +04:00
|
|
|
Parent->getRedeclContext()->isFunctionOrMethod())
|
2010-05-21 04:31:19 +04:00
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
|
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());
|
2010-01-21 00:53:11 +03:00
|
|
|
Field->setAccess(D->getAccess());
|
2009-09-02 23:17:55 +04:00
|
|
|
Owner->addDecl(Field);
|
2009-03-18 00:15:40 +03:00
|
|
|
|
|
|
|
return Field;
|
|
|
|
}
|
|
|
|
|
2010-11-21 09:08:52 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
|
|
|
|
NamedDecl **NamedChain =
|
|
|
|
new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (IndirectFieldDecl::chain_iterator PI =
|
|
|
|
D->chain_begin(), PE = D->chain_end();
|
2011-03-04 22:46:35 +03:00
|
|
|
PI != PE; ++PI) {
|
|
|
|
NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
|
|
|
|
TemplateArgs);
|
|
|
|
if (!Next)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
NamedChain[i++] = Next;
|
|
|
|
}
|
2010-11-21 09:08:52 +03:00
|
|
|
|
2010-12-09 13:07:54 +03:00
|
|
|
QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
|
2010-11-21 09:08:52 +03:00
|
|
|
IndirectFieldDecl* IndirectField
|
|
|
|
= IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
2010-12-09 13:07:54 +03:00
|
|
|
D->getIdentifier(), T,
|
2010-11-21 09:08:52 +03:00
|
|
|
NamedChain, D->getChainingSize());
|
|
|
|
|
|
|
|
|
|
|
|
IndirectField->setImplicit(D->isImplicit());
|
|
|
|
IndirectField->setAccess(D->getAccess());
|
|
|
|
Owner->addDecl(IndirectField);
|
|
|
|
return IndirectField;
|
|
|
|
}
|
|
|
|
|
2009-08-28 11:59:38 +04:00
|
|
|
Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
|
|
|
|
// Handle friend type expressions by simply substituting template
|
2010-04-07 21:57:12 +04:00
|
|
|
// parameters into the pattern type and checking the result.
|
2010-03-25 21:04:51 +03:00
|
|
|
if (TypeSourceInfo *Ty = D->getFriendType()) {
|
|
|
|
TypeSourceInfo *InstTy =
|
|
|
|
SemaRef.SubstType(Ty, TemplateArgs,
|
|
|
|
D->getLocation(), DeclarationName());
|
2010-04-07 21:57:12 +04:00
|
|
|
if (!InstTy)
|
|
|
|
return 0;
|
2009-08-14 06:03:10 +04:00
|
|
|
|
2010-04-07 21:57:12 +04:00
|
|
|
FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
|
|
|
|
if (!FD)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
FD->setAccess(AS_public);
|
2010-10-19 05:40:49 +04:00
|
|
|
FD->setUnsupportedFriend(D->isUnsupportedFriend());
|
2010-04-07 21:57:12 +04:00
|
|
|
Owner->addDecl(FD);
|
|
|
|
return FD;
|
|
|
|
}
|
|
|
|
|
|
|
|
NamedDecl *ND = D->getFriendDecl();
|
|
|
|
assert(ND && "friend decl must be a decl or a type!");
|
2010-03-25 21:04:51 +03:00
|
|
|
|
2010-04-08 13:05:18 +04:00
|
|
|
// All of the Visit implementations for the various potential friend
|
|
|
|
// declarations have to be carefully written to work for friend
|
|
|
|
// objects, with the most important detail being that the target
|
|
|
|
// decl should almost certainly not be placed in Owner.
|
|
|
|
Decl *NewND = Visit(ND);
|
2010-04-07 21:57:12 +04:00
|
|
|
if (!NewND) return 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-28 11:59:38 +04:00
|
|
|
FriendDecl *FD =
|
2010-04-07 21:57:12 +04:00
|
|
|
FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
|
|
|
cast<NamedDecl>(NewND), D->getFriendLoc());
|
2009-08-29 07:50:18 +04:00
|
|
|
FD->setAccess(AS_public);
|
2010-10-19 05:40:49 +04:00
|
|
|
FD->setUnsupportedFriend(D->isUnsupportedFriend());
|
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.
|
2010-08-27 03:41:50 +04:00
|
|
|
EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-24 10:29:42 +04:00
|
|
|
ExprResult 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;
|
|
|
|
|
2010-08-24 10:29:42 +04:00
|
|
|
ExprResult Message(D->getMessage());
|
2010-10-26 11:05:15 +04:00
|
|
|
D->getMessage();
|
2010-08-21 13:40:31 +04:00
|
|
|
return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
|
2010-08-24 03:25:46 +04:00
|
|
|
InstantiatedAssertExpr.get(),
|
|
|
|
Message.get());
|
2009-03-18 00:15:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
2010-12-03 21:54:17 +03:00
|
|
|
/*PrevDecl=*/0, D->isScoped(),
|
|
|
|
D->isScopedUsingClassTag(), D->isFixed());
|
2010-10-09 03:50:27 +04:00
|
|
|
if (D->isFixed()) {
|
|
|
|
if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
|
|
|
|
// If we have type source information for the underlying type, it means it
|
|
|
|
// has been explicitly set by the user. Perform substitution on it before
|
|
|
|
// moving on.
|
|
|
|
SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
|
|
|
|
Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
|
|
|
|
TemplateArgs,
|
|
|
|
UnderlyingLoc,
|
|
|
|
DeclarationName()));
|
|
|
|
|
|
|
|
if (!Enum->getIntegerTypeSourceInfo())
|
|
|
|
Enum->setIntegerType(SemaRef.Context.IntTy);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(!D->getIntegerType()->isDependentType()
|
|
|
|
&& "Dependent type without type source info");
|
|
|
|
Enum->setIntegerType(D->getIntegerType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-23 03:36:17 +04:00
|
|
|
SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
|
|
|
|
|
2009-05-27 21:20:35 +04:00
|
|
|
Enum->setInstantiationOfMemberEnum(D);
|
2009-03-26 01:00:53 +03:00
|
|
|
Enum->setAccess(D->getAccess());
|
2010-03-15 13:12:16 +03:00
|
|
|
if (SubstQualifier(D, Enum)) return 0;
|
2009-06-30 06:36:12 +04:00
|
|
|
Owner->addDecl(Enum);
|
2009-03-18 00:15:40 +03:00
|
|
|
Enum->startDefinition();
|
|
|
|
|
2010-03-01 22:00:07 +03:00
|
|
|
if (D->getDeclContext()->isFunctionOrMethod())
|
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
|
|
|
|
|
2010-08-21 13:40:31 +04:00
|
|
|
llvm::SmallVector<Decl*, 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.
|
2010-08-24 10:29:42 +04:00
|
|
|
ExprResult 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,
|
2010-08-27 03:41:50 +04:00
|
|
|
Sema::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(),
|
2010-08-24 03:25:46 +04:00
|
|
|
Value.get());
|
2009-03-18 00:15:40 +03:00
|
|
|
|
|
|
|
if (isInvalid) {
|
|
|
|
if (EnumConst)
|
|
|
|
EnumConst->setInvalidDecl();
|
|
|
|
Enum->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EnumConst) {
|
2010-10-23 03:36:17 +04:00
|
|
|
SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
|
|
|
|
|
2010-01-24 01:37:59 +03:00
|
|
|
EnumConst->setAccess(Enum->getAccess());
|
2009-06-30 06:36:12 +04:00
|
|
|
Enum->addDecl(EnumConst);
|
2010-08-21 13:40:31 +04:00
|
|
|
Enumerators.push_back(EnumConst);
|
2009-03-18 00:15:40 +03:00
|
|
|
LastEnumConst = EnumConst;
|
2010-03-01 22:00:07 +03:00
|
|
|
|
|
|
|
if (D->getDeclContext()->isFunctionOrMethod()) {
|
|
|
|
// If the enumeration is within a function or method, record the enum
|
|
|
|
// constant as a local.
|
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
|
|
|
|
}
|
2009-03-18 00:15:40 +03:00
|
|
|
}
|
|
|
|
}
|
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(),
|
2010-08-21 13:40:31 +04:00
|
|
|
Enum,
|
2010-08-15 06:27:09 +04:00
|
|
|
Enumerators.data(), Enumerators.size(),
|
2009-08-08 18:36:57 +04:00
|
|
|
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-08-20 05:44:21 +04:00
|
|
|
Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
|
2010-03-25 09:39:04 +03:00
|
|
|
bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
|
|
|
|
|
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.
|
2010-08-25 09:32:35 +04:00
|
|
|
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();
|
2010-03-25 09:39:04 +03:00
|
|
|
|
|
|
|
// Instantiate the qualifier. We have to do this first in case
|
|
|
|
// we're a friend declaration, because if we are then we need to put
|
|
|
|
// the new declaration in the appropriate context.
|
2011-02-25 05:25:35 +03:00
|
|
|
NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
|
|
|
|
if (QualifierLoc) {
|
|
|
|
QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
|
|
|
|
TemplateArgs);
|
|
|
|
if (!QualifierLoc)
|
|
|
|
return 0;
|
2010-03-25 09:39:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CXXRecordDecl *PrevDecl = 0;
|
|
|
|
ClassTemplateDecl *PrevClassTemplate = 0;
|
|
|
|
|
2010-11-09 02:29:42 +03:00
|
|
|
if (!isFriend && Pattern->getPreviousDeclaration()) {
|
|
|
|
DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
|
|
|
|
if (Found.first != Found.second) {
|
|
|
|
PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
|
|
|
|
if (PrevClassTemplate)
|
|
|
|
PrevDecl = PrevClassTemplate->getTemplatedDecl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:39:04 +03:00
|
|
|
// If this isn't a friend, then it's a member template, in which
|
|
|
|
// case we just want to build the instantiation in the
|
|
|
|
// specialization. If it is a friend, we want to build it in
|
|
|
|
// the appropriate context.
|
|
|
|
DeclContext *DC = Owner;
|
|
|
|
if (isFriend) {
|
2011-02-25 05:25:35 +03:00
|
|
|
if (QualifierLoc) {
|
2010-03-25 09:39:04 +03:00
|
|
|
CXXScopeSpec SS;
|
2011-02-25 05:25:35 +03:00
|
|
|
SS.Adopt(QualifierLoc);
|
2010-03-25 09:39:04 +03:00
|
|
|
DC = SemaRef.computeDeclContext(SS);
|
|
|
|
if (!DC) return 0;
|
|
|
|
} else {
|
|
|
|
DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
|
|
|
|
Pattern->getDeclContext(),
|
|
|
|
TemplateArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for a previous declaration of the template in the owning
|
|
|
|
// context.
|
|
|
|
LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
|
|
|
|
Sema::LookupOrdinaryName, Sema::ForRedeclaration);
|
|
|
|
SemaRef.LookupQualifiedName(R, DC);
|
|
|
|
|
|
|
|
if (R.isSingleResult()) {
|
|
|
|
PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
|
|
|
|
if (PrevClassTemplate)
|
|
|
|
PrevDecl = PrevClassTemplate->getTemplatedDecl();
|
|
|
|
}
|
|
|
|
|
2011-02-25 05:25:35 +03:00
|
|
|
if (!PrevClassTemplate && QualifierLoc) {
|
2010-03-25 09:39:04 +03:00
|
|
|
SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
|
2010-04-01 03:17:41 +04:00
|
|
|
<< D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
|
2011-02-25 05:25:35 +03:00
|
|
|
<< QualifierLoc.getSourceRange();
|
2010-03-25 09:39:04 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-08 22:16:15 +04:00
|
|
|
bool AdoptedPreviousTemplateParams = false;
|
2010-03-25 09:39:04 +03:00
|
|
|
if (PrevClassTemplate) {
|
2010-04-08 22:16:15 +04:00
|
|
|
bool Complain = true;
|
|
|
|
|
|
|
|
// HACK: libstdc++ 4.2.1 contains an ill-formed friend class
|
|
|
|
// template for struct std::tr1::__detail::_Map_base, where the
|
|
|
|
// template parameters of the friend declaration don't match the
|
|
|
|
// template parameters of the original declaration. In this one
|
|
|
|
// case, we don't complain about the ill-formed friend
|
|
|
|
// declaration.
|
|
|
|
if (isFriend && Pattern->getIdentifier() &&
|
|
|
|
Pattern->getIdentifier()->isStr("_Map_base") &&
|
|
|
|
DC->isNamespace() &&
|
|
|
|
cast<NamespaceDecl>(DC)->getIdentifier() &&
|
|
|
|
cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
|
|
|
|
DeclContext *DCParent = DC->getParent();
|
|
|
|
if (DCParent->isNamespace() &&
|
|
|
|
cast<NamespaceDecl>(DCParent)->getIdentifier() &&
|
|
|
|
cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
|
|
|
|
DeclContext *DCParent2 = DCParent->getParent();
|
|
|
|
if (DCParent2->isNamespace() &&
|
|
|
|
cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
|
|
|
|
cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
|
|
|
|
DCParent2->getParent()->isTranslationUnit())
|
|
|
|
Complain = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:39:04 +03:00
|
|
|
TemplateParameterList *PrevParams
|
|
|
|
= PrevClassTemplate->getTemplateParameters();
|
|
|
|
|
|
|
|
// Make sure the parameter lists match.
|
|
|
|
if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
|
2010-04-08 22:16:15 +04:00
|
|
|
Complain,
|
|
|
|
Sema::TPL_TemplateMatch)) {
|
|
|
|
if (Complain)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
AdoptedPreviousTemplateParams = true;
|
|
|
|
InstParams = PrevParams;
|
|
|
|
}
|
2010-03-25 09:39:04 +03:00
|
|
|
|
|
|
|
// Do some additional validation, then merge default arguments
|
|
|
|
// from the existing declarations.
|
2010-04-08 22:16:15 +04:00
|
|
|
if (!AdoptedPreviousTemplateParams &&
|
|
|
|
SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
|
2010-03-25 09:39:04 +03:00
|
|
|
Sema::TPC_ClassTemplate))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:44:21 +04:00
|
|
|
CXXRecordDecl *RecordInst
|
2010-03-25 09:39:04 +03:00
|
|
|
= CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
|
2009-08-20 05:44:21 +04:00
|
|
|
Pattern->getLocation(), Pattern->getIdentifier(),
|
2010-03-25 09:39:04 +03:00
|
|
|
Pattern->getTagKeywordLoc(), PrevDecl,
|
2009-10-13 03:11:44 +04:00
|
|
|
/*DelayTypeCreation=*/true);
|
2009-08-20 05:44:21 +04:00
|
|
|
|
2011-02-25 05:25:35 +03:00
|
|
|
if (QualifierLoc)
|
|
|
|
RecordInst->setQualifierInfo(QualifierLoc);
|
2010-03-15 13:12:16 +03:00
|
|
|
|
2009-08-20 05:44:21 +04:00
|
|
|
ClassTemplateDecl *Inst
|
2010-03-25 09:39:04 +03:00
|
|
|
= ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
|
|
|
|
D->getIdentifier(), InstParams, RecordInst,
|
|
|
|
PrevClassTemplate);
|
2009-08-20 05:44:21 +04:00
|
|
|
RecordInst->setDescribedClassTemplate(Inst);
|
2010-04-09 00:25:50 +04:00
|
|
|
|
2010-03-25 09:39:04 +03:00
|
|
|
if (isFriend) {
|
2010-04-09 00:25:50 +04:00
|
|
|
if (PrevClassTemplate)
|
|
|
|
Inst->setAccess(PrevClassTemplate->getAccess());
|
|
|
|
else
|
|
|
|
Inst->setAccess(D->getAccess());
|
|
|
|
|
2010-03-25 09:39:04 +03:00
|
|
|
Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
|
|
|
|
// TODO: do we want to track the instantiation progeny of this
|
|
|
|
// friend target decl?
|
|
|
|
} else {
|
2009-10-31 00:07:27 +03:00
|
|
|
Inst->setAccess(D->getAccess());
|
2010-11-09 02:29:42 +03:00
|
|
|
if (!PrevClassTemplate)
|
|
|
|
Inst->setInstantiatedFromMemberTemplate(D);
|
2010-03-25 09:39:04 +03:00
|
|
|
}
|
2009-10-13 03:11:44 +04:00
|
|
|
|
|
|
|
// Trigger creation of the type for the instantiation.
|
2010-03-10 06:28:59 +03:00
|
|
|
SemaRef.Context.getInjectedClassNameType(RecordInst,
|
2010-07-08 22:37:38 +04:00
|
|
|
Inst->getInjectedClassNameSpecialization());
|
2010-04-09 00:25:50 +04:00
|
|
|
|
2009-10-31 01:42:42 +03:00
|
|
|
// Finish handling of friends.
|
2010-03-25 09:39:04 +03:00
|
|
|
if (isFriend) {
|
|
|
|
DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
|
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);
|
2010-11-10 22:44:59 +03:00
|
|
|
|
|
|
|
if (!PrevClassTemplate) {
|
|
|
|
// Queue up any out-of-line partial specializations of this member
|
|
|
|
// class template; the client will force their instantiation once
|
|
|
|
// the enclosing class has been instantiated.
|
|
|
|
llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
|
|
|
|
D->getPartialSpecializations(PartialSpecs);
|
|
|
|
for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
|
|
|
|
if (PartialSpecs[I]->isOutOfLine())
|
|
|
|
OutOfLinePartialSpecs.push_back(std::make_pair(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;
|
|
|
|
|
2010-11-10 22:44:59 +03:00
|
|
|
if (ClassTemplatePartialSpecializationDecl *Result
|
|
|
|
= InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
|
2009-10-07 21:21:34 +04:00
|
|
|
}
|
|
|
|
|
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.
|
2010-08-25 09:32:35 +04:00
|
|
|
LocalInstantiationScope Scope(SemaRef);
|
2010-04-30 22:55:50 +04:00
|
|
|
|
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;
|
|
|
|
|
2010-01-21 00:53:11 +03:00
|
|
|
Instantiated->setAccess(D->getAccess());
|
|
|
|
|
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!");
|
2009-12-15 02:19:40 +03:00
|
|
|
|
2010-03-27 02:10:15 +03:00
|
|
|
bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
|
|
|
|
|
2009-12-15 02:19:40 +03:00
|
|
|
// Link the instantiation back to the pattern *unless* this is a
|
|
|
|
// non-definition friend declaration.
|
|
|
|
if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
|
2010-03-27 02:10:15 +03:00
|
|
|
!(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
|
2009-10-13 18:39:41 +04:00
|
|
|
InstTemplate->setInstantiatedFromMemberTemplate(D);
|
|
|
|
|
2010-03-27 02:10:15 +03:00
|
|
|
// Make declarations visible in the appropriate context.
|
|
|
|
if (!isFriend)
|
2009-10-13 18:39:41 +04:00
|
|
|
Owner->addDecl(InstTemplate);
|
2010-03-27 02:10:15 +03:00
|
|
|
|
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);
|
2009-12-16 01:29:06 +03:00
|
|
|
else if (D->getPreviousDeclaration()) {
|
2010-03-01 18:56:25 +03:00
|
|
|
NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
|
|
|
|
D->getPreviousDeclaration(),
|
2009-12-16 01:29:06 +03:00
|
|
|
TemplateArgs);
|
|
|
|
if (!Prev) return 0;
|
|
|
|
PrevDecl = cast<CXXRecordDecl>(Prev);
|
|
|
|
}
|
2009-03-26 00:17:03 +03:00
|
|
|
|
|
|
|
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);
|
2010-03-15 13:12:16 +03:00
|
|
|
|
|
|
|
// Substitute the nested name specifier, if any.
|
|
|
|
if (SubstQualifier(D, Record))
|
|
|
|
return 0;
|
|
|
|
|
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);
|
|
|
|
|
2010-05-21 04:31:19 +04:00
|
|
|
// Make sure that anonymous structs and unions are recorded.
|
|
|
|
if (D->isAnonymousStructOrUnion()) {
|
|
|
|
Record->setAnonymousStructOrUnion(true);
|
2010-08-31 04:36:30 +04:00
|
|
|
if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
|
2010-05-21 04:31:19 +04:00
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
|
|
|
|
}
|
2009-09-01 08:26:58 +04:00
|
|
|
|
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-12-24 23:56:24 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
|
2009-10-13 18:39:41 +04:00
|
|
|
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;
|
2010-03-27 08:57:59 +03:00
|
|
|
if (FunctionTemplate && !TemplateParams) {
|
2010-07-08 22:37:38 +04:00
|
|
|
std::pair<const TemplateArgument *, unsigned> Innermost
|
|
|
|
= TemplateArgs.getInnermost();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-07-20 17:59:58 +04:00
|
|
|
FunctionDecl *SpecFunc
|
|
|
|
= FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
|
|
|
|
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.
|
2010-07-20 17:59:58 +04:00
|
|
|
if (SpecFunc)
|
|
|
|
return SpecFunc;
|
2009-06-30 00:59:39 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-03-27 08:57:59 +03:00
|
|
|
bool isFriend;
|
|
|
|
if (FunctionTemplate)
|
|
|
|
isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
|
|
|
|
else
|
|
|
|
isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
|
|
|
|
|
2010-01-16 23:21:20 +03:00
|
|
|
bool MergeWithParentScope = (TemplateParams != 0) ||
|
2010-05-22 01:25:08 +04:00
|
|
|
Owner->isFunctionOrMethod() ||
|
2010-01-16 23:21:20 +03:00
|
|
|
!(isa<Decl>(Owner) &&
|
|
|
|
cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
|
2010-08-25 09:32:35 +04:00
|
|
|
LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-26 02:08:12 +04:00
|
|
|
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
2010-03-11 12:03:00 +03:00
|
|
|
TypeSourceInfo *TInfo = D->getTypeSourceInfo();
|
|
|
|
TInfo = SubstFunctionType(D, Params);
|
|
|
|
if (!TInfo)
|
2009-03-24 02:06:20 +03:00
|
|
|
return 0;
|
2010-03-11 12:03:00 +03:00
|
|
|
QualType T = TInfo->getType();
|
2009-08-14 06:03:10 +04:00
|
|
|
|
2011-02-25 05:25:35 +03:00
|
|
|
NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
|
|
|
|
if (QualifierLoc) {
|
|
|
|
QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
|
|
|
|
TemplateArgs);
|
|
|
|
if (!QualifierLoc)
|
|
|
|
return 0;
|
2010-03-26 07:53:08 +03:00
|
|
|
}
|
|
|
|
|
2010-02-06 04:50:47 +03:00
|
|
|
// If we're instantiating a local function declaration, put the result
|
|
|
|
// in the owner; otherwise we need to find the instantiated context.
|
|
|
|
DeclContext *DC;
|
|
|
|
if (D->getDeclContext()->isFunctionOrMethod())
|
|
|
|
DC = Owner;
|
2011-02-25 05:25:35 +03:00
|
|
|
else if (isFriend && QualifierLoc) {
|
2010-03-26 07:53:08 +03:00
|
|
|
CXXScopeSpec SS;
|
2011-02-25 05:25:35 +03:00
|
|
|
SS.Adopt(QualifierLoc);
|
2010-03-26 07:53:08 +03:00
|
|
|
DC = SemaRef.computeDeclContext(SS);
|
|
|
|
if (!DC) return 0;
|
|
|
|
} else {
|
2010-03-01 18:56:25 +03:00
|
|
|
DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
|
|
|
|
TemplateArgs);
|
2010-03-26 07:53:08 +03:00
|
|
|
}
|
2010-02-06 04:50:47 +03:00
|
|
|
|
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(),
|
2010-03-11 12:03:00 +03:00
|
|
|
D->getDeclName(), T, TInfo,
|
2010-04-20 02:54:31 +04:00
|
|
|
D->getStorageClass(), D->getStorageClassAsWritten(),
|
2009-10-28 00:01:01 +03:00
|
|
|
D->isInlineSpecified(), D->hasWrittenPrototype());
|
2010-03-15 13:12:16 +03:00
|
|
|
|
2011-02-25 05:25:35 +03:00
|
|
|
if (QualifierLoc)
|
|
|
|
Function->setQualifierInfo(QualifierLoc);
|
2010-03-15 13:12:16 +03:00
|
|
|
|
2010-03-27 02:10:15 +03:00
|
|
|
DeclContext *LexicalDC = Owner;
|
|
|
|
if (!isFriend && D->isOutOfLine()) {
|
|
|
|
assert(D->getDeclContext()->isFileContext());
|
|
|
|
LexicalDC = D->getDeclContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
Function->setLexicalDeclContext(LexicalDC);
|
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)
|
2010-09-17 04:50:28 +04:00
|
|
|
if (Params[P])
|
|
|
|
Params[P]->setOwningFunction(Function);
|
2010-02-11 04:19:42 +03:00
|
|
|
Function->setParams(Params.data(), Params.size());
|
2009-08-28 11:59:38 +04:00
|
|
|
|
2010-05-17 20:38:00 +04:00
|
|
|
SourceLocation InstantiateAtPOI;
|
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.
|
2010-03-26 07:53:08 +03:00
|
|
|
FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
|
2009-10-13 18:39:41 +04:00
|
|
|
Function->getLocation(),
|
|
|
|
Function->getDeclName(),
|
|
|
|
TemplateParams, Function);
|
|
|
|
Function->setDescribedFunctionTemplate(FunctionTemplate);
|
2010-03-27 02:10:15 +03:00
|
|
|
|
|
|
|
FunctionTemplate->setLexicalDeclContext(LexicalDC);
|
2010-03-26 07:53:08 +03:00
|
|
|
|
|
|
|
if (isFriend && D->isThisDeclarationADefinition()) {
|
|
|
|
// TODO: should we remember this connection regardless of whether
|
|
|
|
// the friend declaration provided a body?
|
|
|
|
FunctionTemplate->setInstantiatedFromMemberTemplate(
|
|
|
|
D->getDescribedFunctionTemplate());
|
|
|
|
}
|
2009-11-14 04:20:54 +03:00
|
|
|
} else if (FunctionTemplate) {
|
|
|
|
// Record this function template specialization.
|
2010-07-08 22:37:38 +04:00
|
|
|
std::pair<const TemplateArgument *, unsigned> Innermost
|
|
|
|
= TemplateArgs.getInnermost();
|
2010-02-11 04:19:42 +03:00
|
|
|
Function->setFunctionTemplateSpecialization(FunctionTemplate,
|
2010-11-08 02:05:16 +03:00
|
|
|
TemplateArgumentList::CreateCopy(SemaRef.Context,
|
2010-07-08 22:37:38 +04:00
|
|
|
Innermost.first,
|
|
|
|
Innermost.second),
|
2009-11-14 04:20:54 +03:00
|
|
|
InsertPos);
|
2010-03-26 07:53:08 +03:00
|
|
|
} else if (isFriend && D->isThisDeclarationADefinition()) {
|
|
|
|
// TODO: should we remember this connection regardless of whether
|
|
|
|
// the friend declaration provided a body?
|
|
|
|
Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
|
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;
|
2010-04-08 13:05:18 +04:00
|
|
|
bool isExplicitSpecialization = 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);
|
|
|
|
|
2010-04-08 13:05:18 +04:00
|
|
|
if (DependentFunctionTemplateSpecializationInfo *Info
|
|
|
|
= D->getDependentSpecializationInfo()) {
|
|
|
|
assert(isFriend && "non-friend has dependent specialization info?");
|
|
|
|
|
|
|
|
// This needs to be set now for future sanity.
|
|
|
|
Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
|
|
|
|
|
|
|
|
// Instantiate the explicit template arguments.
|
|
|
|
TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
|
|
|
|
Info->getRAngleLoc());
|
2010-12-23 00:19:48 +03:00
|
|
|
if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
|
|
|
|
ExplicitArgs, TemplateArgs))
|
|
|
|
return 0;
|
2010-04-08 13:05:18 +04:00
|
|
|
|
|
|
|
// Map the candidate templates to their instantiations.
|
|
|
|
for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
|
|
|
|
Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
|
|
|
|
Info->getTemplate(I),
|
|
|
|
TemplateArgs);
|
|
|
|
if (!Temp) return 0;
|
|
|
|
|
|
|
|
Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SemaRef.CheckFunctionTemplateSpecialization(Function,
|
|
|
|
&ExplicitArgs,
|
|
|
|
Previous))
|
|
|
|
Function->setInvalidDecl();
|
|
|
|
|
|
|
|
isExplicitSpecialization = true;
|
|
|
|
|
|
|
|
} else if (TemplateParams || !FunctionTemplate) {
|
2009-10-13 18:39:41 +04:00
|
|
|
// 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-12-10 12:41:52 +03:00
|
|
|
SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
|
2011-01-21 05:08:54 +03:00
|
|
|
isExplicitSpecialization, Redeclaration);
|
2009-06-26 02:08:12 +04:00
|
|
|
|
2010-04-24 05:30:58 +04:00
|
|
|
NamedDecl *PrincipalDecl = (TemplateParams
|
|
|
|
? cast<NamedDecl>(FunctionTemplate)
|
|
|
|
: Function);
|
|
|
|
|
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.
|
2010-03-26 07:53:08 +03:00
|
|
|
if (isFriend) {
|
2009-11-19 01:49:29 +03:00
|
|
|
NamedDecl *PrevDecl;
|
2010-04-24 05:30:58 +04:00
|
|
|
if (TemplateParams)
|
2009-10-13 18:39:41 +04:00
|
|
|
PrevDecl = FunctionTemplate->getPreviousDeclaration();
|
2010-04-24 05:30:58 +04:00
|
|
|
else
|
2009-10-13 18:39:41 +04:00
|
|
|
PrevDecl = Function->getPreviousDeclaration();
|
2010-04-24 05:30:58 +04:00
|
|
|
|
|
|
|
PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
|
|
|
|
DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
|
2010-08-31 01:10:05 +04:00
|
|
|
|
2010-08-31 02:25:56 +04:00
|
|
|
bool queuedInstantiation = false;
|
2010-08-31 01:10:05 +04:00
|
|
|
|
2010-05-18 09:45:02 +04:00
|
|
|
if (!SemaRef.getLangOptions().CPlusPlus0x &&
|
|
|
|
D->isThisDeclarationADefinition()) {
|
|
|
|
// Check for a function body.
|
|
|
|
const FunctionDecl *Definition = 0;
|
2010-07-07 15:31:19 +04:00
|
|
|
if (Function->hasBody(Definition) &&
|
2010-05-18 09:45:02 +04:00
|
|
|
Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
|
|
|
|
SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
|
|
|
|
<< Function->getDeclName();
|
|
|
|
SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
|
|
|
|
Function->setInvalidDecl();
|
|
|
|
}
|
|
|
|
// Check for redefinitions due to other instantiations of this or
|
|
|
|
// a similar friend function.
|
|
|
|
else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
|
|
|
|
REnd = Function->redecls_end();
|
|
|
|
R != REnd; ++R) {
|
2010-08-28 19:42:30 +04:00
|
|
|
if (*R == Function)
|
|
|
|
continue;
|
2010-08-31 01:10:05 +04:00
|
|
|
switch (R->getFriendObjectKind()) {
|
|
|
|
case Decl::FOK_None:
|
|
|
|
if (!queuedInstantiation && R->isUsed(false)) {
|
|
|
|
if (MemberSpecializationInfo *MSInfo
|
|
|
|
= Function->getMemberSpecializationInfo()) {
|
|
|
|
if (MSInfo->getPointOfInstantiation().isInvalid()) {
|
|
|
|
SourceLocation Loc = R->getLocation(); // FIXME
|
|
|
|
MSInfo->setPointOfInstantiation(Loc);
|
|
|
|
SemaRef.PendingLocalImplicitInstantiations.push_back(
|
|
|
|
std::make_pair(Function, Loc));
|
|
|
|
queuedInstantiation = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2010-05-18 09:45:02 +04:00
|
|
|
if (const FunctionDecl *RPattern
|
2010-08-28 19:46:56 +04:00
|
|
|
= R->getTemplateInstantiationPattern())
|
2010-07-07 15:31:19 +04:00
|
|
|
if (RPattern->hasBody(RPattern)) {
|
2010-05-18 09:45:02 +04:00
|
|
|
SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
|
|
|
|
<< Function->getDeclName();
|
2010-08-28 19:46:56 +04:00
|
|
|
SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
|
2010-05-18 09:45:02 +04:00
|
|
|
Function->setInvalidDecl();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-13 18:39:41 +04:00
|
|
|
}
|
|
|
|
|
2010-04-24 05:30:58 +04:00
|
|
|
if (Function->isOverloadedOperator() && !DC->isRecord() &&
|
|
|
|
PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
|
|
|
|
PrincipalDecl->setNonMemberOperator();
|
|
|
|
|
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.
|
2010-07-08 22:37:38 +04:00
|
|
|
std::pair<const TemplateArgument *, unsigned> Innermost
|
|
|
|
= TemplateArgs.getInnermost();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-07-20 17:59:58 +04:00
|
|
|
FunctionDecl *SpecFunc
|
|
|
|
= FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
|
|
|
|
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.
|
2010-07-20 17:59:58 +04:00
|
|
|
if (SpecFunc)
|
|
|
|
return SpecFunc;
|
2009-08-21 04:16:32 +04:00
|
|
|
}
|
|
|
|
|
2010-03-27 08:57:59 +03:00
|
|
|
bool isFriend;
|
|
|
|
if (FunctionTemplate)
|
|
|
|
isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
|
|
|
|
else
|
|
|
|
isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
|
|
|
|
|
2010-01-16 23:21:20 +03:00
|
|
|
bool MergeWithParentScope = (TemplateParams != 0) ||
|
|
|
|
!(isa<Decl>(Owner) &&
|
|
|
|
cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
|
2010-08-25 09:32:35 +04:00
|
|
|
LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
|
2009-05-15 01:44:34 +04:00
|
|
|
|
2010-10-19 06:26:41 +04:00
|
|
|
// Instantiate enclosing template arguments for friends.
|
|
|
|
llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
|
|
|
|
unsigned NumTempParamLists = 0;
|
|
|
|
if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
|
|
|
|
TempParamLists.set_size(NumTempParamLists);
|
|
|
|
for (unsigned I = 0; I != NumTempParamLists; ++I) {
|
|
|
|
TemplateParameterList *TempParams = D->getTemplateParameterList(I);
|
|
|
|
TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
|
|
|
|
if (!InstParams)
|
|
|
|
return NULL;
|
|
|
|
TempParamLists[I] = InstParams;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-29 22:27:38 +04:00
|
|
|
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
2010-03-11 12:03:00 +03:00
|
|
|
TypeSourceInfo *TInfo = D->getTypeSourceInfo();
|
|
|
|
TInfo = SubstFunctionType(D, Params);
|
|
|
|
if (!TInfo)
|
2009-03-24 02:06:20 +03:00
|
|
|
return 0;
|
2010-03-11 12:03:00 +03:00
|
|
|
QualType T = TInfo->getType();
|
2009-03-24 02:06:20 +03:00
|
|
|
|
2010-12-15 01:11:44 +03:00
|
|
|
// \brief If the type of this function, after ignoring parentheses,
|
|
|
|
// is not *directly* a function type, then we're instantiating a function
|
|
|
|
// that was declared via a typedef, e.g.,
|
2010-05-04 22:18:31 +04:00
|
|
|
//
|
|
|
|
// typedef int functype(int, int);
|
|
|
|
// functype func;
|
|
|
|
//
|
|
|
|
// In this case, we'll just go instantiate the ParmVarDecls that we
|
|
|
|
// synthesized in the method declaration.
|
2010-12-15 01:11:44 +03:00
|
|
|
if (!isa<FunctionProtoType>(T.IgnoreParens())) {
|
2010-05-04 22:18:31 +04:00
|
|
|
assert(!Params.size() && "Instantiating type could not yield parameters");
|
Implement substitution of a function parameter pack for its set of
instantiated function parameters, enabling instantiation of arbitrary
pack expansions involving function parameter packs. At this point, we
can now correctly compile a simple, variadic print() example:
#include <iostream>
#include <string>
void print() {}
template<typename Head, typename ...Tail>
void print(const Head &head, const Tail &...tail) {
std::cout << head;
print(tail...);
}
int main() {
std::string hello = "Hello";
print(hello, ", world!", " ", 2011, '\n');
}
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123000 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-07 19:43:16 +03:00
|
|
|
llvm::SmallVector<QualType, 4> ParamTypes;
|
|
|
|
if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
|
|
|
|
D->getNumParams(), TemplateArgs, ParamTypes,
|
|
|
|
&Params))
|
|
|
|
return 0;
|
2010-05-04 22:18:31 +04:00
|
|
|
}
|
|
|
|
|
2011-02-25 05:25:35 +03:00
|
|
|
NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
|
|
|
|
if (QualifierLoc) {
|
|
|
|
QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
|
2010-03-27 08:57:59 +03:00
|
|
|
TemplateArgs);
|
2011-02-25 05:25:35 +03:00
|
|
|
if (!QualifierLoc)
|
|
|
|
return 0;
|
2010-03-27 08:57:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
DeclContext *DC = Owner;
|
|
|
|
if (isFriend) {
|
2011-02-25 05:25:35 +03:00
|
|
|
if (QualifierLoc) {
|
2010-03-27 08:57:59 +03:00
|
|
|
CXXScopeSpec SS;
|
2011-02-25 05:25:35 +03:00
|
|
|
SS.Adopt(QualifierLoc);
|
2010-03-27 08:57:59 +03:00
|
|
|
DC = SemaRef.computeDeclContext(SS);
|
2010-10-19 09:01:53 +04:00
|
|
|
|
|
|
|
if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
|
|
|
|
return 0;
|
2010-03-27 08:57:59 +03:00
|
|
|
} else {
|
|
|
|
DC = SemaRef.FindInstantiatedContext(D->getLocation(),
|
|
|
|
D->getDeclContext(),
|
|
|
|
TemplateArgs);
|
|
|
|
}
|
|
|
|
if (!DC) return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-24 02:06:20 +03:00
|
|
|
// Build the instantiated method declaration.
|
2010-03-27 08:57:59 +03:00
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
|
2009-08-21 22:42:58 +04:00
|
|
|
CXXMethodDecl *Method = 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-12 02:01:17 +04:00
|
|
|
DeclarationNameInfo NameInfo
|
|
|
|
= SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
|
2009-08-22 02:43:28 +04:00
|
|
|
if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
|
2010-08-12 02:01:17 +04:00
|
|
|
NameInfo, T, TInfo,
|
2009-09-09 19:08:12 +04:00
|
|
|
Constructor->isExplicit(),
|
2010-04-20 02:54:31 +04:00
|
|
|
Constructor->isInlineSpecified(),
|
|
|
|
false);
|
2009-08-22 02:43:28 +04:00
|
|
|
} else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
|
|
|
|
Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
|
2010-10-21 04:44:50 +04:00
|
|
|
NameInfo, T, TInfo,
|
2010-08-12 02:01:17 +04:00
|
|
|
Destructor->isInlineSpecified(),
|
2010-04-20 02:54:31 +04:00
|
|
|
false);
|
2009-08-22 03:19:43 +04:00
|
|
|
} else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
|
|
|
|
Method = CXXConversionDecl::Create(SemaRef.Context, Record,
|
2010-08-12 02:01:17 +04:00
|
|
|
NameInfo, T, TInfo,
|
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 {
|
2010-08-12 02:01:17 +04:00
|
|
|
Method = CXXMethodDecl::Create(SemaRef.Context, Record,
|
|
|
|
NameInfo, T, TInfo,
|
2010-04-20 02:54:31 +04:00
|
|
|
D->isStatic(),
|
|
|
|
D->getStorageClassAsWritten(),
|
|
|
|
D->isInlineSpecified());
|
2009-08-21 22:42:58 +04:00
|
|
|
}
|
2009-08-21 04:16:32 +04:00
|
|
|
|
2011-02-25 05:25:35 +03:00
|
|
|
if (QualifierLoc)
|
|
|
|
Method->setQualifierInfo(QualifierLoc);
|
2010-03-15 13:12:16 +03: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);
|
2010-03-27 08:57:59 +03:00
|
|
|
if (isFriend) {
|
|
|
|
FunctionTemplate->setLexicalDeclContext(Owner);
|
|
|
|
FunctionTemplate->setObjectOfFriendDecl(true);
|
|
|
|
} else 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.
|
2010-07-08 22:37:38 +04:00
|
|
|
std::pair<const TemplateArgument *, unsigned> Innermost
|
|
|
|
= TemplateArgs.getInnermost();
|
2010-02-11 04:19:42 +03:00
|
|
|
Method->setFunctionTemplateSpecialization(FunctionTemplate,
|
2010-11-08 02:05:16 +03:00
|
|
|
TemplateArgumentList::CreateCopy(SemaRef.Context,
|
|
|
|
Innermost.first,
|
|
|
|
Innermost.second),
|
2009-11-14 04:20:54 +03:00
|
|
|
InsertPos);
|
2010-03-27 08:57:59 +03:00
|
|
|
} else if (!isFriend) {
|
2009-11-14 04:20:54 +03:00
|
|
|
// 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.
|
2010-03-27 08:57:59 +03:00
|
|
|
if (isFriend) {
|
2010-10-19 06:26:41 +04:00
|
|
|
if (NumTempParamLists)
|
|
|
|
Method->setTemplateParameterListsInfo(SemaRef.Context,
|
|
|
|
NumTempParamLists,
|
|
|
|
TempParamLists.data());
|
|
|
|
|
2010-03-27 08:57:59 +03:00
|
|
|
Method->setLexicalDeclContext(Owner);
|
|
|
|
Method->setObjectOfFriendDecl(true);
|
|
|
|
} else if (D->isOutOfLine())
|
2009-07-25 00:34:43 +04:00
|
|
|
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);
|
2010-02-11 04:19:42 +03:00
|
|
|
Method->setParams(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
|
|
|
|
2010-08-12 02:01:17 +04:00
|
|
|
LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
|
|
|
|
Sema::ForRedeclaration);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-03-27 08:57:59 +03:00
|
|
|
if (!FunctionTemplate || TemplateParams || isFriend) {
|
|
|
|
SemaRef.LookupQualifiedName(Previous, Record);
|
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;
|
2011-01-21 05:08:54 +03:00
|
|
|
SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration);
|
2009-08-22 03:19:43 +04:00
|
|
|
|
2009-12-01 20:24:26 +03:00
|
|
|
if (D->isPure())
|
|
|
|
SemaRef.CheckPureMethod(Method, SourceRange());
|
|
|
|
|
2010-01-21 00:53:11 +03:00
|
|
|
Method->setAccess(D->getAccess());
|
|
|
|
|
2011-01-20 09:52:44 +03:00
|
|
|
SemaRef.CheckOverrideControl(Method);
|
|
|
|
|
2010-03-27 08:57:59 +03:00
|
|
|
if (FunctionTemplate) {
|
|
|
|
// If there's a function template, let our caller handle it.
|
|
|
|
} else if (Method->isInvalidDecl() && !Previous.empty()) {
|
|
|
|
// Don't hide a (potentially) valid declaration with an invalid one.
|
|
|
|
} else {
|
|
|
|
NamedDecl *DeclToAdd = (TemplateParams
|
|
|
|
? cast<NamedDecl>(FunctionTemplate)
|
|
|
|
: Method);
|
|
|
|
if (isFriend)
|
|
|
|
Record->makeDeclVisibleInContext(DeclToAdd);
|
|
|
|
else
|
|
|
|
Owner->addDecl(DeclToAdd);
|
|
|
|
}
|
2010-08-15 05:15:20 +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) {
|
2011-01-15 01:40:04 +03:00
|
|
|
return SemaRef.SubstParmVarDecl(D, TemplateArgs, llvm::Optional<unsigned>());
|
2009-03-24 02:06:20 +03:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:44:21 +04:00
|
|
|
Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
|
|
|
|
TemplateTypeParmDecl *D) {
|
|
|
|
// TODO: don't always clone when decls are refcounted.
|
2010-06-16 19:23:05 +04:00
|
|
|
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(),
|
2010-08-31 03:23:59 +04:00
|
|
|
TTPT->getDepth() - TemplateArgs.getNumLevels(),
|
2010-10-30 10:48:20 +04:00
|
|
|
TTPT->getIndex(), D->getIdentifier(),
|
2009-08-20 05:44:21 +04:00
|
|
|
D->wasDeclaredWithTypename(),
|
|
|
|
D->isParameterPack());
|
2011-03-04 20:52:15 +03:00
|
|
|
Inst->setAccess(AS_public);
|
|
|
|
|
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.
|
2011-01-19 23:10:05 +03:00
|
|
|
TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
|
|
|
|
llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
|
|
|
|
llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes;
|
|
|
|
bool IsExpandedParameterPack = false;
|
|
|
|
TypeSourceInfo *DI;
|
2009-10-24 03:25:44 +04:00
|
|
|
QualType T;
|
|
|
|
bool Invalid = false;
|
2011-01-19 23:10:05 +03:00
|
|
|
|
|
|
|
if (D->isExpandedParameterPack()) {
|
|
|
|
// The non-type template parameter pack is an already-expanded pack
|
|
|
|
// expansion of types. Substitute into each of the expanded types.
|
|
|
|
ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
|
|
|
|
ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
|
|
|
|
for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
|
|
|
|
TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
|
|
|
|
TemplateArgs,
|
|
|
|
D->getLocation(),
|
|
|
|
D->getDeclName());
|
|
|
|
if (!NewDI)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ExpandedParameterPackTypesAsWritten.push_back(NewDI);
|
|
|
|
QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
|
|
|
|
D->getLocation());
|
|
|
|
if (NewT.isNull())
|
|
|
|
return 0;
|
|
|
|
ExpandedParameterPackTypes.push_back(NewT);
|
|
|
|
}
|
|
|
|
|
|
|
|
IsExpandedParameterPack = true;
|
|
|
|
DI = D->getTypeSourceInfo();
|
|
|
|
T = DI->getType();
|
|
|
|
} else if (isa<PackExpansionTypeLoc>(TL)) {
|
|
|
|
// The non-type template parameter pack's type is a pack expansion of types.
|
|
|
|
// Determine whether we need to expand this parameter pack into separate
|
|
|
|
// types.
|
|
|
|
PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
|
|
|
|
TypeLoc Pattern = Expansion.getPatternLoc();
|
|
|
|
llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
|
|
|
|
SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
|
|
|
|
|
|
|
|
// Determine whether the set of unexpanded parameter packs can and should
|
|
|
|
// be expanded.
|
|
|
|
bool Expand = true;
|
|
|
|
bool RetainExpansion = false;
|
|
|
|
llvm::Optional<unsigned> OrigNumExpansions
|
|
|
|
= Expansion.getTypePtr()->getNumExpansions();
|
|
|
|
llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
|
|
|
|
if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
|
|
|
|
Pattern.getSourceRange(),
|
|
|
|
Unexpanded.data(),
|
|
|
|
Unexpanded.size(),
|
|
|
|
TemplateArgs,
|
|
|
|
Expand, RetainExpansion,
|
|
|
|
NumExpansions))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (Expand) {
|
|
|
|
for (unsigned I = 0; I != *NumExpansions; ++I) {
|
|
|
|
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
|
|
|
|
TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
|
|
|
|
D->getLocation(),
|
|
|
|
D->getDeclName());
|
|
|
|
if (!NewDI)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ExpandedParameterPackTypesAsWritten.push_back(NewDI);
|
|
|
|
QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
|
|
|
|
NewDI->getType(),
|
|
|
|
D->getLocation());
|
|
|
|
if (NewT.isNull())
|
|
|
|
return 0;
|
|
|
|
ExpandedParameterPackTypes.push_back(NewT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we have an expanded parameter pack. The "type" of this
|
|
|
|
// expanded parameter pack is the original expansion type, but callers
|
|
|
|
// will end up using the expanded parameter pack types for type-checking.
|
|
|
|
IsExpandedParameterPack = true;
|
|
|
|
DI = D->getTypeSourceInfo();
|
|
|
|
T = DI->getType();
|
|
|
|
} else {
|
|
|
|
// We cannot fully expand the pack expansion now, so substitute into the
|
|
|
|
// pattern and create a new pack expansion type.
|
|
|
|
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
|
|
|
|
TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
|
|
|
|
D->getLocation(),
|
|
|
|
D->getDeclName());
|
|
|
|
if (!NewPattern)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
|
|
|
|
NumExpansions);
|
|
|
|
if (!DI)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
T = DI->getType();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Simple case: substitution into a parameter that is not a parameter pack.
|
|
|
|
DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
|
|
|
|
D->getLocation(), D->getDeclName());
|
|
|
|
if (!DI)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Check that this type is acceptable for a non-type template parameter.
|
|
|
|
bool Invalid = false;
|
|
|
|
T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
|
|
|
|
D->getLocation());
|
|
|
|
if (T.isNull()) {
|
|
|
|
T = SemaRef.Context.IntTy;
|
|
|
|
Invalid = true;
|
|
|
|
}
|
2009-10-24 03:25:44 +04:00
|
|
|
}
|
|
|
|
|
2011-01-19 23:10:05 +03:00
|
|
|
NonTypeTemplateParmDecl *Param;
|
|
|
|
if (IsExpandedParameterPack)
|
|
|
|
Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
|
|
|
|
D->getLocation(),
|
2010-08-31 03:23:59 +04:00
|
|
|
D->getDepth() - TemplateArgs.getNumLevels(),
|
2011-01-19 23:10:05 +03:00
|
|
|
D->getPosition(),
|
|
|
|
D->getIdentifier(), T,
|
|
|
|
DI,
|
|
|
|
ExpandedParameterPackTypes.data(),
|
|
|
|
ExpandedParameterPackTypes.size(),
|
|
|
|
ExpandedParameterPackTypesAsWritten.data());
|
|
|
|
else
|
|
|
|
Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
|
|
|
|
D->getLocation(),
|
|
|
|
D->getDepth() - TemplateArgs.getNumLevels(),
|
|
|
|
D->getPosition(),
|
|
|
|
D->getIdentifier(), T,
|
|
|
|
D->isParameterPack(), DI);
|
|
|
|
|
2011-03-04 20:52:15 +03:00
|
|
|
Param->setAccess(AS_public);
|
2009-10-24 03:25:44 +04:00
|
|
|
if (Invalid)
|
|
|
|
Param->setInvalidDecl();
|
|
|
|
|
2010-06-09 13:26:05 +04:00
|
|
|
Param->setDefaultArgument(D->getDefaultArgument(), false);
|
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.
|
2010-08-25 09:32:35 +04:00
|
|
|
LocalInstantiationScope Scope(SemaRef);
|
2009-11-11 19:58:32 +03:00
|
|
|
InstParams = SubstTemplateParams(TempParams);
|
|
|
|
if (!InstParams)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the template template parameter.
|
|
|
|
TemplateTemplateParmDecl *Param
|
|
|
|
= TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
2010-08-31 03:23:59 +04:00
|
|
|
D->getDepth() - TemplateArgs.getNumLevels(),
|
2011-01-05 18:48:55 +03:00
|
|
|
D->getPosition(), D->isParameterPack(),
|
|
|
|
D->getIdentifier(), InstParams);
|
2010-06-09 13:26:05 +04:00
|
|
|
Param->setDefaultArgument(D->getDefaultArgument(), false);
|
2011-03-04 20:52:15 +03:00
|
|
|
Param->setAccess(AS_public);
|
2010-05-19 21:02:24 +04:00
|
|
|
|
2009-11-11 19:58:32 +03:00
|
|
|
// 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) {
|
2011-02-25 19:33:46 +03:00
|
|
|
// Using directives are never dependent (and never contain any types or
|
|
|
|
// expressions), so they require no explicit instantiation work.
|
2009-11-17 09:07:40 +03:00
|
|
|
|
|
|
|
UsingDirectiveDecl *Inst
|
|
|
|
= UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
|
|
|
D->getNamespaceKeyLocation(),
|
2011-02-25 19:33:46 +03:00
|
|
|
D->getQualifierLoc(),
|
2009-11-17 09:07:40 +03:00
|
|
|
D->getIdentLocation(),
|
|
|
|
D->getNominatedNamespace(),
|
|
|
|
D->getCommonAncestor());
|
|
|
|
Owner->addDecl(Inst);
|
|
|
|
return Inst;
|
|
|
|
}
|
|
|
|
|
2009-12-05 01:46:56 +03:00
|
|
|
Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
|
2010-09-29 21:58:28 +04:00
|
|
|
|
|
|
|
// The nested name specifier may be dependent, for example
|
|
|
|
// template <typename T> struct t {
|
|
|
|
// struct s1 { T f1(); };
|
|
|
|
// struct s2 : s1 { using s1::f1; };
|
|
|
|
// };
|
|
|
|
// template struct t<int>;
|
|
|
|
// Here, in using s1::f1, s1 refers to t<T>::s1;
|
|
|
|
// we need to substitute for t<int>::s1.
|
2011-02-25 18:54:31 +03:00
|
|
|
NestedNameSpecifierLoc QualifierLoc
|
|
|
|
= SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
|
|
|
|
TemplateArgs);
|
|
|
|
if (!QualifierLoc)
|
2011-02-25 03:36:19 +03:00
|
|
|
return 0;
|
2010-09-29 21:58:28 +04:00
|
|
|
|
|
|
|
// The name info is non-dependent, so no transformation
|
|
|
|
// is required.
|
2010-08-12 15:46:03 +04:00
|
|
|
DeclarationNameInfo NameInfo = D->getNameInfo();
|
2009-12-05 01:46:56 +03:00
|
|
|
|
2009-12-10 12:41:52 +03:00
|
|
|
// We only need to do redeclaration lookups if we're in a class
|
|
|
|
// scope (in fact, it's not really even possible in non-class
|
|
|
|
// scopes).
|
|
|
|
bool CheckRedeclaration = Owner->isRecord();
|
|
|
|
|
2010-08-12 15:46:03 +04:00
|
|
|
LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
|
|
|
|
Sema::ForRedeclaration);
|
2009-12-10 12:41:52 +03:00
|
|
|
|
2009-12-05 01:46:56 +03:00
|
|
|
UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
|
|
|
|
D->getUsingLocation(),
|
2011-02-25 18:54:31 +03:00
|
|
|
QualifierLoc,
|
2010-08-12 15:46:03 +04:00
|
|
|
NameInfo,
|
2009-12-05 01:46:56 +03:00
|
|
|
D->isTypeName());
|
|
|
|
|
2011-02-25 18:54:31 +03:00
|
|
|
CXXScopeSpec SS;
|
|
|
|
SS.Adopt(QualifierLoc);
|
2009-12-10 12:41:52 +03:00
|
|
|
if (CheckRedeclaration) {
|
|
|
|
Prev.setHideTags(false);
|
|
|
|
SemaRef.LookupQualifiedName(Prev, Owner);
|
|
|
|
|
|
|
|
// Check for invalid redeclarations.
|
|
|
|
if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
|
|
|
|
D->isTypeName(), SS,
|
|
|
|
D->getLocation(), Prev))
|
|
|
|
NewUD->setInvalidDecl();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NewUD->isInvalidDecl() &&
|
|
|
|
SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
|
2009-12-05 01:46:56 +03:00
|
|
|
D->getLocation()))
|
|
|
|
NewUD->setInvalidDecl();
|
2009-12-10 12:41:52 +03:00
|
|
|
|
2009-12-05 01:46:56 +03:00
|
|
|
SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
|
|
|
|
NewUD->setAccess(D->getAccess());
|
|
|
|
Owner->addDecl(NewUD);
|
|
|
|
|
2009-12-10 12:41:52 +03:00
|
|
|
// Don't process the shadow decls for an invalid decl.
|
|
|
|
if (NewUD->isInvalidDecl())
|
|
|
|
return NewUD;
|
|
|
|
|
2009-12-23 01:26:37 +03:00
|
|
|
bool isFunctionScope = Owner->isFunctionOrMethod();
|
|
|
|
|
2009-12-10 12:41:52 +03:00
|
|
|
// Process the shadow decls.
|
|
|
|
for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
UsingShadowDecl *Shadow = *I;
|
|
|
|
NamedDecl *InstTarget =
|
2011-03-04 22:46:35 +03:00
|
|
|
cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
|
|
|
|
Shadow->getLocation(),
|
|
|
|
Shadow->getTargetDecl(),
|
|
|
|
TemplateArgs));
|
|
|
|
if (!InstTarget)
|
|
|
|
return 0;
|
2009-12-10 12:41:52 +03:00
|
|
|
|
|
|
|
if (CheckRedeclaration &&
|
|
|
|
SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
UsingShadowDecl *InstShadow
|
|
|
|
= SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
|
|
|
|
SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
|
2009-12-23 01:26:37 +03:00
|
|
|
|
|
|
|
if (isFunctionScope)
|
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
|
2009-12-10 12:41:52 +03:00
|
|
|
}
|
2009-12-05 01:46:56 +03:00
|
|
|
|
|
|
|
return NewUD;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
|
2009-12-10 12:41:52 +03:00
|
|
|
// Ignore these; we handle them in bulk when processing the UsingDecl.
|
|
|
|
return 0;
|
2009-12-05 01:46:56 +03:00
|
|
|
}
|
|
|
|
|
2009-11-18 05:36:19 +03:00
|
|
|
Decl * TemplateDeclInstantiator
|
|
|
|
::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
|
2011-02-25 18:54:31 +03:00
|
|
|
NestedNameSpecifierLoc QualifierLoc
|
|
|
|
= SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
|
|
|
|
TemplateArgs);
|
|
|
|
if (!QualifierLoc)
|
2009-11-18 05:36:19 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
CXXScopeSpec SS;
|
2011-02-25 18:54:31 +03:00
|
|
|
SS.Adopt(QualifierLoc);
|
2009-11-18 05:36:19 +03:00
|
|
|
|
2010-08-12 15:46:03 +04:00
|
|
|
// Since NameInfo refers to a typename, it cannot be a C++ special name.
|
|
|
|
// Hence, no tranformation is required for it.
|
|
|
|
DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
|
2009-11-18 05:36:19 +03:00
|
|
|
NamedDecl *UD =
|
|
|
|
SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
|
2010-08-12 15:46:03 +04:00
|
|
|
D->getUsingLoc(), SS, NameInfo, 0,
|
2009-11-18 05:36:19 +03:00
|
|
|
/*instantiation*/ true,
|
|
|
|
/*typename*/ true, D->getTypenameLoc());
|
2010-05-19 21:02:24 +04:00
|
|
|
if (UD)
|
2009-12-05 01:46:56 +03:00
|
|
|
SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
|
|
|
|
|
2009-11-18 05:36:19 +03:00
|
|
|
return UD;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decl * TemplateDeclInstantiator
|
|
|
|
::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
|
2011-02-25 18:54:31 +03:00
|
|
|
NestedNameSpecifierLoc QualifierLoc
|
|
|
|
= SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
|
|
|
|
if (!QualifierLoc)
|
2009-08-28 19:18:15 +04:00
|
|
|
return 0;
|
2011-02-25 18:54:31 +03:00
|
|
|
|
2009-08-28 19:18:15 +04:00
|
|
|
CXXScopeSpec SS;
|
2011-02-25 18:54:31 +03:00
|
|
|
SS.Adopt(QualifierLoc);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-12 15:46:03 +04:00
|
|
|
DeclarationNameInfo NameInfo
|
|
|
|
= SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
NamedDecl *UD =
|
2009-11-17 08:59:44 +03:00
|
|
|
SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
|
2010-08-12 15:46:03 +04:00
|
|
|
D->getUsingLoc(), SS, NameInfo, 0,
|
2009-11-18 05:36:19 +03:00
|
|
|
/*instantiation*/ true,
|
|
|
|
/*typename*/ false, SourceLocation());
|
2010-05-19 21:02:24 +04:00
|
|
|
if (UD)
|
2009-12-05 01:46:56 +03:00
|
|
|
SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
|
|
|
|
|
2009-08-29 23:37:28 +04:00
|
|
|
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);
|
2010-02-16 22:28:15 +03:00
|
|
|
if (D->isInvalidDecl())
|
|
|
|
return 0;
|
|
|
|
|
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.
|
2010-07-25 22:17:45 +04:00
|
|
|
if (Invalid)
|
2009-08-20 05:44:21 +04:00
|
|
|
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.
|
|
|
|
///
|
2010-11-10 22:44:59 +03:00
|
|
|
/// \returns The instantiated partial specialization, if successful; otherwise,
|
|
|
|
/// NULL to indicate an error.
|
|
|
|
ClassTemplatePartialSpecializationDecl *
|
2009-10-29 03:04:11 +03:00
|
|
|
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.
|
2010-08-25 09:32:35 +04:00
|
|
|
LocalInstantiationScope Scope(SemaRef);
|
2009-10-31 20:21:17 +03:00
|
|
|
|
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)
|
2010-11-10 22:44:59 +03:00
|
|
|
return 0;
|
2009-10-29 03:04:11 +03:00
|
|
|
|
|
|
|
// Substitute into the template arguments of the class template partial
|
|
|
|
// specialization.
|
2009-11-23 04:53:49 +03:00
|
|
|
TemplateArgumentListInfo InstTemplateArgs; // no angle locations
|
2010-12-23 00:19:48 +03:00
|
|
|
if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
|
|
|
|
PartialSpec->getNumTemplateArgsAsWritten(),
|
|
|
|
InstTemplateArgs, TemplateArgs))
|
|
|
|
return 0;
|
2009-10-29 03:04:11 +03:00
|
|
|
|
|
|
|
// Check that the template argument list is well-formed for this
|
|
|
|
// class template.
|
2010-11-08 02:05:16 +03:00
|
|
|
llvm::SmallVector<TemplateArgument, 4> Converted;
|
2009-10-29 03:04:11 +03:00
|
|
|
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))
|
2010-11-10 22:44:59 +03:00
|
|
|
return 0;
|
2009-10-29 03:04:11 +03:00
|
|
|
|
|
|
|
// Figure out where to insert this class template partial specialization
|
|
|
|
// in the member template's set of class template partial specializations.
|
|
|
|
void *InsertPos = 0;
|
|
|
|
ClassTemplateSpecializationDecl *PrevDecl
|
2010-11-08 02:05:16 +03:00
|
|
|
= ClassTemplate->findPartialSpecialization(Converted.data(),
|
|
|
|
Converted.size(), InsertPos);
|
2009-10-29 03:04:11 +03:00
|
|
|
|
|
|
|
// Build the canonical type that describes the converted template
|
|
|
|
// arguments of the class template partial specialization.
|
|
|
|
QualType CanonType
|
|
|
|
= SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
|
2010-11-08 02:05:16 +03:00
|
|
|
Converted.data(),
|
|
|
|
Converted.size());
|
2009-10-29 03:04:11 +03:00
|
|
|
|
|
|
|
// 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.
|
2010-03-10 06:28:59 +03:00
|
|
|
TypeSourceInfo *WrittenTy
|
|
|
|
= SemaRef.Context.getTemplateSpecializationTypeInfo(
|
|
|
|
TemplateName(ClassTemplate),
|
|
|
|
PartialSpec->getLocation(),
|
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)
|
2010-11-10 22:44:59 +03:00
|
|
|
<< WrittenTy->getType();
|
2009-10-29 03:04:11 +03:00
|
|
|
SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
|
|
|
|
<< SemaRef.Context.getTypeDeclType(PrevDecl);
|
2010-11-10 22:44:59 +03:00
|
|
|
return 0;
|
2009-10-29 03:04:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create the class template partial specialization declaration.
|
|
|
|
ClassTemplatePartialSpecializationDecl *InstPartialSpec
|
2010-05-06 04:28:52 +04:00
|
|
|
= ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
|
|
|
|
PartialSpec->getTagKind(),
|
|
|
|
Owner,
|
2009-10-29 03:04:11 +03:00
|
|
|
PartialSpec->getLocation(),
|
|
|
|
InstParams,
|
|
|
|
ClassTemplate,
|
2010-11-08 02:05:16 +03:00
|
|
|
Converted.data(),
|
|
|
|
Converted.size(),
|
2009-11-23 04:53:49 +03:00
|
|
|
InstTemplateArgs,
|
2010-03-10 06:28:59 +03:00
|
|
|
CanonType,
|
2010-04-30 09:56:50 +04:00
|
|
|
0,
|
2010-07-20 17:59:28 +04:00
|
|
|
ClassTemplate->getNextPartialSpecSequenceNumber());
|
2010-03-15 13:12:16 +03:00
|
|
|
// Substitute the nested name specifier, if any.
|
|
|
|
if (SubstQualifier(PartialSpec, InstPartialSpec))
|
|
|
|
return 0;
|
|
|
|
|
2009-10-29 03:04:11 +03:00
|
|
|
InstPartialSpec->setInstantiatedFromMember(PartialSpec);
|
2010-05-19 21:02:24 +04:00
|
|
|
InstPartialSpec->setTypeAsWritten(WrittenTy);
|
|
|
|
|
2009-10-29 03:04:11 +03:00
|
|
|
// Add this partial specialization to the set of class template partial
|
|
|
|
// specializations.
|
2010-07-20 17:59:28 +04:00
|
|
|
ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
|
2010-11-10 22:44:59 +03:00
|
|
|
return InstPartialSpec;
|
2009-10-29 03:04:11 +03:00
|
|
|
}
|
|
|
|
|
2010-03-11 12:03:00 +03:00
|
|
|
TypeSourceInfo*
|
|
|
|
TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
|
|
|
|
llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
|
|
|
|
TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
|
|
|
|
assert(OldTInfo && "substituting function without type source info");
|
|
|
|
assert(Params.empty() && "parameter vector is non-empty at start");
|
2010-04-09 21:38:44 +04:00
|
|
|
TypeSourceInfo *NewTInfo
|
|
|
|
= SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
|
|
|
|
D->getTypeSpecStartLoc(),
|
|
|
|
D->getDeclName());
|
2010-03-11 12:03:00 +03:00
|
|
|
if (!NewTInfo)
|
|
|
|
return 0;
|
2009-03-24 03:38:23 +03:00
|
|
|
|
Be sure to instantiate the parameters of a function, even when the
function's type is (strictly speaking) non-dependent. This ensures
that, e.g., default function arguments get instantiated properly.
And, since I couldn't resist, collapse the two implementations of
function-parameter instantiation into calls to a single, new function
(Sema::SubstParmVarDecl), since the two had nearly identical code (and
each had bugs the other didn't!). More importantly, factored out the
semantic analysis of a parameter declaration into
Sema::CheckParameter, which is called both by
Sema::ActOnParamDeclarator (when parameters are parsed) and when a
parameter is instantiated. Previously, we were missing some
Objective-C and address-space checks on instantiated function
parameters.
Fixes PR6733.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101029 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-12 11:48:19 +04:00
|
|
|
if (NewTInfo != OldTInfo) {
|
|
|
|
// Get parameters from the new type info.
|
2010-12-14 01:27:55 +03:00
|
|
|
TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
|
2010-05-03 19:32:18 +04:00
|
|
|
if (FunctionProtoTypeLoc *OldProtoLoc
|
|
|
|
= dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
|
2010-12-14 01:27:55 +03:00
|
|
|
TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
|
2010-05-03 19:32:18 +04:00
|
|
|
FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
|
|
|
|
assert(NewProtoLoc && "Missing prototype?");
|
Implement substitution of a function parameter pack for its set of
instantiated function parameters, enabling instantiation of arbitrary
pack expansions involving function parameter packs. At this point, we
can now correctly compile a simple, variadic print() example:
#include <iostream>
#include <string>
void print() {}
template<typename Head, typename ...Tail>
void print(const Head &head, const Tail &...tail) {
std::cout << head;
print(tail...);
}
int main() {
std::string hello = "Hello";
print(hello, ", world!", " ", 2011, '\n');
}
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123000 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-07 19:43:16 +03:00
|
|
|
unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
|
|
|
|
for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
|
|
|
|
OldIdx != NumOldParams; ++OldIdx) {
|
|
|
|
ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
|
|
|
|
if (!OldParam->isParameterPack() ||
|
|
|
|
(NewIdx < NumNewParams &&
|
|
|
|
NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
|
|
|
|
// Simple case: normal parameter, or a parameter pack that's
|
|
|
|
// instantiated to a (still-dependent) parameter pack.
|
|
|
|
ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
|
|
|
|
Params.push_back(NewParam);
|
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
|
|
|
|
NewParam);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parameter pack: make the instantiation an argument pack.
|
|
|
|
SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
|
|
|
|
OldParam);
|
2011-01-11 06:14:20 +03:00
|
|
|
unsigned NumArgumentsInExpansion
|
|
|
|
= SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
|
|
|
|
TemplateArgs);
|
|
|
|
while (NumArgumentsInExpansion--) {
|
Implement substitution of a function parameter pack for its set of
instantiated function parameters, enabling instantiation of arbitrary
pack expansions involving function parameter packs. At this point, we
can now correctly compile a simple, variadic print() example:
#include <iostream>
#include <string>
void print() {}
template<typename Head, typename ...Tail>
void print(const Head &head, const Tail &...tail) {
std::cout << head;
print(tail...);
}
int main() {
std::string hello = "Hello";
print(hello, ", world!", " ", 2011, '\n');
}
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123000 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-07 19:43:16 +03:00
|
|
|
ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
|
|
|
|
Params.push_back(NewParam);
|
|
|
|
SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
|
|
|
|
NewParam);
|
|
|
|
}
|
2010-05-03 19:32:18 +04:00
|
|
|
}
|
2010-04-30 22:55:50 +04:00
|
|
|
}
|
Be sure to instantiate the parameters of a function, even when the
function's type is (strictly speaking) non-dependent. This ensures
that, e.g., default function arguments get instantiated properly.
And, since I couldn't resist, collapse the two implementations of
function-parameter instantiation into calls to a single, new function
(Sema::SubstParmVarDecl), since the two had nearly identical code (and
each had bugs the other didn't!). More importantly, factored out the
semantic analysis of a parameter declaration into
Sema::CheckParameter, which is called both by
Sema::ActOnParamDeclarator (when parameters are parsed) and when a
parameter is instantiated. Previously, we were missing some
Objective-C and address-space checks on instantiated function
parameters.
Fixes PR6733.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101029 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-12 11:48:19 +04:00
|
|
|
} else {
|
|
|
|
// The function type itself was not dependent and therefore no
|
|
|
|
// substitution occurred. However, we still need to instantiate
|
|
|
|
// the function parameters themselves.
|
2010-12-14 01:27:55 +03:00
|
|
|
TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
|
2010-05-03 19:32:18 +04:00
|
|
|
if (FunctionProtoTypeLoc *OldProtoLoc
|
|
|
|
= dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
|
|
|
|
for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
|
|
|
|
ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
|
|
|
|
if (!Parm)
|
|
|
|
return 0;
|
|
|
|
Params.push_back(Parm);
|
|
|
|
}
|
Be sure to instantiate the parameters of a function, even when the
function's type is (strictly speaking) non-dependent. This ensures
that, e.g., default function arguments get instantiated properly.
And, since I couldn't resist, collapse the two implementations of
function-parameter instantiation into calls to a single, new function
(Sema::SubstParmVarDecl), since the two had nearly identical code (and
each had bugs the other didn't!). More importantly, factored out the
semantic analysis of a parameter declaration into
Sema::CheckParameter, which is called both by
Sema::ActOnParamDeclarator (when parameters are parsed) and when a
parameter is instantiated. Previously, we were missing some
Objective-C and address-space checks on instantiated function
parameters.
Fixes PR6733.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101029 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-12 11:48:19 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-11 12:03:00 +03:00
|
|
|
return NewTInfo;
|
2009-03-24 03:38:23 +03:00
|
|
|
}
|
|
|
|
|
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-12-08 20:45:32 +03:00
|
|
|
const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
|
|
|
|
assert(Proto && "Function template without prototype?");
|
|
|
|
|
|
|
|
if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
|
|
|
|
Proto->getNoReturnAttr()) {
|
|
|
|
// The function has an exception specification or a "noreturn"
|
|
|
|
// attribute. Substitute into each of the exception types.
|
|
|
|
llvm::SmallVector<QualType, 4> Exceptions;
|
|
|
|
for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
|
|
|
|
// FIXME: Poor location information!
|
2010-12-21 03:52:54 +03:00
|
|
|
if (const PackExpansionType *PackExpansion
|
|
|
|
= Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
|
|
|
|
// We have a pack expansion. Instantiate it.
|
|
|
|
llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
|
|
|
|
SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
|
|
|
|
Unexpanded);
|
|
|
|
assert(!Unexpanded.empty() &&
|
|
|
|
"Pack expansion without parameter packs?");
|
|
|
|
|
|
|
|
bool Expand = false;
|
Work-in-progress implementation of C++0x [temp.arg.explicit]p9, which
allows an argument pack determines via explicit specification of
function template arguments to be extended by further, deduced
arguments. For example:
template<class ... Types> void f(Types ... values);
void g() {
f<int*, float*>(0, 0, 0); // Types is deduced to the sequence int*, float*, int
}
There are a number of FIXMEs in here that indicate places where we
need to implement + test retained expansions, plus a number of other
places in deduction where we need to correctly cope with the
explicitly-specified arguments when deducing an argument
pack. Furthermore, it appears that the RecursiveASTVisitor needs to be
auditied; it's missing some traversals (especially w.r.t. template
arguments) that cause it not to find unexpanded parameter packs when
it should.
The good news, however, is that the tr1::tuple implementation now
works fully, and the tr1::bind example (both from N2080) is actually
working now.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123163 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-10 10:32:04 +03:00
|
|
|
bool RetainExpansion = false;
|
2011-01-14 20:04:44 +03:00
|
|
|
llvm::Optional<unsigned> NumExpansions
|
|
|
|
= PackExpansion->getNumExpansions();
|
2010-12-21 03:52:54 +03:00
|
|
|
if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
|
|
|
|
SourceRange(),
|
|
|
|
Unexpanded.data(),
|
|
|
|
Unexpanded.size(),
|
|
|
|
TemplateArgs,
|
Work-in-progress implementation of C++0x [temp.arg.explicit]p9, which
allows an argument pack determines via explicit specification of
function template arguments to be extended by further, deduced
arguments. For example:
template<class ... Types> void f(Types ... values);
void g() {
f<int*, float*>(0, 0, 0); // Types is deduced to the sequence int*, float*, int
}
There are a number of FIXMEs in here that indicate places where we
need to implement + test retained expansions, plus a number of other
places in deduction where we need to correctly cope with the
explicitly-specified arguments when deducing an argument
pack. Furthermore, it appears that the RecursiveASTVisitor needs to be
auditied; it's missing some traversals (especially w.r.t. template
arguments) that cause it not to find unexpanded parameter packs when
it should.
The good news, however, is that the tr1::tuple implementation now
works fully, and the tr1::bind example (both from N2080) is actually
working now.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123163 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-10 10:32:04 +03:00
|
|
|
Expand,
|
|
|
|
RetainExpansion,
|
|
|
|
NumExpansions))
|
2010-12-21 03:52:54 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (!Expand) {
|
|
|
|
// We can't expand this pack expansion into separate arguments yet;
|
2011-01-14 20:04:44 +03:00
|
|
|
// just substitute into the pattern and create a new pack expansion
|
|
|
|
// type.
|
2010-12-21 03:52:54 +03:00
|
|
|
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
|
|
|
|
QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
|
|
|
|
TemplateArgs,
|
|
|
|
New->getLocation(), New->getDeclName());
|
|
|
|
if (T.isNull())
|
|
|
|
break;
|
|
|
|
|
2011-01-14 20:04:44 +03:00
|
|
|
T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
|
2010-12-21 03:52:54 +03:00
|
|
|
Exceptions.push_back(T);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Substitute into the pack expansion pattern for each template
|
|
|
|
bool Invalid = false;
|
2011-01-14 20:04:44 +03:00
|
|
|
for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
|
2010-12-21 03:52:54 +03:00
|
|
|
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
|
|
|
|
|
|
|
|
QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
|
|
|
|
TemplateArgs,
|
|
|
|
New->getLocation(), New->getDeclName());
|
|
|
|
if (T.isNull()) {
|
|
|
|
Invalid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Exceptions.push_back(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Invalid)
|
|
|
|
break;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-08 20:45:32 +03:00
|
|
|
QualType T
|
|
|
|
= SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
|
|
|
|
New->getLocation(), New->getDeclName());
|
|
|
|
if (T.isNull() ||
|
|
|
|
SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Exceptions.push_back(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebuild the function type
|
|
|
|
|
2010-12-14 11:05:40 +03:00
|
|
|
FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
|
2011-03-06 13:52:04 +03:00
|
|
|
EPI.ExceptionSpecType = Proto->hasExceptionSpec() ?
|
|
|
|
(Proto->hasAnyExceptionSpec() ? EST_DynamicAny : EST_Dynamic) :
|
|
|
|
EST_None;
|
2010-12-14 11:05:40 +03:00
|
|
|
EPI.NumExceptions = Exceptions.size();
|
|
|
|
EPI.Exceptions = Exceptions.data();
|
|
|
|
EPI.ExtInfo = Proto->getExtInfo();
|
|
|
|
|
2009-12-08 20:45:32 +03:00
|
|
|
const FunctionProtoType *NewProto
|
|
|
|
= New->getType()->getAs<FunctionProtoType>();
|
|
|
|
assert(NewProto && "Template instantiation without function prototype?");
|
|
|
|
New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
|
|
|
|
NewProto->arg_type_begin(),
|
|
|
|
NewProto->getNumArgs(),
|
2010-12-14 11:05:40 +03:00
|
|
|
EPI));
|
2009-12-08 20:45:32 +03:00
|
|
|
}
|
|
|
|
|
2010-08-01 06:01:53 +04:00
|
|
|
SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
|
2010-06-15 21:05:35 +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
|
|
|
New->setAccess(Tmpl->getAccess());
|
2009-12-03 21:44:40 +03:00
|
|
|
if (Tmpl->isVirtualAsWritten())
|
2010-09-29 00:50:54 +04:00
|
|
|
New->setVirtualAsWritten(true);
|
2009-03-24 03:38:23 +03:00
|
|
|
|
|
|
|
// 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) {
|
2010-07-07 15:31:19 +04:00
|
|
|
if (Function->isInvalidDecl() || Function->hasBody())
|
2009-05-15 03:26:13 +04:00
|
|
|
return;
|
|
|
|
|
2009-10-08 11:24:58 +04:00
|
|
|
// Never instantiate an explicit specialization.
|
|
|
|
if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
|
|
|
|
return;
|
2010-05-17 21:34:56 +04:00
|
|
|
|
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);
|
2010-05-17 21:57:54 +04:00
|
|
|
Function->setInvalidDecl();
|
2010-08-25 12:27:02 +04:00
|
|
|
} else if (Function->getTemplateSpecializationKind()
|
|
|
|
== TSK_ExplicitInstantiationDefinition) {
|
2010-08-25 12:44:16 +04:00
|
|
|
PendingInstantiations.push_back(
|
2010-08-25 12:27:02 +04:00
|
|
|
std::make_pair(Function, PointOfInstantiation));
|
2009-10-15 18:05:49 +04:00
|
|
|
}
|
2010-08-25 12:27:02 +04:00
|
|
|
|
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)
|
2010-05-04 03:29:10 +04:00
|
|
|
return;
|
|
|
|
|
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.
|
2010-11-25 03:35:20 +03:00
|
|
|
llvm::SmallVector<VTableUse, 16> SavedVTableUses;
|
2010-08-25 12:44:16 +04:00
|
|
|
std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
|
2010-11-25 03:35:20 +03:00
|
|
|
if (Recursive) {
|
|
|
|
VTableUses.swap(SavedVTableUses);
|
2010-08-25 12:44:16 +04:00
|
|
|
PendingInstantiations.swap(SavedPendingInstantiations);
|
2010-11-25 03:35:20 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-05-12 21:27:19 +04:00
|
|
|
EnterExpressionEvaluationContext EvalContext(*this,
|
2010-08-27 03:41:50 +04:00
|
|
|
Sema::PotentiallyEvaluated);
|
2010-08-21 13:40:31 +04:00
|
|
|
ActOnStartOfFunctionDef(0, Function);
|
2009-05-15 21:59:04 +04:00
|
|
|
|
2009-05-15 03:26:13 +04:00
|
|
|
// Introduce a new scope where local variable instantiations will be
|
2010-01-17 01:29:39 +03:00
|
|
|
// recorded, unless we're actually a member function within a local
|
|
|
|
// class, in which case we need to merge our results with the parent
|
|
|
|
// scope (of the enclosing function).
|
|
|
|
bool MergeWithParentScope = false;
|
|
|
|
if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
|
|
|
|
MergeWithParentScope = Rec->isLocalClass();
|
|
|
|
|
|
|
|
LocalInstantiationScope Scope(*this, MergeWithParentScope);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-15 03:26:13 +04:00
|
|
|
// Introduce the instantiated function parameters into the local
|
2010-07-18 20:45:46 +04:00
|
|
|
// instantiation scope, and set the parameter names to those used
|
|
|
|
// in the template.
|
Implement substitution of a function parameter pack for its set of
instantiated function parameters, enabling instantiation of arbitrary
pack expansions involving function parameter packs. At this point, we
can now correctly compile a simple, variadic print() example:
#include <iostream>
#include <string>
void print() {}
template<typename Head, typename ...Tail>
void print(const Head &head, const Tail &...tail) {
std::cout << head;
print(tail...);
}
int main() {
std::string hello = "Hello";
print(hello, ", world!", " ", 2011, '\n');
}
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123000 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-07 19:43:16 +03:00
|
|
|
unsigned FParamIdx = 0;
|
2010-07-18 20:45:46 +04:00
|
|
|
for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
|
|
|
|
const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
|
Implement substitution of a function parameter pack for its set of
instantiated function parameters, enabling instantiation of arbitrary
pack expansions involving function parameter packs. At this point, we
can now correctly compile a simple, variadic print() example:
#include <iostream>
#include <string>
void print() {}
template<typename Head, typename ...Tail>
void print(const Head &head, const Tail &...tail) {
std::cout << head;
print(tail...);
}
int main() {
std::string hello = "Hello";
print(hello, ", world!", " ", 2011, '\n');
}
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123000 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-07 19:43:16 +03:00
|
|
|
if (!PatternParam->isParameterPack()) {
|
|
|
|
// Simple case: not a parameter pack.
|
|
|
|
assert(FParamIdx < Function->getNumParams());
|
|
|
|
ParmVarDecl *FunctionParam = Function->getParamDecl(I);
|
|
|
|
FunctionParam->setDeclName(PatternParam->getDeclName());
|
|
|
|
Scope.InstantiatedLocal(PatternParam, FunctionParam);
|
|
|
|
++FParamIdx;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand the parameter pack.
|
|
|
|
Scope.MakeInstantiatedLocalArgPack(PatternParam);
|
|
|
|
for (unsigned NumFParams = Function->getNumParams();
|
|
|
|
FParamIdx < NumFParams;
|
|
|
|
++FParamIdx) {
|
|
|
|
ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
|
|
|
|
FunctionParam->setDeclName(PatternParam->getDeclName());
|
|
|
|
Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
|
|
|
|
}
|
2010-07-18 20:45:46 +04:00
|
|
|
}
|
2009-05-15 03:26:13 +04:00
|
|
|
|
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.
|
2011-02-14 10:13:47 +03:00
|
|
|
Sema::ContextRAII savedContext(*this, Function);
|
2009-05-15 04:01:03 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
MultiLevelTemplateArgumentList TemplateArgs =
|
2010-05-04 03:29:10 +04:00
|
|
|
getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
|
2009-08-29 09:16:22 +04:00
|
|
|
|
|
|
|
// 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.
|
2010-08-24 10:29:42 +04:00
|
|
|
StmtResult 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();
|
|
|
|
|
2010-08-24 03:25:46 +04:00
|
|
|
ActOnFinishFunctionBody(Function, Body.get(),
|
2009-05-15 21:59:04 +04:00
|
|
|
/*IsInstantiation=*/true);
|
2009-05-15 04:01:03 +04:00
|
|
|
|
2010-03-24 08:22:00 +03:00
|
|
|
PerformDependentDiagnostics(PatternDecl, TemplateArgs);
|
|
|
|
|
2011-02-14 10:13:47 +03:00
|
|
|
savedContext.pop();
|
2009-05-27 00:50:29 +04:00
|
|
|
|
|
|
|
DeclGroupRef DG(Function);
|
|
|
|
Consumer.HandleTopLevelDecl(DG);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-01-17 01:29:39 +03:00
|
|
|
// This class may have local implicit instantiations that need to be
|
|
|
|
// instantiation within this scope.
|
2010-08-25 12:44:16 +04:00
|
|
|
PerformPendingInstantiations(/*LocalOnly=*/true);
|
2010-01-17 01:29:39 +03:00
|
|
|
Scope.Exit();
|
|
|
|
|
2009-06-30 21:20:14 +04:00
|
|
|
if (Recursive) {
|
2010-11-25 03:35:20 +03:00
|
|
|
// Define any pending vtables.
|
|
|
|
DefineUsedVTables();
|
|
|
|
|
2009-06-30 21:20:14 +04:00
|
|
|
// Instantiate any pending implicit instantiations found during the
|
2009-09-09 19:08:12 +04:00
|
|
|
// instantiation of this template.
|
2010-08-25 12:44:16 +04:00
|
|
|
PerformPendingInstantiations();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-11-25 03:35:20 +03:00
|
|
|
// Restore the set of pending vtables.
|
|
|
|
VTableUses.swap(SavedVTableUses);
|
|
|
|
|
2009-06-30 21:20:14 +04:00
|
|
|
// Restore the set of pending implicit instantiations.
|
2010-08-25 12:44:16 +04:00
|
|
|
PendingInstantiations.swap(SavedPendingInstantiations);
|
2009-06-30 21:20:14 +04:00
|
|
|
}
|
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);
|
2010-08-25 12:27:02 +04:00
|
|
|
} else if (Var->getTemplateSpecializationKind()
|
|
|
|
== TSK_ExplicitInstantiationDefinition) {
|
2010-08-25 12:44:16 +04:00
|
|
|
PendingInstantiations.push_back(
|
2010-08-25 12:27:02 +04:00
|
|
|
std::make_pair(Var, PointOfInstantiation));
|
|
|
|
}
|
|
|
|
|
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.
|
2010-08-25 12:44:16 +04:00
|
|
|
std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
|
2009-07-25 00:34:43 +04:00
|
|
|
if (Recursive)
|
2010-08-25 12:44:16 +04:00
|
|
|
PendingInstantiations.swap(SavedPendingInstantiations);
|
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.
|
2011-02-14 23:37:25 +03:00
|
|
|
ContextRAII previousContext(*this, 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(),
|
2010-11-29 01:53:37 +03:00
|
|
|
getTemplateInstantiationArgs(Var)));
|
2011-02-14 23:37:25 +03:00
|
|
|
|
|
|
|
previousContext.pop();
|
2009-07-25 00:34:43 +04:00
|
|
|
|
|
|
|
if (Var) {
|
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.
|
2010-08-25 12:44:16 +04:00
|
|
|
PerformPendingInstantiations();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 00:34:43 +04:00
|
|
|
// Restore the set of pending implicit instantiations.
|
2010-08-25 12:44:16 +04:00
|
|
|
PendingInstantiations.swap(SavedPendingInstantiations);
|
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;
|
Rework base and member initialization in constructors, with several
(necessarily simultaneous) changes:
- CXXBaseOrMemberInitializer now contains only a single initializer
rather than a set of initialiation arguments + a constructor. The
single initializer covers all aspects of initialization, including
constructor calls as necessary but also cleanup of temporaries
created by the initializer (which we never handled
before!).
- Rework + simplify code generation for CXXBaseOrMemberInitializers,
since we can now just emit the initializer as an initializer.
- Switched base and member initialization over to the new
initialization code (InitializationSequence), so that it
- Improved diagnostics for the new initialization code when
initializing bases and members, to match the diagnostics produced
by the previous (special-purpose) code.
- Simplify the representation of type-checked constructor initializers in
templates; instead of keeping the fully-type-checked AST, which is
rather hard to undo at template instantiation time, throw away the
type-checked AST and store the raw expressions in the AST. This
simplifies instantiation, but loses a little but of information in
the AST.
- When type-checking implicit base or member initializers within a
dependent context, don't add the generated initializers into the
AST, because they'll look like they were explicit.
- Record in CXXConstructExpr when the constructor call is to
initialize a base class, so that CodeGen does not have to infer it
from context. This ensures that we call the right kind of
constructor.
There are also a few "opportunity" fixes here that were needed to not
regress, for example:
- Diagnose default-initialization of a const-qualified class that
does not have a user-declared default constructor. We had this
diagnostic specifically for bases and members, but missed it for
variables. That's fixed now.
- When defining the implicit constructors, destructor, and
copy-assignment operator, set the CurContext to that constructor
when we're defining the body.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94952 91177308-0d34-0410-b5e6-96231b3b80d8
2010-01-31 12:12:51 +03:00
|
|
|
bool AnyErrors = false;
|
|
|
|
|
2009-08-29 09:16:22 +04:00
|
|
|
// 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) {
|
2011-01-08 23:30:50 +03:00
|
|
|
CXXCtorInitializer *Init = *Inits;
|
2009-08-29 09:16:22 +04:00
|
|
|
|
2010-09-04 01:54:20 +04:00
|
|
|
// Only instantiate written initializers, let Sema re-construct implicit
|
|
|
|
// ones.
|
|
|
|
if (!Init->isWritten())
|
|
|
|
continue;
|
|
|
|
|
2010-03-02 10:38:39 +03:00
|
|
|
SourceLocation LParenLoc, RParenLoc;
|
2010-08-23 10:44:23 +04:00
|
|
|
ASTOwningVector<Expr*> NewArgs(*this);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-04 03:32:56 +03:00
|
|
|
SourceLocation EllipsisLoc;
|
|
|
|
|
|
|
|
if (Init->isPackExpansion()) {
|
|
|
|
// This is a pack expansion. We should expand it now.
|
|
|
|
TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
|
|
|
|
llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
|
|
|
|
collectUnexpandedParameterPacks(BaseTL, Unexpanded);
|
|
|
|
bool ShouldExpand = false;
|
Work-in-progress implementation of C++0x [temp.arg.explicit]p9, which
allows an argument pack determines via explicit specification of
function template arguments to be extended by further, deduced
arguments. For example:
template<class ... Types> void f(Types ... values);
void g() {
f<int*, float*>(0, 0, 0); // Types is deduced to the sequence int*, float*, int
}
There are a number of FIXMEs in here that indicate places where we
need to implement + test retained expansions, plus a number of other
places in deduction where we need to correctly cope with the
explicitly-specified arguments when deducing an argument
pack. Furthermore, it appears that the RecursiveASTVisitor needs to be
auditied; it's missing some traversals (especially w.r.t. template
arguments) that cause it not to find unexpanded parameter packs when
it should.
The good news, however, is that the tr1::tuple implementation now
works fully, and the tr1::bind example (both from N2080) is actually
working now.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123163 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-10 10:32:04 +03:00
|
|
|
bool RetainExpansion = false;
|
2011-01-14 20:04:44 +03:00
|
|
|
llvm::Optional<unsigned> NumExpansions;
|
2011-01-04 03:32:56 +03:00
|
|
|
if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
|
|
|
|
BaseTL.getSourceRange(),
|
|
|
|
Unexpanded.data(),
|
|
|
|
Unexpanded.size(),
|
|
|
|
TemplateArgs, ShouldExpand,
|
Work-in-progress implementation of C++0x [temp.arg.explicit]p9, which
allows an argument pack determines via explicit specification of
function template arguments to be extended by further, deduced
arguments. For example:
template<class ... Types> void f(Types ... values);
void g() {
f<int*, float*>(0, 0, 0); // Types is deduced to the sequence int*, float*, int
}
There are a number of FIXMEs in here that indicate places where we
need to implement + test retained expansions, plus a number of other
places in deduction where we need to correctly cope with the
explicitly-specified arguments when deducing an argument
pack. Furthermore, it appears that the RecursiveASTVisitor needs to be
auditied; it's missing some traversals (especially w.r.t. template
arguments) that cause it not to find unexpanded parameter packs when
it should.
The good news, however, is that the tr1::tuple implementation now
works fully, and the tr1::bind example (both from N2080) is actually
working now.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123163 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-10 10:32:04 +03:00
|
|
|
RetainExpansion,
|
2011-01-04 03:32:56 +03:00
|
|
|
NumExpansions)) {
|
|
|
|
AnyErrors = true;
|
|
|
|
New->setInvalidDecl();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
assert(ShouldExpand && "Partial instantiation of base initializer?");
|
|
|
|
|
|
|
|
// Loop over all of the arguments in the argument pack(s),
|
2011-01-14 20:04:44 +03:00
|
|
|
for (unsigned I = 0; I != *NumExpansions; ++I) {
|
2011-01-04 03:32:56 +03:00
|
|
|
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
|
|
|
|
|
|
|
|
// Instantiate the initializer.
|
|
|
|
if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
|
|
|
|
LParenLoc, NewArgs, RParenLoc)) {
|
|
|
|
AnyErrors = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate the base type.
|
|
|
|
TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
|
|
|
|
TemplateArgs,
|
|
|
|
Init->getSourceLocation(),
|
|
|
|
New->getDeclName());
|
|
|
|
if (!BaseTInfo) {
|
|
|
|
AnyErrors = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the initializer.
|
|
|
|
MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
|
|
|
|
BaseTInfo,
|
|
|
|
(Expr **)NewArgs.data(),
|
|
|
|
NewArgs.size(),
|
|
|
|
Init->getLParenLoc(),
|
|
|
|
Init->getRParenLoc(),
|
|
|
|
New->getParent(),
|
|
|
|
SourceLocation());
|
|
|
|
if (NewInit.isInvalid()) {
|
|
|
|
AnyErrors = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NewInits.push_back(NewInit.get());
|
|
|
|
NewArgs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-03-02 10:38:39 +03:00
|
|
|
// Instantiate the initializer.
|
|
|
|
if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
|
2010-09-09 20:33:13 +04:00
|
|
|
LParenLoc, NewArgs, RParenLoc)) {
|
2010-03-02 10:38:39 +03:00
|
|
|
AnyErrors = true;
|
|
|
|
continue;
|
2009-08-29 09:16:22 +04:00
|
|
|
}
|
Rework base and member initialization in constructors, with several
(necessarily simultaneous) changes:
- CXXBaseOrMemberInitializer now contains only a single initializer
rather than a set of initialiation arguments + a constructor. The
single initializer covers all aspects of initialization, including
constructor calls as necessary but also cleanup of temporaries
created by the initializer (which we never handled
before!).
- Rework + simplify code generation for CXXBaseOrMemberInitializers,
since we can now just emit the initializer as an initializer.
- Switched base and member initialization over to the new
initialization code (InitializationSequence), so that it
- Improved diagnostics for the new initialization code when
initializing bases and members, to match the diagnostics produced
by the previous (special-purpose) code.
- Simplify the representation of type-checked constructor initializers in
templates; instead of keeping the fully-type-checked AST, which is
rather hard to undo at template instantiation time, throw away the
type-checked AST and store the raw expressions in the AST. This
simplifies instantiation, but loses a little but of information in
the AST.
- When type-checking implicit base or member initializers within a
dependent context, don't add the generated initializers into the
AST, because they'll look like they were explicit.
- Record in CXXConstructExpr when the constructor call is to
initialize a base class, so that CodeGen does not have to infer it
from context. This ensures that we call the right kind of
constructor.
There are also a few "opportunity" fixes here that were needed to not
regress, for example:
- Diagnose default-initialization of a const-qualified class that
does not have a user-declared default constructor. We had this
diagnostic specifically for bases and members, but missed it for
variables. That's fixed now.
- When defining the implicit constructors, destructor, and
copy-assignment operator, set the CurContext to that constructor
when we're defining the body.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94952 91177308-0d34-0410-b5e6-96231b3b80d8
2010-01-31 12:12:51 +03:00
|
|
|
|
2009-08-29 09:16:22 +04:00
|
|
|
MemInitResult NewInit;
|
|
|
|
if (Init->isBaseInitializer()) {
|
2009-12-07 05:54:59 +03:00
|
|
|
TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
|
2009-12-03 01:36:29 +03:00
|
|
|
TemplateArgs,
|
|
|
|
Init->getSourceLocation(),
|
|
|
|
New->getDeclName());
|
2009-12-07 05:54:59 +03:00
|
|
|
if (!BaseTInfo) {
|
Rework base and member initialization in constructors, with several
(necessarily simultaneous) changes:
- CXXBaseOrMemberInitializer now contains only a single initializer
rather than a set of initialiation arguments + a constructor. The
single initializer covers all aspects of initialization, including
constructor calls as necessary but also cleanup of temporaries
created by the initializer (which we never handled
before!).
- Rework + simplify code generation for CXXBaseOrMemberInitializers,
since we can now just emit the initializer as an initializer.
- Switched base and member initialization over to the new
initialization code (InitializationSequence), so that it
- Improved diagnostics for the new initialization code when
initializing bases and members, to match the diagnostics produced
by the previous (special-purpose) code.
- Simplify the representation of type-checked constructor initializers in
templates; instead of keeping the fully-type-checked AST, which is
rather hard to undo at template instantiation time, throw away the
type-checked AST and store the raw expressions in the AST. This
simplifies instantiation, but loses a little but of information in
the AST.
- When type-checking implicit base or member initializers within a
dependent context, don't add the generated initializers into the
AST, because they'll look like they were explicit.
- Record in CXXConstructExpr when the constructor call is to
initialize a base class, so that CodeGen does not have to infer it
from context. This ensures that we call the right kind of
constructor.
There are also a few "opportunity" fixes here that were needed to not
regress, for example:
- Diagnose default-initialization of a const-qualified class that
does not have a user-declared default constructor. We had this
diagnostic specifically for bases and members, but missed it for
variables. That's fixed now.
- When defining the implicit constructors, destructor, and
copy-assignment operator, set the CurContext to that constructor
when we're defining the body.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94952 91177308-0d34-0410-b5e6-96231b3b80d8
2010-01-31 12:12:51 +03:00
|
|
|
AnyErrors = true;
|
2009-12-03 01:36:29 +03:00
|
|
|
New->setInvalidDecl();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-07 05:54:59 +03:00
|
|
|
NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
|
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(),
|
2011-01-04 03:32:56 +03:00
|
|
|
New->getParent(),
|
|
|
|
EllipsisLoc);
|
2009-08-29 09:16:22 +04:00
|
|
|
} else if (Init->isMemberInitializer()) {
|
2011-03-04 22:46:35 +03:00
|
|
|
FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
|
2010-12-04 12:14:42 +03:00
|
|
|
Init->getMemberLocation(),
|
|
|
|
Init->getMember(),
|
|
|
|
TemplateArgs));
|
2011-03-04 22:46:35 +03:00
|
|
|
if (!Member) {
|
|
|
|
AnyErrors = true;
|
|
|
|
New->setInvalidDecl();
|
|
|
|
continue;
|
|
|
|
}
|
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());
|
2010-12-04 12:14:42 +03:00
|
|
|
} else if (Init->isIndirectMemberInitializer()) {
|
|
|
|
IndirectFieldDecl *IndirectMember =
|
2011-03-04 22:46:35 +03:00
|
|
|
cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
|
2010-12-04 12:14:42 +03:00
|
|
|
Init->getMemberLocation(),
|
|
|
|
Init->getIndirectMember(), TemplateArgs));
|
|
|
|
|
2011-03-04 22:46:35 +03:00
|
|
|
if (!IndirectMember) {
|
|
|
|
AnyErrors = true;
|
|
|
|
New->setInvalidDecl();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-12-04 12:14:42 +03:00
|
|
|
NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
|
|
|
|
NewArgs.size(),
|
|
|
|
Init->getSourceLocation(),
|
|
|
|
Init->getLParenLoc(),
|
|
|
|
Init->getRParenLoc());
|
2009-08-29 09:16:22 +04:00
|
|
|
}
|
|
|
|
|
Rework base and member initialization in constructors, with several
(necessarily simultaneous) changes:
- CXXBaseOrMemberInitializer now contains only a single initializer
rather than a set of initialiation arguments + a constructor. The
single initializer covers all aspects of initialization, including
constructor calls as necessary but also cleanup of temporaries
created by the initializer (which we never handled
before!).
- Rework + simplify code generation for CXXBaseOrMemberInitializers,
since we can now just emit the initializer as an initializer.
- Switched base and member initialization over to the new
initialization code (InitializationSequence), so that it
- Improved diagnostics for the new initialization code when
initializing bases and members, to match the diagnostics produced
by the previous (special-purpose) code.
- Simplify the representation of type-checked constructor initializers in
templates; instead of keeping the fully-type-checked AST, which is
rather hard to undo at template instantiation time, throw away the
type-checked AST and store the raw expressions in the AST. This
simplifies instantiation, but loses a little but of information in
the AST.
- When type-checking implicit base or member initializers within a
dependent context, don't add the generated initializers into the
AST, because they'll look like they were explicit.
- Record in CXXConstructExpr when the constructor call is to
initialize a base class, so that CodeGen does not have to infer it
from context. This ensures that we call the right kind of
constructor.
There are also a few "opportunity" fixes here that were needed to not
regress, for example:
- Diagnose default-initialization of a const-qualified class that
does not have a user-declared default constructor. We had this
diagnostic specifically for bases and members, but missed it for
variables. That's fixed now.
- When defining the implicit constructors, destructor, and
copy-assignment operator, set the CurContext to that constructor
when we're defining the body.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94952 91177308-0d34-0410-b5e6-96231b3b80d8
2010-01-31 12:12:51 +03:00
|
|
|
if (NewInit.isInvalid()) {
|
|
|
|
AnyErrors = true;
|
2009-08-29 09:16:22 +04:00
|
|
|
New->setInvalidDecl();
|
Rework base and member initialization in constructors, with several
(necessarily simultaneous) changes:
- CXXBaseOrMemberInitializer now contains only a single initializer
rather than a set of initialiation arguments + a constructor. The
single initializer covers all aspects of initialization, including
constructor calls as necessary but also cleanup of temporaries
created by the initializer (which we never handled
before!).
- Rework + simplify code generation for CXXBaseOrMemberInitializers,
since we can now just emit the initializer as an initializer.
- Switched base and member initialization over to the new
initialization code (InitializationSequence), so that it
- Improved diagnostics for the new initialization code when
initializing bases and members, to match the diagnostics produced
by the previous (special-purpose) code.
- Simplify the representation of type-checked constructor initializers in
templates; instead of keeping the fully-type-checked AST, which is
rather hard to undo at template instantiation time, throw away the
type-checked AST and store the raw expressions in the AST. This
simplifies instantiation, but loses a little but of information in
the AST.
- When type-checking implicit base or member initializers within a
dependent context, don't add the generated initializers into the
AST, because they'll look like they were explicit.
- Record in CXXConstructExpr when the constructor call is to
initialize a base class, so that CodeGen does not have to infer it
from context. This ensures that we call the right kind of
constructor.
There are also a few "opportunity" fixes here that were needed to not
regress, for example:
- Diagnose default-initialization of a const-qualified class that
does not have a user-declared default constructor. We had this
diagnostic specifically for bases and members, but missed it for
variables. That's fixed now.
- When defining the implicit constructors, destructor, and
copy-assignment operator, set the CurContext to that constructor
when we're defining the body.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94952 91177308-0d34-0410-b5e6-96231b3b80d8
2010-01-31 12:12:51 +03:00
|
|
|
} else {
|
2009-08-29 09:16:22 +04:00
|
|
|
// 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.
|
2010-08-21 13:40:31 +04:00
|
|
|
ActOnMemInitializers(New,
|
2009-08-29 09:16:22 +04:00
|
|
|
/*FIXME: ColonLoc */
|
|
|
|
SourceLocation(),
|
Rework base and member initialization in constructors, with several
(necessarily simultaneous) changes:
- CXXBaseOrMemberInitializer now contains only a single initializer
rather than a set of initialiation arguments + a constructor. The
single initializer covers all aspects of initialization, including
constructor calls as necessary but also cleanup of temporaries
created by the initializer (which we never handled
before!).
- Rework + simplify code generation for CXXBaseOrMemberInitializers,
since we can now just emit the initializer as an initializer.
- Switched base and member initialization over to the new
initialization code (InitializationSequence), so that it
- Improved diagnostics for the new initialization code when
initializing bases and members, to match the diagnostics produced
by the previous (special-purpose) code.
- Simplify the representation of type-checked constructor initializers in
templates; instead of keeping the fully-type-checked AST, which is
rather hard to undo at template instantiation time, throw away the
type-checked AST and store the raw expressions in the AST. This
simplifies instantiation, but loses a little but of information in
the AST.
- When type-checking implicit base or member initializers within a
dependent context, don't add the generated initializers into the
AST, because they'll look like they were explicit.
- Record in CXXConstructExpr when the constructor call is to
initialize a base class, so that CodeGen does not have to infer it
from context. This ensures that we call the right kind of
constructor.
There are also a few "opportunity" fixes here that were needed to not
regress, for example:
- Diagnose default-initialization of a const-qualified class that
does not have a user-declared default constructor. We had this
diagnostic specifically for bases and members, but missed it for
variables. That's fixed now.
- When defining the implicit constructors, destructor, and
copy-assignment operator, set the CurContext to that constructor
when we're defining the body.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94952 91177308-0d34-0410-b5e6-96231b3b80d8
2010-01-31 12:12:51 +03:00
|
|
|
NewInits.data(), NewInits.size(),
|
|
|
|
AnyErrors);
|
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-12-05 01:46:56 +03:00
|
|
|
static bool isInstantiationOf(UsingShadowDecl *Pattern,
|
|
|
|
UsingShadowDecl *Instance,
|
|
|
|
ASTContext &C) {
|
|
|
|
return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isInstantiationOf(UsingDecl *Pattern,
|
|
|
|
UsingDecl *Instance,
|
|
|
|
ASTContext &C) {
|
|
|
|
return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
|
|
|
|
}
|
|
|
|
|
2009-11-18 05:36:19 +03:00
|
|
|
static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
|
|
|
|
UsingDecl *Instance,
|
|
|
|
ASTContext &C) {
|
2009-12-05 01:46:56 +03:00
|
|
|
return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
|
2009-11-18 05:36:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
|
2009-08-29 23:37:28 +04:00
|
|
|
UsingDecl *Instance,
|
|
|
|
ASTContext &C) {
|
2009-12-05 01:46:56 +03:00
|
|
|
return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
|
2009-08-29 23:37:28 +04:00
|
|
|
}
|
|
|
|
|
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-12-05 01:46:56 +03:00
|
|
|
// Other is the prospective instantiation
|
|
|
|
// D is the prospective pattern
|
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-12-05 01:46:56 +03:00
|
|
|
if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
|
|
|
|
return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
|
|
|
|
|
|
|
|
if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
|
|
|
|
return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
|
|
|
|
|
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
|
2010-03-01 18:56:25 +03:00
|
|
|
DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
|
2009-09-16 22:34:49 +04:00
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs) {
|
2009-08-28 11:59:38 +04:00
|
|
|
if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
|
2010-03-01 18:56:25 +03:00
|
|
|
Decl* ID = FindInstantiatedDecl(Loc, 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>.
|
2010-03-01 18:56:25 +03:00
|
|
|
NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
|
2009-09-16 22:34:49 +04:00
|
|
|
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) ||
|
2010-02-05 22:54:12 +03:00
|
|
|
isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
|
2010-08-20 03:06:02 +04:00
|
|
|
(ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
|
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.
|
2011-02-17 22:47:42 +03:00
|
|
|
typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
|
|
|
|
llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
|
|
|
|
= CurrentInstantiationScope->findInstantiationOf(D);
|
|
|
|
|
2011-02-17 23:34:02 +03:00
|
|
|
if (Found) {
|
|
|
|
if (Decl *FD = Found->dyn_cast<Decl *>())
|
|
|
|
return cast<NamedDecl>(FD);
|
|
|
|
|
|
|
|
unsigned PackIdx = ArgumentPackSubstitutionIndex;
|
|
|
|
return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find the decl, then we must have a label decl that hasn't
|
|
|
|
// been found yet. Lazily instantiate it and return it now.
|
|
|
|
assert(isa<LabelDecl>(D));
|
|
|
|
|
|
|
|
Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
|
|
|
|
assert(Inst && "Failed to instantiate label??");
|
2011-02-17 22:47:42 +03:00
|
|
|
|
2011-02-17 23:34:02 +03:00
|
|
|
CurrentInstantiationScope->InstantiatedLocal(D, Inst);
|
|
|
|
return cast<LabelDecl>(Inst);
|
2009-05-27 21:07:49 +04:00
|
|
|
}
|
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;
|
|
|
|
|
2010-02-06 01:40:03 +03:00
|
|
|
// If the RecordDecl is actually the injected-class-name or a
|
|
|
|
// "templated" declaration for a class template, class template
|
|
|
|
// partial specialization, or a member class of a class template,
|
|
|
|
// substitute into the injected-class-name of the class template
|
|
|
|
// or partial specialization to find the new DeclContext.
|
2009-09-16 22:34:49 +04:00
|
|
|
QualType T;
|
|
|
|
ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
|
|
|
|
|
|
|
|
if (ClassTemplate) {
|
2010-07-08 22:37:38 +04:00
|
|
|
T = ClassTemplate->getInjectedClassNameSpecialization();
|
2009-09-16 22:34:49 +04:00
|
|
|
} else if (ClassTemplatePartialSpecializationDecl *PartialSpec
|
|
|
|
= dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
|
|
|
|
ClassTemplate = PartialSpec->getSpecializedTemplate();
|
2010-03-10 06:28:59 +03:00
|
|
|
|
|
|
|
// If we call SubstType with an InjectedClassNameType here we
|
|
|
|
// can end up in an infinite loop.
|
|
|
|
T = Context.getTypeDeclType(Record);
|
|
|
|
assert(isa<InjectedClassNameType>(T) &&
|
|
|
|
"type of partial specialization is not an InjectedClassNameType");
|
2010-04-27 04:57:59 +04:00
|
|
|
T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
|
2010-03-10 06:28:59 +03:00
|
|
|
}
|
2009-09-16 22:34:49 +04:00
|
|
|
|
|
|
|
if (!T.isNull()) {
|
2010-02-06 01:40:03 +03:00
|
|
|
// Substitute into the injected-class-name to get the type
|
|
|
|
// corresponding to the instantiation we want, which may also be
|
|
|
|
// the current instantiation (if we're in a template
|
|
|
|
// definition). 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?
|
2011-03-04 00:48:55 +03:00
|
|
|
T = SubstType(T, TemplateArgs, Loc, DeclarationName());
|
2009-09-16 22:34:49 +04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2010-02-06 01:40:03 +03:00
|
|
|
// 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.
|
|
|
|
bool SawNonDependentContext = false;
|
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
|
2010-02-06 01:40:03 +03: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;
|
2010-02-06 01:40:03 +03:00
|
|
|
|
|
|
|
if (!DC->isDependentContext())
|
|
|
|
SawNonDependentContext = true;
|
2009-08-29 12:11:13 +04:00
|
|
|
}
|
|
|
|
|
2010-02-06 01:40:03 +03:00
|
|
|
// We're performing "instantiation" of a member of the current
|
|
|
|
// instantiation while we are type-checking the
|
|
|
|
// definition. Compute the declaration context and return that.
|
|
|
|
assert(!SawNonDependentContext &&
|
|
|
|
"No dependent context while instantiating record");
|
|
|
|
DeclContext *DC = computeDeclContext(T);
|
|
|
|
assert(DC &&
|
2009-08-29 12:11:13 +04:00
|
|
|
"Unable to find declaration for the current instantiation");
|
2010-02-06 01:40:03 +03:00
|
|
|
return cast<CXXRecordDecl>(DC);
|
2009-08-29 12:11:13 +04:00
|
|
|
}
|
2010-02-06 01:40:03 +03: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;
|
|
|
|
|
2010-03-01 18:56:25 +03:00
|
|
|
ParentDC = FindInstantiatedContext(Loc, 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.
|
2010-03-01 18:56:25 +03:00
|
|
|
|
2010-03-10 06:28:59 +03:00
|
|
|
// If our context used to be dependent, we may need to instantiate
|
|
|
|
// it before performing lookup into that context.
|
|
|
|
if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
|
2010-03-01 18:56:25 +03:00
|
|
|
if (!Spec->isDependentContext()) {
|
|
|
|
QualType T = Context.getTypeDeclType(Spec);
|
2010-03-10 06:28:59 +03:00
|
|
|
const RecordType *Tag = T->getAs<RecordType>();
|
|
|
|
assert(Tag && "type of non-dependent record is not a RecordType");
|
|
|
|
if (!Tag->isBeingDefined() &&
|
|
|
|
RequireCompleteType(Loc, T, diag::err_incomplete_type))
|
|
|
|
return 0;
|
2010-11-06 02:22:45 +03:00
|
|
|
|
|
|
|
ParentDC = Tag->getDecl();
|
2010-03-01 18:56:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-27 09:35:12 +04:00
|
|
|
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-12-10 12:41:52 +03:00
|
|
|
// UsingShadowDecls can instantiate to nothing because of using hiding.
|
2011-03-04 23:42:52 +03:00
|
|
|
// Note: this assertion end up firing in invalid code even when none of the
|
|
|
|
// AST invariants have been broken, so we explicitly check whether any
|
|
|
|
// errors have been emitted
|
|
|
|
assert((Result || isa<UsingShadowDecl>(D) || Diags.hasErrorOccurred()) &&
|
|
|
|
"Unable to find instantiation of declaration!");
|
2009-12-10 12:41:52 +03:00
|
|
|
|
2009-05-27 09:35:12 +04:00
|
|
|
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.
|
2010-08-25 12:44:16 +04:00
|
|
|
void Sema::PerformPendingInstantiations(bool LocalOnly) {
|
2010-01-17 01:29:39 +03:00
|
|
|
while (!PendingLocalImplicitInstantiations.empty() ||
|
2010-08-25 12:44:16 +04:00
|
|
|
(!LocalOnly && !PendingInstantiations.empty())) {
|
2010-01-17 01:29:39 +03:00
|
|
|
PendingImplicitInstantiation Inst;
|
|
|
|
|
|
|
|
if (PendingLocalImplicitInstantiations.empty()) {
|
2010-08-25 12:44:16 +04:00
|
|
|
Inst = PendingInstantiations.front();
|
|
|
|
PendingInstantiations.pop_front();
|
2010-01-17 01:29:39 +03:00
|
|
|
} else {
|
|
|
|
Inst = PendingLocalImplicitInstantiations.front();
|
|
|
|
PendingLocalImplicitInstantiations.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)) {
|
2010-08-27 03:41:50 +04:00
|
|
|
PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
|
|
|
|
"instantiating function definition");
|
2010-08-25 12:27:02 +04:00
|
|
|
bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
|
|
|
|
TSK_ExplicitInstantiationDefinition;
|
|
|
|
InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
|
|
|
|
DefinitionRequired);
|
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
|
|
|
|
2010-02-13 13:17:50 +03:00
|
|
|
// Don't try to instantiate declarations if the most recent redeclaration
|
|
|
|
// is invalid.
|
|
|
|
if (Var->getMostRecentDeclaration()->isInvalidDecl())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check if the most recent declaration has changed the specialization kind
|
|
|
|
// and removed the need for implicit instantiation.
|
|
|
|
switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
assert(false && "Cannot instantitiate an undeclared specialization.");
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
|
|
|
case TSK_ExplicitSpecialization:
|
2010-08-25 12:27:02 +04:00
|
|
|
continue; // No longer need to instantiate this type.
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
// We only need an instantiation if the pending instantiation *is* the
|
|
|
|
// explicit instantiation.
|
|
|
|
if (Var != Var->getMostRecentDeclaration()) continue;
|
2010-02-13 13:17:50 +03:00
|
|
|
case TSK_ImplicitInstantiation:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-27 03:41:50 +04:00
|
|
|
PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
|
|
|
|
"instantiating static data member "
|
|
|
|
"definition");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-25 12:27:02 +04:00
|
|
|
bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
|
|
|
|
TSK_ExplicitInstantiationDefinition;
|
|
|
|
InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
|
|
|
|
DefinitionRequired);
|
2009-06-23 03:06:13 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-24 08:22:00 +03:00
|
|
|
|
|
|
|
void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
|
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs) {
|
|
|
|
for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
|
|
|
|
E = Pattern->ddiag_end(); I != E; ++I) {
|
|
|
|
DependentDiagnostic *DD = *I;
|
|
|
|
|
|
|
|
switch (DD->getKind()) {
|
|
|
|
case DependentDiagnostic::Access:
|
|
|
|
HandleDependentAccessCheck(*DD, TemplateArgs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|