2017-11-22 07:26:08 +08:00
|
|
|
//===- TemplateName.cpp - C++ Template Name Representation ----------------===//
|
2009-03-31 06:58:21 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-03-31 06:58:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the TemplateName interface and subclasses.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-04-02 14:07:12 +08:00
|
|
|
|
2009-03-31 06:58:21 +08:00
|
|
|
#include "clang/AST/TemplateName.h"
|
2017-11-22 07:26:08 +08:00
|
|
|
#include "clang/AST/DeclBase.h"
|
2009-03-31 06:58:21 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
|
|
|
#include "clang/AST/NestedNameSpecifier.h"
|
2009-05-30 04:38:28 +08:00
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/TemplateBase.h"
|
2010-04-08 08:03:06 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2017-11-22 07:26:08 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
2009-06-30 09:26:17 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2017-11-22 07:26:08 +08:00
|
|
|
#include "clang/Basic/OperatorKinds.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-03-31 06:58:21 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-11-22 07:26:08 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
|
2009-03-31 06:58:21 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
TemplateArgument
|
2011-01-15 14:45:20 +08:00
|
|
|
SubstTemplateTemplateParmPackStorage::getArgumentPack() const {
|
2015-08-05 17:40:22 +08:00
|
|
|
return TemplateArgument(llvm::makeArrayRef(Arguments, size()));
|
2011-01-15 14:45:20 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 16:33:18 +08:00
|
|
|
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
|
|
|
|
Profile(ID, Parameter, Replacement);
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID,
|
2011-06-30 16:33:18 +08:00
|
|
|
TemplateTemplateParmDecl *parameter,
|
|
|
|
TemplateName replacement) {
|
|
|
|
ID.AddPointer(parameter);
|
|
|
|
ID.AddPointer(replacement.getAsVoidPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
|
|
|
|
ASTContext &Context) {
|
2015-08-05 17:40:22 +08:00
|
|
|
Profile(ID, Context, Parameter, getArgumentPack());
|
2011-01-15 14:45:20 +08:00
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void SubstTemplateTemplateParmPackStorage::Profile(llvm::FoldingSetNodeID &ID,
|
2011-01-15 14:45:20 +08:00
|
|
|
ASTContext &Context,
|
|
|
|
TemplateTemplateParmDecl *Parameter,
|
|
|
|
const TemplateArgument &ArgPack) {
|
|
|
|
ID.AddPointer(Parameter);
|
|
|
|
ArgPack.Profile(ID, Context);
|
|
|
|
}
|
|
|
|
|
2015-12-30 14:21:02 +08:00
|
|
|
TemplateName::TemplateName(void *Ptr) {
|
|
|
|
Storage = StorageType::getFromOpaqueValue(Ptr);
|
|
|
|
}
|
|
|
|
|
2015-12-30 11:24:14 +08:00
|
|
|
TemplateName::TemplateName(TemplateDecl *Template) : Storage(Template) {}
|
|
|
|
TemplateName::TemplateName(OverloadedTemplateStorage *Storage)
|
|
|
|
: Storage(Storage) {}
|
2019-05-09 11:31:27 +08:00
|
|
|
TemplateName::TemplateName(AssumedTemplateStorage *Storage)
|
|
|
|
: Storage(Storage) {}
|
2015-12-30 11:24:14 +08:00
|
|
|
TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
|
|
|
|
: Storage(Storage) {}
|
|
|
|
TemplateName::TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
|
|
|
|
: Storage(Storage) {}
|
|
|
|
TemplateName::TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) {}
|
|
|
|
TemplateName::TemplateName(DependentTemplateName *Dep) : Storage(Dep) {}
|
|
|
|
|
|
|
|
bool TemplateName::isNull() const { return Storage.isNull(); }
|
|
|
|
|
2010-06-20 03:28:53 +08:00
|
|
|
TemplateName::NameKind TemplateName::getKind() const {
|
|
|
|
if (Storage.is<TemplateDecl *>())
|
|
|
|
return Template;
|
2011-01-15 14:45:20 +08:00
|
|
|
if (Storage.is<DependentTemplateName *>())
|
|
|
|
return DependentTemplate;
|
2010-06-20 03:28:53 +08:00
|
|
|
if (Storage.is<QualifiedTemplateName *>())
|
|
|
|
return QualifiedTemplate;
|
2011-06-30 16:33:18 +08:00
|
|
|
|
|
|
|
UncommonTemplateNameStorage *uncommon
|
|
|
|
= Storage.get<UncommonTemplateNameStorage*>();
|
|
|
|
if (uncommon->getAsOverloadedStorage())
|
|
|
|
return OverloadedTemplate;
|
2019-05-09 11:31:27 +08:00
|
|
|
if (uncommon->getAsAssumedTemplateName())
|
|
|
|
return AssumedTemplate;
|
2011-06-30 16:33:18 +08:00
|
|
|
if (uncommon->getAsSubstTemplateTemplateParm())
|
|
|
|
return SubstTemplateTemplateParm;
|
|
|
|
return SubstTemplateTemplateParmPack;
|
2010-06-20 03:28:53 +08:00
|
|
|
}
|
|
|
|
|
2009-03-31 06:58:21 +08:00
|
|
|
TemplateDecl *TemplateName::getAsTemplateDecl() const {
|
|
|
|
if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
|
|
|
|
return Template;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-31 06:58:21 +08:00
|
|
|
if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
|
|
|
|
return QTN->getTemplateDecl();
|
|
|
|
|
2011-06-30 16:33:18 +08:00
|
|
|
if (SubstTemplateTemplateParmStorage *sub = getAsSubstTemplateTemplateParm())
|
|
|
|
return sub->getReplacement().getAsTemplateDecl();
|
|
|
|
|
2014-05-12 13:36:57 +08:00
|
|
|
return nullptr;
|
2009-03-31 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
2015-12-30 11:24:14 +08:00
|
|
|
OverloadedTemplateStorage *TemplateName::getAsOverloadedTemplate() const {
|
|
|
|
if (UncommonTemplateNameStorage *Uncommon =
|
|
|
|
Storage.dyn_cast<UncommonTemplateNameStorage *>())
|
|
|
|
return Uncommon->getAsOverloadedStorage();
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-09 11:31:27 +08:00
|
|
|
AssumedTemplateStorage *TemplateName::getAsAssumedTemplateName() const {
|
|
|
|
if (UncommonTemplateNameStorage *Uncommon =
|
|
|
|
Storage.dyn_cast<UncommonTemplateNameStorage *>())
|
|
|
|
return Uncommon->getAsAssumedTemplateName();
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-12-30 11:24:14 +08:00
|
|
|
SubstTemplateTemplateParmStorage *
|
|
|
|
TemplateName::getAsSubstTemplateTemplateParm() const {
|
|
|
|
if (UncommonTemplateNameStorage *uncommon =
|
|
|
|
Storage.dyn_cast<UncommonTemplateNameStorage *>())
|
|
|
|
return uncommon->getAsSubstTemplateTemplateParm();
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubstTemplateTemplateParmPackStorage *
|
|
|
|
TemplateName::getAsSubstTemplateTemplateParmPack() const {
|
|
|
|
if (UncommonTemplateNameStorage *Uncommon =
|
|
|
|
Storage.dyn_cast<UncommonTemplateNameStorage *>())
|
|
|
|
return Uncommon->getAsSubstTemplateTemplateParmPack();
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
QualifiedTemplateName *TemplateName::getAsQualifiedTemplateName() const {
|
|
|
|
return Storage.dyn_cast<QualifiedTemplateName *>();
|
|
|
|
}
|
|
|
|
|
|
|
|
DependentTemplateName *TemplateName::getAsDependentTemplateName() const {
|
|
|
|
return Storage.dyn_cast<DependentTemplateName *>();
|
|
|
|
}
|
|
|
|
|
2017-08-30 06:14:43 +08:00
|
|
|
TemplateName TemplateName::getNameToSubstitute() const {
|
|
|
|
TemplateDecl *Decl = getAsTemplateDecl();
|
|
|
|
|
|
|
|
// Substituting a dependent template name: preserve it as written.
|
|
|
|
if (!Decl)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
// If we have a template declaration, use the most recent non-friend
|
|
|
|
// declaration of that template.
|
|
|
|
Decl = cast<TemplateDecl>(Decl->getMostRecentDecl());
|
|
|
|
while (Decl->getFriendObjectKind()) {
|
|
|
|
Decl = cast<TemplateDecl>(Decl->getPreviousDecl());
|
|
|
|
assert(Decl && "all declarations of template are friends");
|
|
|
|
}
|
|
|
|
return TemplateName(Decl);
|
|
|
|
}
|
|
|
|
|
2009-03-31 06:58:21 +08:00
|
|
|
bool TemplateName::isDependent() const {
|
|
|
|
if (TemplateDecl *Template = getAsTemplateDecl()) {
|
2010-09-09 03:31:22 +08:00
|
|
|
if (isa<TemplateTemplateParmDecl>(Template))
|
|
|
|
return true;
|
|
|
|
// FIXME: Hack, getDeclContext() can be null if Template is still
|
|
|
|
// initializing due to PCH reading, so we check it before using it.
|
|
|
|
// Should probably modify TemplateSpecializationType to allow constructing
|
|
|
|
// it without the isDependent() checking.
|
|
|
|
return Template->getDeclContext() &&
|
|
|
|
Template->getDeclContext()->isDependentContext();
|
2009-03-31 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
2009-12-02 16:04:21 +08:00
|
|
|
assert(!getAsOverloadedTemplate() &&
|
|
|
|
"overloaded templates shouldn't survive to here");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-31 06:58:21 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-01 09:22:09 +08:00
|
|
|
bool TemplateName::isInstantiationDependent() const {
|
|
|
|
if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
|
|
|
|
if (QTN->getQualifier()->isInstantiationDependent())
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-07-01 09:22:09 +08:00
|
|
|
return isDependent();
|
|
|
|
}
|
|
|
|
|
Variadic templates: extend Type, NestedNameSpecifier, TemplateName,
and TemplateArgument with an operation that determines whether there
are any unexpanded parameter packs within that construct. Use this
information to diagnose the appearance of the names of parameter packs
that have not been expanded (C++ [temp.variadic]p5). Since this
property is checked often (every declaration, ever expression
statement, etc.), we extend Type and Expr with a bit storing the
result of this computation, rather than walking the AST each time to
determine whether any unexpanded parameter packs occur.
This commit is deficient in several ways, which will be remedied with
future commits:
- Expr has a bit to store the presence of an unexpanded parameter
pack, but it is never set.
- The error messages don't point out where the unexpanded parameter
packs were named in the type/expression, but they should.
- We don't check for unexpanded parameter packs in all of the places
where we should.
- Testing is sparse, pending the resolution of the above three
issues.
llvm-svn: 121724
2010-12-14 06:49:22 +08:00
|
|
|
bool TemplateName::containsUnexpandedParameterPack() const {
|
2018-02-28 11:02:23 +08:00
|
|
|
if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
|
|
|
|
if (QTN->getQualifier()->containsUnexpandedParameterPack())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Variadic templates: extend Type, NestedNameSpecifier, TemplateName,
and TemplateArgument with an operation that determines whether there
are any unexpanded parameter packs within that construct. Use this
information to diagnose the appearance of the names of parameter packs
that have not been expanded (C++ [temp.variadic]p5). Since this
property is checked often (every declaration, ever expression
statement, etc.), we extend Type and Expr with a bit storing the
result of this computation, rather than walking the AST each time to
determine whether any unexpanded parameter packs occur.
This commit is deficient in several ways, which will be remedied with
future commits:
- Expr has a bit to store the presence of an unexpanded parameter
pack, but it is never set.
- The error messages don't point out where the unexpanded parameter
packs were named in the type/expression, but they should.
- We don't check for unexpanded parameter packs in all of the places
where we should.
- Testing is sparse, pending the resolution of the above three
issues.
llvm-svn: 121724
2010-12-14 06:49:22 +08:00
|
|
|
if (TemplateDecl *Template = getAsTemplateDecl()) {
|
2018-07-31 03:24:48 +08:00
|
|
|
if (TemplateTemplateParmDecl *TTP
|
Variadic templates: extend Type, NestedNameSpecifier, TemplateName,
and TemplateArgument with an operation that determines whether there
are any unexpanded parameter packs within that construct. Use this
information to diagnose the appearance of the names of parameter packs
that have not been expanded (C++ [temp.variadic]p5). Since this
property is checked often (every declaration, ever expression
statement, etc.), we extend Type and Expr with a bit storing the
result of this computation, rather than walking the AST each time to
determine whether any unexpanded parameter packs occur.
This commit is deficient in several ways, which will be remedied with
future commits:
- Expr has a bit to store the presence of an unexpanded parameter
pack, but it is never set.
- The error messages don't point out where the unexpanded parameter
packs were named in the type/expression, but they should.
- We don't check for unexpanded parameter packs in all of the places
where we should.
- Testing is sparse, pending the resolution of the above three
issues.
llvm-svn: 121724
2010-12-14 06:49:22 +08:00
|
|
|
= dyn_cast<TemplateTemplateParmDecl>(Template))
|
|
|
|
return TTP->isParameterPack();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DependentTemplateName *DTN = getAsDependentTemplateName())
|
2018-07-31 03:24:48 +08:00
|
|
|
return DTN->getQualifier() &&
|
Variadic templates: extend Type, NestedNameSpecifier, TemplateName,
and TemplateArgument with an operation that determines whether there
are any unexpanded parameter packs within that construct. Use this
information to diagnose the appearance of the names of parameter packs
that have not been expanded (C++ [temp.variadic]p5). Since this
property is checked often (every declaration, ever expression
statement, etc.), we extend Type and Expr with a bit storing the
result of this computation, rather than walking the AST each time to
determine whether any unexpanded parameter packs occur.
This commit is deficient in several ways, which will be remedied with
future commits:
- Expr has a bit to store the presence of an unexpanded parameter
pack, but it is never set.
- The error messages don't point out where the unexpanded parameter
packs were named in the type/expression, but they should.
- We don't check for unexpanded parameter packs in all of the places
where we should.
- Testing is sparse, pending the resolution of the above three
issues.
llvm-svn: 121724
2010-12-14 06:49:22 +08:00
|
|
|
DTN->getQualifier()->containsUnexpandedParameterPack();
|
|
|
|
|
2014-05-12 13:36:57 +08:00
|
|
|
return getAsSubstTemplateTemplateParmPack() != nullptr;
|
Variadic templates: extend Type, NestedNameSpecifier, TemplateName,
and TemplateArgument with an operation that determines whether there
are any unexpanded parameter packs within that construct. Use this
information to diagnose the appearance of the names of parameter packs
that have not been expanded (C++ [temp.variadic]p5). Since this
property is checked often (every declaration, ever expression
statement, etc.), we extend Type and Expr with a bit storing the
result of this computation, rather than walking the AST each time to
determine whether any unexpanded parameter packs occur.
This commit is deficient in several ways, which will be remedied with
future commits:
- Expr has a bit to store the presence of an unexpanded parameter
pack, but it is never set.
- The error messages don't point out where the unexpanded parameter
packs were named in the type/expression, but they should.
- We don't check for unexpanded parameter packs in all of the places
where we should.
- Testing is sparse, pending the resolution of the above three
issues.
llvm-svn: 121724
2010-12-14 06:49:22 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void
|
2011-07-23 18:55:15 +08:00
|
|
|
TemplateName::print(raw_ostream &OS, const PrintingPolicy &Policy,
|
2009-05-30 04:38:28 +08:00
|
|
|
bool SuppressNNS) const {
|
2009-03-31 06:58:21 +08:00
|
|
|
if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
|
2011-10-15 02:45:37 +08:00
|
|
|
OS << *Template;
|
2009-03-31 06:58:21 +08:00
|
|
|
else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
|
2009-04-01 08:28:59 +08:00
|
|
|
if (!SuppressNNS)
|
2009-05-30 04:38:28 +08:00
|
|
|
QTN->getQualifier()->print(OS, Policy);
|
2009-03-31 06:58:21 +08:00
|
|
|
if (QTN->hasTemplateKeyword())
|
|
|
|
OS << "template ";
|
2011-10-15 02:45:37 +08:00
|
|
|
OS << *QTN->getDecl();
|
2009-03-31 06:58:21 +08:00
|
|
|
} else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
|
2009-09-09 08:23:06 +08:00
|
|
|
if (!SuppressNNS && DTN->getQualifier())
|
2009-05-30 04:38:28 +08:00
|
|
|
DTN->getQualifier()->print(OS, Policy);
|
2009-03-31 06:58:21 +08:00
|
|
|
OS << "template ";
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-11-04 08:56:37 +08:00
|
|
|
if (DTN->isIdentifier())
|
|
|
|
OS << DTN->getIdentifier()->getName();
|
|
|
|
else
|
|
|
|
OS << "operator " << getOperatorSpelling(DTN->getOperator());
|
2011-06-30 16:33:18 +08:00
|
|
|
} else if (SubstTemplateTemplateParmStorage *subst
|
|
|
|
= getAsSubstTemplateTemplateParm()) {
|
|
|
|
subst->getReplacement().print(OS, Policy, SuppressNNS);
|
2011-01-15 14:45:20 +08:00
|
|
|
} else if (SubstTemplateTemplateParmPackStorage *SubstPack
|
|
|
|
= getAsSubstTemplateTemplateParmPack())
|
2012-02-07 19:57:57 +08:00
|
|
|
OS << *SubstPack->getParameterPack();
|
2019-05-09 11:31:27 +08:00
|
|
|
else if (AssumedTemplateStorage *Assumed = getAsAssumedTemplateName()) {
|
|
|
|
Assumed->getDeclName().print(OS, Policy);
|
|
|
|
} else {
|
2011-03-05 05:37:14 +08:00
|
|
|
OverloadedTemplateStorage *OTS = getAsOverloadedTemplate();
|
|
|
|
(*OTS->begin())->printName(OS);
|
|
|
|
}
|
2009-03-31 06:58:21 +08:00
|
|
|
}
|
2009-04-01 02:38:02 +08:00
|
|
|
|
2010-04-08 08:03:06 +08:00
|
|
|
const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
|
|
|
|
TemplateName N) {
|
|
|
|
std::string NameStr;
|
2017-11-22 07:26:08 +08:00
|
|
|
llvm::raw_string_ostream OS(NameStr);
|
2010-04-08 08:03:06 +08:00
|
|
|
LangOptions LO;
|
|
|
|
LO.CPlusPlus = true;
|
|
|
|
LO.Bool = true;
|
2013-03-05 14:21:38 +08:00
|
|
|
OS << '\'';
|
2010-04-08 08:03:06 +08:00
|
|
|
N.print(OS, PrintingPolicy(LO));
|
2013-03-05 14:21:38 +08:00
|
|
|
OS << '\'';
|
2010-04-08 08:03:06 +08:00
|
|
|
OS.flush();
|
|
|
|
return DB << NameStr;
|
|
|
|
}
|
|
|
|
|
2019-05-03 08:44:50 +08:00
|
|
|
const PartialDiagnostic&clang::operator<<(const PartialDiagnostic &PD,
|
|
|
|
TemplateName N) {
|
|
|
|
std::string NameStr;
|
|
|
|
llvm::raw_string_ostream OS(NameStr);
|
|
|
|
LangOptions LO;
|
|
|
|
LO.CPlusPlus = true;
|
|
|
|
LO.Bool = true;
|
|
|
|
OS << '\'';
|
|
|
|
N.print(OS, PrintingPolicy(LO));
|
|
|
|
OS << '\'';
|
|
|
|
OS.flush();
|
|
|
|
return PD << NameStr;
|
|
|
|
}
|
|
|
|
|
2012-12-20 10:09:13 +08:00
|
|
|
void TemplateName::dump(raw_ostream &OS) const {
|
2009-06-30 09:26:17 +08:00
|
|
|
LangOptions LO; // FIXME!
|
|
|
|
LO.CPlusPlus = true;
|
|
|
|
LO.Bool = true;
|
2012-12-20 10:09:13 +08:00
|
|
|
print(OS, PrintingPolicy(LO));
|
|
|
|
}
|
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void TemplateName::dump() const {
|
2012-12-20 10:09:13 +08:00
|
|
|
dump(llvm::errs());
|
2009-04-01 02:38:02 +08:00
|
|
|
}
|