2008-12-06 02:15:24 +08:00
|
|
|
//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2009-02-28 03:31:52 +08:00
|
|
|
//===----------------------------------------------------------------------===/
|
2008-12-06 02:15:24 +08:00
|
|
|
//
|
|
|
|
// This file implements semantic analysis for C++ templates.
|
2009-02-28 03:31:52 +08:00
|
|
|
//===----------------------------------------------------------------------===/
|
2008-12-06 02:15:24 +08:00
|
|
|
|
|
|
|
#include "Sema.h"
|
2009-08-07 00:20:37 +08:00
|
|
|
#include "TreeTransform.h"
|
2009-02-07 06:42:48 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-12-06 07:32:09 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2009-02-12 03:52:55 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2009-02-05 03:02:06 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2008-12-06 02:15:24 +08:00
|
|
|
#include "clang/Parse/DeclSpec.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
2009-08-07 00:20:37 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-12-06 02:15:24 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
/// \brief Determine whether the declaration found is acceptable as the name
|
|
|
|
/// of a template and, if so, return that template declaration. Otherwise,
|
|
|
|
/// returns NULL.
|
|
|
|
static NamedDecl *isAcceptableTemplateName(ASTContext &Context, NamedDecl *D) {
|
|
|
|
if (!D)
|
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
if (isa<TemplateDecl>(D))
|
|
|
|
return D;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
|
|
|
|
// C++ [temp.local]p1:
|
|
|
|
// Like normal (non-template) classes, class templates have an
|
|
|
|
// injected-class-name (Clause 9). The injected-class-name
|
|
|
|
// can be used with or without a template-argument-list. When
|
|
|
|
// it is used without a template-argument-list, it is
|
|
|
|
// equivalent to the injected-class-name followed by the
|
|
|
|
// template-parameters of the class template enclosed in
|
|
|
|
// <>. When it is used with a template-argument-list, it
|
|
|
|
// refers to the specified class template specialization,
|
|
|
|
// which could be the current specialization or another
|
|
|
|
// specialization.
|
|
|
|
if (Record->isInjectedClassName()) {
|
|
|
|
Record = cast<CXXRecordDecl>(Record->getCanonicalDecl());
|
|
|
|
if (Record->getDescribedClassTemplate())
|
|
|
|
return Record->getDescribedClassTemplate();
|
|
|
|
|
|
|
|
if (ClassTemplateSpecializationDecl *Spec
|
|
|
|
= dyn_cast<ClassTemplateSpecializationDecl>(Record))
|
|
|
|
return Spec->getSpecializedTemplate();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D);
|
|
|
|
if (!Ovl)
|
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
|
|
|
|
FEnd = Ovl->function_end();
|
|
|
|
F != FEnd; ++F) {
|
|
|
|
if (FunctionTemplateDecl *FuncTmpl = dyn_cast<FunctionTemplateDecl>(*F)) {
|
|
|
|
// We've found a function template. Determine whether there are
|
|
|
|
// any other function templates we need to bundle together in an
|
|
|
|
// OverloadedFunctionDecl
|
|
|
|
for (++F; F != FEnd; ++F) {
|
|
|
|
if (isa<FunctionTemplateDecl>(*F))
|
|
|
|
break;
|
2009-03-26 08:10:35 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
if (F != FEnd) {
|
|
|
|
// Build an overloaded function decl containing only the
|
|
|
|
// function templates in Ovl.
|
2009-09-09 23:08:12 +08:00
|
|
|
OverloadedFunctionDecl *OvlTemplate
|
2009-09-03 06:59:36 +08:00
|
|
|
= OverloadedFunctionDecl::Create(Context,
|
|
|
|
Ovl->getDeclContext(),
|
|
|
|
Ovl->getDeclName());
|
|
|
|
OvlTemplate->addOverload(FuncTmpl);
|
|
|
|
OvlTemplate->addOverload(*F);
|
|
|
|
for (++F; F != FEnd; ++F) {
|
|
|
|
if (isa<FunctionTemplateDecl>(*F))
|
2009-07-30 00:56:42 +08:00
|
|
|
OvlTemplate->addOverload(*F);
|
2009-02-10 02:46:07 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
return OvlTemplate;
|
2008-12-19 03:37:40 +08:00
|
|
|
}
|
2009-09-03 06:59:36 +08:00
|
|
|
|
|
|
|
return FuncTmpl;
|
2008-12-19 03:37:40 +08:00
|
|
|
}
|
2009-09-03 06:59:36 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-03-31 06:58:21 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
TemplateNameKind Sema::isTemplateName(Scope *S,
|
2009-09-09 23:08:12 +08:00
|
|
|
const IdentifierInfo &II,
|
2009-09-03 06:59:36 +08:00
|
|
|
SourceLocation IdLoc,
|
|
|
|
const CXXScopeSpec *SS,
|
|
|
|
TypeTy *ObjectTypePtr,
|
|
|
|
bool EnteringContext,
|
|
|
|
TemplateTy &TemplateResult) {
|
|
|
|
// Determine where to perform name lookup
|
|
|
|
DeclContext *LookupCtx = 0;
|
|
|
|
bool isDependent = false;
|
|
|
|
if (ObjectTypePtr) {
|
|
|
|
// This nested-name-specifier occurs in a member access expression, e.g.,
|
|
|
|
// x->B::f, and we are looking into the type of the object.
|
2009-09-09 23:08:12 +08:00
|
|
|
assert((!SS || !SS->isSet()) &&
|
2009-09-03 06:59:36 +08:00
|
|
|
"ObjectType and scope specifier cannot coexist");
|
|
|
|
QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr);
|
|
|
|
LookupCtx = computeDeclContext(ObjectType);
|
|
|
|
isDependent = ObjectType->isDependentType();
|
|
|
|
} else if (SS && SS->isSet()) {
|
|
|
|
// This nested-name-specifier occurs after another nested-name-specifier,
|
|
|
|
// so long into the context associated with the prior nested-name-specifier.
|
|
|
|
|
|
|
|
LookupCtx = computeDeclContext(*SS, EnteringContext);
|
|
|
|
isDependent = isDependentScopeSpecifier(*SS);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
LookupResult Found;
|
|
|
|
bool ObjectTypeSearchedInScope = false;
|
|
|
|
if (LookupCtx) {
|
|
|
|
// Perform "qualified" name lookup into the declaration context we
|
|
|
|
// computed, which is either the type of the base of a member access
|
2009-09-09 23:08:12 +08:00
|
|
|
// expression or the declaration context associated with a prior
|
2009-09-03 06:59:36 +08:00
|
|
|
// nested-name-specifier.
|
|
|
|
|
|
|
|
// The declaration context must be complete.
|
|
|
|
if (!LookupCtx->isDependentContext() && RequireCompleteDeclContext(*SS))
|
|
|
|
return TNK_Non_template;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
Found = LookupQualifiedName(LookupCtx, &II, LookupOrdinaryName);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
if (ObjectTypePtr && Found.getKind() == LookupResult::NotFound) {
|
|
|
|
// C++ [basic.lookup.classref]p1:
|
|
|
|
// In a class member access expression (5.2.5), if the . or -> token is
|
2009-09-09 23:08:12 +08:00
|
|
|
// immediately followed by an identifier followed by a <, the
|
|
|
|
// identifier must be looked up to determine whether the < is the
|
2009-09-03 06:59:36 +08:00
|
|
|
// beginning of a template argument list (14.2) or a less-than operator.
|
2009-09-09 23:08:12 +08:00
|
|
|
// The identifier is first looked up in the class of the object
|
|
|
|
// expression. If the identifier is not found, it is then looked up in
|
2009-09-03 06:59:36 +08:00
|
|
|
// the context of the entire postfix-expression and shall name a class
|
|
|
|
// or function template.
|
|
|
|
//
|
|
|
|
// FIXME: When we're instantiating a template, do we actually have to
|
|
|
|
// look in the scope of the template? Seems fishy...
|
|
|
|
Found = LookupName(S, &II, LookupOrdinaryName);
|
|
|
|
ObjectTypeSearchedInScope = true;
|
2009-03-31 06:58:21 +08:00
|
|
|
}
|
2009-09-03 06:59:36 +08:00
|
|
|
} else if (isDependent) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// We cannot look into a dependent object type or
|
2009-09-03 06:59:36 +08:00
|
|
|
return TNK_Non_template;
|
|
|
|
} else {
|
|
|
|
// Perform unqualified name lookup in the current scope.
|
|
|
|
Found = LookupName(S, &II, LookupOrdinaryName);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
// FIXME: Cope with ambiguous name-lookup results.
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(!Found.isAmbiguous() &&
|
2009-09-03 06:59:36 +08:00
|
|
|
"Cannot handle template name-lookup ambiguities");
|
|
|
|
|
|
|
|
NamedDecl *Template = isAcceptableTemplateName(Context, Found);
|
|
|
|
if (!Template)
|
|
|
|
return TNK_Non_template;
|
|
|
|
|
|
|
|
if (ObjectTypePtr && !ObjectTypeSearchedInScope) {
|
|
|
|
// C++ [basic.lookup.classref]p1:
|
2009-09-09 23:08:12 +08:00
|
|
|
// [...] If the lookup in the class of the object expression finds a
|
2009-09-03 06:59:36 +08:00
|
|
|
// template, the name is also looked up in the context of the entire
|
|
|
|
// postfix-expression and [...]
|
|
|
|
//
|
|
|
|
LookupResult FoundOuter = LookupName(S, &II, LookupOrdinaryName);
|
|
|
|
// FIXME: Handle ambiguities in this lookup better
|
|
|
|
NamedDecl *OuterTemplate = isAcceptableTemplateName(Context, FoundOuter);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
if (!OuterTemplate) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// - if the name is not found, the name found in the class of the
|
2009-09-03 06:59:36 +08:00
|
|
|
// object expression is used, otherwise
|
|
|
|
} else if (!isa<ClassTemplateDecl>(OuterTemplate)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// - if the name is found in the context of the entire
|
|
|
|
// postfix-expression and does not name a class template, the name
|
2009-09-03 06:59:36 +08:00
|
|
|
// found in the class of the object expression is used, otherwise
|
|
|
|
} else {
|
|
|
|
// - if the name found is a class template, it must refer to the same
|
2009-09-09 23:08:12 +08:00
|
|
|
// entity as the one found in the class of the object expression,
|
2009-09-03 06:59:36 +08:00
|
|
|
// otherwise the program is ill-formed.
|
|
|
|
if (OuterTemplate->getCanonicalDecl() != Template->getCanonicalDecl()) {
|
|
|
|
Diag(IdLoc, diag::err_nested_name_member_ref_lookup_ambiguous)
|
|
|
|
<< &II;
|
|
|
|
Diag(Template->getLocation(), diag::note_ambig_member_ref_object_type)
|
|
|
|
<< QualType::getFromOpaquePtr(ObjectTypePtr);
|
|
|
|
Diag(OuterTemplate->getLocation(), diag::note_ambig_member_ref_scope);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Recover by taking the template that we found in the object
|
2009-09-03 06:59:36 +08:00
|
|
|
// expression's type.
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-12-19 03:37:40 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
if (SS && SS->isSet() && !SS->isInvalid()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
NestedNameSpecifier *Qualifier
|
2009-09-03 06:59:36 +08:00
|
|
|
= static_cast<NestedNameSpecifier *>(SS->getScopeRep());
|
2009-09-09 23:08:12 +08:00
|
|
|
if (OverloadedFunctionDecl *Ovl
|
2009-09-03 06:59:36 +08:00
|
|
|
= dyn_cast<OverloadedFunctionDecl>(Template))
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateResult
|
2009-09-03 06:59:36 +08:00
|
|
|
= TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, false,
|
|
|
|
Ovl));
|
|
|
|
else
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateResult
|
2009-09-03 06:59:36 +08:00
|
|
|
= TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, false,
|
2009-09-09 23:08:12 +08:00
|
|
|
cast<TemplateDecl>(Template)));
|
|
|
|
} else if (OverloadedFunctionDecl *Ovl
|
2009-09-03 06:59:36 +08:00
|
|
|
= dyn_cast<OverloadedFunctionDecl>(Template)) {
|
|
|
|
TemplateResult = TemplateTy::make(TemplateName(Ovl));
|
|
|
|
} else {
|
|
|
|
TemplateResult = TemplateTy::make(
|
|
|
|
TemplateName(cast<TemplateDecl>(Template)));
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (isa<ClassTemplateDecl>(Template) ||
|
2009-09-03 06:59:36 +08:00
|
|
|
isa<TemplateTemplateParmDecl>(Template))
|
|
|
|
return TNK_Type_template;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
assert((isa<FunctionTemplateDecl>(Template) ||
|
2009-09-03 06:59:36 +08:00
|
|
|
isa<OverloadedFunctionDecl>(Template)) &&
|
|
|
|
"Unhandled template kind in Sema::isTemplateName");
|
|
|
|
return TNK_Function_template;
|
2008-12-19 03:37:40 +08:00
|
|
|
}
|
|
|
|
|
2008-12-06 02:15:24 +08:00
|
|
|
/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
|
|
|
|
/// that the template parameter 'PrevDecl' is being shadowed by a new
|
|
|
|
/// declaration at location Loc. Returns true to indicate that this is
|
|
|
|
/// an error, and false otherwise.
|
|
|
|
bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
|
2008-12-09 02:40:42 +08:00
|
|
|
assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
|
2008-12-06 02:15:24 +08:00
|
|
|
|
|
|
|
// Microsoft Visual C++ permits template parameters to be shadowed.
|
|
|
|
if (getLangOptions().Microsoft)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// C++ [temp.local]p4:
|
|
|
|
// A template-parameter shall not be redeclared within its
|
|
|
|
// scope (including nested scopes).
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Loc, diag::err_template_param_shadow)
|
2008-12-06 02:15:24 +08:00
|
|
|
<< cast<NamedDecl>(PrevDecl)->getDeclName();
|
|
|
|
Diag(PrevDecl->getLocation(), diag::note_template_param_here);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-03 12:44:36 +08:00
|
|
|
/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
|
2009-02-05 03:02:06 +08:00
|
|
|
/// the parameter D to reference the templated declaration and return a pointer
|
|
|
|
/// to the template declaration. Otherwise, do nothing to D and return null.
|
2009-03-29 03:18:32 +08:00
|
|
|
TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) {
|
|
|
|
if (TemplateDecl *Temp = dyn_cast<TemplateDecl>(D.getAs<Decl>())) {
|
|
|
|
D = DeclPtrTy::make(Temp->getTemplatedDecl());
|
2009-02-05 03:02:06 +08:00
|
|
|
return Temp;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-06 02:15:24 +08:00
|
|
|
/// ActOnTypeParameter - Called when a C++ template type parameter
|
|
|
|
/// (e.g., "typename T") has been parsed. Typename specifies whether
|
|
|
|
/// the keyword "typename" was used to declare the type parameter
|
|
|
|
/// (otherwise, "class" was used), and KeyLoc is the location of the
|
|
|
|
/// "class" or "typename" keyword. ParamName is the name of the
|
|
|
|
/// parameter (NULL indicates an unnamed template parameter) and
|
2009-09-09 23:08:12 +08:00
|
|
|
/// ParamName is the location of the parameter name (if any).
|
2008-12-06 02:15:24 +08:00
|
|
|
/// If the type parameter has a default argument, it will be added
|
|
|
|
/// later via ActOnTypeParameterDefault.
|
2009-09-09 23:08:12 +08:00
|
|
|
Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
|
2009-06-13 03:58:00 +08:00
|
|
|
SourceLocation EllipsisLoc,
|
2009-03-29 03:18:32 +08:00
|
|
|
SourceLocation KeyLoc,
|
|
|
|
IdentifierInfo *ParamName,
|
|
|
|
SourceLocation ParamNameLoc,
|
|
|
|
unsigned Depth, unsigned Position) {
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(S->isTemplateParamScope() &&
|
|
|
|
"Template type parameter not in template parameter scope!");
|
2008-12-06 02:15:24 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
|
|
|
|
if (ParamName) {
|
2009-02-05 01:27:36 +08:00
|
|
|
NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName);
|
2008-12-09 02:40:42 +08:00
|
|
|
if (PrevDecl && PrevDecl->isTemplateParameter())
|
2008-12-06 02:15:24 +08:00
|
|
|
Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
PrevDecl);
|
2008-12-06 02:15:24 +08:00
|
|
|
}
|
|
|
|
|
2009-02-07 06:42:48 +08:00
|
|
|
SourceLocation Loc = ParamNameLoc;
|
|
|
|
if (!ParamName)
|
|
|
|
Loc = KeyLoc;
|
|
|
|
|
2008-12-06 02:15:24 +08:00
|
|
|
TemplateTypeParmDecl *Param
|
2009-09-09 23:08:12 +08:00
|
|
|
= TemplateTypeParmDecl::Create(Context, CurContext, Loc,
|
|
|
|
Depth, Position, ParamName, Typename,
|
2009-06-13 06:23:22 +08:00
|
|
|
Ellipsis);
|
2008-12-06 02:15:24 +08:00
|
|
|
if (Invalid)
|
|
|
|
Param->setInvalidDecl();
|
|
|
|
|
|
|
|
if (ParamName) {
|
|
|
|
// Add the template parameter into the current scope.
|
2009-03-29 03:18:32 +08:00
|
|
|
S->AddDecl(DeclPtrTy::make(Param));
|
2008-12-06 02:15:24 +08:00
|
|
|
IdResolver.AddDecl(Param);
|
|
|
|
}
|
|
|
|
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(Param);
|
2008-12-06 02:15:24 +08:00
|
|
|
}
|
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
/// ActOnTypeParameterDefault - Adds a default argument (the type
|
2009-09-09 23:08:12 +08:00
|
|
|
/// Default) to the given template type parameter (TypeParam).
|
|
|
|
void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam,
|
2009-02-11 03:49:53 +08:00
|
|
|
SourceLocation EqualLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation DefaultLoc,
|
2009-02-11 03:49:53 +08:00
|
|
|
TypeTy *DefaultT) {
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateTypeParmDecl *Parm
|
2009-03-29 03:18:32 +08:00
|
|
|
= cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>());
|
2009-08-19 09:28:28 +08:00
|
|
|
// FIXME: Preserve type source info.
|
|
|
|
QualType Default = GetTypeFromParser(DefaultT);
|
2009-02-11 03:49:53 +08:00
|
|
|
|
2009-06-13 06:30:13 +08:00
|
|
|
// C++0x [temp.param]p9:
|
|
|
|
// A default template-argument may be specified for any kind of
|
2009-09-09 23:08:12 +08:00
|
|
|
// template-parameter that is not a template parameter pack.
|
2009-06-13 06:30:13 +08:00
|
|
|
if (Parm->isParameterPack()) {
|
|
|
|
Diag(DefaultLoc, diag::err_template_param_pack_default_arg);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
// C++ [temp.param]p14:
|
|
|
|
// A template-parameter shall not be used in its own default argument.
|
|
|
|
// FIXME: Implement this check! Needs a recursive walk over the types.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
// Check the template argument itself.
|
|
|
|
if (CheckTemplateArgument(Parm, Default, DefaultLoc)) {
|
|
|
|
Parm->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Parm->setDefaultArgument(Default, DefaultLoc, false);
|
|
|
|
}
|
|
|
|
|
2009-03-03 12:44:36 +08:00
|
|
|
/// \brief Check that the type of a non-type template parameter is
|
|
|
|
/// well-formed.
|
|
|
|
///
|
|
|
|
/// \returns the (possibly-promoted) parameter type if valid;
|
|
|
|
/// otherwise, produces a diagnostic and returns a NULL type.
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType
|
2009-03-03 12:44:36 +08:00
|
|
|
Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
|
2009-02-11 01:43:50 +08:00
|
|
|
// C++ [temp.param]p4:
|
|
|
|
//
|
|
|
|
// A non-type template-parameter shall have one of the following
|
|
|
|
// (optionally cv-qualified) types:
|
|
|
|
//
|
|
|
|
// -- integral or enumeration type,
|
|
|
|
if (T->isIntegralType() || T->isEnumeralType() ||
|
2009-09-09 23:08:12 +08:00
|
|
|
// -- pointer to object or pointer to function,
|
|
|
|
(T->isPointerType() &&
|
2009-07-30 05:53:49 +08:00
|
|
|
(T->getAs<PointerType>()->getPointeeType()->isObjectType() ||
|
|
|
|
T->getAs<PointerType>()->getPointeeType()->isFunctionType())) ||
|
2009-09-09 23:08:12 +08:00
|
|
|
// -- reference to object or reference to function,
|
2009-02-11 01:43:50 +08:00
|
|
|
T->isReferenceType() ||
|
|
|
|
// -- pointer to member.
|
|
|
|
T->isMemberPointerType() ||
|
|
|
|
// If T is a dependent type, we can't do the check now, so we
|
|
|
|
// assume that it is well-formed.
|
2009-03-03 12:44:36 +08:00
|
|
|
T->isDependentType())
|
|
|
|
return T;
|
2009-02-11 01:43:50 +08:00
|
|
|
// C++ [temp.param]p8:
|
|
|
|
//
|
|
|
|
// A non-type template-parameter of type "array of T" or
|
|
|
|
// "function returning T" is adjusted to be of type "pointer to
|
|
|
|
// T" or "pointer to function returning T", respectively.
|
|
|
|
else if (T->isArrayType())
|
|
|
|
// FIXME: Keep the type prior to promotion?
|
2009-03-03 12:44:36 +08:00
|
|
|
return Context.getArrayDecayedType(T);
|
2009-02-11 01:43:50 +08:00
|
|
|
else if (T->isFunctionType())
|
|
|
|
// FIXME: Keep the type prior to promotion?
|
2009-03-03 12:44:36 +08:00
|
|
|
return Context.getPointerType(T);
|
|
|
|
|
|
|
|
Diag(Loc, diag::err_template_nontype_parm_bad_type)
|
|
|
|
<< T;
|
|
|
|
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ActOnNonTypeTemplateParameter - Called when a C++ non-type
|
|
|
|
/// template parameter (e.g., "int Size" in "template<int Size>
|
|
|
|
/// class Array") has been parsed. S is the current scope and D is
|
|
|
|
/// the parsed declarator.
|
2009-03-29 03:18:32 +08:00
|
|
|
Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned Depth,
|
2009-03-29 03:18:32 +08:00
|
|
|
unsigned Position) {
|
2009-08-19 09:27:57 +08:00
|
|
|
DeclaratorInfo *DInfo = 0;
|
|
|
|
QualType T = GetTypeForDeclarator(D, S, &DInfo);
|
2009-03-03 12:44:36 +08:00
|
|
|
|
|
|
|
assert(S->isTemplateParamScope() &&
|
|
|
|
"Non-type template parameter not in template parameter scope!");
|
|
|
|
bool Invalid = false;
|
|
|
|
|
|
|
|
IdentifierInfo *ParamName = D.getIdentifier();
|
|
|
|
if (ParamName) {
|
|
|
|
NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName);
|
|
|
|
if (PrevDecl && PrevDecl->isTemplateParameter())
|
|
|
|
Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
|
|
|
|
PrevDecl);
|
2009-02-11 01:43:50 +08:00
|
|
|
}
|
|
|
|
|
2009-03-03 12:44:36 +08:00
|
|
|
T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
|
2009-03-10 00:46:39 +08:00
|
|
|
if (T.isNull()) {
|
2009-03-03 12:44:36 +08:00
|
|
|
T = Context.IntTy; // Recover with an 'int' type.
|
2009-03-10 00:46:39 +08:00
|
|
|
Invalid = true;
|
|
|
|
}
|
2009-03-03 12:44:36 +08:00
|
|
|
|
2008-12-06 02:15:24 +08:00
|
|
|
NonTypeTemplateParmDecl *Param
|
|
|
|
= NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(),
|
2009-08-19 09:27:57 +08:00
|
|
|
Depth, Position, ParamName, T, DInfo);
|
2008-12-06 02:15:24 +08:00
|
|
|
if (Invalid)
|
|
|
|
Param->setInvalidDecl();
|
|
|
|
|
|
|
|
if (D.getIdentifier()) {
|
|
|
|
// Add the template parameter into the current scope.
|
2009-03-29 03:18:32 +08:00
|
|
|
S->AddDecl(DeclPtrTy::make(Param));
|
2008-12-06 02:15:24 +08:00
|
|
|
IdResolver.AddDecl(Param);
|
|
|
|
}
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(Param);
|
2008-12-06 02:15:24 +08:00
|
|
|
}
|
2008-12-24 10:52:09 +08:00
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
/// \brief Adds a default argument to the given non-type template
|
|
|
|
/// parameter.
|
2009-03-29 03:18:32 +08:00
|
|
|
void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD,
|
2009-02-11 03:49:53 +08:00
|
|
|
SourceLocation EqualLoc,
|
|
|
|
ExprArg DefaultE) {
|
2009-09-09 23:08:12 +08:00
|
|
|
NonTypeTemplateParmDecl *TemplateParm
|
2009-03-29 03:18:32 +08:00
|
|
|
= cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>());
|
2009-02-11 03:49:53 +08:00
|
|
|
Expr *Default = static_cast<Expr *>(DefaultE.get());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
// C++ [temp.param]p14:
|
|
|
|
// A template-parameter shall not be used in its own default argument.
|
|
|
|
// FIXME: Implement this check! Needs a recursive walk over the types.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
// Check the well-formedness of the default template argument.
|
2009-06-12 02:10:32 +08:00
|
|
|
TemplateArgument Converted;
|
|
|
|
if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default,
|
|
|
|
Converted)) {
|
2009-02-11 03:49:53 +08:00
|
|
|
TemplateParm->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-02 03:49:17 +08:00
|
|
|
TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>());
|
2009-02-11 03:49:53 +08:00
|
|
|
}
|
|
|
|
|
2009-02-05 03:02:06 +08:00
|
|
|
|
|
|
|
/// ActOnTemplateTemplateParameter - Called when a C++ template template
|
|
|
|
/// parameter (e.g. T in template <template <typename> class T> class array)
|
|
|
|
/// has been parsed. S is the current scope.
|
2009-03-29 03:18:32 +08:00
|
|
|
Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S,
|
|
|
|
SourceLocation TmpLoc,
|
|
|
|
TemplateParamsTy *Params,
|
|
|
|
IdentifierInfo *Name,
|
|
|
|
SourceLocation NameLoc,
|
|
|
|
unsigned Depth,
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned Position) {
|
2009-02-05 03:02:06 +08:00
|
|
|
assert(S->isTemplateParamScope() &&
|
|
|
|
"Template template parameter not in template parameter scope!");
|
|
|
|
|
|
|
|
// Construct the parameter object.
|
|
|
|
TemplateTemplateParmDecl *Param =
|
|
|
|
TemplateTemplateParmDecl::Create(Context, CurContext, TmpLoc, Depth,
|
|
|
|
Position, Name,
|
|
|
|
(TemplateParameterList*)Params);
|
|
|
|
|
|
|
|
// Make sure the parameter is valid.
|
|
|
|
// FIXME: Decl object is not currently invalidated anywhere so this doesn't
|
|
|
|
// do anything yet. However, if the template parameter list or (eventual)
|
|
|
|
// default value is ever invalidated, that will propagate here.
|
|
|
|
bool Invalid = false;
|
|
|
|
if (Invalid) {
|
|
|
|
Param->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the tt-param has a name, then link the identifier into the scope
|
|
|
|
// and lookup mechanisms.
|
|
|
|
if (Name) {
|
2009-03-29 03:18:32 +08:00
|
|
|
S->AddDecl(DeclPtrTy::make(Param));
|
2009-02-05 03:02:06 +08:00
|
|
|
IdResolver.AddDecl(Param);
|
|
|
|
}
|
|
|
|
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(Param);
|
2009-02-05 03:02:06 +08:00
|
|
|
}
|
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
/// \brief Adds a default argument to the given template template
|
|
|
|
/// parameter.
|
2009-03-29 03:18:32 +08:00
|
|
|
void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD,
|
2009-02-11 03:49:53 +08:00
|
|
|
SourceLocation EqualLoc,
|
|
|
|
ExprArg DefaultE) {
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateTemplateParmDecl *TemplateParm
|
2009-03-29 03:18:32 +08:00
|
|
|
= cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>());
|
2009-02-11 03:49:53 +08:00
|
|
|
|
|
|
|
// Since a template-template parameter's default argument is an
|
|
|
|
// id-expression, it must be a DeclRefExpr.
|
2009-09-09 23:08:12 +08:00
|
|
|
DeclRefExpr *Default
|
2009-02-11 03:49:53 +08:00
|
|
|
= cast<DeclRefExpr>(static_cast<Expr *>(DefaultE.get()));
|
|
|
|
|
|
|
|
// C++ [temp.param]p14:
|
|
|
|
// A template-parameter shall not be used in its own default argument.
|
|
|
|
// FIXME: Implement this check! Needs a recursive walk over the types.
|
|
|
|
|
|
|
|
// Check the well-formedness of the template argument.
|
|
|
|
if (!isa<TemplateDecl>(Default->getDecl())) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Default->getSourceRange().getBegin(),
|
2009-02-11 03:49:53 +08:00
|
|
|
diag::err_template_arg_must_be_template)
|
|
|
|
<< Default->getSourceRange();
|
|
|
|
TemplateParm->setInvalidDecl();
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-02-11 03:49:53 +08:00
|
|
|
if (CheckTemplateArgument(TemplateParm, Default)) {
|
|
|
|
TemplateParm->setInvalidDecl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultE.release();
|
|
|
|
TemplateParm->setDefaultArgument(Default);
|
|
|
|
}
|
|
|
|
|
2008-12-24 10:52:09 +08:00
|
|
|
/// ActOnTemplateParameterList - Builds a TemplateParameterList that
|
|
|
|
/// contains the template parameters in Params/NumParams.
|
|
|
|
Sema::TemplateParamsTy *
|
|
|
|
Sema::ActOnTemplateParameterList(unsigned Depth,
|
|
|
|
SourceLocation ExportLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation TemplateLoc,
|
2008-12-24 10:52:09 +08:00
|
|
|
SourceLocation LAngleLoc,
|
2009-03-29 03:18:32 +08:00
|
|
|
DeclPtrTy *Params, unsigned NumParams,
|
2008-12-24 10:52:09 +08:00
|
|
|
SourceLocation RAngleLoc) {
|
|
|
|
if (ExportLoc.isValid())
|
|
|
|
Diag(ExportLoc, diag::note_template_export_unsupported);
|
|
|
|
|
2009-02-07 06:42:48 +08:00
|
|
|
return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
|
|
|
|
(Decl**)Params, NumParams, RAngleLoc);
|
|
|
|
}
|
|
|
|
|
2009-03-25 08:13:59 +08:00
|
|
|
Sema::DeclResult
|
2009-07-31 10:45:11 +08:00
|
|
|
Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
|
2009-02-07 06:42:48 +08:00
|
|
|
SourceLocation KWLoc, const CXXScopeSpec &SS,
|
|
|
|
IdentifierInfo *Name, SourceLocation NameLoc,
|
|
|
|
AttributeList *Attr,
|
2009-08-26 01:23:04 +08:00
|
|
|
TemplateParameterList *TemplateParams,
|
2009-03-26 08:52:18 +08:00
|
|
|
AccessSpecifier AS) {
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(TemplateParams && TemplateParams->size() > 0 &&
|
2009-08-26 01:23:04 +08:00
|
|
|
"No template parameters");
|
2009-07-31 10:45:11 +08:00
|
|
|
assert(TUK != TUK_Reference && "Can only declare or define class templates");
|
2009-02-11 03:49:53 +08:00
|
|
|
bool Invalid = false;
|
2009-02-07 06:42:48 +08:00
|
|
|
|
|
|
|
// Check that we can declare a template here.
|
2009-08-26 01:23:04 +08:00
|
|
|
if (CheckTemplateDeclScope(S, TemplateParams))
|
2009-03-25 08:13:59 +08:00
|
|
|
return true;
|
2009-02-07 06:42:48 +08:00
|
|
|
|
|
|
|
TagDecl::TagKind Kind;
|
|
|
|
switch (TagSpec) {
|
|
|
|
default: assert(0 && "Unknown tag type!");
|
|
|
|
case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
|
|
|
|
case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
|
|
|
|
case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is no such thing as an unnamed class template.
|
|
|
|
if (!Name) {
|
|
|
|
Diag(KWLoc, diag::err_template_unnamed_class);
|
2009-03-25 08:13:59 +08:00
|
|
|
return true;
|
2009-02-07 06:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find any previous declaration with this name.
|
2009-08-26 01:23:04 +08:00
|
|
|
DeclContext *SemanticContext;
|
|
|
|
LookupResult Previous;
|
|
|
|
if (SS.isNotEmpty() && !SS.isInvalid()) {
|
|
|
|
SemanticContext = computeDeclContext(SS, true);
|
|
|
|
if (!SemanticContext) {
|
|
|
|
// FIXME: Produce a reasonable diagnostic here
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
Previous = LookupQualifiedName(SemanticContext, Name, LookupOrdinaryName,
|
2009-08-26 01:23:04 +08:00
|
|
|
true);
|
|
|
|
} else {
|
|
|
|
SemanticContext = CurContext;
|
|
|
|
Previous = LookupName(S, Name, LookupOrdinaryName, true);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-07 06:42:48 +08:00
|
|
|
assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?");
|
|
|
|
NamedDecl *PrevDecl = 0;
|
|
|
|
if (Previous.begin() != Previous.end())
|
|
|
|
PrevDecl = *Previous.begin();
|
|
|
|
|
2009-08-26 01:23:04 +08:00
|
|
|
if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
|
2009-06-18 07:37:01 +08:00
|
|
|
PrevDecl = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-07 06:42:48 +08:00
|
|
|
// If there is a previous declaration with the same name, check
|
|
|
|
// whether this is a valid redeclaration.
|
2009-09-09 23:08:12 +08:00
|
|
|
ClassTemplateDecl *PrevClassTemplate
|
2009-02-07 06:42:48 +08:00
|
|
|
= dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
|
|
|
|
if (PrevClassTemplate) {
|
|
|
|
// Ensure that the template parameter lists are compatible.
|
|
|
|
if (!TemplateParameterListsAreEqual(TemplateParams,
|
|
|
|
PrevClassTemplate->getTemplateParameters(),
|
|
|
|
/*Complain=*/true))
|
2009-03-25 08:13:59 +08:00
|
|
|
return true;
|
2009-02-07 06:42:48 +08:00
|
|
|
|
|
|
|
// C++ [temp.class]p4:
|
|
|
|
// In a redeclaration, partial specialization, explicit
|
|
|
|
// specialization or explicit instantiation of a class template,
|
|
|
|
// the class-key shall agree in kind with the original class
|
|
|
|
// template declaration (7.1.5.3).
|
|
|
|
RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
|
2009-05-15 00:41:31 +08:00
|
|
|
if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(KWLoc, diag::err_use_with_wrong_tag)
|
2009-04-02 07:51:29 +08:00
|
|
|
<< Name
|
2009-09-09 23:08:12 +08:00
|
|
|
<< CodeModificationHint::CreateReplacement(KWLoc,
|
2009-04-02 07:51:29 +08:00
|
|
|
PrevRecordDecl->getKindName());
|
2009-02-07 06:42:48 +08:00
|
|
|
Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
|
2009-04-02 07:51:29 +08:00
|
|
|
Kind = PrevRecordDecl->getTagKind();
|
2009-02-07 06:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for redefinition of this class template.
|
2009-07-31 10:45:11 +08:00
|
|
|
if (TUK == TUK_Definition) {
|
2009-02-07 06:42:48 +08:00
|
|
|
if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) {
|
|
|
|
Diag(NameLoc, diag::err_redefinition) << Name;
|
|
|
|
Diag(Def->getLocation(), diag::note_previous_definition);
|
|
|
|
// FIXME: Would it make sense to try to "forget" the previous
|
|
|
|
// definition, as part of error recovery?
|
2009-03-25 08:13:59 +08:00
|
|
|
return true;
|
2009-02-07 06:42:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (PrevDecl && PrevDecl->isTemplateParameter()) {
|
|
|
|
// Maybe we will complain about the shadowed template parameter.
|
|
|
|
DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
|
|
|
|
// Just pretend that we didn't see the previous declaration.
|
|
|
|
PrevDecl = 0;
|
|
|
|
} else if (PrevDecl) {
|
|
|
|
// C++ [temp]p5:
|
|
|
|
// A class template shall not have the same name as any other
|
|
|
|
// template, class, function, object, enumeration, enumerator,
|
|
|
|
// namespace, or type in the same scope (3.3), except as specified
|
|
|
|
// in (14.5.4).
|
|
|
|
Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
|
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_definition);
|
2009-03-25 08:13:59 +08:00
|
|
|
return true;
|
2009-02-07 06:42:48 +08:00
|
|
|
}
|
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
// Check the template parameter list of this declaration, possibly
|
|
|
|
// merging in the template parameter list from the previous class
|
|
|
|
// template declaration.
|
|
|
|
if (CheckTemplateParameterList(TemplateParams,
|
|
|
|
PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0))
|
|
|
|
Invalid = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-11 06:57:19 +08:00
|
|
|
// FIXME: If we had a scope specifier, we better have a previous template
|
2009-02-07 06:42:48 +08:00
|
|
|
// declaration!
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
CXXRecordDecl *NewClass =
|
2009-07-21 22:46:17 +08:00
|
|
|
CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
PrevClassTemplate?
|
2009-05-16 03:11:46 +08:00
|
|
|
PrevClassTemplate->getTemplatedDecl() : 0,
|
|
|
|
/*DelayTypeCreation=*/true);
|
2009-02-07 06:42:48 +08:00
|
|
|
|
|
|
|
ClassTemplateDecl *NewTemplate
|
|
|
|
= ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
|
|
|
|
DeclarationName(Name), TemplateParams,
|
Introduce a new expression type, UnresolvedDeclRefExpr, that describes
dependent qualified-ids such as
Fibonacci<N - 1>::value
where N is a template parameter. These references are "unresolved"
because the name is dependent and, therefore, cannot be resolved to a
declaration node (as we would do for a DeclRefExpr or
QualifiedDeclRefExpr). UnresolvedDeclRefExprs instantiate to
DeclRefExprs, QualifiedDeclRefExprs, etc.
Also, be a bit more careful about keeping only a single set of
specializations for a class template, and instantiating from the
definition of that template rather than a previous declaration. In
general, we need a better solution for this for all TagDecls, because
it's too easy to accidentally look at a declaration that isn't the
definition.
We can now process a simple Fibonacci computation described as a
template metaprogram.
llvm-svn: 67308
2009-03-20 01:26:29 +08:00
|
|
|
NewClass, PrevClassTemplate);
|
2009-03-26 08:10:35 +08:00
|
|
|
NewClass->setDescribedClassTemplate(NewTemplate);
|
|
|
|
|
2009-05-16 03:11:46 +08:00
|
|
|
// Build the type for the class template declaration now.
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType T =
|
|
|
|
Context.getTypeDeclType(NewClass,
|
|
|
|
PrevClassTemplate?
|
|
|
|
PrevClassTemplate->getTemplatedDecl() : 0);
|
2009-05-16 03:11:46 +08:00
|
|
|
assert(T->isDependentType() && "Class template type is not dependent?");
|
|
|
|
(void)T;
|
|
|
|
|
2009-03-26 09:24:28 +08:00
|
|
|
// Set the access specifier.
|
|
|
|
SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-07 06:42:48 +08:00
|
|
|
// Set the lexical context of these templates
|
|
|
|
NewClass->setLexicalDeclContext(CurContext);
|
|
|
|
NewTemplate->setLexicalDeclContext(CurContext);
|
|
|
|
|
2009-07-31 10:45:11 +08:00
|
|
|
if (TUK == TUK_Definition)
|
2009-02-07 06:42:48 +08:00
|
|
|
NewClass->startDefinition();
|
|
|
|
|
|
|
|
if (Attr)
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(S, NewClass, Attr);
|
2009-02-07 06:42:48 +08:00
|
|
|
|
|
|
|
PushOnScopeChains(NewTemplate, S);
|
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
if (Invalid) {
|
|
|
|
NewTemplate->setInvalidDecl();
|
|
|
|
NewClass->setInvalidDecl();
|
|
|
|
}
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(NewTemplate);
|
2008-12-24 10:52:09 +08:00
|
|
|
}
|
2009-02-05 03:02:06 +08:00
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
/// \brief Checks the validity of a template parameter list, possibly
|
|
|
|
/// considering the template parameter list from a previous
|
|
|
|
/// declaration.
|
|
|
|
///
|
|
|
|
/// If an "old" template parameter list is provided, it must be
|
|
|
|
/// equivalent (per TemplateParameterListsAreEqual) to the "new"
|
|
|
|
/// template parameter list.
|
|
|
|
///
|
|
|
|
/// \param NewParams Template parameter list for a new template
|
|
|
|
/// declaration. This template parameter list will be updated with any
|
|
|
|
/// default arguments that are carried through from the previous
|
|
|
|
/// template parameter list.
|
|
|
|
///
|
|
|
|
/// \param OldParams If provided, template parameter list from a
|
|
|
|
/// previous declaration of the same template. Default template
|
|
|
|
/// arguments will be merged from the old template parameter list to
|
|
|
|
/// the new template parameter list.
|
|
|
|
///
|
|
|
|
/// \returns true if an error occurred, false otherwise.
|
|
|
|
bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
|
|
|
|
TemplateParameterList *OldParams) {
|
|
|
|
bool Invalid = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
// C++ [temp.param]p10:
|
|
|
|
// The set of default template-arguments available for use with a
|
|
|
|
// template declaration or definition is obtained by merging the
|
|
|
|
// default arguments from the definition (if in scope) and all
|
|
|
|
// declarations in scope in the same way default function
|
|
|
|
// arguments are (8.3.6).
|
|
|
|
bool SawDefaultArgument = false;
|
|
|
|
SourceLocation PreviousDefaultArgLoc;
|
|
|
|
|
2009-06-13 07:20:15 +08:00
|
|
|
bool SawParameterPack = false;
|
|
|
|
SourceLocation ParameterPackLoc;
|
|
|
|
|
2009-02-12 07:03:27 +08:00
|
|
|
// Dummy initialization to avoid warnings.
|
2009-02-12 04:46:19 +08:00
|
|
|
TemplateParameterList::iterator OldParam = NewParams->end();
|
2009-02-11 03:49:53 +08:00
|
|
|
if (OldParams)
|
|
|
|
OldParam = OldParams->begin();
|
|
|
|
|
|
|
|
for (TemplateParameterList::iterator NewParam = NewParams->begin(),
|
|
|
|
NewParamEnd = NewParams->end();
|
|
|
|
NewParam != NewParamEnd; ++NewParam) {
|
|
|
|
// Variables used to diagnose redundant default arguments
|
|
|
|
bool RedundantDefaultArg = false;
|
|
|
|
SourceLocation OldDefaultLoc;
|
|
|
|
SourceLocation NewDefaultLoc;
|
|
|
|
|
|
|
|
// Variables used to diagnose missing default arguments
|
|
|
|
bool MissingDefaultArg = false;
|
|
|
|
|
2009-06-13 07:20:15 +08:00
|
|
|
// C++0x [temp.param]p11:
|
|
|
|
// If a template parameter of a class template is a template parameter pack,
|
|
|
|
// it must be the last template parameter.
|
|
|
|
if (SawParameterPack) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(ParameterPackLoc,
|
2009-06-13 07:20:15 +08:00
|
|
|
diag::err_template_param_pack_must_be_last_template_parameter);
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
// Merge default arguments for template type parameters.
|
|
|
|
if (TemplateTypeParmDecl *NewTypeParm
|
|
|
|
= dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateTypeParmDecl *OldTypeParm
|
2009-02-11 03:49:53 +08:00
|
|
|
= OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 07:20:15 +08:00
|
|
|
if (NewTypeParm->isParameterPack()) {
|
|
|
|
assert(!NewTypeParm->hasDefaultArgument() &&
|
|
|
|
"Parameter packs can't have a default argument!");
|
|
|
|
SawParameterPack = true;
|
|
|
|
ParameterPackLoc = NewTypeParm->getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
|
2009-02-11 03:49:53 +08:00
|
|
|
NewTypeParm->hasDefaultArgument()) {
|
|
|
|
OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
|
|
|
|
NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
|
|
|
|
SawDefaultArgument = true;
|
|
|
|
RedundantDefaultArg = true;
|
|
|
|
PreviousDefaultArgLoc = NewDefaultLoc;
|
|
|
|
} else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
|
|
|
|
// Merge the default argument from the old declaration to the
|
|
|
|
// new declaration.
|
|
|
|
SawDefaultArgument = true;
|
|
|
|
NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgument(),
|
|
|
|
OldTypeParm->getDefaultArgumentLoc(),
|
|
|
|
true);
|
|
|
|
PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
|
|
|
|
} else if (NewTypeParm->hasDefaultArgument()) {
|
|
|
|
SawDefaultArgument = true;
|
|
|
|
PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
|
|
|
|
} else if (SawDefaultArgument)
|
|
|
|
MissingDefaultArg = true;
|
2009-08-05 05:02:39 +08:00
|
|
|
} else if (NonTypeTemplateParmDecl *NewNonTypeParm
|
2009-02-11 03:49:53 +08:00
|
|
|
= dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
|
2009-08-05 05:02:39 +08:00
|
|
|
// Merge default arguments for non-type template parameters
|
2009-02-11 03:49:53 +08:00
|
|
|
NonTypeTemplateParmDecl *OldNonTypeParm
|
|
|
|
= OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
|
2009-02-11 03:49:53 +08:00
|
|
|
NewNonTypeParm->hasDefaultArgument()) {
|
|
|
|
OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
|
|
|
|
NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
|
|
|
|
SawDefaultArgument = true;
|
|
|
|
RedundantDefaultArg = true;
|
|
|
|
PreviousDefaultArgLoc = NewDefaultLoc;
|
|
|
|
} else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
|
|
|
|
// Merge the default argument from the old declaration to the
|
|
|
|
// new declaration.
|
|
|
|
SawDefaultArgument = true;
|
|
|
|
// FIXME: We need to create a new kind of "default argument"
|
|
|
|
// expression that points to a previous template template
|
|
|
|
// parameter.
|
|
|
|
NewNonTypeParm->setDefaultArgument(
|
|
|
|
OldNonTypeParm->getDefaultArgument());
|
|
|
|
PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
|
|
|
|
} else if (NewNonTypeParm->hasDefaultArgument()) {
|
|
|
|
SawDefaultArgument = true;
|
|
|
|
PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
|
|
|
|
} else if (SawDefaultArgument)
|
2009-09-09 23:08:12 +08:00
|
|
|
MissingDefaultArg = true;
|
2009-08-05 05:02:39 +08:00
|
|
|
} else {
|
2009-02-11 03:49:53 +08:00
|
|
|
// Merge default arguments for template template parameters
|
|
|
|
TemplateTemplateParmDecl *NewTemplateParm
|
|
|
|
= cast<TemplateTemplateParmDecl>(*NewParam);
|
|
|
|
TemplateTemplateParmDecl *OldTemplateParm
|
|
|
|
= OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
|
2009-02-11 03:49:53 +08:00
|
|
|
NewTemplateParm->hasDefaultArgument()) {
|
|
|
|
OldDefaultLoc = OldTemplateParm->getDefaultArgumentLoc();
|
|
|
|
NewDefaultLoc = NewTemplateParm->getDefaultArgumentLoc();
|
|
|
|
SawDefaultArgument = true;
|
|
|
|
RedundantDefaultArg = true;
|
|
|
|
PreviousDefaultArgLoc = NewDefaultLoc;
|
|
|
|
} else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
|
|
|
|
// Merge the default argument from the old declaration to the
|
|
|
|
// new declaration.
|
|
|
|
SawDefaultArgument = true;
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: We need to create a new kind of "default argument" expression
|
|
|
|
// that points to a previous template template parameter.
|
2009-02-11 03:49:53 +08:00
|
|
|
NewTemplateParm->setDefaultArgument(
|
|
|
|
OldTemplateParm->getDefaultArgument());
|
|
|
|
PreviousDefaultArgLoc = OldTemplateParm->getDefaultArgumentLoc();
|
|
|
|
} else if (NewTemplateParm->hasDefaultArgument()) {
|
|
|
|
SawDefaultArgument = true;
|
|
|
|
PreviousDefaultArgLoc = NewTemplateParm->getDefaultArgumentLoc();
|
|
|
|
} else if (SawDefaultArgument)
|
2009-09-09 23:08:12 +08:00
|
|
|
MissingDefaultArg = true;
|
2009-02-11 03:49:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (RedundantDefaultArg) {
|
|
|
|
// C++ [temp.param]p12:
|
|
|
|
// A template-parameter shall not be given default arguments
|
|
|
|
// by two different declarations in the same scope.
|
|
|
|
Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
|
|
|
|
Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
|
|
|
|
Invalid = true;
|
|
|
|
} else if (MissingDefaultArg) {
|
|
|
|
// C++ [temp.param]p11:
|
|
|
|
// If a template-parameter has a default template-argument,
|
|
|
|
// all subsequent template-parameters shall have a default
|
|
|
|
// template-argument supplied.
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag((*NewParam)->getLocation(),
|
2009-02-11 03:49:53 +08:00
|
|
|
diag::err_template_param_default_arg_missing);
|
|
|
|
Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
|
|
|
|
Invalid = true;
|
|
|
|
}
|
2009-02-10 07:23:08 +08:00
|
|
|
|
2009-02-11 03:49:53 +08:00
|
|
|
// If we have an old template parameter list that we're merging
|
|
|
|
// in, move on to the next parameter.
|
|
|
|
if (OldParams)
|
|
|
|
++OldParam;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Invalid;
|
|
|
|
}
|
2009-02-10 07:23:08 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// \brief Match the given template parameter lists to the given scope
|
2009-07-22 07:53:31 +08:00
|
|
|
/// specifier, returning the template parameter list that applies to the
|
|
|
|
/// name.
|
|
|
|
///
|
|
|
|
/// \param DeclStartLoc the start of the declaration that has a scope
|
|
|
|
/// specifier or a template parameter list.
|
2009-09-09 23:08:12 +08:00
|
|
|
///
|
2009-07-22 07:53:31 +08:00
|
|
|
/// \param SS the scope specifier that will be matched to the given template
|
|
|
|
/// parameter lists. This scope specifier precedes a qualified name that is
|
|
|
|
/// being declared.
|
|
|
|
///
|
|
|
|
/// \param ParamLists the template parameter lists, from the outermost to the
|
|
|
|
/// innermost template parameter lists.
|
|
|
|
///
|
|
|
|
/// \param NumParamLists the number of template parameter lists in ParamLists.
|
|
|
|
///
|
2009-09-09 23:08:12 +08:00
|
|
|
/// \returns the template parameter list, if any, that corresponds to the
|
2009-07-22 07:53:31 +08:00
|
|
|
/// name that is preceded by the scope specifier @p SS. This template
|
|
|
|
/// parameter list may be have template parameters (if we're declaring a
|
2009-09-09 23:08:12 +08:00
|
|
|
/// template) or may have no template parameters (if we're declaring a
|
2009-07-22 07:53:31 +08:00
|
|
|
/// template specialization), or may be NULL (if we were's declaring isn't
|
|
|
|
/// itself a template).
|
|
|
|
TemplateParameterList *
|
|
|
|
Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
|
|
|
|
const CXXScopeSpec &SS,
|
|
|
|
TemplateParameterList **ParamLists,
|
|
|
|
unsigned NumParamLists) {
|
|
|
|
// Find the template-ids that occur within the nested-name-specifier. These
|
|
|
|
// template-ids will match up with the template parameter lists.
|
|
|
|
llvm::SmallVector<const TemplateSpecializationType *, 4>
|
|
|
|
TemplateIdsInSpecifier;
|
|
|
|
for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
|
|
|
|
NNS; NNS = NNS->getPrefix()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (const TemplateSpecializationType *SpecType
|
2009-07-22 07:53:31 +08:00
|
|
|
= dyn_cast_or_null<TemplateSpecializationType>(NNS->getAsType())) {
|
|
|
|
TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
|
|
|
|
if (!Template)
|
|
|
|
continue; // FIXME: should this be an error? probably...
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-30 05:53:49 +08:00
|
|
|
if (const RecordType *Record = SpecType->getAs<RecordType>()) {
|
2009-07-22 07:53:31 +08:00
|
|
|
ClassTemplateSpecializationDecl *SpecDecl
|
|
|
|
= cast<ClassTemplateSpecializationDecl>(Record->getDecl());
|
|
|
|
// If the nested name specifier refers to an explicit specialization,
|
|
|
|
// we don't need a template<> header.
|
2009-09-09 23:08:12 +08:00
|
|
|
// FIXME: revisit this approach once we cope with specialization
|
2009-07-31 01:40:51 +08:00
|
|
|
// properly.
|
2009-07-22 07:53:31 +08:00
|
|
|
if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization)
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 07:53:31 +08:00
|
|
|
TemplateIdsInSpecifier.push_back(SpecType);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 07:53:31 +08:00
|
|
|
// Reverse the list of template-ids in the scope specifier, so that we can
|
|
|
|
// more easily match up the template-ids and the template parameter lists.
|
|
|
|
std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 07:53:31 +08:00
|
|
|
SourceLocation FirstTemplateLoc = DeclStartLoc;
|
|
|
|
if (NumParamLists)
|
|
|
|
FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 07:53:31 +08:00
|
|
|
// Match the template-ids found in the specifier to the template parameter
|
|
|
|
// lists.
|
|
|
|
unsigned Idx = 0;
|
|
|
|
for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
|
|
|
|
Idx != NumTemplateIds; ++Idx) {
|
2009-07-31 01:40:51 +08:00
|
|
|
QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0);
|
|
|
|
bool DependentTemplateId = TemplateId->isDependentType();
|
2009-07-22 07:53:31 +08:00
|
|
|
if (Idx >= NumParamLists) {
|
|
|
|
// We have a template-id without a corresponding template parameter
|
|
|
|
// list.
|
|
|
|
if (DependentTemplateId) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// FIXME: the location information here isn't great.
|
|
|
|
Diag(SS.getRange().getBegin(),
|
2009-07-22 07:53:31 +08:00
|
|
|
diag::err_template_spec_needs_template_parameters)
|
2009-07-31 01:40:51 +08:00
|
|
|
<< TemplateId
|
2009-07-22 07:53:31 +08:00
|
|
|
<< SS.getRange();
|
|
|
|
} else {
|
|
|
|
Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
|
|
|
|
<< SS.getRange()
|
|
|
|
<< CodeModificationHint::CreateInsertion(FirstTemplateLoc,
|
|
|
|
"template<> ");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 07:53:31 +08:00
|
|
|
// Check the template parameter list against its corresponding template-id.
|
2009-07-31 01:40:51 +08:00
|
|
|
if (DependentTemplateId) {
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateDecl *Template
|
2009-07-31 01:40:51 +08:00
|
|
|
= TemplateIdsInSpecifier[Idx]->getTemplateName().getAsTemplateDecl();
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ClassTemplateDecl *ClassTemplate
|
2009-07-31 01:40:51 +08:00
|
|
|
= dyn_cast<ClassTemplateDecl>(Template)) {
|
|
|
|
TemplateParameterList *ExpectedTemplateParams = 0;
|
|
|
|
// Is this template-id naming the primary template?
|
|
|
|
if (Context.hasSameType(TemplateId,
|
|
|
|
ClassTemplate->getInjectedClassNameType(Context)))
|
|
|
|
ExpectedTemplateParams = ClassTemplate->getTemplateParameters();
|
|
|
|
// ... or a partial specialization?
|
|
|
|
else if (ClassTemplatePartialSpecializationDecl *PartialSpec
|
|
|
|
= ClassTemplate->findPartialSpecialization(TemplateId))
|
|
|
|
ExpectedTemplateParams = PartialSpec->getTemplateParameters();
|
|
|
|
|
|
|
|
if (ExpectedTemplateParams)
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateParameterListsAreEqual(ParamLists[Idx],
|
2009-07-31 01:40:51 +08:00
|
|
|
ExpectedTemplateParams,
|
|
|
|
true);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-07-31 01:40:51 +08:00
|
|
|
} else if (ParamLists[Idx]->size() > 0)
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(ParamLists[Idx]->getTemplateLoc(),
|
2009-07-31 01:40:51 +08:00
|
|
|
diag::err_template_param_list_matches_nontemplate)
|
|
|
|
<< TemplateId
|
|
|
|
<< ParamLists[Idx]->getSourceRange();
|
2009-07-22 07:53:31 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 07:53:31 +08:00
|
|
|
// If there were at least as many template-ids as there were template
|
|
|
|
// parameter lists, then there are no template parameter lists remaining for
|
|
|
|
// the declaration itself.
|
|
|
|
if (Idx >= NumParamLists)
|
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 07:53:31 +08:00
|
|
|
// If there were too many template parameter lists, complain about that now.
|
|
|
|
if (Idx != NumParamLists - 1) {
|
|
|
|
while (Idx < NumParamLists - 1) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(ParamLists[Idx]->getTemplateLoc(),
|
2009-07-22 07:53:31 +08:00
|
|
|
diag::err_template_spec_extra_headers)
|
|
|
|
<< SourceRange(ParamLists[Idx]->getTemplateLoc(),
|
|
|
|
ParamLists[Idx]->getRAngleLoc());
|
|
|
|
++Idx;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 07:53:31 +08:00
|
|
|
// Return the last template parameter list, which corresponds to the
|
|
|
|
// entity being declared.
|
|
|
|
return ParamLists[NumParamLists - 1];
|
|
|
|
}
|
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
/// \brief Translates template arguments as provided by the parser
|
|
|
|
/// into template arguments used by semantic analysis.
|
2009-09-09 23:08:12 +08:00
|
|
|
static void
|
|
|
|
translateTemplateArguments(ASTTemplateArgsPtr &TemplateArgsIn,
|
2009-02-18 07:15:12 +08:00
|
|
|
SourceLocation *TemplateArgLocs,
|
2009-03-10 07:48:35 +08:00
|
|
|
llvm::SmallVector<TemplateArgument, 16> &TemplateArgs) {
|
|
|
|
TemplateArgs.reserve(TemplateArgsIn.size());
|
|
|
|
|
|
|
|
void **Args = TemplateArgsIn.getArgs();
|
|
|
|
bool *ArgIsType = TemplateArgsIn.getArgIsType();
|
|
|
|
for (unsigned Arg = 0, Last = TemplateArgsIn.size(); Arg != Last; ++Arg) {
|
|
|
|
TemplateArgs.push_back(
|
|
|
|
ArgIsType[Arg]? TemplateArgument(TemplateArgLocs[Arg],
|
2009-08-19 09:28:28 +08:00
|
|
|
//FIXME: Preserve type source info.
|
|
|
|
Sema::GetTypeFromParser(Args[Arg]))
|
2009-03-10 07:48:35 +08:00
|
|
|
: TemplateArgument(reinterpret_cast<Expr *>(Args[Arg])));
|
|
|
|
}
|
|
|
|
}
|
2009-02-10 02:46:07 +08:00
|
|
|
|
2009-03-31 06:58:21 +08:00
|
|
|
QualType Sema::CheckTemplateIdType(TemplateName Name,
|
|
|
|
SourceLocation TemplateLoc,
|
|
|
|
SourceLocation LAngleLoc,
|
|
|
|
const TemplateArgument *TemplateArgs,
|
|
|
|
unsigned NumTemplateArgs,
|
|
|
|
SourceLocation RAngleLoc) {
|
|
|
|
TemplateDecl *Template = Name.getAsTemplateDecl();
|
2009-03-31 08:43:58 +08:00
|
|
|
if (!Template) {
|
|
|
|
// The template name does not resolve to a template, so we just
|
|
|
|
// build a dependent template-id type.
|
|
|
|
return Context.getTemplateSpecializationType(Name, TemplateArgs,
|
2009-07-29 07:00:59 +08:00
|
|
|
NumTemplateArgs);
|
2009-03-31 08:43:58 +08:00
|
|
|
}
|
2009-03-31 06:58:21 +08:00
|
|
|
|
2009-02-10 07:23:08 +08:00
|
|
|
// Check that the template argument list is well-formed for this
|
|
|
|
// template.
|
2009-06-23 09:26:57 +08:00
|
|
|
TemplateArgumentListBuilder Converted(Template->getTemplateParameters(),
|
|
|
|
NumTemplateArgs);
|
2009-09-09 23:08:12 +08:00
|
|
|
if (CheckTemplateArgumentList(Template, TemplateLoc, LAngleLoc,
|
2009-03-10 07:48:35 +08:00
|
|
|
TemplateArgs, NumTemplateArgs, RAngleLoc,
|
2009-07-01 08:28:38 +08:00
|
|
|
false, Converted))
|
2009-03-10 07:48:35 +08:00
|
|
|
return QualType();
|
2009-02-10 07:23:08 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
assert((Converted.structuredSize() ==
|
2009-03-31 06:58:21 +08:00
|
|
|
Template->getTemplateParameters()->size()) &&
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
"Converted template argument list is too short!");
|
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
QualType CanonType;
|
|
|
|
|
2009-03-31 06:58:21 +08:00
|
|
|
if (TemplateSpecializationType::anyDependentTemplateArguments(
|
2009-03-10 07:48:35 +08:00
|
|
|
TemplateArgs,
|
|
|
|
NumTemplateArgs)) {
|
|
|
|
// This class template specialization is a dependent
|
|
|
|
// type. Therefore, its canonical type is another class template
|
|
|
|
// specialization type that contains all of the converted
|
|
|
|
// arguments in canonical form. This ensures that, e.g., A<T> and
|
|
|
|
// A<T, T> have identical types when A is declared as:
|
|
|
|
//
|
|
|
|
// template<typename T, typename U = T> struct A;
|
2009-05-07 14:41:52 +08:00
|
|
|
TemplateName CanonName = Context.getCanonicalTemplateName(Name);
|
2009-09-09 23:08:12 +08:00
|
|
|
CanonType = Context.getTemplateSpecializationType(CanonName,
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.getFlatArguments(),
|
|
|
|
Converted.flatSize());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-29 07:00:59 +08:00
|
|
|
// FIXME: CanonType is not actually the canonical type, and unfortunately
|
|
|
|
// it is a TemplateTypeSpecializationType that we will never use again.
|
|
|
|
// In the future, we need to teach getTemplateSpecializationType to only
|
|
|
|
// build the canonical type and return that to us.
|
|
|
|
CanonType = Context.getCanonicalType(CanonType);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (ClassTemplateDecl *ClassTemplate
|
2009-03-31 06:58:21 +08:00
|
|
|
= dyn_cast<ClassTemplateDecl>(Template)) {
|
2009-03-10 07:48:35 +08:00
|
|
|
// Find the class template specialization declaration that
|
|
|
|
// corresponds to these arguments.
|
|
|
|
llvm::FoldingSetNodeID ID;
|
2009-09-09 23:08:12 +08:00
|
|
|
ClassTemplateSpecializationDecl::Profile(ID,
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.getFlatArguments(),
|
2009-07-30 00:09:57 +08:00
|
|
|
Converted.flatSize(),
|
|
|
|
Context);
|
2009-03-10 07:48:35 +08:00
|
|
|
void *InsertPos = 0;
|
|
|
|
ClassTemplateSpecializationDecl *Decl
|
|
|
|
= ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
if (!Decl) {
|
|
|
|
// This is the first time we have referenced this class template
|
|
|
|
// specialization. Create the canonical declaration and add it to
|
|
|
|
// the set of specializations.
|
2009-09-09 23:08:12 +08:00
|
|
|
Decl = ClassTemplateSpecializationDecl::Create(Context,
|
2009-06-05 11:43:12 +08:00
|
|
|
ClassTemplate->getDeclContext(),
|
|
|
|
TemplateLoc,
|
|
|
|
ClassTemplate,
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted, 0);
|
2009-03-10 07:48:35 +08:00
|
|
|
ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos);
|
|
|
|
Decl->setLexicalDeclContext(CurContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
CanonType = Context.getTypeDeclType(Decl);
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
// Build the fully-sugared type for this class template
|
|
|
|
// specialization, which refers back to the class template
|
|
|
|
// specialization we created or found.
|
2009-08-19 09:28:28 +08:00
|
|
|
//FIXME: Preserve type source info.
|
2009-03-31 06:58:21 +08:00
|
|
|
return Context.getTemplateSpecializationType(Name, TemplateArgs,
|
|
|
|
NumTemplateArgs, CanonType);
|
2009-03-10 07:48:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::TypeResult
|
2009-03-31 06:58:21 +08:00
|
|
|
Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation LAngleLoc,
|
2009-03-31 06:58:21 +08:00
|
|
|
ASTTemplateArgsPtr TemplateArgsIn,
|
|
|
|
SourceLocation *TemplateArgLocs,
|
2009-09-09 01:47:29 +08:00
|
|
|
SourceLocation RAngleLoc) {
|
2009-03-31 06:58:21 +08:00
|
|
|
TemplateName Template = TemplateD.getAsVal<TemplateName>();
|
2009-03-10 07:48:35 +08:00
|
|
|
|
|
|
|
// Translate the parser's template argument list in our AST format.
|
|
|
|
llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
|
|
|
|
translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
|
|
|
|
|
2009-03-31 06:58:21 +08:00
|
|
|
QualType Result = CheckTemplateIdType(Template, TemplateLoc, LAngleLoc,
|
2009-05-21 17:52:38 +08:00
|
|
|
TemplateArgs.data(),
|
|
|
|
TemplateArgs.size(),
|
2009-03-31 06:58:21 +08:00
|
|
|
RAngleLoc);
|
2009-03-10 07:48:35 +08:00
|
|
|
TemplateArgsIn.release();
|
2009-04-02 05:51:26 +08:00
|
|
|
|
|
|
|
if (Result.isNull())
|
|
|
|
return true;
|
|
|
|
|
2009-09-09 01:47:29 +08:00
|
|
|
return Result.getAsOpaquePtr();
|
|
|
|
}
|
2009-09-04 09:14:41 +08:00
|
|
|
|
2009-09-09 01:47:29 +08:00
|
|
|
Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult,
|
|
|
|
TagUseKind TUK,
|
|
|
|
DeclSpec::TST TagSpec,
|
|
|
|
SourceLocation TagLoc) {
|
|
|
|
if (TypeResult.isInvalid())
|
|
|
|
return Sema::TypeResult();
|
2009-09-04 09:14:41 +08:00
|
|
|
|
2009-09-09 01:47:29 +08:00
|
|
|
QualType Type = QualType::getFromOpaquePtr(TypeResult.get());
|
2009-09-04 09:14:41 +08:00
|
|
|
|
2009-09-09 01:47:29 +08:00
|
|
|
// Verify the tag specifier.
|
|
|
|
TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-09 01:47:29 +08:00
|
|
|
if (const RecordType *RT = Type->getAs<RecordType>()) {
|
|
|
|
RecordDecl *D = RT->getDecl();
|
|
|
|
|
|
|
|
IdentifierInfo *Id = D->getIdentifier();
|
|
|
|
assert(Id && "templated class must have an identifier");
|
|
|
|
|
|
|
|
if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
|
|
|
|
Diag(TagLoc, diag::err_use_with_wrong_tag)
|
|
|
|
<< Id
|
|
|
|
<< CodeModificationHint::CreateReplacement(SourceRange(TagLoc),
|
|
|
|
D->getKindName());
|
2009-09-04 09:14:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 01:47:29 +08:00
|
|
|
QualType ElabType = Context.getElaboratedType(Type, TagKind);
|
|
|
|
|
|
|
|
return ElabType.getAsOpaquePtr();
|
2009-02-10 02:46:07 +08:00
|
|
|
}
|
|
|
|
|
2009-07-01 06:34:41 +08:00
|
|
|
Sema::OwningExprResult Sema::BuildTemplateIdExpr(TemplateName Template,
|
|
|
|
SourceLocation TemplateNameLoc,
|
|
|
|
SourceLocation LAngleLoc,
|
|
|
|
const TemplateArgument *TemplateArgs,
|
|
|
|
unsigned NumTemplateArgs,
|
|
|
|
SourceLocation RAngleLoc) {
|
|
|
|
// FIXME: Can we do any checking at this point? I guess we could check the
|
|
|
|
// template arguments that we have against the template name, if the template
|
2009-09-09 23:08:12 +08:00
|
|
|
// name refers to a single template. That's not a terribly common case,
|
2009-07-01 06:34:41 +08:00
|
|
|
// though.
|
2009-09-09 23:08:12 +08:00
|
|
|
return Owned(TemplateIdRefExpr::Create(Context,
|
2009-07-01 06:34:41 +08:00
|
|
|
/*FIXME: New type?*/Context.OverloadTy,
|
|
|
|
/*FIXME: Necessary?*/0,
|
|
|
|
/*FIXME: Necessary?*/SourceRange(),
|
|
|
|
Template, TemplateNameLoc, LAngleLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateArgs,
|
2009-07-01 06:34:41 +08:00
|
|
|
NumTemplateArgs, RAngleLoc));
|
|
|
|
}
|
|
|
|
|
|
|
|
Sema::OwningExprResult Sema::ActOnTemplateIdExpr(TemplateTy TemplateD,
|
|
|
|
SourceLocation TemplateNameLoc,
|
|
|
|
SourceLocation LAngleLoc,
|
|
|
|
ASTTemplateArgsPtr TemplateArgsIn,
|
|
|
|
SourceLocation *TemplateArgLocs,
|
|
|
|
SourceLocation RAngleLoc) {
|
|
|
|
TemplateName Template = TemplateD.getAsVal<TemplateName>();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-01 06:34:41 +08:00
|
|
|
// Translate the parser's template argument list in our AST format.
|
|
|
|
llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
|
|
|
|
translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
|
2009-07-23 04:55:49 +08:00
|
|
|
TemplateArgsIn.release();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-01 06:34:41 +08:00
|
|
|
return BuildTemplateIdExpr(Template, TemplateNameLoc, LAngleLoc,
|
|
|
|
TemplateArgs.data(), TemplateArgs.size(),
|
|
|
|
RAngleLoc);
|
|
|
|
}
|
|
|
|
|
2009-09-01 08:37:14 +08:00
|
|
|
Sema::OwningExprResult
|
|
|
|
Sema::ActOnMemberTemplateIdReferenceExpr(Scope *S, ExprArg Base,
|
|
|
|
SourceLocation OpLoc,
|
|
|
|
tok::TokenKind OpKind,
|
|
|
|
const CXXScopeSpec &SS,
|
|
|
|
TemplateTy TemplateD,
|
|
|
|
SourceLocation TemplateNameLoc,
|
|
|
|
SourceLocation LAngleLoc,
|
|
|
|
ASTTemplateArgsPtr TemplateArgsIn,
|
|
|
|
SourceLocation *TemplateArgLocs,
|
|
|
|
SourceLocation RAngleLoc) {
|
|
|
|
TemplateName Template = TemplateD.getAsVal<TemplateName>();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-01 08:37:14 +08:00
|
|
|
// FIXME: We're going to end up looking up the template based on its name,
|
|
|
|
// twice!
|
|
|
|
DeclarationName Name;
|
|
|
|
if (TemplateDecl *ActualTemplate = Template.getAsTemplateDecl())
|
|
|
|
Name = ActualTemplate->getDeclName();
|
|
|
|
else if (OverloadedFunctionDecl *Ovl = Template.getAsOverloadedFunctionDecl())
|
|
|
|
Name = Ovl->getDeclName();
|
|
|
|
else
|
2009-09-09 08:23:06 +08:00
|
|
|
Name = Template.getAsDependentTemplateName()->getName();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-01 08:37:14 +08:00
|
|
|
// Translate the parser's template argument list in our AST format.
|
|
|
|
llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
|
|
|
|
translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
|
|
|
|
TemplateArgsIn.release();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-01 08:37:14 +08:00
|
|
|
// Do we have the save the actual template name? We might need it...
|
|
|
|
return BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, TemplateNameLoc,
|
|
|
|
Name, true, LAngleLoc,
|
|
|
|
TemplateArgs.data(), TemplateArgs.size(),
|
2009-09-09 23:08:12 +08:00
|
|
|
RAngleLoc, DeclPtrTy(), &SS);
|
2009-09-01 08:37:14 +08:00
|
|
|
}
|
|
|
|
|
2009-03-31 08:43:58 +08:00
|
|
|
/// \brief Form a dependent template name.
|
|
|
|
///
|
|
|
|
/// This action forms a dependent template name given the template
|
|
|
|
/// name and its (presumably dependent) scope specifier. For
|
|
|
|
/// example, given "MetaFun::template apply", the scope specifier \p
|
|
|
|
/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
|
|
|
|
/// of the "template" keyword, and "apply" is the \p Name.
|
2009-09-09 23:08:12 +08:00
|
|
|
Sema::TemplateTy
|
2009-03-31 08:43:58 +08:00
|
|
|
Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc,
|
|
|
|
const IdentifierInfo &Name,
|
|
|
|
SourceLocation NameLoc,
|
2009-09-03 06:59:36 +08:00
|
|
|
const CXXScopeSpec &SS,
|
|
|
|
TypeTy *ObjectType) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if ((ObjectType &&
|
2009-09-03 06:59:36 +08:00
|
|
|
computeDeclContext(QualType::getFromOpaquePtr(ObjectType))) ||
|
|
|
|
(SS.isSet() && computeDeclContext(SS, false))) {
|
2009-03-31 08:43:58 +08:00
|
|
|
// C++0x [temp.names]p5:
|
|
|
|
// If a name prefixed by the keyword template is not the name of
|
|
|
|
// a template, the program is ill-formed. [Note: the keyword
|
|
|
|
// template may not be applied to non-template members of class
|
|
|
|
// templates. -end note ] [ Note: as is the case with the
|
|
|
|
// typename prefix, the template prefix is allowed in cases
|
|
|
|
// where it is not strictly necessary; i.e., when the
|
|
|
|
// nested-name-specifier or the expression on the left of the ->
|
|
|
|
// or . is not dependent on a template-parameter, or the use
|
|
|
|
// does not appear in the scope of a template. -end note]
|
|
|
|
//
|
|
|
|
// Note: C++03 was more strict here, because it banned the use of
|
|
|
|
// the "template" keyword prior to a template-name that was not a
|
|
|
|
// dependent name. C++ DR468 relaxed this requirement (the
|
|
|
|
// "template" keyword is now permitted). We follow the C++0x
|
|
|
|
// rules, even in C++03 mode, retroactively applying the DR.
|
|
|
|
TemplateTy Template;
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateNameKind TNK = isTemplateName(0, Name, NameLoc, &SS, ObjectType,
|
2009-09-03 06:59:36 +08:00
|
|
|
false, Template);
|
2009-03-31 08:43:58 +08:00
|
|
|
if (TNK == TNK_Non_template) {
|
|
|
|
Diag(NameLoc, diag::err_template_kw_refers_to_non_template)
|
|
|
|
<< &Name;
|
|
|
|
return TemplateTy();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Template;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
NestedNameSpecifier *Qualifier
|
2009-09-03 06:59:36 +08:00
|
|
|
= static_cast<NestedNameSpecifier *>(SS.getScopeRep());
|
2009-03-31 08:43:58 +08:00
|
|
|
return TemplateTy::make(Context.getDependentTemplateName(Qualifier, &Name));
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
|
2009-06-13 08:33:33 +08:00
|
|
|
const TemplateArgument &Arg,
|
|
|
|
TemplateArgumentListBuilder &Converted) {
|
|
|
|
// Check template type parameter.
|
|
|
|
if (Arg.getKind() != TemplateArgument::Type) {
|
|
|
|
// C++ [temp.arg.type]p1:
|
|
|
|
// A template-argument for a template-parameter which is a
|
|
|
|
// type shall be a type-id.
|
|
|
|
|
|
|
|
// We have a template type parameter but the template argument
|
|
|
|
// is not a type.
|
|
|
|
Diag(Arg.getLocation(), diag::err_template_arg_must_be_type);
|
|
|
|
Diag(Param->getLocation(), diag::note_template_param_here);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 08:33:33 +08:00
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-06-13 08:33:33 +08:00
|
|
|
|
|
|
|
if (CheckTemplateArgument(Param, Arg.getAsType(), Arg.getLocation()))
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 08:33:33 +08:00
|
|
|
// Add the converted template type argument.
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.Append(
|
2009-06-13 08:33:33 +08:00
|
|
|
TemplateArgument(Arg.getLocation(),
|
|
|
|
Context.getCanonicalType(Arg.getAsType())));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-10 07:23:08 +08:00
|
|
|
/// \brief Check that the given template argument list is well-formed
|
|
|
|
/// for specializing the given template.
|
|
|
|
bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
|
|
|
|
SourceLocation TemplateLoc,
|
|
|
|
SourceLocation LAngleLoc,
|
2009-03-10 07:48:35 +08:00
|
|
|
const TemplateArgument *TemplateArgs,
|
|
|
|
unsigned NumTemplateArgs,
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
SourceLocation RAngleLoc,
|
2009-07-01 08:28:38 +08:00
|
|
|
bool PartialTemplateArgs,
|
2009-06-05 11:43:12 +08:00
|
|
|
TemplateArgumentListBuilder &Converted) {
|
2009-02-10 07:23:08 +08:00
|
|
|
TemplateParameterList *Params = Template->getTemplateParameters();
|
|
|
|
unsigned NumParams = Params->size();
|
2009-03-10 07:48:35 +08:00
|
|
|
unsigned NumArgs = NumTemplateArgs;
|
2009-02-10 07:23:08 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool HasParameterPack =
|
2009-06-13 10:08:00 +08:00
|
|
|
NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 10:08:00 +08:00
|
|
|
if ((NumArgs > NumParams && !HasParameterPack) ||
|
2009-07-01 08:28:38 +08:00
|
|
|
(NumArgs < Params->getMinRequiredArguments() &&
|
|
|
|
!PartialTemplateArgs)) {
|
2009-02-10 07:23:08 +08:00
|
|
|
// FIXME: point at either the first arg beyond what we can handle,
|
|
|
|
// or the '>', depending on whether we have too many or too few
|
|
|
|
// arguments.
|
|
|
|
SourceRange Range;
|
|
|
|
if (NumArgs > NumParams)
|
2009-03-10 07:48:35 +08:00
|
|
|
Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
|
2009-02-10 07:23:08 +08:00
|
|
|
Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
|
|
|
|
<< (NumArgs > NumParams)
|
|
|
|
<< (isa<ClassTemplateDecl>(Template)? 0 :
|
|
|
|
isa<FunctionTemplateDecl>(Template)? 1 :
|
|
|
|
isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
|
|
|
|
<< Template << Range;
|
2009-02-12 02:16:40 +08:00
|
|
|
Diag(Template->getLocation(), diag::note_template_decl_here)
|
|
|
|
<< Params->getSourceRange();
|
2009-02-10 07:23:08 +08:00
|
|
|
Invalid = true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// C++ [temp.arg]p1:
|
2009-02-10 07:23:08 +08:00
|
|
|
// [...] The type and form of each template-argument specified in
|
|
|
|
// a template-id shall match the type and form specified for the
|
|
|
|
// corresponding parameter declared by the template in its
|
|
|
|
// template-parameter-list.
|
|
|
|
unsigned ArgIdx = 0;
|
|
|
|
for (TemplateParameterList::iterator Param = Params->begin(),
|
|
|
|
ParamEnd = Params->end();
|
|
|
|
Param != ParamEnd; ++Param, ++ArgIdx) {
|
2009-07-01 08:28:38 +08:00
|
|
|
if (ArgIdx > NumArgs && PartialTemplateArgs)
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-10 07:23:08 +08:00
|
|
|
// Decode the template argument
|
2009-03-10 07:48:35 +08:00
|
|
|
TemplateArgument Arg;
|
2009-02-10 07:23:08 +08:00
|
|
|
if (ArgIdx >= NumArgs) {
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
// Retrieve the default template argument from the template
|
|
|
|
// parameter.
|
|
|
|
if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
|
2009-06-13 10:08:00 +08:00
|
|
|
if (TTP->isParameterPack()) {
|
2009-06-23 09:26:57 +08:00
|
|
|
// We have an empty argument pack.
|
|
|
|
Converted.BeginPack();
|
|
|
|
Converted.EndPack();
|
2009-06-13 10:08:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
if (!TTP->hasDefaultArgument())
|
|
|
|
break;
|
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
QualType ArgType = TTP->getDefaultArgument();
|
2009-02-28 03:31:52 +08:00
|
|
|
|
|
|
|
// If the argument type is dependent, instantiate it now based
|
|
|
|
// on the previously-computed template arguments.
|
2009-03-11 04:44:00 +08:00
|
|
|
if (ArgType->isDependentType()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
InstantiatingTemplate Inst(*this, TemplateLoc,
|
2009-06-23 09:26:57 +08:00
|
|
|
Template, Converted.getFlatArguments(),
|
2009-06-05 11:43:12 +08:00
|
|
|
Converted.flatSize(),
|
2009-03-11 04:44:00 +08:00
|
|
|
SourceRange(TemplateLoc, RAngleLoc));
|
2009-05-12 07:53:27 +08:00
|
|
|
|
2009-06-05 12:47:51 +08:00
|
|
|
TemplateArgumentList TemplateArgs(Context, Converted,
|
2009-06-23 09:26:57 +08:00
|
|
|
/*TakeArgs=*/false);
|
2009-09-09 23:08:12 +08:00
|
|
|
ArgType = SubstType(ArgType,
|
2009-08-29 04:31:08 +08:00
|
|
|
MultiLevelTemplateArgumentList(TemplateArgs),
|
2009-08-26 06:02:44 +08:00
|
|
|
TTP->getDefaultArgumentLoc(),
|
|
|
|
TTP->getDeclName());
|
2009-03-11 04:44:00 +08:00
|
|
|
}
|
2009-02-28 03:31:52 +08:00
|
|
|
|
|
|
|
if (ArgType.isNull())
|
2009-02-28 08:25:32 +08:00
|
|
|
return true;
|
2009-02-28 03:31:52 +08:00
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
Arg = TemplateArgument(TTP->getLocation(), ArgType);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (NonTypeTemplateParmDecl *NTTP
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
= dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
|
|
|
|
if (!NTTP->hasDefaultArgument())
|
|
|
|
break;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
InstantiatingTemplate Inst(*this, TemplateLoc,
|
2009-06-23 09:26:57 +08:00
|
|
|
Template, Converted.getFlatArguments(),
|
2009-06-12 00:06:49 +08:00
|
|
|
Converted.flatSize(),
|
|
|
|
SourceRange(TemplateLoc, RAngleLoc));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-12 00:06:49 +08:00
|
|
|
TemplateArgumentList TemplateArgs(Context, Converted,
|
2009-06-23 09:26:57 +08:00
|
|
|
/*TakeArgs=*/false);
|
2009-06-12 00:06:49 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Sema::OwningExprResult E
|
|
|
|
= SubstExpr(NTTP->getDefaultArgument(),
|
2009-08-29 04:31:08 +08:00
|
|
|
MultiLevelTemplateArgumentList(TemplateArgs));
|
2009-06-12 00:06:49 +08:00
|
|
|
if (E.isInvalid())
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-12 00:06:49 +08:00
|
|
|
Arg = TemplateArgument(E.takeAs<Expr>());
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateTemplateParmDecl *TempParm
|
|
|
|
= cast<TemplateTemplateParmDecl>(*Param);
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
|
|
|
|
if (!TempParm->hasDefaultArgument())
|
|
|
|
break;
|
|
|
|
|
2009-08-26 06:02:44 +08:00
|
|
|
// FIXME: Subst default argument
|
2009-03-10 07:48:35 +08:00
|
|
|
Arg = TemplateArgument(TempParm->getDefaultArgument());
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Retrieve the template argument produced by the user.
|
2009-03-10 07:48:35 +08:00
|
|
|
Arg = TemplateArgs[ArgIdx];
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
}
|
|
|
|
|
2009-02-10 07:23:08 +08:00
|
|
|
|
|
|
|
if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
|
2009-06-13 10:08:00 +08:00
|
|
|
if (TTP->isParameterPack()) {
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.BeginPack();
|
2009-06-13 10:08:00 +08:00
|
|
|
// Check all the remaining arguments (if any).
|
|
|
|
for (; ArgIdx < NumArgs; ++ArgIdx) {
|
|
|
|
if (CheckTemplateTypeArgument(TTP, TemplateArgs[ArgIdx], Converted))
|
|
|
|
Invalid = true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.EndPack();
|
2009-06-13 10:08:00 +08:00
|
|
|
} else {
|
|
|
|
if (CheckTemplateTypeArgument(TTP, Arg, Converted))
|
|
|
|
Invalid = true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (NonTypeTemplateParmDecl *NTTP
|
2009-02-10 07:23:08 +08:00
|
|
|
= dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
|
|
|
|
// Check non-type template parameters.
|
2009-03-03 12:44:36 +08:00
|
|
|
|
2009-08-26 06:02:44 +08:00
|
|
|
// Do substitution on the type of the non-type template parameter
|
|
|
|
// with the template arguments we've seen thus far.
|
2009-03-03 12:44:36 +08:00
|
|
|
QualType NTTPType = NTTP->getType();
|
|
|
|
if (NTTPType->isDependentType()) {
|
2009-08-26 06:02:44 +08:00
|
|
|
// Do substitution on the type of the non-type template parameter.
|
2009-09-09 23:08:12 +08:00
|
|
|
InstantiatingTemplate Inst(*this, TemplateLoc,
|
2009-06-23 09:26:57 +08:00
|
|
|
Template, Converted.getFlatArguments(),
|
2009-06-05 11:43:12 +08:00
|
|
|
Converted.flatSize(),
|
2009-03-11 04:44:00 +08:00
|
|
|
SourceRange(TemplateLoc, RAngleLoc));
|
|
|
|
|
2009-06-05 12:47:51 +08:00
|
|
|
TemplateArgumentList TemplateArgs(Context, Converted,
|
2009-06-23 09:26:57 +08:00
|
|
|
/*TakeArgs=*/false);
|
2009-09-09 23:08:12 +08:00
|
|
|
NTTPType = SubstType(NTTPType,
|
2009-08-29 04:50:45 +08:00
|
|
|
MultiLevelTemplateArgumentList(TemplateArgs),
|
2009-08-26 06:02:44 +08:00
|
|
|
NTTP->getLocation(),
|
|
|
|
NTTP->getDeclName());
|
2009-03-03 12:44:36 +08:00
|
|
|
// If that worked, check the non-type template parameter type
|
|
|
|
// for validity.
|
|
|
|
if (!NTTPType.isNull())
|
2009-09-09 23:08:12 +08:00
|
|
|
NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
|
2009-03-03 12:44:36 +08:00
|
|
|
NTTP->getLocation());
|
|
|
|
if (NTTPType.isNull()) {
|
|
|
|
Invalid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
switch (Arg.getKind()) {
|
2009-06-04 08:03:07 +08:00
|
|
|
case TemplateArgument::Null:
|
|
|
|
assert(false && "Should never see a NULL template argument here");
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
case TemplateArgument::Expression: {
|
|
|
|
Expr *E = Arg.getAsExpr();
|
2009-06-12 02:10:32 +08:00
|
|
|
TemplateArgument Result;
|
|
|
|
if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
|
2009-02-10 07:23:08 +08:00
|
|
|
Invalid = true;
|
2009-06-12 02:10:32 +08:00
|
|
|
else
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.Append(Result);
|
2009-03-10 07:48:35 +08:00
|
|
|
break;
|
2009-02-10 07:23:08 +08:00
|
|
|
}
|
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
case TemplateArgument::Declaration:
|
|
|
|
case TemplateArgument::Integral:
|
|
|
|
// We've already checked this template argument, so just copy
|
|
|
|
// it to the list of converted arguments.
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.Append(Arg);
|
2009-03-10 07:48:35 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TemplateArgument::Type:
|
|
|
|
// We have a non-type template parameter but the template
|
|
|
|
// argument is a type.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
// C++ [temp.arg]p2:
|
|
|
|
// In a template-argument, an ambiguity between a type-id and
|
|
|
|
// an expression is resolved to a type-id, regardless of the
|
|
|
|
// form of the corresponding template-parameter.
|
|
|
|
//
|
|
|
|
// We warn specifically about this case, since it can be rather
|
|
|
|
// confusing for users.
|
|
|
|
if (Arg.getAsType()->isFunctionType())
|
|
|
|
Diag(Arg.getLocation(), diag::err_template_arg_nontype_ambig)
|
|
|
|
<< Arg.getAsType();
|
|
|
|
else
|
|
|
|
Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr);
|
|
|
|
Diag((*Param)->getLocation(), diag::note_template_param_here);
|
|
|
|
Invalid = true;
|
2009-06-16 01:04:53 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-16 01:04:53 +08:00
|
|
|
case TemplateArgument::Pack:
|
|
|
|
assert(0 && "FIXME: Implement!");
|
|
|
|
break;
|
2009-03-10 07:48:35 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
} else {
|
2009-02-10 07:23:08 +08:00
|
|
|
// Check template template parameters.
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateTemplateParmDecl *TempParm
|
2009-02-10 07:23:08 +08:00
|
|
|
= cast<TemplateTemplateParmDecl>(*Param);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
switch (Arg.getKind()) {
|
2009-06-04 08:03:07 +08:00
|
|
|
case TemplateArgument::Null:
|
|
|
|
assert(false && "Should never see a NULL template argument here");
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
case TemplateArgument::Expression: {
|
|
|
|
Expr *ArgExpr = Arg.getAsExpr();
|
|
|
|
if (ArgExpr && isa<DeclRefExpr>(ArgExpr) &&
|
|
|
|
isa<TemplateDecl>(cast<DeclRefExpr>(ArgExpr)->getDecl())) {
|
|
|
|
if (CheckTemplateArgument(TempParm, cast<DeclRefExpr>(ArgExpr)))
|
|
|
|
Invalid = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
// Add the converted template argument.
|
2009-09-09 23:08:12 +08:00
|
|
|
Decl *D
|
2009-07-18 08:34:25 +08:00
|
|
|
= cast<DeclRefExpr>(ArgExpr)->getDecl()->getCanonicalDecl();
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.Append(TemplateArgument(Arg.getLocation(), D));
|
2009-03-10 07:48:35 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// fall through
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
case TemplateArgument::Type: {
|
|
|
|
// We have a template template parameter but the template
|
|
|
|
// argument does not refer to a template.
|
|
|
|
Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
|
|
|
|
Invalid = true;
|
|
|
|
break;
|
2009-02-10 07:23:08 +08:00
|
|
|
}
|
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
case TemplateArgument::Declaration:
|
|
|
|
// We've already checked this template argument, so just copy
|
|
|
|
// it to the list of converted arguments.
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.Append(Arg);
|
2009-03-10 07:48:35 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
case TemplateArgument::Integral:
|
|
|
|
assert(false && "Integral argument with template template parameter");
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-16 01:04:53 +08:00
|
|
|
case TemplateArgument::Pack:
|
|
|
|
assert(0 && "FIXME: Implement!");
|
|
|
|
break;
|
2009-03-10 07:48:35 +08:00
|
|
|
}
|
2009-02-10 07:23:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Check a template argument against its corresponding
|
|
|
|
/// template type parameter.
|
|
|
|
///
|
|
|
|
/// This routine implements the semantics of C++ [temp.arg.type]. It
|
|
|
|
/// returns true if an error occurred, and false otherwise.
|
2009-09-09 23:08:12 +08:00
|
|
|
bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
|
2009-02-10 07:23:08 +08:00
|
|
|
QualType Arg, SourceLocation ArgLoc) {
|
|
|
|
// C++ [temp.arg.type]p2:
|
|
|
|
// A local type, a type with no linkage, an unnamed type or a type
|
|
|
|
// compounded from any of these types shall not be used as a
|
|
|
|
// template-argument for a template type-parameter.
|
|
|
|
//
|
|
|
|
// FIXME: Perform the recursive and no-linkage type checks.
|
|
|
|
const TagType *Tag = 0;
|
|
|
|
if (const EnumType *EnumT = Arg->getAsEnumType())
|
|
|
|
Tag = EnumT;
|
2009-07-30 05:53:49 +08:00
|
|
|
else if (const RecordType *RecordT = Arg->getAs<RecordType>())
|
2009-02-10 07:23:08 +08:00
|
|
|
Tag = RecordT;
|
|
|
|
if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod())
|
|
|
|
return Diag(ArgLoc, diag::err_template_arg_local_type)
|
|
|
|
<< QualType(Tag, 0);
|
2009-09-09 23:08:12 +08:00
|
|
|
else if (Tag && !Tag->getDecl()->getDeclName() &&
|
2009-03-11 02:33:27 +08:00
|
|
|
!Tag->getDecl()->getTypedefForAnonDecl()) {
|
2009-02-10 07:23:08 +08:00
|
|
|
Diag(ArgLoc, diag::err_template_arg_unnamed_type);
|
|
|
|
Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-12 03:52:55 +08:00
|
|
|
/// \brief Checks whether the given template argument is the address
|
|
|
|
/// of an object or function according to C++ [temp.arg.nontype]p1.
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg,
|
|
|
|
NamedDecl *&Entity) {
|
2009-02-12 03:52:55 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
|
|
|
|
// See through any implicit casts we added to fix the type.
|
|
|
|
if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
|
|
|
|
Arg = Cast->getSubExpr();
|
|
|
|
|
2009-05-11 02:38:11 +08:00
|
|
|
// C++0x allows nullptr, and there's no further checking to be done for that.
|
|
|
|
if (Arg->getType()->isNullPtrType())
|
|
|
|
return false;
|
|
|
|
|
2009-02-12 03:52:55 +08:00
|
|
|
// C++ [temp.arg.nontype]p1:
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2009-02-12 03:52:55 +08:00
|
|
|
// A template-argument for a non-type, non-template
|
|
|
|
// template-parameter shall be one of: [...]
|
|
|
|
//
|
|
|
|
// -- the address of an object or function with external
|
|
|
|
// linkage, including function templates and function
|
|
|
|
// template-ids but excluding non-static class members,
|
|
|
|
// expressed as & id-expression where the & is optional if
|
|
|
|
// the name refers to a function or array, or if the
|
|
|
|
// corresponding template-parameter is a reference; or
|
|
|
|
DeclRefExpr *DRE = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 03:52:55 +08:00
|
|
|
// Ignore (and complain about) any excess parentheses.
|
|
|
|
while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
|
|
|
|
if (!Invalid) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-12 03:52:55 +08:00
|
|
|
diag::err_template_arg_extra_parens)
|
|
|
|
<< Arg->getSourceRange();
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg = Parens->getSubExpr();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
|
|
|
|
if (UnOp->getOpcode() == UnaryOperator::AddrOf)
|
|
|
|
DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
|
|
|
|
} else
|
|
|
|
DRE = dyn_cast<DeclRefExpr>(Arg);
|
|
|
|
|
|
|
|
if (!DRE || !isa<ValueDecl>(DRE->getDecl()))
|
2009-09-09 23:08:12 +08:00
|
|
|
return Diag(Arg->getSourceRange().getBegin(),
|
2009-02-12 03:52:55 +08:00
|
|
|
diag::err_template_arg_not_object_or_func_form)
|
|
|
|
<< Arg->getSourceRange();
|
|
|
|
|
|
|
|
// Cannot refer to non-static data members
|
|
|
|
if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl()))
|
|
|
|
return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
|
|
|
|
<< Field << Arg->getSourceRange();
|
|
|
|
|
|
|
|
// Cannot refer to non-static member functions
|
|
|
|
if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
|
|
|
|
if (!Method->isStatic())
|
2009-09-09 23:08:12 +08:00
|
|
|
return Diag(Arg->getSourceRange().getBegin(),
|
2009-02-12 03:52:55 +08:00
|
|
|
diag::err_template_arg_method)
|
|
|
|
<< Method << Arg->getSourceRange();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 03:52:55 +08:00
|
|
|
// Functions must have external linkage.
|
|
|
|
if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
|
|
|
|
if (Func->getStorageClass() == FunctionDecl::Static) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-12 03:52:55 +08:00
|
|
|
diag::err_template_arg_function_not_extern)
|
|
|
|
<< Func << Arg->getSourceRange();
|
|
|
|
Diag(Func->getLocation(), diag::note_template_arg_internal_object)
|
|
|
|
<< true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay: we've named a function with external linkage.
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
Entity = Func;
|
2009-02-12 03:52:55 +08:00
|
|
|
return Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
|
|
|
|
if (!Var->hasGlobalStorage()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-12 03:52:55 +08:00
|
|
|
diag::err_template_arg_object_not_extern)
|
|
|
|
<< Var << Arg->getSourceRange();
|
|
|
|
Diag(Var->getLocation(), diag::note_template_arg_internal_object)
|
|
|
|
<< true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay: we've named an object with external linkage
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
Entity = Var;
|
2009-02-12 03:52:55 +08:00
|
|
|
return Invalid;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 03:52:55 +08:00
|
|
|
// We found something else, but we don't know specifically what it is.
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-12 03:52:55 +08:00
|
|
|
diag::err_template_arg_not_object_or_func)
|
|
|
|
<< Arg->getSourceRange();
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(DRE->getDecl()->getLocation(),
|
2009-02-12 03:52:55 +08:00
|
|
|
diag::note_template_arg_refers_here);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Checks whether the given template argument is a pointer to
|
|
|
|
/// member constant according to C++ [temp.arg.nontype]p1.
|
2009-09-09 23:08:12 +08:00
|
|
|
bool
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, NamedDecl *&Member) {
|
2009-02-12 03:52:55 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
|
|
|
|
// See through any implicit casts we added to fix the type.
|
|
|
|
if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
|
|
|
|
Arg = Cast->getSubExpr();
|
|
|
|
|
2009-05-11 02:38:11 +08:00
|
|
|
// C++0x allows nullptr, and there's no further checking to be done for that.
|
|
|
|
if (Arg->getType()->isNullPtrType())
|
|
|
|
return false;
|
|
|
|
|
2009-02-12 03:52:55 +08:00
|
|
|
// C++ [temp.arg.nontype]p1:
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2009-02-12 03:52:55 +08:00
|
|
|
// A template-argument for a non-type, non-template
|
|
|
|
// template-parameter shall be one of: [...]
|
|
|
|
//
|
|
|
|
// -- a pointer to member expressed as described in 5.3.1.
|
|
|
|
QualifiedDeclRefExpr *DRE = 0;
|
|
|
|
|
|
|
|
// Ignore (and complain about) any excess parentheses.
|
|
|
|
while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
|
|
|
|
if (!Invalid) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-12 03:52:55 +08:00
|
|
|
diag::err_template_arg_extra_parens)
|
|
|
|
<< Arg->getSourceRange();
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg = Parens->getSubExpr();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg))
|
|
|
|
if (UnOp->getOpcode() == UnaryOperator::AddrOf)
|
|
|
|
DRE = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr());
|
|
|
|
|
|
|
|
if (!DRE)
|
|
|
|
return Diag(Arg->getSourceRange().getBegin(),
|
|
|
|
diag::err_template_arg_not_pointer_to_member_form)
|
|
|
|
<< Arg->getSourceRange();
|
|
|
|
|
|
|
|
if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
|
|
|
|
assert((isa<FieldDecl>(DRE->getDecl()) ||
|
|
|
|
!cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
|
|
|
|
"Only non-static member pointers can make it here");
|
|
|
|
|
|
|
|
// Okay: this is the address of a non-static member, and therefore
|
|
|
|
// a member pointer constant.
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
Member = DRE->getDecl();
|
2009-02-12 03:52:55 +08:00
|
|
|
return Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We found something else, but we don't know specifically what it is.
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-12 03:52:55 +08:00
|
|
|
diag::err_template_arg_not_pointer_to_member_form)
|
|
|
|
<< Arg->getSourceRange();
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(DRE->getDecl()->getLocation(),
|
2009-02-12 03:52:55 +08:00
|
|
|
diag::note_template_arg_refers_here);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-10 07:23:08 +08:00
|
|
|
/// \brief Check a template argument against its corresponding
|
|
|
|
/// non-type template parameter.
|
|
|
|
///
|
2009-03-03 12:44:36 +08:00
|
|
|
/// This routine implements the semantics of C++ [temp.arg.nontype].
|
|
|
|
/// It returns true if an error occurred, and false otherwise. \p
|
|
|
|
/// InstantiatedParamType is the type of the non-type template
|
|
|
|
/// parameter after it has been instantiated.
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
///
|
2009-06-12 02:10:32 +08:00
|
|
|
/// If no error was detected, Converted receives the converted template argument.
|
2009-02-10 07:23:08 +08:00
|
|
|
bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType InstantiatedParamType, Expr *&Arg,
|
2009-06-12 02:10:32 +08:00
|
|
|
TemplateArgument &Converted) {
|
2009-03-10 07:48:35 +08:00
|
|
|
SourceLocation StartLoc = Arg->getSourceRange().getBegin();
|
|
|
|
|
2009-02-11 07:36:10 +08:00
|
|
|
// If either the parameter has a dependent type or the argument is
|
|
|
|
// type-dependent, there's nothing we can check now.
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
// FIXME: Add template argument to Converted!
|
2009-03-10 07:48:35 +08:00
|
|
|
if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
|
|
|
|
// FIXME: Produce a cloned, canonical expression?
|
2009-06-12 02:10:32 +08:00
|
|
|
Converted = TemplateArgument(Arg);
|
2009-02-11 07:36:10 +08:00
|
|
|
return false;
|
2009-03-10 07:48:35 +08:00
|
|
|
}
|
2009-02-11 07:36:10 +08:00
|
|
|
|
|
|
|
// C++ [temp.arg.nontype]p5:
|
|
|
|
// The following conversions are performed on each expression used
|
|
|
|
// as a non-type template-argument. If a non-type
|
|
|
|
// template-argument cannot be converted to the type of the
|
|
|
|
// corresponding template-parameter then the program is
|
|
|
|
// ill-formed.
|
|
|
|
//
|
|
|
|
// -- for a non-type template-parameter of integral or
|
|
|
|
// enumeration type, integral promotions (4.5) and integral
|
|
|
|
// conversions (4.7) are applied.
|
2009-03-03 12:44:36 +08:00
|
|
|
QualType ParamType = InstantiatedParamType;
|
2009-02-11 08:19:33 +08:00
|
|
|
QualType ArgType = Arg->getType();
|
2009-02-11 07:36:10 +08:00
|
|
|
if (ParamType->isIntegralType() || ParamType->isEnumeralType()) {
|
|
|
|
// C++ [temp.arg.nontype]p1:
|
|
|
|
// A template-argument for a non-type, non-template
|
|
|
|
// template-parameter shall be one of:
|
|
|
|
//
|
|
|
|
// -- an integral constant-expression of integral or enumeration
|
|
|
|
// type; or
|
|
|
|
// -- the name of a non-type template-parameter; or
|
|
|
|
SourceLocation NonConstantLoc;
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
llvm::APSInt Value;
|
2009-02-11 07:36:10 +08:00
|
|
|
if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-11 07:36:10 +08:00
|
|
|
diag::err_template_arg_not_integral_or_enumeral)
|
|
|
|
<< ArgType << Arg->getSourceRange();
|
|
|
|
Diag(Param->getLocation(), diag::note_template_param_here);
|
|
|
|
return true;
|
|
|
|
} else if (!Arg->isValueDependent() &&
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
!Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
|
2009-02-11 07:36:10 +08:00
|
|
|
Diag(NonConstantLoc, diag::err_template_arg_not_ice)
|
|
|
|
<< ArgType << Arg->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: We need some way to more easily get the unqualified form
|
|
|
|
// of the types without going all the way to the
|
|
|
|
// canonical type.
|
|
|
|
if (Context.getCanonicalType(ParamType).getCVRQualifiers())
|
|
|
|
ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType();
|
|
|
|
if (Context.getCanonicalType(ArgType).getCVRQualifiers())
|
|
|
|
ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType();
|
|
|
|
|
|
|
|
// Try to convert the argument to the parameter's type.
|
|
|
|
if (ParamType == ArgType) {
|
|
|
|
// Okay: no conversion necessary
|
|
|
|
} else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
|
|
|
|
!ParamType->isEnumeralType()) {
|
|
|
|
// This is an integral promotion or conversion.
|
|
|
|
ImpCastExprToType(Arg, ParamType);
|
|
|
|
} else {
|
|
|
|
// We can't perform this conversion.
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-11 07:36:10 +08:00
|
|
|
diag::err_template_arg_not_convertible)
|
2009-03-03 12:44:36 +08:00
|
|
|
<< Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
|
2009-02-11 07:36:10 +08:00
|
|
|
Diag(Param->getLocation(), diag::note_template_param_here);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-14 08:20:21 +08:00
|
|
|
QualType IntegerType = Context.getCanonicalType(ParamType);
|
|
|
|
if (const EnumType *Enum = IntegerType->getAsEnumType())
|
2009-06-12 02:10:32 +08:00
|
|
|
IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
|
2009-03-14 08:20:21 +08:00
|
|
|
|
|
|
|
if (!Arg->isValueDependent()) {
|
|
|
|
// Check that an unsigned parameter does not receive a negative
|
|
|
|
// value.
|
|
|
|
if (IntegerType->isUnsignedIntegerType()
|
|
|
|
&& (Value.isSigned() && Value.isNegative())) {
|
|
|
|
Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative)
|
|
|
|
<< Value.toString(10) << Param->getType()
|
|
|
|
<< Arg->getSourceRange();
|
|
|
|
Diag(Param->getLocation(), diag::note_template_param_here);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we don't overflow the template parameter type.
|
|
|
|
unsigned AllowedBits = Context.getTypeSize(IntegerType);
|
|
|
|
if (Value.getActiveBits() > AllowedBits) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-03-14 08:20:21 +08:00
|
|
|
diag::err_template_arg_too_large)
|
|
|
|
<< Value.toString(10) << Param->getType()
|
|
|
|
<< Arg->getSourceRange();
|
|
|
|
Diag(Param->getLocation(), diag::note_template_param_here);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Value.getBitWidth() != AllowedBits)
|
|
|
|
Value.extOrTrunc(AllowedBits);
|
|
|
|
Value.setIsSigned(IntegerType->isSignedIntegerType());
|
|
|
|
}
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
|
2009-06-12 02:10:32 +08:00
|
|
|
// Add the value of this argument to the list of converted
|
|
|
|
// arguments. We use the bitwidth and signedness of the template
|
|
|
|
// parameter.
|
|
|
|
if (Arg->isValueDependent()) {
|
|
|
|
// The argument is value-dependent. Create a new
|
|
|
|
// TemplateArgument with the converted expression.
|
|
|
|
Converted = TemplateArgument(Arg);
|
|
|
|
return false;
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
}
|
|
|
|
|
2009-06-12 02:10:32 +08:00
|
|
|
Converted = TemplateArgument(StartLoc, Value,
|
2009-09-09 23:08:12 +08:00
|
|
|
ParamType->isEnumeralType() ? ParamType
|
2009-06-12 02:10:32 +08:00
|
|
|
: IntegerType);
|
2009-02-11 07:36:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-02-11 08:19:33 +08:00
|
|
|
|
2009-02-11 09:18:59 +08:00
|
|
|
// Handle pointer-to-function, reference-to-function, and
|
|
|
|
// pointer-to-member-function all in (roughly) the same way.
|
|
|
|
if (// -- For a non-type template-parameter of type pointer to
|
|
|
|
// function, only the function-to-pointer conversion (4.3) is
|
|
|
|
// applied. If the template-argument represents a set of
|
|
|
|
// overloaded functions (or a pointer to such), the matching
|
|
|
|
// function is selected from the set (13.4).
|
2009-05-11 02:38:11 +08:00
|
|
|
// In C++0x, any std::nullptr_t value can be converted.
|
2009-02-11 09:18:59 +08:00
|
|
|
(ParamType->isPointerType() &&
|
2009-07-30 05:53:49 +08:00
|
|
|
ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
|
2009-02-11 09:18:59 +08:00
|
|
|
// -- For a non-type template-parameter of type reference to
|
|
|
|
// function, no conversions apply. If the template-argument
|
|
|
|
// represents a set of overloaded functions, the matching
|
|
|
|
// function is selected from the set (13.4).
|
|
|
|
(ParamType->isReferenceType() &&
|
2009-07-30 05:53:49 +08:00
|
|
|
ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
|
2009-02-11 09:18:59 +08:00
|
|
|
// -- For a non-type template-parameter of type pointer to
|
|
|
|
// member function, no conversions apply. If the
|
|
|
|
// template-argument represents a set of overloaded member
|
|
|
|
// functions, the matching member function is selected from
|
|
|
|
// the set (13.4).
|
2009-05-11 02:38:11 +08:00
|
|
|
// Again, C++0x allows a std::nullptr_t value.
|
2009-02-11 09:18:59 +08:00
|
|
|
(ParamType->isMemberPointerType() &&
|
2009-07-30 05:53:49 +08:00
|
|
|
ParamType->getAs<MemberPointerType>()->getPointeeType()
|
2009-02-11 09:18:59 +08:00
|
|
|
->isFunctionType())) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (Context.hasSameUnqualifiedType(ArgType,
|
2009-02-12 03:52:55 +08:00
|
|
|
ParamType.getNonReferenceType())) {
|
2009-02-11 08:19:33 +08:00
|
|
|
// We don't have to do anything: the types already match.
|
2009-05-11 02:38:11 +08:00
|
|
|
} else if (ArgType->isNullPtrType() && (ParamType->isPointerType() ||
|
|
|
|
ParamType->isMemberPointerType())) {
|
|
|
|
ArgType = ParamType;
|
|
|
|
ImpCastExprToType(Arg, ParamType);
|
2009-02-11 09:18:59 +08:00
|
|
|
} else if (ArgType->isFunctionType() && ParamType->isPointerType()) {
|
2009-02-11 08:19:33 +08:00
|
|
|
ArgType = Context.getPointerType(ArgType);
|
|
|
|
ImpCastExprToType(Arg, ArgType);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (FunctionDecl *Fn
|
2009-02-11 08:19:33 +08:00
|
|
|
= ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) {
|
2009-02-19 05:56:37 +08:00
|
|
|
if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
|
|
|
|
return true;
|
|
|
|
|
2009-02-11 08:19:33 +08:00
|
|
|
FixOverloadedFunctionReference(Arg, Fn);
|
|
|
|
ArgType = Arg->getType();
|
2009-02-11 09:18:59 +08:00
|
|
|
if (ArgType->isFunctionType() && ParamType->isPointerType()) {
|
2009-02-11 08:19:33 +08:00
|
|
|
ArgType = Context.getPointerType(Arg->getType());
|
|
|
|
ImpCastExprToType(Arg, ArgType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!Context.hasSameUnqualifiedType(ArgType,
|
2009-02-12 03:52:55 +08:00
|
|
|
ParamType.getNonReferenceType())) {
|
2009-02-11 08:19:33 +08:00
|
|
|
// We can't perform this conversion.
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-11 08:19:33 +08:00
|
|
|
diag::err_template_arg_not_convertible)
|
2009-03-03 12:44:36 +08:00
|
|
|
<< Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
|
2009-02-11 08:19:33 +08:00
|
|
|
Diag(Param->getLocation(), diag::note_template_param_here);
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
if (ParamType->isMemberPointerType()) {
|
|
|
|
NamedDecl *Member = 0;
|
|
|
|
if (CheckTemplateArgumentPointerToMember(Arg, Member))
|
|
|
|
return true;
|
|
|
|
|
2009-07-18 08:34:25 +08:00
|
|
|
if (Member)
|
|
|
|
Member = cast<NamedDecl>(Member->getCanonicalDecl());
|
2009-06-12 02:10:32 +08:00
|
|
|
Converted = TemplateArgument(StartLoc, Member);
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
NamedDecl *Entity = 0;
|
|
|
|
if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
|
|
|
|
return true;
|
|
|
|
|
2009-07-18 08:34:25 +08:00
|
|
|
if (Entity)
|
|
|
|
Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
|
2009-06-12 02:10:32 +08:00
|
|
|
Converted = TemplateArgument(StartLoc, Entity);
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
return false;
|
2009-02-11 08:19:33 +08:00
|
|
|
}
|
|
|
|
|
2009-02-21 05:37:53 +08:00
|
|
|
if (ParamType->isPointerType()) {
|
2009-02-11 09:18:59 +08:00
|
|
|
// -- for a non-type template-parameter of type pointer to
|
|
|
|
// object, qualification conversions (4.4) and the
|
|
|
|
// array-to-pointer conversion (4.2) are applied.
|
2009-05-11 02:38:11 +08:00
|
|
|
// C++0x also allows a value of std::nullptr_t.
|
2009-07-30 05:53:49 +08:00
|
|
|
assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() &&
|
2009-02-11 09:18:59 +08:00
|
|
|
"Only object pointers allowed here");
|
|
|
|
|
2009-05-11 02:38:11 +08:00
|
|
|
if (ArgType->isNullPtrType()) {
|
|
|
|
ArgType = ParamType;
|
|
|
|
ImpCastExprToType(Arg, ParamType);
|
|
|
|
} else if (ArgType->isArrayType()) {
|
2009-02-11 09:18:59 +08:00
|
|
|
ArgType = Context.getArrayDecayedType(ArgType);
|
|
|
|
ImpCastExprToType(Arg, ArgType);
|
|
|
|
}
|
2009-05-11 02:38:11 +08:00
|
|
|
|
2009-02-11 09:18:59 +08:00
|
|
|
if (IsQualificationConversion(ArgType, ParamType)) {
|
|
|
|
ArgType = ParamType;
|
|
|
|
ImpCastExprToType(Arg, ParamType);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 02:22:40 +08:00
|
|
|
if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) {
|
2009-02-11 09:18:59 +08:00
|
|
|
// We can't perform this conversion.
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-11 09:18:59 +08:00
|
|
|
diag::err_template_arg_not_convertible)
|
2009-03-03 12:44:36 +08:00
|
|
|
<< Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
|
2009-02-11 09:18:59 +08:00
|
|
|
Diag(Param->getLocation(), diag::note_template_param_here);
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
NamedDecl *Entity = 0;
|
|
|
|
if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
|
|
|
|
return true;
|
|
|
|
|
2009-07-18 08:34:25 +08:00
|
|
|
if (Entity)
|
|
|
|
Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
|
2009-06-12 02:10:32 +08:00
|
|
|
Converted = TemplateArgument(StartLoc, Entity);
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
return false;
|
2009-02-11 09:18:59 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-30 05:53:49 +08:00
|
|
|
if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
|
2009-02-11 09:18:59 +08:00
|
|
|
// -- For a non-type template-parameter of type reference to
|
|
|
|
// object, no conversions apply. The type referred to by the
|
|
|
|
// reference may be more cv-qualified than the (otherwise
|
|
|
|
// identical) type of the template-argument. The
|
|
|
|
// template-parameter is bound directly to the
|
|
|
|
// template-argument, which must be an lvalue.
|
2009-03-25 04:32:41 +08:00
|
|
|
assert(ParamRefType->getPointeeType()->isObjectType() &&
|
2009-02-11 09:18:59 +08:00
|
|
|
"Only object references allowed here");
|
|
|
|
|
2009-02-12 02:22:40 +08:00
|
|
|
if (!Context.hasSameUnqualifiedType(ParamRefType->getPointeeType(), ArgType)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-11 09:18:59 +08:00
|
|
|
diag::err_template_arg_no_ref_bind)
|
2009-03-03 12:44:36 +08:00
|
|
|
<< InstantiatedParamType << Arg->getType()
|
2009-02-11 09:18:59 +08:00
|
|
|
<< Arg->getSourceRange();
|
|
|
|
Diag(Param->getLocation(), diag::note_template_param_here);
|
|
|
|
return true;
|
|
|
|
}
|
2009-02-11 08:44:29 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned ParamQuals
|
2009-02-11 09:18:59 +08:00
|
|
|
= Context.getCanonicalType(ParamType).getCVRQualifiers();
|
|
|
|
unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-11 09:18:59 +08:00
|
|
|
if ((ParamQuals | ArgQuals) != ParamQuals) {
|
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
|
|
|
diag::err_template_arg_ref_bind_ignores_quals)
|
2009-03-03 12:44:36 +08:00
|
|
|
<< InstantiatedParamType << Arg->getType()
|
2009-02-11 09:18:59 +08:00
|
|
|
<< Arg->getSourceRange();
|
|
|
|
Diag(Param->getLocation(), diag::note_template_param_here);
|
|
|
|
return true;
|
2009-02-11 08:44:29 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
NamedDecl *Entity = 0;
|
|
|
|
if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
|
|
|
|
return true;
|
|
|
|
|
2009-07-18 08:34:25 +08:00
|
|
|
Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
|
2009-06-12 02:10:32 +08:00
|
|
|
Converted = TemplateArgument(StartLoc, Entity);
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
return false;
|
2009-02-11 08:44:29 +08:00
|
|
|
}
|
2009-02-12 00:16:59 +08:00
|
|
|
|
|
|
|
// -- For a non-type template-parameter of type pointer to data
|
|
|
|
// member, qualification conversions (4.4) are applied.
|
2009-05-11 02:38:11 +08:00
|
|
|
// C++0x allows std::nullptr_t values.
|
2009-02-12 00:16:59 +08:00
|
|
|
assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
|
|
|
|
|
2009-02-12 02:22:40 +08:00
|
|
|
if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
|
2009-02-12 00:16:59 +08:00
|
|
|
// Types match exactly: nothing more to do here.
|
2009-05-11 02:38:11 +08:00
|
|
|
} else if (ArgType->isNullPtrType()) {
|
|
|
|
ImpCastExprToType(Arg, ParamType);
|
2009-02-12 00:16:59 +08:00
|
|
|
} else if (IsQualificationConversion(ArgType, ParamType)) {
|
|
|
|
ImpCastExprToType(Arg, ParamType);
|
|
|
|
} else {
|
|
|
|
// We can't perform this conversion.
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Arg->getSourceRange().getBegin(),
|
2009-02-12 00:16:59 +08:00
|
|
|
diag::err_template_arg_not_convertible)
|
2009-03-03 12:44:36 +08:00
|
|
|
<< Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
|
2009-02-12 00:16:59 +08:00
|
|
|
Diag(Param->getLocation(), diag::note_template_param_here);
|
2009-09-09 23:08:12 +08:00
|
|
|
return true;
|
2009-02-12 00:16:59 +08:00
|
|
|
}
|
|
|
|
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
NamedDecl *Member = 0;
|
|
|
|
if (CheckTemplateArgumentPointerToMember(Arg, Member))
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-18 08:34:25 +08:00
|
|
|
if (Member)
|
|
|
|
Member = cast<NamedDecl>(Member->getCanonicalDecl());
|
2009-06-12 02:10:32 +08:00
|
|
|
Converted = TemplateArgument(StartLoc, Member);
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 09:05:43 +08:00
|
|
|
return false;
|
2009-02-10 07:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Check a template argument against its corresponding
|
|
|
|
/// template template parameter.
|
|
|
|
///
|
|
|
|
/// This routine implements the semantics of C++ [temp.arg.template].
|
|
|
|
/// It returns true if an error occurred, and false otherwise.
|
|
|
|
bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
|
|
|
|
DeclRefExpr *Arg) {
|
2009-02-10 08:24:35 +08:00
|
|
|
assert(isa<TemplateDecl>(Arg->getDecl()) && "Only template decls allowed");
|
|
|
|
TemplateDecl *Template = cast<TemplateDecl>(Arg->getDecl());
|
|
|
|
|
|
|
|
// C++ [temp.arg.template]p1:
|
|
|
|
// A template-argument for a template template-parameter shall be
|
|
|
|
// the name of a class template, expressed as id-expression. Only
|
|
|
|
// primary class templates are considered when matching the
|
|
|
|
// template template argument with the corresponding parameter;
|
|
|
|
// partial specializations are not considered even if their
|
|
|
|
// parameter lists match that of the template template parameter.
|
2009-06-13 03:43:02 +08:00
|
|
|
//
|
|
|
|
// Note that we also allow template template parameters here, which
|
|
|
|
// will happen when we are dealing with, e.g., class template
|
|
|
|
// partial specializations.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!isa<ClassTemplateDecl>(Template) &&
|
2009-06-13 03:43:02 +08:00
|
|
|
!isa<TemplateTemplateParmDecl>(Template)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(isa<FunctionTemplateDecl>(Template) &&
|
2009-02-10 08:24:35 +08:00
|
|
|
"Only function templates are possible here");
|
2009-06-26 06:08:12 +08:00
|
|
|
Diag(Arg->getLocStart(), diag::err_template_arg_not_class_template);
|
|
|
|
Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
|
2009-02-10 08:24:35 +08:00
|
|
|
<< Template;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
|
|
|
|
Param->getTemplateParameters(),
|
|
|
|
true, true,
|
|
|
|
Arg->getSourceRange().getBegin());
|
2009-02-10 07:23:08 +08:00
|
|
|
}
|
|
|
|
|
2009-02-07 06:42:48 +08:00
|
|
|
/// \brief Determine whether the given template parameter lists are
|
|
|
|
/// equivalent.
|
|
|
|
///
|
2009-09-09 23:08:12 +08:00
|
|
|
/// \param New The new template parameter list, typically written in the
|
2009-02-07 06:42:48 +08:00
|
|
|
/// source code as part of a new template declaration.
|
|
|
|
///
|
|
|
|
/// \param Old The old template parameter list, typically found via
|
|
|
|
/// name lookup of the template declared with this template parameter
|
|
|
|
/// list.
|
|
|
|
///
|
|
|
|
/// \param Complain If true, this routine will produce a diagnostic if
|
|
|
|
/// the template parameter lists are not equivalent.
|
|
|
|
///
|
2009-02-10 08:24:35 +08:00
|
|
|
/// \param IsTemplateTemplateParm If true, this routine is being
|
|
|
|
/// called to compare the template parameter lists of a template
|
|
|
|
/// template parameter.
|
|
|
|
///
|
|
|
|
/// \param TemplateArgLoc If this source location is valid, then we
|
|
|
|
/// are actually checking the template parameter list of a template
|
|
|
|
/// argument (New) against the template parameter list of its
|
|
|
|
/// corresponding template template parameter (Old). We produce
|
|
|
|
/// slightly different diagnostics in this scenario.
|
|
|
|
///
|
2009-02-07 06:42:48 +08:00
|
|
|
/// \returns True if the template parameter lists are equal, false
|
|
|
|
/// otherwise.
|
2009-09-09 23:08:12 +08:00
|
|
|
bool
|
2009-02-07 06:42:48 +08:00
|
|
|
Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
|
|
|
|
TemplateParameterList *Old,
|
|
|
|
bool Complain,
|
2009-02-10 08:24:35 +08:00
|
|
|
bool IsTemplateTemplateParm,
|
|
|
|
SourceLocation TemplateArgLoc) {
|
2009-02-07 06:42:48 +08:00
|
|
|
if (Old->size() != New->size()) {
|
|
|
|
if (Complain) {
|
2009-02-10 08:24:35 +08:00
|
|
|
unsigned NextDiag = diag::err_template_param_list_different_arity;
|
|
|
|
if (TemplateArgLoc.isValid()) {
|
|
|
|
Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
|
|
|
|
NextDiag = diag::note_template_param_list_different_arity;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-02-10 08:24:35 +08:00
|
|
|
Diag(New->getTemplateLoc(), NextDiag)
|
|
|
|
<< (New->size() > Old->size())
|
|
|
|
<< IsTemplateTemplateParm
|
|
|
|
<< SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
|
2009-02-07 06:42:48 +08:00
|
|
|
Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
|
|
|
|
<< IsTemplateTemplateParm
|
|
|
|
<< SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (TemplateParameterList::iterator OldParm = Old->begin(),
|
|
|
|
OldParmEnd = Old->end(), NewParm = New->begin();
|
|
|
|
OldParm != OldParmEnd; ++OldParm, ++NewParm) {
|
|
|
|
if ((*OldParm)->getKind() != (*NewParm)->getKind()) {
|
2009-06-25 00:50:40 +08:00
|
|
|
if (Complain) {
|
|
|
|
unsigned NextDiag = diag::err_template_param_different_kind;
|
|
|
|
if (TemplateArgLoc.isValid()) {
|
|
|
|
Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
|
|
|
|
NextDiag = diag::note_template_param_different_kind;
|
|
|
|
}
|
|
|
|
Diag((*NewParm)->getLocation(), NextDiag)
|
2009-02-07 06:42:48 +08:00
|
|
|
<< IsTemplateTemplateParm;
|
2009-06-25 00:50:40 +08:00
|
|
|
Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration)
|
2009-02-07 06:42:48 +08:00
|
|
|
<< IsTemplateTemplateParm;
|
2009-06-25 00:50:40 +08:00
|
|
|
}
|
2009-02-07 06:42:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<TemplateTypeParmDecl>(*OldParm)) {
|
|
|
|
// Okay; all template type parameters are equivalent (since we
|
2009-02-10 08:24:35 +08:00
|
|
|
// know we're at the same index).
|
|
|
|
#if 0
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: Enable this code in debug mode *after* we properly go through
|
|
|
|
// and "instantiate" the template parameter lists of template template
|
|
|
|
// parameters. It's only after this instantiation that (1) any dependent
|
|
|
|
// types within the template parameter list of the template template
|
|
|
|
// parameter can be checked, and (2) the template type parameter depths
|
2009-02-10 08:24:35 +08:00
|
|
|
// will match up.
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType OldParmType
|
2009-02-07 06:42:48 +08:00
|
|
|
= Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*OldParm));
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType NewParmType
|
2009-02-07 06:42:48 +08:00
|
|
|
= Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*NewParm));
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(Context.getCanonicalType(OldParmType) ==
|
|
|
|
Context.getCanonicalType(NewParmType) &&
|
2009-02-07 06:42:48 +08:00
|
|
|
"type parameter mismatch?");
|
|
|
|
#endif
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (NonTypeTemplateParmDecl *OldNTTP
|
2009-02-07 06:42:48 +08:00
|
|
|
= dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) {
|
|
|
|
// The types of non-type template parameters must agree.
|
|
|
|
NonTypeTemplateParmDecl *NewNTTP
|
|
|
|
= cast<NonTypeTemplateParmDecl>(*NewParm);
|
|
|
|
if (Context.getCanonicalType(OldNTTP->getType()) !=
|
|
|
|
Context.getCanonicalType(NewNTTP->getType())) {
|
|
|
|
if (Complain) {
|
2009-02-10 08:24:35 +08:00
|
|
|
unsigned NextDiag = diag::err_template_nontype_parm_different_type;
|
|
|
|
if (TemplateArgLoc.isValid()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(TemplateArgLoc,
|
2009-02-10 08:24:35 +08:00
|
|
|
diag::err_template_arg_template_params_mismatch);
|
|
|
|
NextDiag = diag::note_template_nontype_parm_different_type;
|
|
|
|
}
|
|
|
|
Diag(NewNTTP->getLocation(), NextDiag)
|
2009-02-07 06:42:48 +08:00
|
|
|
<< NewNTTP->getType()
|
|
|
|
<< IsTemplateTemplateParm;
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(OldNTTP->getLocation(),
|
2009-02-07 06:42:48 +08:00
|
|
|
diag::note_template_nontype_parm_prev_declaration)
|
|
|
|
<< OldNTTP->getType();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The template parameter lists of template template
|
|
|
|
// parameters must agree.
|
|
|
|
// FIXME: Could we perform a faster "type" comparison here?
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(isa<TemplateTemplateParmDecl>(*OldParm) &&
|
2009-02-07 06:42:48 +08:00
|
|
|
"Only template template parameters handled here");
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateTemplateParmDecl *OldTTP
|
2009-02-07 06:42:48 +08:00
|
|
|
= cast<TemplateTemplateParmDecl>(*OldParm);
|
|
|
|
TemplateTemplateParmDecl *NewTTP
|
|
|
|
= cast<TemplateTemplateParmDecl>(*NewParm);
|
|
|
|
if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
|
|
|
|
OldTTP->getTemplateParameters(),
|
|
|
|
Complain,
|
2009-02-10 08:24:35 +08:00
|
|
|
/*IsTemplateTemplateParm=*/true,
|
|
|
|
TemplateArgLoc))
|
2009-02-07 06:42:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Check whether a template can be declared within this scope.
|
|
|
|
///
|
|
|
|
/// If the template declaration is valid in this scope, returns
|
|
|
|
/// false. Otherwise, issues a diagnostic and returns true.
|
2009-09-09 23:08:12 +08:00
|
|
|
bool
|
2009-08-26 01:23:04 +08:00
|
|
|
Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
|
2009-02-07 06:42:48 +08:00
|
|
|
// Find the nearest enclosing declaration scope.
|
|
|
|
while ((S->getFlags() & Scope::DeclScope) == 0 ||
|
|
|
|
(S->getFlags() & Scope::TemplateParamScope) != 0)
|
|
|
|
S = S->getParent();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-07 06:42:48 +08:00
|
|
|
// C++ [temp]p2:
|
|
|
|
// A template-declaration can appear only as a namespace scope or
|
|
|
|
// class scope declaration.
|
|
|
|
DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
|
2009-07-31 09:43:05 +08:00
|
|
|
if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
|
|
|
|
cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
|
2009-09-09 23:08:12 +08:00
|
|
|
return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
|
2009-08-26 01:23:04 +08:00
|
|
|
<< TemplateParams->getSourceRange();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-31 09:43:05 +08:00
|
|
|
while (Ctx && isa<LinkageSpecDecl>(Ctx))
|
2009-02-07 06:42:48 +08:00
|
|
|
Ctx = Ctx->getParent();
|
|
|
|
|
|
|
|
if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
|
|
|
|
return false;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return Diag(TemplateParams->getTemplateLoc(),
|
2009-08-26 01:23:04 +08:00
|
|
|
diag::err_template_outside_namespace_or_class_scope)
|
|
|
|
<< TemplateParams->getSourceRange();
|
2009-02-07 06:42:48 +08:00
|
|
|
}
|
2009-02-18 07:15:12 +08:00
|
|
|
|
2009-05-14 02:28:20 +08:00
|
|
|
/// \brief Check whether a class template specialization or explicit
|
|
|
|
/// instantiation in the current context is well-formed.
|
2009-02-26 06:02:03 +08:00
|
|
|
///
|
2009-05-14 02:28:20 +08:00
|
|
|
/// This routine determines whether a class template specialization or
|
2009-09-09 23:08:12 +08:00
|
|
|
/// explicit instantiation can be declared in the current context
|
|
|
|
/// (C++ [temp.expl.spec]p2, C++0x [temp.explicit]p2) and emits
|
|
|
|
/// appropriate diagnostics if there was an error. It returns true if
|
2009-05-14 02:28:20 +08:00
|
|
|
// there was an error that we cannot recover from, and false otherwise.
|
2009-09-09 23:08:12 +08:00
|
|
|
bool
|
2009-02-26 06:02:03 +08:00
|
|
|
Sema::CheckClassTemplateSpecializationScope(ClassTemplateDecl *ClassTemplate,
|
|
|
|
ClassTemplateSpecializationDecl *PrevDecl,
|
|
|
|
SourceLocation TemplateNameLoc,
|
2009-05-14 02:28:20 +08:00
|
|
|
SourceRange ScopeSpecifierRange,
|
2009-06-13 06:21:45 +08:00
|
|
|
bool PartialSpecialization,
|
2009-05-14 02:28:20 +08:00
|
|
|
bool ExplicitInstantiation) {
|
2009-02-26 06:02:03 +08:00
|
|
|
// C++ [temp.expl.spec]p2:
|
|
|
|
// An explicit specialization shall be declared in the namespace
|
|
|
|
// of which the template is a member, or, for member templates, in
|
|
|
|
// the namespace of which the enclosing class or enclosing class
|
|
|
|
// template is a member. An explicit specialization of a member
|
|
|
|
// function, member class or static data member of a class
|
|
|
|
// template shall be declared in the namespace of which the class
|
|
|
|
// template is a member. Such a declaration may also be a
|
|
|
|
// definition. If the declaration is not a definition, the
|
|
|
|
// specialization may be defined later in the name- space in which
|
|
|
|
// the explicit specialization was declared, or in a namespace
|
|
|
|
// that encloses the one in which the explicit specialization was
|
|
|
|
// declared.
|
|
|
|
if (CurContext->getLookupContext()->isFunctionOrMethod()) {
|
2009-06-13 06:21:45 +08:00
|
|
|
int Kind = ExplicitInstantiation? 2 : PartialSpecialization? 1 : 0;
|
2009-02-26 06:02:03 +08:00
|
|
|
Diag(TemplateNameLoc, diag::err_template_spec_decl_function_scope)
|
2009-06-13 06:21:45 +08:00
|
|
|
<< Kind << ClassTemplate;
|
2009-02-26 06:02:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeclContext *DC = CurContext->getEnclosingNamespaceContext();
|
2009-09-09 23:08:12 +08:00
|
|
|
DeclContext *TemplateContext
|
2009-02-26 06:02:03 +08:00
|
|
|
= ClassTemplate->getDeclContext()->getEnclosingNamespaceContext();
|
2009-05-14 02:28:20 +08:00
|
|
|
if ((!PrevDecl || PrevDecl->getSpecializationKind() == TSK_Undeclared) &&
|
|
|
|
!ExplicitInstantiation) {
|
2009-02-26 06:02:03 +08:00
|
|
|
// There is no prior declaration of this entity, so this
|
|
|
|
// specialization must be in the same context as the template
|
|
|
|
// itself.
|
|
|
|
if (DC != TemplateContext) {
|
|
|
|
if (isa<TranslationUnitDecl>(TemplateContext))
|
|
|
|
Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope_global)
|
2009-06-13 06:21:45 +08:00
|
|
|
<< PartialSpecialization
|
2009-02-26 06:02:03 +08:00
|
|
|
<< ClassTemplate << ScopeSpecifierRange;
|
|
|
|
else if (isa<NamespaceDecl>(TemplateContext))
|
|
|
|
Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope)
|
2009-09-09 23:08:12 +08:00
|
|
|
<< PartialSpecialization << ClassTemplate
|
2009-06-13 06:21:45 +08:00
|
|
|
<< cast<NamedDecl>(TemplateContext) << ScopeSpecifierRange;
|
2009-02-26 06:02:03 +08:00
|
|
|
|
|
|
|
Diag(ClassTemplate->getLocation(), diag::note_template_decl_here);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a previous declaration of this entity. Make sure that
|
|
|
|
// this redeclaration (or definition) occurs in an enclosing namespace.
|
|
|
|
if (!CurContext->Encloses(TemplateContext)) {
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: In C++98, we would like to turn these errors into warnings,
|
|
|
|
// dependent on a -Wc++0x flag.
|
2009-05-14 02:28:20 +08:00
|
|
|
bool SuppressedDiag = false;
|
2009-06-13 06:21:45 +08:00
|
|
|
int Kind = ExplicitInstantiation? 2 : PartialSpecialization? 1 : 0;
|
2009-05-14 02:28:20 +08:00
|
|
|
if (isa<TranslationUnitDecl>(TemplateContext)) {
|
|
|
|
if (!ExplicitInstantiation || getLangOptions().CPlusPlus0x)
|
|
|
|
Diag(TemplateNameLoc, diag::err_template_spec_redecl_global_scope)
|
2009-06-13 06:21:45 +08:00
|
|
|
<< Kind << ClassTemplate << ScopeSpecifierRange;
|
2009-05-14 02:28:20 +08:00
|
|
|
else
|
|
|
|
SuppressedDiag = true;
|
|
|
|
} else if (isa<NamespaceDecl>(TemplateContext)) {
|
|
|
|
if (!ExplicitInstantiation || getLangOptions().CPlusPlus0x)
|
|
|
|
Diag(TemplateNameLoc, diag::err_template_spec_redecl_out_of_scope)
|
2009-06-13 06:21:45 +08:00
|
|
|
<< Kind << ClassTemplate
|
2009-05-14 02:28:20 +08:00
|
|
|
<< cast<NamedDecl>(TemplateContext) << ScopeSpecifierRange;
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
2009-05-14 02:28:20 +08:00
|
|
|
SuppressedDiag = true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-14 02:28:20 +08:00
|
|
|
if (!SuppressedDiag)
|
|
|
|
Diag(ClassTemplate->getLocation(), diag::note_template_decl_here);
|
2009-02-26 06:02:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-06-13 05:21:02 +08:00
|
|
|
/// \brief Check the non-type template arguments of a class template
|
|
|
|
/// partial specialization according to C++ [temp.class.spec]p9.
|
|
|
|
///
|
2009-06-13 06:08:06 +08:00
|
|
|
/// \param TemplateParams the template parameters of the primary class
|
|
|
|
/// template.
|
|
|
|
///
|
|
|
|
/// \param TemplateArg the template arguments of the class template
|
|
|
|
/// partial specialization.
|
|
|
|
///
|
|
|
|
/// \param MirrorsPrimaryTemplate will be set true if the class
|
|
|
|
/// template partial specialization arguments are identical to the
|
|
|
|
/// implicit template arguments of the primary template. This is not
|
|
|
|
/// necessarily an error (C++0x), and it is left to the caller to diagnose
|
|
|
|
/// this condition when it is an error.
|
|
|
|
///
|
2009-06-13 05:21:02 +08:00
|
|
|
/// \returns true if there was an error, false otherwise.
|
|
|
|
bool Sema::CheckClassTemplatePartialSpecializationArgs(
|
|
|
|
TemplateParameterList *TemplateParams,
|
2009-06-14 02:20:51 +08:00
|
|
|
const TemplateArgumentListBuilder &TemplateArgs,
|
2009-06-13 06:08:06 +08:00
|
|
|
bool &MirrorsPrimaryTemplate) {
|
2009-06-13 05:21:02 +08:00
|
|
|
// FIXME: the interface to this function will have to change to
|
|
|
|
// accommodate variadic templates.
|
2009-06-13 06:08:06 +08:00
|
|
|
MirrorsPrimaryTemplate = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-23 09:26:57 +08:00
|
|
|
const TemplateArgument *ArgList = TemplateArgs.getFlatArguments();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 05:21:02 +08:00
|
|
|
for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
|
2009-06-13 06:08:06 +08:00
|
|
|
// Determine whether the template argument list of the partial
|
|
|
|
// specialization is identical to the implicit argument list of
|
|
|
|
// the primary template. The caller may need to diagnostic this as
|
|
|
|
// an error per C++ [temp.class.spec]p9b3.
|
|
|
|
if (MirrorsPrimaryTemplate) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (TemplateTypeParmDecl *TTP
|
2009-06-13 06:08:06 +08:00
|
|
|
= dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) {
|
|
|
|
if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) !=
|
2009-06-14 02:20:51 +08:00
|
|
|
Context.getCanonicalType(ArgList[I].getAsType()))
|
2009-06-13 06:08:06 +08:00
|
|
|
MirrorsPrimaryTemplate = false;
|
|
|
|
} else if (TemplateTemplateParmDecl *TTP
|
|
|
|
= dyn_cast<TemplateTemplateParmDecl>(
|
|
|
|
TemplateParams->getParam(I))) {
|
|
|
|
// FIXME: We should settle on either Declaration storage or
|
|
|
|
// Expression storage for template template parameters.
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateTemplateParmDecl *ArgDecl
|
2009-06-13 06:08:06 +08:00
|
|
|
= dyn_cast_or_null<TemplateTemplateParmDecl>(
|
2009-06-14 02:20:51 +08:00
|
|
|
ArgList[I].getAsDecl());
|
2009-06-13 06:08:06 +08:00
|
|
|
if (!ArgDecl)
|
2009-09-09 23:08:12 +08:00
|
|
|
if (DeclRefExpr *DRE
|
2009-06-14 02:20:51 +08:00
|
|
|
= dyn_cast_or_null<DeclRefExpr>(ArgList[I].getAsExpr()))
|
2009-06-13 06:08:06 +08:00
|
|
|
ArgDecl = dyn_cast<TemplateTemplateParmDecl>(DRE->getDecl());
|
|
|
|
|
|
|
|
if (!ArgDecl ||
|
|
|
|
ArgDecl->getIndex() != TTP->getIndex() ||
|
|
|
|
ArgDecl->getDepth() != TTP->getDepth())
|
|
|
|
MirrorsPrimaryTemplate = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
NonTypeTemplateParmDecl *Param
|
2009-06-13 05:21:02 +08:00
|
|
|
= dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
|
2009-06-13 06:08:06 +08:00
|
|
|
if (!Param) {
|
2009-06-13 05:21:02 +08:00
|
|
|
continue;
|
2009-06-13 06:08:06 +08:00
|
|
|
}
|
|
|
|
|
2009-06-14 02:20:51 +08:00
|
|
|
Expr *ArgExpr = ArgList[I].getAsExpr();
|
2009-06-13 06:08:06 +08:00
|
|
|
if (!ArgExpr) {
|
|
|
|
MirrorsPrimaryTemplate = false;
|
2009-06-13 05:21:02 +08:00
|
|
|
continue;
|
2009-06-13 06:08:06 +08:00
|
|
|
}
|
2009-06-13 05:21:02 +08:00
|
|
|
|
|
|
|
// C++ [temp.class.spec]p8:
|
|
|
|
// A non-type argument is non-specialized if it is the name of a
|
|
|
|
// non-type parameter. All other non-type arguments are
|
|
|
|
// specialized.
|
|
|
|
//
|
|
|
|
// Below, we check the two conditions that only apply to
|
|
|
|
// specialized non-type arguments, so skip any non-specialized
|
|
|
|
// arguments.
|
|
|
|
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
|
2009-09-09 23:08:12 +08:00
|
|
|
if (NonTypeTemplateParmDecl *NTTP
|
2009-06-13 06:08:06 +08:00
|
|
|
= dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (MirrorsPrimaryTemplate &&
|
2009-06-13 06:08:06 +08:00
|
|
|
(Param->getIndex() != NTTP->getIndex() ||
|
|
|
|
Param->getDepth() != NTTP->getDepth()))
|
|
|
|
MirrorsPrimaryTemplate = false;
|
|
|
|
|
2009-06-13 05:21:02 +08:00
|
|
|
continue;
|
2009-06-13 06:08:06 +08:00
|
|
|
}
|
2009-06-13 05:21:02 +08:00
|
|
|
|
|
|
|
// C++ [temp.class.spec]p9:
|
|
|
|
// Within the argument list of a class template partial
|
|
|
|
// specialization, the following restrictions apply:
|
|
|
|
// -- A partially specialized non-type argument expression
|
|
|
|
// shall not involve a template parameter of the partial
|
|
|
|
// specialization except when the argument expression is a
|
|
|
|
// simple identifier.
|
|
|
|
if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(ArgExpr->getLocStart(),
|
2009-06-13 05:21:02 +08:00
|
|
|
diag::err_dependent_non_type_arg_in_partial_spec)
|
|
|
|
<< ArgExpr->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- The type of a template parameter corresponding to a
|
|
|
|
// specialized non-type argument shall not be dependent on a
|
|
|
|
// parameter of the specialization.
|
|
|
|
if (Param->getType()->isDependentType()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(ArgExpr->getLocStart(),
|
2009-06-13 05:21:02 +08:00
|
|
|
diag::err_dependent_typed_non_type_arg_in_partial_spec)
|
|
|
|
<< Param->getType()
|
|
|
|
<< ArgExpr->getSourceRange();
|
|
|
|
Diag(Param->getLocation(), diag::note_template_param_here);
|
|
|
|
return true;
|
|
|
|
}
|
2009-06-13 06:08:06 +08:00
|
|
|
|
|
|
|
MirrorsPrimaryTemplate = false;
|
2009-06-13 05:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-25 08:13:59 +08:00
|
|
|
Sema::DeclResult
|
2009-07-31 10:45:11 +08:00
|
|
|
Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
|
|
|
|
TagUseKind TUK,
|
2009-09-09 23:08:12 +08:00
|
|
|
SourceLocation KWLoc,
|
2009-02-18 07:15:12 +08:00
|
|
|
const CXXScopeSpec &SS,
|
2009-03-31 06:58:21 +08:00
|
|
|
TemplateTy TemplateD,
|
2009-02-18 07:15:12 +08:00
|
|
|
SourceLocation TemplateNameLoc,
|
|
|
|
SourceLocation LAngleLoc,
|
2009-03-10 07:48:35 +08:00
|
|
|
ASTTemplateArgsPtr TemplateArgsIn,
|
2009-02-18 07:15:12 +08:00
|
|
|
SourceLocation *TemplateArgLocs,
|
|
|
|
SourceLocation RAngleLoc,
|
|
|
|
AttributeList *Attr,
|
|
|
|
MultiTemplateParamsArg TemplateParameterLists) {
|
2009-09-04 09:14:41 +08:00
|
|
|
assert(TUK == TUK_Declaration || TUK == TUK_Definition);
|
|
|
|
|
2009-02-18 07:15:12 +08:00
|
|
|
// Find the class template we're specializing
|
2009-03-31 06:58:21 +08:00
|
|
|
TemplateName Name = TemplateD.getAsVal<TemplateName>();
|
2009-09-09 23:08:12 +08:00
|
|
|
ClassTemplateDecl *ClassTemplate
|
2009-03-31 06:58:21 +08:00
|
|
|
= cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
|
2009-02-18 07:15:12 +08:00
|
|
|
|
2009-05-31 17:31:02 +08:00
|
|
|
bool isPartialSpecialization = false;
|
|
|
|
|
2009-02-26 06:02:03 +08:00
|
|
|
// Check the validity of the template headers that introduce this
|
|
|
|
// template.
|
2009-08-26 01:23:04 +08:00
|
|
|
TemplateParameterList *TemplateParams
|
2009-09-09 23:08:12 +08:00
|
|
|
= MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
|
|
|
|
(TemplateParameterList**)TemplateParameterLists.get(),
|
2009-08-26 01:23:04 +08:00
|
|
|
TemplateParameterLists.size());
|
|
|
|
if (TemplateParams && TemplateParams->size() > 0) {
|
|
|
|
isPartialSpecialization = true;
|
|
|
|
|
|
|
|
// C++ [temp.class.spec]p10:
|
|
|
|
// The template parameter list of a specialization shall not
|
|
|
|
// contain default template argument values.
|
|
|
|
for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
|
|
|
|
Decl *Param = TemplateParams->getParam(I);
|
|
|
|
if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
|
|
|
|
if (TTP->hasDefaultArgument()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(TTP->getDefaultArgumentLoc(),
|
2009-08-26 01:23:04 +08:00
|
|
|
diag::err_default_arg_in_partial_spec);
|
|
|
|
TTP->setDefaultArgument(QualType(), SourceLocation(), false);
|
|
|
|
}
|
|
|
|
} else if (NonTypeTemplateParmDecl *NTTP
|
|
|
|
= dyn_cast<NonTypeTemplateParmDecl>(Param)) {
|
|
|
|
if (Expr *DefArg = NTTP->getDefaultArgument()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(NTTP->getDefaultArgumentLoc(),
|
2009-08-26 01:23:04 +08:00
|
|
|
diag::err_default_arg_in_partial_spec)
|
|
|
|
<< DefArg->getSourceRange();
|
|
|
|
NTTP->setDefaultArgument(0);
|
|
|
|
DefArg->Destroy(Context);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
|
|
|
|
if (Expr *DefArg = TTP->getDefaultArgument()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(TTP->getDefaultArgumentLoc(),
|
2009-08-26 01:23:04 +08:00
|
|
|
diag::err_default_arg_in_partial_spec)
|
|
|
|
<< DefArg->getSourceRange();
|
|
|
|
TTP->setDefaultArgument(0);
|
|
|
|
DefArg->Destroy(Context);
|
2009-06-13 03:43:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-26 01:23:04 +08:00
|
|
|
} else if (!TemplateParams)
|
|
|
|
Diag(KWLoc, diag::err_template_spec_needs_header)
|
|
|
|
<< CodeModificationHint::CreateInsertion(KWLoc, "template<> ");
|
2009-02-26 06:02:03 +08:00
|
|
|
|
2009-02-18 07:15:12 +08:00
|
|
|
// Check that the specialization uses the same tag kind as the
|
|
|
|
// original template.
|
|
|
|
TagDecl::TagKind Kind;
|
|
|
|
switch (TagSpec) {
|
|
|
|
default: assert(0 && "Unknown tag type!");
|
|
|
|
case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
|
|
|
|
case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
|
|
|
|
case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
|
|
|
|
}
|
2009-05-15 00:41:31 +08:00
|
|
|
if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
|
2009-09-09 23:08:12 +08:00
|
|
|
Kind, KWLoc,
|
2009-05-15 00:41:31 +08:00
|
|
|
*ClassTemplate->getIdentifier())) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(KWLoc, diag::err_use_with_wrong_tag)
|
2009-04-02 07:51:29 +08:00
|
|
|
<< ClassTemplate
|
2009-09-09 23:08:12 +08:00
|
|
|
<< CodeModificationHint::CreateReplacement(KWLoc,
|
2009-04-02 07:51:29 +08:00
|
|
|
ClassTemplate->getTemplatedDecl()->getKindName());
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
|
2009-02-18 07:15:12 +08:00
|
|
|
diag::note_previous_use);
|
|
|
|
Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
|
|
|
|
}
|
|
|
|
|
2009-03-10 07:48:35 +08:00
|
|
|
// Translate the parser's template argument list in our AST format.
|
|
|
|
llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
|
|
|
|
translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
|
|
|
|
|
2009-02-18 07:15:12 +08:00
|
|
|
// Check that the template argument list is well-formed for this
|
|
|
|
// template.
|
2009-06-23 09:26:57 +08:00
|
|
|
TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
|
|
|
|
TemplateArgs.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc,
|
2009-06-14 02:20:51 +08:00
|
|
|
TemplateArgs.data(), TemplateArgs.size(),
|
2009-07-01 08:28:38 +08:00
|
|
|
RAngleLoc, false, Converted))
|
2009-03-25 08:13:59 +08:00
|
|
|
return true;
|
2009-02-18 07:15:12 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
assert((Converted.structuredSize() ==
|
2009-02-18 07:15:12 +08:00
|
|
|
ClassTemplate->getTemplateParameters()->size()) &&
|
|
|
|
"Converted template argument list is too short!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-31 17:31:02 +08:00
|
|
|
// Find the class template (partial) specialization declaration that
|
2009-02-18 07:15:12 +08:00
|
|
|
// corresponds to these arguments.
|
|
|
|
llvm::FoldingSetNodeID ID;
|
2009-06-13 03:43:02 +08:00
|
|
|
if (isPartialSpecialization) {
|
2009-06-13 06:08:06 +08:00
|
|
|
bool MirrorsPrimaryTemplate;
|
2009-06-13 05:21:02 +08:00
|
|
|
if (CheckClassTemplatePartialSpecializationArgs(
|
|
|
|
ClassTemplate->getTemplateParameters(),
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted, MirrorsPrimaryTemplate))
|
2009-06-13 05:21:02 +08:00
|
|
|
return true;
|
|
|
|
|
2009-06-13 06:08:06 +08:00
|
|
|
if (MirrorsPrimaryTemplate) {
|
|
|
|
// C++ [temp.class.spec]p9b3:
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
// -- The argument list of the specialization shall not be identical
|
|
|
|
// to the implicit argument list of the primary template.
|
2009-06-13 06:08:06 +08:00
|
|
|
Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
|
2009-07-31 10:45:11 +08:00
|
|
|
<< (TUK == TUK_Definition)
|
2009-09-09 23:08:12 +08:00
|
|
|
<< CodeModificationHint::CreateRemoval(SourceRange(LAngleLoc,
|
2009-06-13 06:08:06 +08:00
|
|
|
RAngleLoc));
|
2009-07-31 10:45:11 +08:00
|
|
|
return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
|
2009-06-13 06:08:06 +08:00
|
|
|
ClassTemplate->getIdentifier(),
|
|
|
|
TemplateNameLoc,
|
|
|
|
Attr,
|
2009-08-26 01:23:04 +08:00
|
|
|
TemplateParams,
|
2009-06-13 06:08:06 +08:00
|
|
|
AS_none);
|
|
|
|
}
|
|
|
|
|
2009-05-31 17:31:02 +08:00
|
|
|
// FIXME: Template parameter list matters, too
|
2009-09-09 23:08:12 +08:00
|
|
|
ClassTemplatePartialSpecializationDecl::Profile(ID,
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.getFlatArguments(),
|
2009-07-30 00:09:57 +08:00
|
|
|
Converted.flatSize(),
|
|
|
|
Context);
|
2009-08-05 05:02:39 +08:00
|
|
|
} else
|
2009-06-05 11:43:12 +08:00
|
|
|
ClassTemplateSpecializationDecl::Profile(ID,
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.getFlatArguments(),
|
2009-07-30 00:09:57 +08:00
|
|
|
Converted.flatSize(),
|
|
|
|
Context);
|
2009-02-18 07:15:12 +08:00
|
|
|
void *InsertPos = 0;
|
2009-05-31 17:31:02 +08:00
|
|
|
ClassTemplateSpecializationDecl *PrevDecl = 0;
|
|
|
|
|
|
|
|
if (isPartialSpecialization)
|
|
|
|
PrevDecl
|
2009-09-09 23:08:12 +08:00
|
|
|
= ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
|
2009-05-31 17:31:02 +08:00
|
|
|
InsertPos);
|
|
|
|
else
|
|
|
|
PrevDecl
|
|
|
|
= ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
|
2009-02-18 07:15:12 +08:00
|
|
|
|
|
|
|
ClassTemplateSpecializationDecl *Specialization = 0;
|
|
|
|
|
2009-02-26 06:02:03 +08:00
|
|
|
// Check whether we can declare a class template specialization in
|
|
|
|
// the current scope.
|
|
|
|
if (CheckClassTemplateSpecializationScope(ClassTemplate, PrevDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateNameLoc,
|
2009-05-14 02:28:20 +08:00
|
|
|
SS.getRange(),
|
2009-06-13 06:21:45 +08:00
|
|
|
isPartialSpecialization,
|
2009-05-14 02:28:20 +08:00
|
|
|
/*ExplicitInstantiation=*/false))
|
2009-03-25 08:13:59 +08:00
|
|
|
return true;
|
2009-02-26 06:02:03 +08:00
|
|
|
|
2009-07-31 01:40:51 +08:00
|
|
|
// The canonical type
|
|
|
|
QualType CanonType;
|
2009-02-18 07:15:12 +08:00
|
|
|
if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) {
|
|
|
|
// Since the only prior class template specialization with these
|
|
|
|
// arguments was referenced but not declared, reuse that
|
|
|
|
// declaration node as our own, updating its source location to
|
|
|
|
// reflect our new declaration.
|
|
|
|
Specialization = PrevDecl;
|
2009-02-26 06:18:32 +08:00
|
|
|
Specialization->setLocation(TemplateNameLoc);
|
2009-02-18 07:15:12 +08:00
|
|
|
PrevDecl = 0;
|
2009-07-31 01:40:51 +08:00
|
|
|
CanonType = Context.getTypeDeclType(Specialization);
|
2009-05-31 17:31:02 +08:00
|
|
|
} else if (isPartialSpecialization) {
|
2009-07-31 01:40:51 +08:00
|
|
|
// Build the canonical type that describes the converted template
|
|
|
|
// arguments of the class template partial specialization.
|
|
|
|
CanonType = Context.getTemplateSpecializationType(
|
|
|
|
TemplateName(ClassTemplate),
|
|
|
|
Converted.getFlatArguments(),
|
|
|
|
Converted.flatSize());
|
|
|
|
|
2009-05-31 17:31:02 +08:00
|
|
|
// Create a new class template partial specialization declaration node.
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateParameterList *TemplateParams
|
2009-05-31 17:31:02 +08:00
|
|
|
= static_cast<TemplateParameterList*>(*TemplateParameterLists.get());
|
|
|
|
ClassTemplatePartialSpecializationDecl *PrevPartial
|
|
|
|
= cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
ClassTemplatePartialSpecializationDecl *Partial
|
|
|
|
= ClassTemplatePartialSpecializationDecl::Create(Context,
|
2009-05-31 17:31:02 +08:00
|
|
|
ClassTemplate->getDeclContext(),
|
2009-06-05 12:06:48 +08:00
|
|
|
TemplateNameLoc,
|
|
|
|
TemplateParams,
|
|
|
|
ClassTemplate,
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted,
|
2009-06-05 12:06:48 +08:00
|
|
|
PrevPartial);
|
2009-05-31 17:31:02 +08:00
|
|
|
|
|
|
|
if (PrevPartial) {
|
|
|
|
ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial);
|
|
|
|
ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial);
|
|
|
|
} else {
|
|
|
|
ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos);
|
|
|
|
}
|
|
|
|
Specialization = Partial;
|
2009-06-13 08:26:55 +08:00
|
|
|
|
|
|
|
// Check that all of the template parameters of the class template
|
|
|
|
// partial specialization are deducible from the template
|
|
|
|
// arguments. If not, this class template partial specialization
|
|
|
|
// will never be used.
|
|
|
|
llvm::SmallVector<bool, 8> DeducibleParams;
|
|
|
|
DeducibleParams.resize(TemplateParams->size());
|
|
|
|
MarkDeducedTemplateParameters(Partial->getTemplateArgs(), DeducibleParams);
|
|
|
|
unsigned NumNonDeducible = 0;
|
|
|
|
for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
|
|
|
|
if (!DeducibleParams[I])
|
|
|
|
++NumNonDeducible;
|
|
|
|
|
|
|
|
if (NumNonDeducible) {
|
|
|
|
Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
|
|
|
|
<< (NumNonDeducible > 1)
|
|
|
|
<< SourceRange(TemplateNameLoc, RAngleLoc);
|
|
|
|
for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
|
|
|
|
if (!DeducibleParams[I]) {
|
|
|
|
NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
|
|
|
|
if (Param->getDeclName())
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Param->getLocation(),
|
2009-06-13 08:26:55 +08:00
|
|
|
diag::note_partial_spec_unused_parameter)
|
|
|
|
<< Param->getDeclName();
|
|
|
|
else
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(Param->getLocation(),
|
2009-06-13 08:26:55 +08:00
|
|
|
diag::note_partial_spec_unused_parameter)
|
|
|
|
<< std::string("<anonymous>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-18 07:15:12 +08:00
|
|
|
} else {
|
|
|
|
// Create a new class template specialization declaration node for
|
|
|
|
// this explicit specialization.
|
|
|
|
Specialization
|
2009-09-09 23:08:12 +08:00
|
|
|
= ClassTemplateSpecializationDecl::Create(Context,
|
2009-02-18 07:15:12 +08:00
|
|
|
ClassTemplate->getDeclContext(),
|
|
|
|
TemplateNameLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
ClassTemplate,
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted,
|
2009-02-18 07:15:12 +08:00
|
|
|
PrevDecl);
|
|
|
|
|
|
|
|
if (PrevDecl) {
|
|
|
|
ClassTemplate->getSpecializations().RemoveNode(PrevDecl);
|
|
|
|
ClassTemplate->getSpecializations().GetOrInsertNode(Specialization);
|
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
ClassTemplate->getSpecializations().InsertNode(Specialization,
|
2009-02-18 07:15:12 +08:00
|
|
|
InsertPos);
|
|
|
|
}
|
2009-07-31 01:40:51 +08:00
|
|
|
|
|
|
|
CanonType = Context.getTypeDeclType(Specialization);
|
2009-02-18 07:15:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note that this is an explicit specialization.
|
|
|
|
Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
|
|
|
|
|
|
|
|
// Check that this isn't a redefinition of this specialization.
|
2009-07-31 10:45:11 +08:00
|
|
|
if (TUK == TUK_Definition) {
|
2009-02-18 07:15:12 +08:00
|
|
|
if (RecordDecl *Def = Specialization->getDefinition(Context)) {
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: Should also handle explicit specialization after implicit
|
|
|
|
// instantiation with a special diagnostic.
|
2009-02-18 07:15:12 +08:00
|
|
|
SourceRange Range(TemplateNameLoc, RAngleLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(TemplateNameLoc, diag::err_redefinition)
|
2009-05-31 17:31:02 +08:00
|
|
|
<< Context.getTypeDeclType(Specialization) << Range;
|
2009-02-18 07:15:12 +08:00
|
|
|
Diag(Def->getLocation(), diag::note_previous_definition);
|
|
|
|
Specialization->setInvalidDecl();
|
2009-03-25 08:13:59 +08:00
|
|
|
return true;
|
2009-02-18 07:15:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-27 06:19:44 +08: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.
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType WrittenTy
|
|
|
|
= Context.getTemplateSpecializationType(Name,
|
2009-06-14 02:20:51 +08:00
|
|
|
TemplateArgs.data(),
|
2009-03-31 06:58:21 +08:00
|
|
|
TemplateArgs.size(),
|
2009-07-31 01:40:51 +08:00
|
|
|
CanonType);
|
2009-03-31 06:58:21 +08:00
|
|
|
Specialization->setTypeAsWritten(WrittenTy);
|
2009-03-10 07:48:35 +08:00
|
|
|
TemplateArgsIn.release();
|
2009-02-18 07:15:12 +08:00
|
|
|
|
2009-02-26 06:18:32 +08:00
|
|
|
// C++ [temp.expl.spec]p9:
|
|
|
|
// A template explicit specialization is in the scope of the
|
|
|
|
// namespace in which the template was defined.
|
|
|
|
//
|
|
|
|
// We actually implement this paragraph where we set the semantic
|
|
|
|
// context (in the creation of the ClassTemplateSpecializationDecl),
|
|
|
|
// but we also maintain the lexical context where the actual
|
|
|
|
// definition occurs.
|
2009-02-18 07:15:12 +08:00
|
|
|
Specialization->setLexicalDeclContext(CurContext);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-18 07:15:12 +08:00
|
|
|
// We may be starting the definition of this specialization.
|
2009-07-31 10:45:11 +08:00
|
|
|
if (TUK == TUK_Definition)
|
2009-02-18 07:15:12 +08:00
|
|
|
Specialization->startDefinition();
|
|
|
|
|
|
|
|
// Add the specialization into its lexical context, so that it can
|
|
|
|
// be seen when iterating through the list of declarations in that
|
|
|
|
// context. However, specializations are not found by name lookup.
|
2009-06-30 10:36:12 +08:00
|
|
|
CurContext->addDecl(Specialization);
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(Specialization);
|
2009-02-18 07:15:12 +08:00
|
|
|
}
|
2009-03-28 07:10:48 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Sema::DeclPtrTy
|
|
|
|
Sema::ActOnTemplateDeclarator(Scope *S,
|
2009-06-24 07:11:28 +08:00
|
|
|
MultiTemplateParamsArg TemplateParameterLists,
|
|
|
|
Declarator &D) {
|
|
|
|
return HandleDeclarator(S, D, move(TemplateParameterLists), false);
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Sema::DeclPtrTy
|
|
|
|
Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
|
2009-06-24 08:54:41 +08:00
|
|
|
MultiTemplateParamsArg TemplateParameterLists,
|
|
|
|
Declarator &D) {
|
|
|
|
assert(getCurFunctionDecl() == 0 && "Function parsing confused");
|
|
|
|
assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
|
|
|
|
"Not a function declarator!");
|
|
|
|
DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-24 08:54:41 +08:00
|
|
|
if (FTI.hasPrototype) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// FIXME: Diagnose arguments without names in C.
|
2009-06-24 08:54:41 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-24 08:54:41 +08:00
|
|
|
Scope *ParentScope = FnBodyScope->getParent();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
DeclPtrTy DP = HandleDeclarator(ParentScope, D,
|
2009-06-24 08:54:41 +08:00
|
|
|
move(TemplateParameterLists),
|
|
|
|
/*IsFunctionDefinition=*/true);
|
2009-09-09 23:08:12 +08:00
|
|
|
if (FunctionTemplateDecl *FunctionTemplate
|
2009-07-22 07:53:31 +08:00
|
|
|
= dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>()))
|
2009-09-09 23:08:12 +08:00
|
|
|
return ActOnStartOfFunctionDef(FnBodyScope,
|
2009-06-26 06:08:12 +08:00
|
|
|
DeclPtrTy::make(FunctionTemplate->getTemplatedDecl()));
|
2009-07-22 07:53:31 +08:00
|
|
|
if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>()))
|
|
|
|
return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function));
|
2009-06-26 06:08:12 +08:00
|
|
|
return DeclPtrTy();
|
2009-06-24 08:54:41 +08:00
|
|
|
}
|
|
|
|
|
2009-05-14 08:28:11 +08:00
|
|
|
// Explicit instantiation of a class template specialization
|
2009-09-04 14:33:52 +08:00
|
|
|
// FIXME: Implement extern template semantics
|
2009-05-13 08:25:59 +08:00
|
|
|
Sema::DeclResult
|
2009-09-09 23:08:12 +08:00
|
|
|
Sema::ActOnExplicitInstantiation(Scope *S,
|
2009-09-04 14:33:52 +08:00
|
|
|
SourceLocation ExternLoc,
|
|
|
|
SourceLocation TemplateLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned TagSpec,
|
2009-05-13 08:25:59 +08:00
|
|
|
SourceLocation KWLoc,
|
|
|
|
const CXXScopeSpec &SS,
|
|
|
|
TemplateTy TemplateD,
|
|
|
|
SourceLocation TemplateNameLoc,
|
|
|
|
SourceLocation LAngleLoc,
|
|
|
|
ASTTemplateArgsPtr TemplateArgsIn,
|
|
|
|
SourceLocation *TemplateArgLocs,
|
|
|
|
SourceLocation RAngleLoc,
|
|
|
|
AttributeList *Attr) {
|
|
|
|
// Find the class template we're specializing
|
|
|
|
TemplateName Name = TemplateD.getAsVal<TemplateName>();
|
2009-09-09 23:08:12 +08:00
|
|
|
ClassTemplateDecl *ClassTemplate
|
2009-05-13 08:25:59 +08:00
|
|
|
= cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
|
|
|
|
|
|
|
|
// Check that the specialization uses the same tag kind as the
|
|
|
|
// original template.
|
|
|
|
TagDecl::TagKind Kind;
|
|
|
|
switch (TagSpec) {
|
|
|
|
default: assert(0 && "Unknown tag type!");
|
|
|
|
case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
|
|
|
|
case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
|
|
|
|
case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
|
|
|
|
}
|
2009-05-15 00:41:31 +08:00
|
|
|
if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
|
2009-09-09 23:08:12 +08:00
|
|
|
Kind, KWLoc,
|
2009-05-15 00:41:31 +08:00
|
|
|
*ClassTemplate->getIdentifier())) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(KWLoc, diag::err_use_with_wrong_tag)
|
2009-05-13 08:25:59 +08:00
|
|
|
<< ClassTemplate
|
2009-09-09 23:08:12 +08:00
|
|
|
<< CodeModificationHint::CreateReplacement(KWLoc,
|
2009-05-13 08:25:59 +08:00
|
|
|
ClassTemplate->getTemplatedDecl()->getKindName());
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
|
2009-05-13 08:25:59 +08:00
|
|
|
diag::note_previous_use);
|
|
|
|
Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
|
|
|
|
}
|
|
|
|
|
2009-05-14 02:28:20 +08:00
|
|
|
// C++0x [temp.explicit]p2:
|
|
|
|
// [...] An explicit instantiation shall appear in an enclosing
|
|
|
|
// namespace of its template. [...]
|
|
|
|
//
|
|
|
|
// This is C++ DR 275.
|
|
|
|
if (CheckClassTemplateSpecializationScope(ClassTemplate, 0,
|
2009-09-09 23:08:12 +08:00
|
|
|
TemplateNameLoc,
|
2009-05-14 02:28:20 +08:00
|
|
|
SS.getRange(),
|
2009-06-13 06:21:45 +08:00
|
|
|
/*PartialSpecialization=*/false,
|
2009-05-14 02:28:20 +08:00
|
|
|
/*ExplicitInstantiation=*/true))
|
|
|
|
return true;
|
|
|
|
|
2009-05-13 08:25:59 +08:00
|
|
|
// Translate the parser's template argument list in our AST format.
|
|
|
|
llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
|
|
|
|
translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
|
|
|
|
|
|
|
|
// Check that the template argument list is well-formed for this
|
|
|
|
// template.
|
2009-06-23 09:26:57 +08:00
|
|
|
TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
|
|
|
|
TemplateArgs.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc,
|
2009-06-05 10:12:32 +08:00
|
|
|
TemplateArgs.data(), TemplateArgs.size(),
|
2009-07-01 08:28:38 +08:00
|
|
|
RAngleLoc, false, Converted))
|
2009-05-13 08:25:59 +08:00
|
|
|
return true;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
assert((Converted.structuredSize() ==
|
2009-05-13 08:25:59 +08:00
|
|
|
ClassTemplate->getTemplateParameters()->size()) &&
|
|
|
|
"Converted template argument list is too short!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-13 08:25:59 +08:00
|
|
|
// Find the class template specialization declaration that
|
|
|
|
// corresponds to these arguments.
|
|
|
|
llvm::FoldingSetNodeID ID;
|
2009-09-09 23:08:12 +08:00
|
|
|
ClassTemplateSpecializationDecl::Profile(ID,
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted.getFlatArguments(),
|
2009-07-30 00:09:57 +08:00
|
|
|
Converted.flatSize(),
|
|
|
|
Context);
|
2009-05-13 08:25:59 +08:00
|
|
|
void *InsertPos = 0;
|
|
|
|
ClassTemplateSpecializationDecl *PrevDecl
|
|
|
|
= ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
|
|
|
|
ClassTemplateSpecializationDecl *Specialization = 0;
|
|
|
|
|
2009-05-14 02:28:20 +08:00
|
|
|
bool SpecializationRequiresInstantiation = true;
|
2009-05-13 08:25:59 +08:00
|
|
|
if (PrevDecl) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (PrevDecl->getSpecializationKind()
|
2009-09-05 06:48:11 +08:00
|
|
|
== TSK_ExplicitInstantiationDefinition) {
|
2009-05-13 08:25:59 +08:00
|
|
|
// This particular specialization has already been declared or
|
|
|
|
// instantiated. We cannot explicitly instantiate it.
|
2009-05-14 02:28:20 +08:00
|
|
|
Diag(TemplateNameLoc, diag::err_explicit_instantiation_duplicate)
|
|
|
|
<< Context.getTypeDeclType(PrevDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(PrevDecl->getLocation(),
|
2009-05-14 02:28:20 +08:00
|
|
|
diag::note_previous_explicit_instantiation);
|
2009-05-13 08:25:59 +08:00
|
|
|
return DeclPtrTy::make(PrevDecl);
|
|
|
|
}
|
|
|
|
|
2009-05-14 02:28:20 +08:00
|
|
|
if (PrevDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
|
2009-05-14 08:28:11 +08:00
|
|
|
// C++ DR 259, C++0x [temp.explicit]p4:
|
2009-05-14 02:28:20 +08:00
|
|
|
// For a given set of template parameters, if an explicit
|
|
|
|
// instantiation of a template appears after a declaration of
|
|
|
|
// an explicit specialization for that template, the explicit
|
|
|
|
// instantiation has no effect.
|
|
|
|
if (!getLangOptions().CPlusPlus0x) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(TemplateNameLoc,
|
2009-05-14 02:28:20 +08:00
|
|
|
diag::ext_explicit_instantiation_after_specialization)
|
|
|
|
<< Context.getTypeDeclType(PrevDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(PrevDecl->getLocation(),
|
2009-05-14 02:28:20 +08:00
|
|
|
diag::note_previous_template_specialization);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new class template specialization declaration node
|
|
|
|
// for this explicit specialization. This node is only used to
|
|
|
|
// record the existence of this explicit instantiation for
|
|
|
|
// accurate reproduction of the source code; we don't actually
|
|
|
|
// use it for anything, since it is semantically irrelevant.
|
|
|
|
Specialization
|
2009-09-09 23:08:12 +08:00
|
|
|
= ClassTemplateSpecializationDecl::Create(Context,
|
2009-05-14 02:28:20 +08:00
|
|
|
ClassTemplate->getDeclContext(),
|
|
|
|
TemplateNameLoc,
|
|
|
|
ClassTemplate,
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted, 0);
|
2009-05-14 02:28:20 +08:00
|
|
|
Specialization->setLexicalDeclContext(CurContext);
|
2009-06-30 10:36:12 +08:00
|
|
|
CurContext->addDecl(Specialization);
|
2009-05-14 02:28:20 +08:00
|
|
|
return DeclPtrTy::make(Specialization);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have already (implicitly) instantiated this
|
|
|
|
// specialization, there is less work to do.
|
|
|
|
if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation)
|
|
|
|
SpecializationRequiresInstantiation = false;
|
|
|
|
|
2009-05-13 08:25:59 +08:00
|
|
|
// Since the only prior class template specialization with these
|
|
|
|
// arguments was referenced but not declared, reuse that
|
|
|
|
// declaration node as our own, updating its source location to
|
|
|
|
// reflect our new declaration.
|
|
|
|
Specialization = PrevDecl;
|
|
|
|
Specialization->setLocation(TemplateNameLoc);
|
|
|
|
PrevDecl = 0;
|
|
|
|
} else {
|
|
|
|
// Create a new class template specialization declaration node for
|
|
|
|
// this explicit specialization.
|
|
|
|
Specialization
|
2009-09-09 23:08:12 +08:00
|
|
|
= ClassTemplateSpecializationDecl::Create(Context,
|
2009-05-13 08:25:59 +08:00
|
|
|
ClassTemplate->getDeclContext(),
|
|
|
|
TemplateNameLoc,
|
|
|
|
ClassTemplate,
|
2009-06-23 09:26:57 +08:00
|
|
|
Converted, 0);
|
2009-05-13 08:25:59 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
ClassTemplate->getSpecializations().InsertNode(Specialization,
|
2009-05-13 08:25:59 +08:00
|
|
|
InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the fully-sugared type for this explicit instantiation as
|
|
|
|
// the user wrote in the explicit instantiation itself. This means
|
|
|
|
// that we'll pretty-print the type retrieved from the
|
|
|
|
// specialization's declaration the way that the user actually wrote
|
|
|
|
// the explicit instantiation, rather than formatting the name based
|
|
|
|
// on the "canonical" representation used to store the template
|
|
|
|
// arguments in the specialization.
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType WrittenTy
|
|
|
|
= Context.getTemplateSpecializationType(Name,
|
2009-06-05 10:45:24 +08:00
|
|
|
TemplateArgs.data(),
|
2009-05-13 08:25:59 +08:00
|
|
|
TemplateArgs.size(),
|
|
|
|
Context.getTypeDeclType(Specialization));
|
|
|
|
Specialization->setTypeAsWritten(WrittenTy);
|
|
|
|
TemplateArgsIn.release();
|
|
|
|
|
|
|
|
// Add the explicit instantiation into its lexical context. However,
|
|
|
|
// since explicit instantiations are never found by name lookup, we
|
|
|
|
// just put it into the declaration context directly.
|
|
|
|
Specialization->setLexicalDeclContext(CurContext);
|
2009-06-30 10:36:12 +08:00
|
|
|
CurContext->addDecl(Specialization);
|
2009-05-13 08:25:59 +08:00
|
|
|
|
|
|
|
// C++ [temp.explicit]p3:
|
|
|
|
// A definition of a class template or class member template
|
|
|
|
// shall be in scope at the point of the explicit instantiation of
|
|
|
|
// the class template or class member template.
|
|
|
|
//
|
|
|
|
// This check comes when we actually try to perform the
|
|
|
|
// instantiation.
|
2009-09-05 06:48:11 +08:00
|
|
|
TemplateSpecializationKind TSK
|
2009-09-09 23:08:12 +08:00
|
|
|
= ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
|
2009-09-05 06:48:11 +08:00
|
|
|
: TSK_ExplicitInstantiationDeclaration;
|
2009-05-16 01:59:04 +08:00
|
|
|
if (SpecializationRequiresInstantiation)
|
2009-09-05 06:48:11 +08:00
|
|
|
InstantiateClassTemplateSpecialization(Specialization, TSK);
|
2009-05-19 01:01:57 +08:00
|
|
|
else // Instantiate the members of this class template specialization.
|
2009-09-05 06:48:11 +08:00
|
|
|
InstantiateClassTemplateSpecializationMembers(TemplateLoc, Specialization,
|
|
|
|
TSK);
|
2009-05-13 08:25:59 +08:00
|
|
|
|
|
|
|
return DeclPtrTy::make(Specialization);
|
|
|
|
}
|
|
|
|
|
2009-05-14 08:28:11 +08:00
|
|
|
// Explicit instantiation of a member class of a class template.
|
|
|
|
Sema::DeclResult
|
2009-09-09 23:08:12 +08:00
|
|
|
Sema::ActOnExplicitInstantiation(Scope *S,
|
2009-09-04 14:33:52 +08:00
|
|
|
SourceLocation ExternLoc,
|
|
|
|
SourceLocation TemplateLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned TagSpec,
|
2009-05-14 08:28:11 +08:00
|
|
|
SourceLocation KWLoc,
|
|
|
|
const CXXScopeSpec &SS,
|
|
|
|
IdentifierInfo *Name,
|
|
|
|
SourceLocation NameLoc,
|
|
|
|
AttributeList *Attr) {
|
|
|
|
|
When we parse a tag specifier, keep track of whether that tag
specifier resulted in the creation of a new TagDecl node, which
happens either when the tag specifier was a definition or when the tag
specifier was the first declaration of that tag type. This information
has several uses, the first of which is implemented in this commit:
1) In C++, one is not allowed to define tag types within a type
specifier (e.g., static_cast<struct S { int x; } *>(0) is
ill-formed) or within the result or parameter types of a
function. We now diagnose this.
2) We can extend DeclGroups to contain information about any tags
that are declared/defined within the declaration specifiers of a
variable, e.g.,
struct Point { int x, y, z; } p;
This will help improve AST printing and template instantiation,
among other things.
3) For C99, we can keep track of whether a tag type is defined
within the type of a parameter, to properly cope with cases like,
e.g.,
int bar(struct T2 { int x; } y) {
struct T2 z;
}
We can also do similar things wherever there is a type specifier,
e.g., to keep track of where the definition of S occurs in this
legal C99 code:
(struct S { int x, y; } *)0
llvm-svn: 72555
2009-05-29 07:31:59 +08:00
|
|
|
bool Owned = false;
|
2009-07-31 10:45:11 +08:00
|
|
|
DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference,
|
2009-07-23 07:48:44 +08:00
|
|
|
KWLoc, SS, Name, NameLoc, Attr, AS_none,
|
|
|
|
MultiTemplateParamsArg(*this, 0, 0), Owned);
|
2009-05-14 08:28:11 +08:00
|
|
|
if (!TagD)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>());
|
|
|
|
if (Tag->isEnum()) {
|
|
|
|
Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
|
|
|
|
<< Context.getTypeDeclType(Tag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-28 01:30:49 +08:00
|
|
|
if (Tag->isInvalidDecl())
|
|
|
|
return true;
|
|
|
|
|
2009-05-14 08:28:11 +08:00
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
|
|
|
|
CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
|
|
|
|
if (!Pattern) {
|
|
|
|
Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
|
|
|
|
<< Context.getTypeDeclType(Record);
|
|
|
|
Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++0x [temp.explicit]p2:
|
|
|
|
// [...] An explicit instantiation shall appear in an enclosing
|
|
|
|
// namespace of its template. [...]
|
|
|
|
//
|
|
|
|
// This is C++ DR 275.
|
|
|
|
if (getLangOptions().CPlusPlus0x) {
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: In C++98, we would like to turn these errors into warnings,
|
|
|
|
// dependent on a -Wc++0x flag.
|
2009-09-09 23:08:12 +08:00
|
|
|
DeclContext *PatternContext
|
2009-05-14 08:28:11 +08:00
|
|
|
= Pattern->getDeclContext()->getEnclosingNamespaceContext();
|
|
|
|
if (!CurContext->Encloses(PatternContext)) {
|
|
|
|
Diag(TemplateLoc, diag::err_explicit_instantiation_out_of_scope)
|
|
|
|
<< Record << cast<NamedDecl>(PatternContext) << SS.getRange();
|
|
|
|
Diag(Pattern->getLocation(), diag::note_previous_declaration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-05 06:48:11 +08:00
|
|
|
TemplateSpecializationKind TSK
|
2009-09-09 23:08:12 +08:00
|
|
|
= ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
|
2009-09-05 06:48:11 +08:00
|
|
|
: TSK_ExplicitInstantiationDeclaration;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-14 08:28:11 +08:00
|
|
|
if (!Record->getDefinition(Context)) {
|
|
|
|
// If the class has a definition, instantiate it (and all of its
|
|
|
|
// members, recursively).
|
|
|
|
Pattern = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
|
2009-09-09 23:08:12 +08:00
|
|
|
if (Pattern && InstantiateClass(TemplateLoc, Record, Pattern,
|
2009-05-15 07:26:13 +08:00
|
|
|
getTemplateInstantiationArgs(Record),
|
2009-09-05 06:48:11 +08:00
|
|
|
TSK))
|
2009-05-14 08:28:11 +08:00
|
|
|
return true;
|
2009-08-26 06:02:44 +08:00
|
|
|
} else // Instantiate all of the members of the class.
|
2009-09-09 23:08:12 +08:00
|
|
|
InstantiateClassMembers(TemplateLoc, Record,
|
2009-09-05 06:48:11 +08:00
|
|
|
getTemplateInstantiationArgs(Record), TSK);
|
2009-05-14 08:28:11 +08:00
|
|
|
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: We don't have any representation for explicit instantiations of
|
|
|
|
// member classes. Such a representation is not needed for compilation, but it
|
|
|
|
// should be available for clients that want to see all of the declarations in
|
|
|
|
// the source code.
|
2009-05-14 08:28:11 +08:00
|
|
|
return TagD;
|
|
|
|
}
|
|
|
|
|
2009-03-28 07:10:48 +08:00
|
|
|
Sema::TypeResult
|
|
|
|
Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
|
|
|
|
const IdentifierInfo &II, SourceLocation IdLoc) {
|
2009-09-09 23:08:12 +08:00
|
|
|
NestedNameSpecifier *NNS
|
2009-03-28 07:10:48 +08:00
|
|
|
= static_cast<NestedNameSpecifier *>(SS.getScopeRep());
|
|
|
|
if (!NNS)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc));
|
2009-04-02 05:51:26 +08:00
|
|
|
if (T.isNull())
|
|
|
|
return true;
|
2009-03-28 07:10:48 +08:00
|
|
|
return T.getAsOpaquePtr();
|
|
|
|
}
|
|
|
|
|
2009-04-01 08:28:59 +08:00
|
|
|
Sema::TypeResult
|
|
|
|
Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
|
|
|
|
SourceLocation TemplateLoc, TypeTy *Ty) {
|
2009-08-19 09:28:28 +08:00
|
|
|
QualType T = GetTypeFromParser(Ty);
|
2009-09-09 23:08:12 +08:00
|
|
|
NestedNameSpecifier *NNS
|
2009-04-01 08:28:59 +08:00
|
|
|
= static_cast<NestedNameSpecifier *>(SS.getScopeRep());
|
2009-09-09 23:08:12 +08:00
|
|
|
const TemplateSpecializationType *TemplateId
|
2009-04-01 08:28:59 +08:00
|
|
|
= T->getAsTemplateSpecializationType();
|
|
|
|
assert(TemplateId && "Expected a template specialization type");
|
|
|
|
|
2009-09-02 21:05:45 +08:00
|
|
|
if (computeDeclContext(SS, false)) {
|
|
|
|
// If we can compute a declaration context, then the "typename"
|
|
|
|
// keyword was superfluous. Just build a QualifiedNameType to keep
|
|
|
|
// track of the nested-name-specifier.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-02 21:05:45 +08:00
|
|
|
// FIXME: Note that the QualifiedNameType had the "typename" keyword!
|
|
|
|
return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-02 21:05:45 +08:00
|
|
|
return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr();
|
2009-04-01 08:28:59 +08:00
|
|
|
}
|
|
|
|
|
2009-03-28 07:10:48 +08:00
|
|
|
/// \brief Build the type that describes a C++ typename specifier,
|
|
|
|
/// e.g., "typename T::type".
|
|
|
|
QualType
|
|
|
|
Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II,
|
|
|
|
SourceRange Range) {
|
2009-05-12 03:58:34 +08:00
|
|
|
CXXRecordDecl *CurrentInstantiation = 0;
|
|
|
|
if (NNS->isDependent()) {
|
|
|
|
CurrentInstantiation = getCurrentInstantiationOf(NNS);
|
|
|
|
|
|
|
|
// If the nested-name-specifier does not refer to the current
|
|
|
|
// instantiation, then build a typename type.
|
|
|
|
if (!CurrentInstantiation)
|
|
|
|
return Context.getTypenameType(NNS, &II);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-02 21:12:51 +08:00
|
|
|
// The nested-name-specifier refers to the current instantiation, so the
|
|
|
|
// "typename" keyword itself is superfluous. In C++03, the program is
|
2009-09-09 23:08:12 +08:00
|
|
|
// actually ill-formed. However, DR 382 (in C++0x CD1) allows such
|
2009-09-02 21:12:51 +08:00
|
|
|
// extraneous "typename" keywords, and we retroactively apply this DR to
|
|
|
|
// C++03 code.
|
2009-05-12 03:58:34 +08:00
|
|
|
}
|
2009-03-28 07:10:48 +08:00
|
|
|
|
2009-05-12 03:58:34 +08:00
|
|
|
DeclContext *Ctx = 0;
|
2009-03-28 07:10:48 +08:00
|
|
|
|
2009-05-12 03:58:34 +08:00
|
|
|
if (CurrentInstantiation)
|
|
|
|
Ctx = CurrentInstantiation;
|
|
|
|
else {
|
|
|
|
CXXScopeSpec SS;
|
|
|
|
SS.setScopeRep(NNS);
|
|
|
|
SS.setRange(Range);
|
|
|
|
if (RequireCompleteDeclContext(SS))
|
|
|
|
return QualType();
|
|
|
|
|
|
|
|
Ctx = computeDeclContext(SS);
|
|
|
|
}
|
2009-03-28 07:10:48 +08:00
|
|
|
assert(Ctx && "No declaration context?");
|
|
|
|
|
|
|
|
DeclarationName Name(&II);
|
2009-09-09 23:08:12 +08:00
|
|
|
LookupResult Result = LookupQualifiedName(Ctx, Name, LookupOrdinaryName,
|
2009-03-28 07:10:48 +08:00
|
|
|
false);
|
|
|
|
unsigned DiagID = 0;
|
|
|
|
Decl *Referenced = 0;
|
|
|
|
switch (Result.getKind()) {
|
|
|
|
case LookupResult::NotFound:
|
|
|
|
if (Ctx->isTranslationUnit())
|
|
|
|
DiagID = diag::err_typename_nested_not_found_global;
|
|
|
|
else
|
|
|
|
DiagID = diag::err_typename_nested_not_found;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LookupResult::Found:
|
|
|
|
if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getAsDecl())) {
|
|
|
|
// We found a type. Build a QualifiedNameType, since the
|
|
|
|
// typename-specifier was just sugar. FIXME: Tell
|
|
|
|
// QualifiedNameType that it has a "typename" prefix.
|
|
|
|
return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type));
|
|
|
|
}
|
|
|
|
|
|
|
|
DiagID = diag::err_typename_nested_not_type;
|
|
|
|
Referenced = Result.getAsDecl();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LookupResult::FoundOverloaded:
|
|
|
|
DiagID = diag::err_typename_nested_not_type;
|
|
|
|
Referenced = *Result.begin();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LookupResult::AmbiguousBaseSubobjectTypes:
|
|
|
|
case LookupResult::AmbiguousBaseSubobjects:
|
|
|
|
case LookupResult::AmbiguousReference:
|
|
|
|
DiagnoseAmbiguousLookup(Result, Name, Range.getEnd(), Range);
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, it's because name lookup did not find a
|
|
|
|
// type. Emit an appropriate diagnostic and return an error.
|
|
|
|
if (NamedDecl *NamedCtx = dyn_cast<NamedDecl>(Ctx))
|
|
|
|
Diag(Range.getEnd(), DiagID) << Range << Name << NamedCtx;
|
|
|
|
else
|
|
|
|
Diag(Range.getEnd(), DiagID) << Range << Name;
|
|
|
|
if (Referenced)
|
|
|
|
Diag(Referenced->getLocation(), diag::note_typename_refers_here)
|
|
|
|
<< Name;
|
|
|
|
return QualType();
|
|
|
|
}
|
2009-08-07 00:20:37 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
// See Sema::RebuildTypeInCurrentInstantiation
|
2009-09-09 23:08:12 +08:00
|
|
|
class VISIBILITY_HIDDEN CurrentInstantiationRebuilder
|
|
|
|
: public TreeTransform<CurrentInstantiationRebuilder> {
|
2009-08-07 00:20:37 +08:00
|
|
|
SourceLocation Loc;
|
|
|
|
DeclarationName Entity;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-07 00:20:37 +08:00
|
|
|
public:
|
2009-09-09 23:08:12 +08:00
|
|
|
CurrentInstantiationRebuilder(Sema &SemaRef,
|
2009-08-07 00:20:37 +08:00
|
|
|
SourceLocation Loc,
|
2009-09-09 23:08:12 +08:00
|
|
|
DeclarationName Entity)
|
|
|
|
: TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
|
2009-08-07 00:20:37 +08:00
|
|
|
Loc(Loc), Entity(Entity) { }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
/// \brief Determine whether the given type \p T has already been
|
2009-08-07 00:20:37 +08:00
|
|
|
/// transformed.
|
|
|
|
///
|
|
|
|
/// For the purposes of type reconstruction, a type has already been
|
|
|
|
/// transformed if it is NULL or if it is not dependent.
|
|
|
|
bool AlreadyTransformed(QualType T) {
|
|
|
|
return T.isNull() || !T->isDependentType();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
/// \brief Returns the location of the entity whose type is being
|
2009-08-07 00:20:37 +08:00
|
|
|
/// rebuilt.
|
|
|
|
SourceLocation getBaseLocation() { return Loc; }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-07 00:20:37 +08:00
|
|
|
/// \brief Returns the name of the entity whose type is being rebuilt.
|
|
|
|
DeclarationName getBaseEntity() { return Entity; }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-07 00:20:37 +08:00
|
|
|
/// \brief Transforms an expression by returning the expression itself
|
|
|
|
/// (an identity function).
|
|
|
|
///
|
|
|
|
/// FIXME: This is completely unsafe; we will need to actually clone the
|
|
|
|
/// expressions.
|
|
|
|
Sema::OwningExprResult TransformExpr(Expr *E) {
|
|
|
|
return getSema().Owned(E);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-07 00:20:37 +08:00
|
|
|
/// \brief Transforms a typename type by determining whether the type now
|
|
|
|
/// refers to a member of the current instantiation, and then
|
|
|
|
/// type-checking and building a QualifiedNameType (when possible).
|
|
|
|
QualType TransformTypenameType(const TypenameType *T);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType
|
2009-08-07 00:20:37 +08:00
|
|
|
CurrentInstantiationRebuilder::TransformTypenameType(const TypenameType *T) {
|
|
|
|
NestedNameSpecifier *NNS
|
|
|
|
= TransformNestedNameSpecifier(T->getQualifier(),
|
|
|
|
/*FIXME:*/SourceRange(getBaseLocation()));
|
|
|
|
if (!NNS)
|
|
|
|
return QualType();
|
|
|
|
|
|
|
|
// If the nested-name-specifier did not change, and we cannot compute the
|
|
|
|
// context corresponding to the nested-name-specifier, then this
|
|
|
|
// typename type will not change; exit early.
|
|
|
|
CXXScopeSpec SS;
|
|
|
|
SS.setRange(SourceRange(getBaseLocation()));
|
|
|
|
SS.setScopeRep(NNS);
|
|
|
|
if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0)
|
|
|
|
return QualType(T, 0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Rebuild the typename type, which will probably turn into a
|
2009-08-07 00:20:37 +08:00
|
|
|
// QualifiedNameType.
|
|
|
|
if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType NewTemplateId
|
2009-08-07 00:20:37 +08:00
|
|
|
= TransformType(QualType(TemplateId, 0));
|
|
|
|
if (NewTemplateId.isNull())
|
|
|
|
return QualType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-07 00:20:37 +08:00
|
|
|
if (NNS == T->getQualifier() &&
|
|
|
|
NewTemplateId == QualType(TemplateId, 0))
|
|
|
|
return QualType(T, 0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-07 00:20:37 +08:00
|
|
|
return getDerived().RebuildTypenameType(NNS, NewTemplateId);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-07 00:20:37 +08:00
|
|
|
return getDerived().RebuildTypenameType(NNS, T->getIdentifier());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Rebuilds a type within the context of the current instantiation.
|
|
|
|
///
|
2009-09-09 23:08:12 +08:00
|
|
|
/// The type \p T is part of the type of an out-of-line member definition of
|
2009-08-07 00:20:37 +08:00
|
|
|
/// a class template (or class template partial specialization) that was parsed
|
2009-09-09 23:08:12 +08:00
|
|
|
/// and constructed before we entered the scope of the class template (or
|
2009-08-07 00:20:37 +08:00
|
|
|
/// partial specialization thereof). This routine will rebuild that type now
|
|
|
|
/// that we have entered the declarator's scope, which may produce different
|
|
|
|
/// canonical types, e.g.,
|
|
|
|
///
|
|
|
|
/// \code
|
|
|
|
/// template<typename T>
|
|
|
|
/// struct X {
|
|
|
|
/// typedef T* pointer;
|
|
|
|
/// pointer data();
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// template<typename T>
|
|
|
|
/// typename X<T>::pointer X<T>::data() { ... }
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// Here, the type "typename X<T>::pointer" will be created as a TypenameType,
|
|
|
|
/// since we do not know that we can look into X<T> when we parsed the type.
|
|
|
|
/// This function will rebuild the type, performing the lookup of "pointer"
|
|
|
|
/// in X<T> and returning a QualifiedNameType whose canonical type is the same
|
|
|
|
/// as the canonical type of T*, allowing the return types of the out-of-line
|
|
|
|
/// definition and the declaration to match.
|
|
|
|
QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc,
|
|
|
|
DeclarationName Name) {
|
|
|
|
if (T.isNull() || !T->isDependentType())
|
|
|
|
return T;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-07 00:20:37 +08:00
|
|
|
CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
|
|
|
|
return Rebuilder.TransformType(T);
|
2009-08-12 06:33:06 +08:00
|
|
|
}
|