2017-11-29 22:39:22 +00:00
|
|
|
//===- DeclTemplate.cpp - Template Declaration AST Node Implementation ----===//
|
2009-02-04 19:02:06 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00: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-02-04 19:02:06 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the C++ related Decl classes for templates.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/ASTMutationListener.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
2017-11-29 22:39:22 +00:00
|
|
|
#include "clang/AST/DeclarationName.h"
|
2009-02-09 18:46:07 +00:00
|
|
|
#include "clang/AST/Expr.h"
|
2024-12-11 09:40:47 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2018-03-29 20:51:59 +00:00
|
|
|
#include "clang/AST/ExternalASTSource.h"
|
2024-12-11 09:40:47 +08:00
|
|
|
#include "clang/AST/ODRHash.h"
|
2017-11-29 22:39:22 +00:00
|
|
|
#include "clang/AST/TemplateBase.h"
|
|
|
|
#include "clang/AST/TemplateName.h"
|
|
|
|
#include "clang/AST/Type.h"
|
2009-10-29 08:12:44 +00:00
|
|
|
#include "clang/AST/TypeLoc.h"
|
2015-11-04 03:40:30 +00:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2017-11-29 22:39:22 +00:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/ADT/PointerUnion.h"
|
2022-02-17 17:18:43 +01:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-11-29 22:39:22 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include <cassert>
|
2010-11-07 23:05:16 +00:00
|
|
|
#include <memory>
|
2023-01-14 11:07:21 -08:00
|
|
|
#include <optional>
|
2017-11-29 22:39:22 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2009-02-04 19:02:06 +00:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateParameterList Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2024-07-23 19:19:16 +02:00
|
|
|
template <class TemplateParam>
|
|
|
|
static bool
|
|
|
|
DefaultTemplateArgumentContainsUnexpandedPack(const TemplateParam &P) {
|
|
|
|
return P.hasDefaultArgument() &&
|
|
|
|
P.getDefaultArgument().getArgument().containsUnexpandedParameterPack();
|
|
|
|
}
|
2020-01-15 02:48:42 +02:00
|
|
|
|
2024-10-29 11:36:55 -06:00
|
|
|
TemplateParameterList::TemplateParameterList(const ASTContext &C,
|
2020-01-15 02:48:42 +02:00
|
|
|
SourceLocation TemplateLoc,
|
2009-02-06 22:42:48 +00:00
|
|
|
SourceLocation LAngleLoc,
|
2015-12-27 07:16:27 +00:00
|
|
|
ArrayRef<NamedDecl *> Params,
|
2016-07-30 22:33:34 +00:00
|
|
|
SourceLocation RAngleLoc,
|
|
|
|
Expr *RequiresClause)
|
2017-11-29 22:39:22 +00:00
|
|
|
: TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
|
|
|
|
NumParams(Params.size()), ContainsUnexpandedParameterPack(false),
|
2020-01-15 02:48:42 +02:00
|
|
|
HasRequiresClause(RequiresClause != nullptr),
|
|
|
|
HasConstrainedParameters(false) {
|
2012-09-07 02:06:42 +00:00
|
|
|
for (unsigned Idx = 0; Idx < NumParams; ++Idx) {
|
|
|
|
NamedDecl *P = Params[Idx];
|
|
|
|
begin()[Idx] = P;
|
|
|
|
|
2020-01-15 02:48:42 +02:00
|
|
|
bool IsPack = P->isTemplateParameterPack();
|
|
|
|
if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
|
2024-07-23 19:19:16 +02:00
|
|
|
if (!IsPack && (NTTP->getType()->containsUnexpandedParameterPack() ||
|
|
|
|
DefaultTemplateArgumentContainsUnexpandedPack(*NTTP)))
|
2020-01-15 02:48:42 +02:00
|
|
|
ContainsUnexpandedParameterPack = true;
|
|
|
|
if (NTTP->hasPlaceholderTypeConstraint())
|
|
|
|
HasConstrainedParameters = true;
|
|
|
|
} else if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) {
|
|
|
|
if (!IsPack &&
|
2024-07-23 19:19:16 +02:00
|
|
|
(TTP->getTemplateParameters()->containsUnexpandedParameterPack() ||
|
|
|
|
DefaultTemplateArgumentContainsUnexpandedPack(*TTP))) {
|
2020-01-15 02:48:42 +02:00
|
|
|
ContainsUnexpandedParameterPack = true;
|
2024-07-23 19:19:16 +02:00
|
|
|
}
|
2021-05-12 15:25:52 -07:00
|
|
|
} else if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) {
|
2024-07-23 19:19:16 +02:00
|
|
|
if (!IsPack && DefaultTemplateArgumentContainsUnexpandedPack(*TTP)) {
|
|
|
|
ContainsUnexpandedParameterPack = true;
|
|
|
|
} else if (const TypeConstraint *TC = TTP->getTypeConstraint();
|
|
|
|
TC && TC->getImmediatelyDeclaredConstraint()
|
|
|
|
->containsUnexpandedParameterPack()) {
|
|
|
|
ContainsUnexpandedParameterPack = true;
|
2021-05-12 15:25:52 -07:00
|
|
|
}
|
2021-06-02 12:57:18 -07:00
|
|
|
if (TTP->hasTypeConstraint())
|
|
|
|
HasConstrainedParameters = true;
|
2021-05-12 15:25:52 -07:00
|
|
|
} else {
|
2021-09-20 20:06:03 -04:00
|
|
|
llvm_unreachable("unexpected template parameter type");
|
2012-09-07 02:06:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-15 02:48:42 +02:00
|
|
|
|
|
|
|
if (HasRequiresClause) {
|
2019-10-15 18:44:06 +00:00
|
|
|
if (RequiresClause->containsUnexpandedParameterPack())
|
|
|
|
ContainsUnexpandedParameterPack = true;
|
2020-01-15 02:48:42 +02:00
|
|
|
*getTrailingObjects<Expr *>() = RequiresClause;
|
2016-07-30 22:33:34 +00:00
|
|
|
}
|
2009-02-04 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 18:29:26 -07:00
|
|
|
bool TemplateParameterList::containsUnexpandedParameterPack() const {
|
|
|
|
if (ContainsUnexpandedParameterPack)
|
|
|
|
return true;
|
|
|
|
if (!HasConstrainedParameters)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// An implicit constrained parameter might have had a use of an unexpanded
|
|
|
|
// pack added to it after the template parameter list was created. All
|
|
|
|
// implicit parameters are at the end of the parameter list.
|
|
|
|
for (const NamedDecl *Param : llvm::reverse(asArray())) {
|
|
|
|
if (!Param->isImplicit())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
|
|
|
|
const auto *TC = TTP->getTypeConstraint();
|
|
|
|
if (TC && TC->getImmediatelyDeclaredConstraint()
|
|
|
|
->containsUnexpandedParameterPack())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-30 22:33:34 +00:00
|
|
|
TemplateParameterList *
|
|
|
|
TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
|
|
|
|
SourceLocation LAngleLoc,
|
|
|
|
ArrayRef<NamedDecl *> Params,
|
|
|
|
SourceLocation RAngleLoc, Expr *RequiresClause) {
|
|
|
|
void *Mem = C.Allocate(totalSizeToAlloc<NamedDecl *, Expr *>(
|
|
|
|
Params.size(), RequiresClause ? 1u : 0u),
|
2016-10-20 14:27:22 +00:00
|
|
|
alignof(TemplateParameterList));
|
2020-01-15 02:48:42 +02:00
|
|
|
return new (Mem) TemplateParameterList(C, TemplateLoc, LAngleLoc, Params,
|
2016-07-30 22:33:34 +00:00
|
|
|
RAngleLoc, RequiresClause);
|
2009-02-04 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 11:42:14 -07:00
|
|
|
void TemplateParameterList::Profile(llvm::FoldingSetNodeID &ID,
|
|
|
|
const ASTContext &C) const {
|
|
|
|
const Expr *RC = getRequiresClause();
|
|
|
|
ID.AddBoolean(RC != nullptr);
|
|
|
|
if (RC)
|
|
|
|
RC->Profile(ID, C, /*Canonical=*/true);
|
|
|
|
ID.AddInteger(size());
|
|
|
|
for (NamedDecl *D : *this) {
|
|
|
|
if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
|
|
|
|
ID.AddInteger(0);
|
|
|
|
ID.AddBoolean(NTTP->isParameterPack());
|
|
|
|
NTTP->getType().getCanonicalType().Profile(ID);
|
|
|
|
ID.AddBoolean(NTTP->hasPlaceholderTypeConstraint());
|
|
|
|
if (const Expr *E = NTTP->getPlaceholderTypeConstraint())
|
|
|
|
E->Profile(ID, C, /*Canonical=*/true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(D)) {
|
|
|
|
ID.AddInteger(1);
|
|
|
|
ID.AddBoolean(TTP->isParameterPack());
|
|
|
|
ID.AddBoolean(TTP->hasTypeConstraint());
|
|
|
|
if (const TypeConstraint *TC = TTP->getTypeConstraint())
|
|
|
|
TC->getImmediatelyDeclaredConstraint()->Profile(ID, C,
|
|
|
|
/*Canonical=*/true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto *TTP = cast<TemplateTemplateParmDecl>(D);
|
|
|
|
ID.AddInteger(2);
|
|
|
|
ID.AddBoolean(TTP->isParameterPack());
|
|
|
|
TTP->getTemplateParameters()->Profile(ID, C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-11 18:16:40 +00:00
|
|
|
unsigned TemplateParameterList::getMinRequiredArguments() const {
|
2011-01-19 20:10:05 +00:00
|
|
|
unsigned NumRequiredArgs = 0;
|
2016-07-06 04:19:16 +00:00
|
|
|
for (const NamedDecl *P : asArray()) {
|
|
|
|
if (P->isTemplateParameterPack()) {
|
2023-01-14 12:31:01 -08:00
|
|
|
if (std::optional<unsigned> Expansions = getExpandedPackSize(P)) {
|
2020-12-13 00:50:01 -08:00
|
|
|
NumRequiredArgs += *Expansions;
|
|
|
|
continue;
|
2020-01-15 02:48:42 +02:00
|
|
|
}
|
2009-02-11 18:16:40 +00:00
|
|
|
break;
|
2011-01-19 20:10:05 +00:00
|
|
|
}
|
2016-07-06 04:19:16 +00:00
|
|
|
|
|
|
|
if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) {
|
2011-01-19 20:10:05 +00:00
|
|
|
if (TTP->hasDefaultArgument())
|
|
|
|
break;
|
2016-07-06 04:19:16 +00:00
|
|
|
} else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
|
2011-01-19 20:10:05 +00:00
|
|
|
if (NTTP->hasDefaultArgument())
|
|
|
|
break;
|
2016-07-06 04:19:16 +00:00
|
|
|
} else if (cast<TemplateTemplateParmDecl>(P)->hasDefaultArgument())
|
2011-01-19 20:10:05 +00:00
|
|
|
break;
|
2016-07-06 04:19:16 +00:00
|
|
|
|
2011-01-19 20:10:05 +00:00
|
|
|
++NumRequiredArgs;
|
2009-02-11 18:16:40 +00:00
|
|
|
}
|
2016-07-06 04:19:16 +00:00
|
|
|
|
2009-02-11 18:16:40 +00:00
|
|
|
return NumRequiredArgs;
|
|
|
|
}
|
|
|
|
|
2009-10-29 00:04:11 +00:00
|
|
|
unsigned TemplateParameterList::getDepth() const {
|
|
|
|
if (size() == 0)
|
|
|
|
return 0;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2009-10-29 00:04:11 +00:00
|
|
|
const NamedDecl *FirstParm = getParam(0);
|
2018-03-29 20:51:59 +00:00
|
|
|
if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(FirstParm))
|
2009-10-29 00:04:11 +00:00
|
|
|
return TTP->getDepth();
|
2018-03-29 20:51:59 +00:00
|
|
|
else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
|
2009-10-29 00:04:11 +00:00
|
|
|
return NTTP->getDepth();
|
|
|
|
else
|
|
|
|
return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
|
|
|
|
}
|
|
|
|
|
2021-09-24 22:18:54 +02:00
|
|
|
static bool AdoptTemplateParameterList(TemplateParameterList *Params,
|
2011-03-04 18:32:38 +00:00
|
|
|
DeclContext *Owner) {
|
2021-09-24 22:18:54 +02:00
|
|
|
bool Invalid = false;
|
2016-07-06 04:19:16 +00:00
|
|
|
for (NamedDecl *P : *Params) {
|
|
|
|
P->setDeclContext(Owner);
|
|
|
|
|
2018-03-29 20:51:59 +00:00
|
|
|
if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
|
2021-09-24 22:18:54 +02:00
|
|
|
if (AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner))
|
|
|
|
Invalid = true;
|
|
|
|
|
|
|
|
if (P->isInvalidDecl())
|
|
|
|
Invalid = true;
|
2011-03-04 18:32:38 +00:00
|
|
|
}
|
2021-09-24 22:18:54 +02:00
|
|
|
return Invalid;
|
2011-03-04 18:32:38 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 18:44:06 +00:00
|
|
|
void TemplateParameterList::
|
|
|
|
getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
|
2020-01-15 02:48:42 +02:00
|
|
|
if (HasConstrainedParameters)
|
2020-01-22 02:03:05 +02:00
|
|
|
for (const NamedDecl *Param : *this) {
|
|
|
|
if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
|
2020-01-15 02:48:42 +02:00
|
|
|
if (const auto *TC = TTP->getTypeConstraint())
|
|
|
|
AC.push_back(TC->getImmediatelyDeclaredConstraint());
|
2020-01-22 02:03:05 +02:00
|
|
|
} else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
|
|
|
|
if (const Expr *E = NTTP->getPlaceholderTypeConstraint())
|
|
|
|
AC.push_back(E);
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 18:44:06 +00:00
|
|
|
if (HasRequiresClause)
|
|
|
|
AC.push_back(getRequiresClause());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TemplateParameterList::hasAssociatedConstraints() const {
|
2020-01-15 02:48:42 +02:00
|
|
|
return HasRequiresClause || HasConstrainedParameters;
|
2019-10-15 18:44:06 +00:00
|
|
|
}
|
|
|
|
|
2024-10-29 11:36:55 -06:00
|
|
|
ArrayRef<TemplateArgument>
|
|
|
|
TemplateParameterList::getInjectedTemplateArgs(const ASTContext &Context) {
|
|
|
|
if (!InjectedArgs) {
|
|
|
|
InjectedArgs = new (Context) TemplateArgument[size()];
|
|
|
|
llvm::transform(*this, InjectedArgs, [&](NamedDecl *ND) {
|
|
|
|
return Context.getInjectedTemplateArg(ND);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {InjectedArgs, NumParams};
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:28:41 +00:00
|
|
|
bool TemplateParameterList::shouldIncludeTypeForArgument(
|
2021-09-19 20:58:47 -07:00
|
|
|
const PrintingPolicy &Policy, const TemplateParameterList *TPL,
|
|
|
|
unsigned Idx) {
|
2021-11-11 21:47:30 -08:00
|
|
|
if (!TPL || Idx >= TPL->size() || Policy.AlwaysIncludeTypeForTemplateArgument)
|
2021-05-12 17:28:41 +00:00
|
|
|
return true;
|
|
|
|
const NamedDecl *TemplParam = TPL->getParam(Idx);
|
|
|
|
if (const auto *ParamValueDecl =
|
|
|
|
dyn_cast<NonTypeTemplateParmDecl>(TemplParam))
|
|
|
|
if (ParamValueDecl->getType()->getContainedDeducedType())
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-10 20:30:23 +00:00
|
|
|
namespace clang {
|
2017-11-29 22:39:22 +00:00
|
|
|
|
2015-06-10 20:30:23 +00:00
|
|
|
void *allocateDefaultArgStorageChain(const ASTContext &C) {
|
|
|
|
return new (C) char[sizeof(void*) * 2];
|
|
|
|
}
|
2017-11-29 22:39:22 +00:00
|
|
|
|
|
|
|
} // namespace clang
|
2015-06-10 20:30:23 +00:00
|
|
|
|
2019-10-15 18:44:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
TemplateDecl::TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
|
|
|
|
DeclarationName Name, TemplateParameterList *Params,
|
|
|
|
NamedDecl *Decl)
|
|
|
|
: NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl), TemplateParams(Params) {}
|
|
|
|
|
|
|
|
void TemplateDecl::anchor() {}
|
|
|
|
|
|
|
|
void TemplateDecl::
|
|
|
|
getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
|
|
|
|
TemplateParams->getAssociatedConstraints(AC);
|
2020-01-09 15:07:51 +02:00
|
|
|
if (auto *FD = dyn_cast_or_null<FunctionDecl>(getTemplatedDecl()))
|
|
|
|
if (const Expr *TRC = FD->getTrailingRequiresClause())
|
|
|
|
AC.push_back(TRC);
|
2019-10-15 18:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TemplateDecl::hasAssociatedConstraints() const {
|
2020-01-09 15:07:51 +02:00
|
|
|
if (TemplateParams->hasAssociatedConstraints())
|
|
|
|
return true;
|
|
|
|
if (auto *FD = dyn_cast_or_null<FunctionDecl>(getTemplatedDecl()))
|
|
|
|
return FD->getTrailingRequiresClause();
|
|
|
|
return false;
|
2019-10-15 18:44:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 18:36:59 +02:00
|
|
|
bool TemplateDecl::isTypeAlias() const {
|
|
|
|
switch (getKind()) {
|
|
|
|
case TemplateDecl::TypeAliasTemplate:
|
|
|
|
case TemplateDecl::BuiltinTemplate:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-07-29 16:11:51 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RedeclarableTemplateDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-12-29 02:02:30 +00:00
|
|
|
void RedeclarableTemplateDecl::anchor() {}
|
|
|
|
|
2013-01-23 16:52:57 +00:00
|
|
|
RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const {
|
2024-11-06 07:25:29 -07:00
|
|
|
if (Common)
|
|
|
|
return Common;
|
2010-07-29 16:11:51 +00:00
|
|
|
|
2013-10-19 02:28:17 +00:00
|
|
|
// Walk the previous-declaration chain until we either find a declaration
|
|
|
|
// with a common pointer or we run out of previous declarations.
|
|
|
|
SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls;
|
|
|
|
for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
|
|
|
|
Prev = Prev->getPreviousDecl()) {
|
2024-11-06 07:25:29 -07:00
|
|
|
if (Prev->Common) {
|
|
|
|
Common = Prev->Common;
|
2013-10-19 02:28:17 +00:00
|
|
|
break;
|
2012-01-14 15:30:55 +00:00
|
|
|
}
|
2013-10-19 02:28:17 +00:00
|
|
|
|
|
|
|
PrevDecls.push_back(Prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we never found a common pointer, allocate one now.
|
2024-11-06 07:25:29 -07:00
|
|
|
if (!Common) {
|
2013-10-19 02:28:17 +00:00
|
|
|
// FIXME: If any of the declarations is from an AST file, we probably
|
|
|
|
// need an update record to add the common data.
|
|
|
|
|
2024-11-06 07:25:29 -07:00
|
|
|
Common = newCommon(getASTContext());
|
2010-07-29 16:12:01 +00:00
|
|
|
}
|
|
|
|
|
2013-10-19 02:28:17 +00:00
|
|
|
// Update any previous declarations we saw with the common pointer.
|
2016-07-06 04:19:16 +00:00
|
|
|
for (const RedeclarableTemplateDecl *Prev : PrevDecls)
|
2024-11-06 07:25:29 -07:00
|
|
|
Prev->Common = Common;
|
2013-10-19 02:28:17 +00:00
|
|
|
|
2024-11-06 07:25:29 -07:00
|
|
|
return Common;
|
2010-07-29 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
2024-12-11 09:40:47 +08:00
|
|
|
void RedeclarableTemplateDecl::loadLazySpecializationsImpl(
|
|
|
|
bool OnlyPartial /*=false*/) const {
|
|
|
|
auto *ExternalSource = getASTContext().getExternalSource();
|
|
|
|
if (!ExternalSource)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ExternalSource->LoadExternalSpecializations(this->getCanonicalDecl(),
|
|
|
|
OnlyPartial);
|
|
|
|
return;
|
2024-12-06 10:31:37 +08:00
|
|
|
}
|
|
|
|
|
2024-12-11 09:40:47 +08:00
|
|
|
bool RedeclarableTemplateDecl::loadLazySpecializationsImpl(
|
|
|
|
ArrayRef<TemplateArgument> Args, TemplateParameterList *TPL) const {
|
|
|
|
auto *ExternalSource = getASTContext().getExternalSource();
|
|
|
|
if (!ExternalSource)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If TPL is not null, it implies that we're loading specializations for
|
|
|
|
// partial templates. We need to load all specializations in such cases.
|
|
|
|
if (TPL)
|
|
|
|
return ExternalSource->LoadExternalSpecializations(this->getCanonicalDecl(),
|
|
|
|
/*OnlyPartial=*/false);
|
|
|
|
|
|
|
|
return ExternalSource->LoadExternalSpecializations(this->getCanonicalDecl(),
|
|
|
|
Args);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class EntryType, typename... ProfileArguments>
|
2015-02-24 01:23:23 +00:00
|
|
|
typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
|
2024-12-11 09:40:47 +08:00
|
|
|
RedeclarableTemplateDecl::findSpecializationLocally(
|
2019-12-23 08:37:35 +02:00
|
|
|
llvm::FoldingSetVector<EntryType> &Specs, void *&InsertPos,
|
2024-12-11 09:40:47 +08:00
|
|
|
ProfileArguments &&...ProfileArgs) {
|
|
|
|
using SETraits = RedeclarableTemplateDecl::SpecEntryTraits<EntryType>;
|
2017-11-29 22:39:22 +00:00
|
|
|
|
2010-07-30 17:09:04 +00:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2019-12-23 08:37:35 +02:00
|
|
|
EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
|
|
|
|
getASTContext());
|
2010-07-30 17:09:04 +00:00
|
|
|
EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
|
2015-02-24 01:23:23 +00:00
|
|
|
return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr;
|
|
|
|
}
|
|
|
|
|
2024-12-11 09:40:47 +08:00
|
|
|
template <class EntryType, typename... ProfileArguments>
|
|
|
|
typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
|
|
|
|
RedeclarableTemplateDecl::findSpecializationImpl(
|
|
|
|
llvm::FoldingSetVector<EntryType> &Specs, void *&InsertPos,
|
|
|
|
ProfileArguments &&...ProfileArgs) {
|
|
|
|
|
|
|
|
if (auto *Found = findSpecializationLocally(
|
|
|
|
Specs, InsertPos, std::forward<ProfileArguments>(ProfileArgs)...))
|
|
|
|
return Found;
|
|
|
|
|
|
|
|
if (!loadLazySpecializationsImpl(
|
|
|
|
std::forward<ProfileArguments>(ProfileArgs)...))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return findSpecializationLocally(
|
|
|
|
Specs, InsertPos, std::forward<ProfileArguments>(ProfileArgs)...);
|
|
|
|
}
|
|
|
|
|
2015-02-24 01:23:23 +00:00
|
|
|
template<class Derived, class EntryType>
|
|
|
|
void RedeclarableTemplateDecl::addSpecializationImpl(
|
|
|
|
llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry,
|
|
|
|
void *InsertPos) {
|
2017-11-29 22:39:22 +00:00
|
|
|
using SETraits = SpecEntryTraits<EntryType>;
|
|
|
|
|
2015-02-24 01:23:23 +00:00
|
|
|
if (InsertPos) {
|
|
|
|
#ifndef NDEBUG
|
2024-12-11 09:40:47 +08:00
|
|
|
auto Args = SETraits::getTemplateArgs(Entry);
|
|
|
|
// Due to hash collisions, it can happen that we load another template
|
|
|
|
// specialization with the same hash. This is fine, as long as the next
|
|
|
|
// call to findSpecializationImpl does not find a matching Decl for the
|
|
|
|
// template arguments.
|
|
|
|
loadLazySpecializationsImpl(Args);
|
2015-02-24 01:23:23 +00:00
|
|
|
void *CorrectInsertPos;
|
2024-12-11 09:40:47 +08:00
|
|
|
assert(!findSpecializationImpl(Specializations, CorrectInsertPos, Args) &&
|
2015-02-24 01:23:23 +00:00
|
|
|
InsertPos == CorrectInsertPos &&
|
|
|
|
"given incorrect InsertPos for specialization");
|
|
|
|
#endif
|
|
|
|
Specializations.InsertNode(Entry, InsertPos);
|
|
|
|
} else {
|
|
|
|
EntryType *Existing = Specializations.GetOrInsertNode(Entry);
|
|
|
|
(void)Existing;
|
|
|
|
assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
|
|
|
|
"non-canonical specialization?");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ASTMutationListener *L = getASTMutationListener())
|
|
|
|
L->AddedCXXTemplateSpecialization(cast<Derived>(this),
|
|
|
|
SETraits::getDecl(Entry));
|
2010-07-30 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
2009-02-04 19:02:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FunctionTemplateDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-09-24 22:18:54 +02:00
|
|
|
FunctionTemplateDecl *
|
|
|
|
FunctionTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
|
|
|
|
DeclarationName Name,
|
|
|
|
TemplateParameterList *Params, NamedDecl *Decl) {
|
|
|
|
bool Invalid = AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
|
|
|
|
auto *TD = new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl);
|
|
|
|
if (Invalid)
|
|
|
|
TD->setInvalidDecl();
|
|
|
|
return TD;
|
2009-02-04 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 11:43:13 +08:00
|
|
|
FunctionTemplateDecl *
|
|
|
|
FunctionTemplateDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(),
|
2014-05-12 05:36:57 +00:00
|
|
|
DeclarationName(), nullptr, nullptr);
|
2011-03-04 17:52:15 +00:00
|
|
|
}
|
|
|
|
|
2010-09-08 19:31:22 +00:00
|
|
|
RedeclarableTemplateDecl::CommonBase *
|
2013-01-23 16:52:57 +00:00
|
|
|
FunctionTemplateDecl::newCommon(ASTContext &C) const {
|
2018-03-29 20:51:59 +00:00
|
|
|
auto *CommonPtr = new (C) Common;
|
2017-02-14 05:37:36 +00:00
|
|
|
C.addDestruction(CommonPtr);
|
2010-07-29 16:11:51 +00:00
|
|
|
return CommonPtr;
|
|
|
|
}
|
|
|
|
|
2013-06-28 04:37:53 +00:00
|
|
|
void FunctionTemplateDecl::LoadLazySpecializations() const {
|
2017-12-14 23:30:18 +00:00
|
|
|
loadLazySpecializationsImpl();
|
2013-06-28 04:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
|
|
|
|
FunctionTemplateDecl::getSpecializations() const {
|
|
|
|
LoadLazySpecializations();
|
|
|
|
return getCommonPtr()->Specializations;
|
|
|
|
}
|
|
|
|
|
2010-07-20 13:59:58 +00:00
|
|
|
FunctionDecl *
|
2014-06-26 04:58:53 +00:00
|
|
|
FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
|
|
|
|
void *&InsertPos) {
|
2024-12-11 09:40:47 +08:00
|
|
|
auto *Common = getCommonPtr();
|
|
|
|
return findSpecializationImpl(Common->Specializations, InsertPos, Args);
|
2010-07-20 13:59:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 14:07:59 +00:00
|
|
|
void FunctionTemplateDecl::addSpecialization(
|
|
|
|
FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
|
2024-12-11 09:40:47 +08:00
|
|
|
auto *Common = getCommonPtr();
|
|
|
|
addSpecializationImpl<FunctionTemplateDecl>(Common->Specializations, Info,
|
2015-02-24 01:23:23 +00:00
|
|
|
InsertPos);
|
2011-04-14 14:07:59 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 17:17:51 +00:00
|
|
|
void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) {
|
2024-11-06 07:25:29 -07:00
|
|
|
using Base = RedeclarableTemplateDecl;
|
|
|
|
|
2018-10-10 17:17:51 +00:00
|
|
|
// If we haven't created a common pointer yet, then it can just be created
|
|
|
|
// with the usual method.
|
2024-11-06 07:25:29 -07:00
|
|
|
if (!Base::Common)
|
2018-10-10 17:17:51 +00:00
|
|
|
return;
|
|
|
|
|
2024-11-06 07:25:29 -07:00
|
|
|
Common *ThisCommon = static_cast<Common *>(Base::Common);
|
2018-10-10 17:17:51 +00:00
|
|
|
Common *PrevCommon = nullptr;
|
|
|
|
SmallVector<FunctionTemplateDecl *, 8> PreviousDecls;
|
|
|
|
for (; Prev; Prev = Prev->getPreviousDecl()) {
|
2024-11-06 07:25:29 -07:00
|
|
|
if (Prev->Base::Common) {
|
|
|
|
PrevCommon = static_cast<Common *>(Prev->Base::Common);
|
2018-10-10 17:17:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
PreviousDecls.push_back(Prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the previous redecl chain hasn't created a common pointer yet, then just
|
|
|
|
// use this common pointer.
|
|
|
|
if (!PrevCommon) {
|
|
|
|
for (auto *D : PreviousDecls)
|
2024-11-06 07:25:29 -07:00
|
|
|
D->Base::Common = ThisCommon;
|
2018-10-10 17:17:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we don't leak any important state.
|
|
|
|
assert(ThisCommon->Specializations.size() == 0 &&
|
|
|
|
"Can't merge incompatible declarations!");
|
|
|
|
|
2024-11-06 07:25:29 -07:00
|
|
|
Base::Common = PrevCommon;
|
2018-10-10 17:17:51 +00:00
|
|
|
}
|
|
|
|
|
2009-02-04 19:02:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ClassTemplateDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-09-24 22:18:54 +02:00
|
|
|
ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, DeclContext *DC,
|
2010-06-19 19:29:09 +00:00
|
|
|
SourceLocation L,
|
|
|
|
DeclarationName Name,
|
|
|
|
TemplateParameterList *Params,
|
2019-10-15 18:44:06 +00:00
|
|
|
NamedDecl *Decl) {
|
2021-09-24 22:18:54 +02:00
|
|
|
bool Invalid = AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
|
|
|
|
auto *TD = new (C, DC) ClassTemplateDecl(C, DC, L, Name, Params, Decl);
|
|
|
|
if (Invalid)
|
|
|
|
TD->setInvalidDecl();
|
|
|
|
return TD;
|
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-19 17:26:29 +00:00
|
|
|
}
|
|
|
|
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
|
2024-04-25 11:43:13 +08:00
|
|
|
GlobalDeclID ID) {
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(),
|
|
|
|
DeclarationName(), nullptr, nullptr);
|
2011-03-04 17:52:15 +00:00
|
|
|
}
|
|
|
|
|
2024-12-11 09:40:47 +08:00
|
|
|
void ClassTemplateDecl::LoadLazySpecializations(
|
|
|
|
bool OnlyPartial /*=false*/) const {
|
|
|
|
loadLazySpecializationsImpl(OnlyPartial);
|
2010-10-27 22:21:36 +00:00
|
|
|
}
|
|
|
|
|
2012-05-03 23:49:05 +00:00
|
|
|
llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
|
2013-02-14 13:20:36 +00:00
|
|
|
ClassTemplateDecl::getSpecializations() const {
|
2010-10-27 22:21:36 +00:00
|
|
|
LoadLazySpecializations();
|
|
|
|
return getCommonPtr()->Specializations;
|
2018-07-30 19:24:48 +00:00
|
|
|
}
|
2010-10-27 22:21:36 +00:00
|
|
|
|
2012-05-03 23:49:05 +00:00
|
|
|
llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
|
2020-10-14 09:57:55 +02:00
|
|
|
ClassTemplateDecl::getPartialSpecializations() const {
|
2024-12-11 09:40:47 +08:00
|
|
|
LoadLazySpecializations(/*PartialOnly = */ true);
|
2010-10-27 22:21:36 +00:00
|
|
|
return getCommonPtr()->PartialSpecializations;
|
2018-07-30 19:24:48 +00:00
|
|
|
}
|
2010-10-27 22:21:36 +00:00
|
|
|
|
2010-09-08 19:31:22 +00:00
|
|
|
RedeclarableTemplateDecl::CommonBase *
|
2013-01-23 16:52:57 +00:00
|
|
|
ClassTemplateDecl::newCommon(ASTContext &C) const {
|
2018-03-29 20:51:59 +00:00
|
|
|
auto *CommonPtr = new (C) Common;
|
2017-02-14 05:37:36 +00:00
|
|
|
C.addDestruction(CommonPtr);
|
2010-07-29 16:11:51 +00:00
|
|
|
return CommonPtr;
|
|
|
|
}
|
|
|
|
|
2010-07-20 13:59:28 +00:00
|
|
|
ClassTemplateSpecializationDecl *
|
2014-06-26 04:58:53 +00:00
|
|
|
ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
|
|
|
|
void *&InsertPos) {
|
2024-12-11 09:40:47 +08:00
|
|
|
auto *Common = getCommonPtr();
|
|
|
|
return findSpecializationImpl(Common->Specializations, InsertPos, Args);
|
2010-07-20 13:59:28 +00:00
|
|
|
}
|
|
|
|
|
2010-10-28 07:38:42 +00:00
|
|
|
void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
|
|
|
|
void *InsertPos) {
|
2024-12-11 09:40:47 +08:00
|
|
|
auto *Common = getCommonPtr();
|
|
|
|
addSpecializationImpl<ClassTemplateDecl>(Common->Specializations, D,
|
|
|
|
InsertPos);
|
2010-10-28 07:38:42 +00:00
|
|
|
}
|
|
|
|
|
2010-07-20 13:59:28 +00:00
|
|
|
ClassTemplatePartialSpecializationDecl *
|
2019-12-23 08:37:35 +02:00
|
|
|
ClassTemplateDecl::findPartialSpecialization(
|
|
|
|
ArrayRef<TemplateArgument> Args,
|
|
|
|
TemplateParameterList *TPL, void *&InsertPos) {
|
|
|
|
return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args,
|
|
|
|
TPL);
|
|
|
|
}
|
|
|
|
|
2023-03-30 11:42:14 -07:00
|
|
|
void ClassTemplatePartialSpecializationDecl::Profile(
|
|
|
|
llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
|
|
|
|
TemplateParameterList *TPL, const ASTContext &Context) {
|
2019-12-23 08:37:35 +02:00
|
|
|
ID.AddInteger(TemplateArgs.size());
|
|
|
|
for (const TemplateArgument &TemplateArg : TemplateArgs)
|
|
|
|
TemplateArg.Profile(ID, Context);
|
2023-03-30 11:42:14 -07:00
|
|
|
TPL->Profile(ID, Context);
|
2010-07-20 13:59:28 +00:00
|
|
|
}
|
|
|
|
|
2010-10-28 07:38:42 +00:00
|
|
|
void ClassTemplateDecl::AddPartialSpecialization(
|
|
|
|
ClassTemplatePartialSpecializationDecl *D,
|
|
|
|
void *InsertPos) {
|
2012-03-28 14:34:23 +00:00
|
|
|
if (InsertPos)
|
|
|
|
getPartialSpecializations().InsertNode(D, InsertPos);
|
|
|
|
else {
|
|
|
|
ClassTemplatePartialSpecializationDecl *Existing
|
|
|
|
= getPartialSpecializations().GetOrInsertNode(D);
|
|
|
|
(void)Existing;
|
|
|
|
assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
|
|
|
|
}
|
|
|
|
|
2010-10-28 07:38:42 +00:00
|
|
|
if (ASTMutationListener *L = getASTMutationListener())
|
|
|
|
L->AddedCXXTemplateSpecialization(this, D);
|
|
|
|
}
|
|
|
|
|
2010-04-30 05:56:50 +00:00
|
|
|
void ClassTemplateDecl::getPartialSpecializations(
|
2020-10-14 09:57:55 +02:00
|
|
|
SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) const {
|
2012-05-03 23:49:05 +00:00
|
|
|
llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
|
2010-06-21 10:57:41 +00:00
|
|
|
= getPartialSpecializations();
|
2010-04-30 05:56:50 +00:00
|
|
|
PS.clear();
|
2013-08-22 23:27:37 +00:00
|
|
|
PS.reserve(PartialSpecs.size());
|
2016-07-06 04:19:16 +00:00
|
|
|
for (ClassTemplatePartialSpecializationDecl &P : PartialSpecs)
|
|
|
|
PS.push_back(P.getMostRecentDecl());
|
2010-04-30 05:56:50 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 17:40:51 +00:00
|
|
|
ClassTemplatePartialSpecializationDecl *
|
|
|
|
ClassTemplateDecl::findPartialSpecialization(QualType T) {
|
|
|
|
ASTContext &Context = getASTContext();
|
2016-07-06 04:19:16 +00:00
|
|
|
for (ClassTemplatePartialSpecializationDecl &P :
|
|
|
|
getPartialSpecializations()) {
|
|
|
|
if (Context.hasSameType(P.getInjectedSpecializationType(), T))
|
|
|
|
return P.getMostRecentDecl();
|
2010-07-20 13:59:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-12 05:36:57 +00:00
|
|
|
return nullptr;
|
2010-07-20 13:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClassTemplatePartialSpecializationDecl *
|
|
|
|
ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
|
|
|
|
ClassTemplatePartialSpecializationDecl *D) {
|
|
|
|
Decl *DCanon = D->getCanonicalDecl();
|
2016-07-06 04:19:16 +00:00
|
|
|
for (ClassTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
|
|
|
|
if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
|
|
|
|
return P.getMostRecentDecl();
|
2009-07-30 17:40:51 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2014-05-12 05:36:57 +00:00
|
|
|
return nullptr;
|
2009-07-30 17:40:51 +00:00
|
|
|
}
|
|
|
|
|
2010-03-10 03:28:59 +00:00
|
|
|
QualType
|
2010-07-08 18:37:38 +00:00
|
|
|
ClassTemplateDecl::getInjectedClassNameSpecialization() {
|
2010-06-21 10:57:41 +00:00
|
|
|
Common *CommonPtr = getCommonPtr();
|
2009-05-10 22:57:19 +00:00
|
|
|
if (!CommonPtr->InjectedClassNameType.isNull())
|
|
|
|
return CommonPtr->InjectedClassNameType;
|
|
|
|
|
2010-12-23 16:00:30 +00:00
|
|
|
// C++0x [temp.dep.type]p2:
|
2018-07-30 19:24:48 +00:00
|
|
|
// The template argument list of a primary template is a template argument
|
2010-12-23 16:00:30 +00:00
|
|
|
// list in which the nth template argument has the value of the nth template
|
2018-07-30 19:24:48 +00:00
|
|
|
// parameter of the class template. If the nth template parameter is a
|
|
|
|
// template parameter pack (14.5.3), the nth template argument is a pack
|
|
|
|
// expansion (14.5.3) whose pattern is the name of the template parameter
|
2010-12-23 16:00:30 +00:00
|
|
|
// pack.
|
2010-07-08 18:37:38 +00:00
|
|
|
ASTContext &Context = getASTContext();
|
2024-05-29 17:02:15 -03:00
|
|
|
TemplateName Name = Context.getQualifiedTemplateName(
|
|
|
|
/*NNS=*/nullptr, /*TemplateKeyword=*/false, TemplateName(this));
|
2024-10-29 11:36:55 -06:00
|
|
|
CommonPtr->InjectedClassNameType = Context.getTemplateSpecializationType(
|
|
|
|
Name, getTemplateParameters()->getInjectedTemplateArgs(Context));
|
2009-05-10 22:57:19 +00:00
|
|
|
return CommonPtr->InjectedClassNameType;
|
|
|
|
}
|
|
|
|
|
2009-02-04 19:02:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateTypeParm Allocation/Deallocation Method Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2023-01-14 12:31:01 -08:00
|
|
|
TemplateTypeParmDecl *TemplateTypeParmDecl::Create(
|
|
|
|
const ASTContext &C, DeclContext *DC, SourceLocation KeyLoc,
|
|
|
|
SourceLocation NameLoc, unsigned D, unsigned P, IdentifierInfo *Id,
|
|
|
|
bool Typename, bool ParameterPack, bool HasTypeConstraint,
|
|
|
|
std::optional<unsigned> NumExpanded) {
|
2018-03-29 20:51:59 +00:00
|
|
|
auto *TTPDecl =
|
2020-01-15 02:48:42 +02:00
|
|
|
new (C, DC,
|
|
|
|
additionalSizeToAlloc<TypeConstraint>(HasTypeConstraint ? 1 : 0))
|
|
|
|
TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename,
|
|
|
|
HasTypeConstraint, NumExpanded);
|
Re-applies the patch first applied way back in r106099, with
accompanying fixes to make it work today.
The core of this patch is to provide a link from a TemplateTypeParmType
back to the TemplateTypeParmDecl node which declared it. This in turn
provides much more precise information about the type, where it came
from, and how it functions for AST consumers.
To make the patch work almost a year after its first attempt, it needed
serialization support, and it now retains the old getName() interface.
Finally, it requires us to not attempt to instantiate the type in an
unsupported friend decl -- specifically those coming from template
friend decls but which refer to a specific type through a dependent
name.
A cleaner representation of the last item would be to build
FriendTemplateDecl nodes for these, storing their template parameters
etc, and to perform proper instantation of them like any other template
declaration. They can still be flagged as unsupported for the purpose of
access checking, etc.
This passed an asserts-enabled bootstrap for me, and the reduced test
case mentioned in the original review thread no longer causes issues,
likely fixed at somewhere amidst the 24k revisions that have elapsed.
llvm-svn: 130628
2011-05-01 00:51:33 +00:00
|
|
|
QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
|
2014-04-23 18:20:42 +00:00
|
|
|
TTPDecl->setTypeForDecl(TTPType.getTypePtr());
|
Re-applies the patch first applied way back in r106099, with
accompanying fixes to make it work today.
The core of this patch is to provide a link from a TemplateTypeParmType
back to the TemplateTypeParmDecl node which declared it. This in turn
provides much more precise information about the type, where it came
from, and how it functions for AST consumers.
To make the patch work almost a year after its first attempt, it needed
serialization support, and it now retains the old getName() interface.
Finally, it requires us to not attempt to instantiate the type in an
unsupported friend decl -- specifically those coming from template
friend decls but which refer to a specific type through a dependent
name.
A cleaner representation of the last item would be to build
FriendTemplateDecl nodes for these, storing their template parameters
etc, and to perform proper instantation of them like any other template
declaration. They can still be flagged as unsupported for the purpose of
access checking, etc.
This passed an asserts-enabled bootstrap for me, and the reduced test
case mentioned in the original review thread no longer causes issues,
likely fixed at somewhere amidst the 24k revisions that have elapsed.
llvm-svn: 130628
2011-05-01 00:51:33 +00:00
|
|
|
return TTPDecl;
|
2009-02-04 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2010-07-02 11:54:55 +00:00
|
|
|
TemplateTypeParmDecl *
|
2024-04-25 11:43:13 +08:00
|
|
|
TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, GlobalDeclID ID) {
|
2022-12-03 11:13:41 -08:00
|
|
|
return new (C, ID)
|
|
|
|
TemplateTypeParmDecl(nullptr, SourceLocation(), SourceLocation(), nullptr,
|
|
|
|
false, false, std::nullopt);
|
2020-01-15 02:48:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TemplateTypeParmDecl *
|
2024-04-25 11:43:13 +08:00
|
|
|
TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, GlobalDeclID ID,
|
2020-01-15 02:48:42 +02:00
|
|
|
bool HasTypeConstraint) {
|
|
|
|
return new (C, ID,
|
|
|
|
additionalSizeToAlloc<TypeConstraint>(HasTypeConstraint ? 1 : 0))
|
2022-12-03 11:13:41 -08:00
|
|
|
TemplateTypeParmDecl(nullptr, SourceLocation(), SourceLocation(), nullptr,
|
|
|
|
false, HasTypeConstraint, std::nullopt);
|
2010-07-02 11:54:55 +00:00
|
|
|
}
|
|
|
|
|
2009-10-29 08:12:44 +00:00
|
|
|
SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
|
2024-05-21 20:27:50 -03:00
|
|
|
return hasDefaultArgument() ? getDefaultArgument().getLocation()
|
|
|
|
: SourceLocation();
|
2011-03-04 12:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SourceRange TemplateTypeParmDecl::getSourceRange() const {
|
|
|
|
if (hasDefaultArgument() && !defaultArgumentWasInherited())
|
2018-08-09 21:08:08 +00:00
|
|
|
return SourceRange(getBeginLoc(),
|
2024-05-21 20:27:50 -03:00
|
|
|
getDefaultArgument().getSourceRange().getEnd());
|
2019-10-01 14:08:51 +00:00
|
|
|
// TypeDecl::getSourceRange returns a range containing name location, which is
|
|
|
|
// wrong for unnamed template parameters. e.g:
|
|
|
|
// it will return <[[typename>]] instead of <[[typename]]>
|
2024-05-21 20:27:50 -03:00
|
|
|
if (getDeclName().isEmpty())
|
2019-10-01 14:08:51 +00:00
|
|
|
return SourceRange(getBeginLoc());
|
|
|
|
return TypeDecl::getSourceRange();
|
2009-10-29 08:12:44 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 20:27:50 -03:00
|
|
|
void TemplateTypeParmDecl::setDefaultArgument(
|
|
|
|
const ASTContext &C, const TemplateArgumentLoc &DefArg) {
|
|
|
|
if (DefArg.getArgument().isNull())
|
|
|
|
DefaultArgument.set(nullptr);
|
|
|
|
else
|
|
|
|
DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg));
|
|
|
|
}
|
|
|
|
|
2009-10-29 00:04:11 +00:00
|
|
|
unsigned TemplateTypeParmDecl::getDepth() const {
|
2019-10-03 16:58:01 +00:00
|
|
|
return getTypeForDecl()->castAs<TemplateTypeParmType>()->getDepth();
|
2009-10-29 00:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned TemplateTypeParmDecl::getIndex() const {
|
2019-10-03 16:58:01 +00:00
|
|
|
return getTypeForDecl()->castAs<TemplateTypeParmType>()->getIndex();
|
2009-10-29 00:04:11 +00:00
|
|
|
}
|
|
|
|
|
Re-applies the patch first applied way back in r106099, with
accompanying fixes to make it work today.
The core of this patch is to provide a link from a TemplateTypeParmType
back to the TemplateTypeParmDecl node which declared it. This in turn
provides much more precise information about the type, where it came
from, and how it functions for AST consumers.
To make the patch work almost a year after its first attempt, it needed
serialization support, and it now retains the old getName() interface.
Finally, it requires us to not attempt to instantiate the type in an
unsupported friend decl -- specifically those coming from template
friend decls but which refer to a specific type through a dependent
name.
A cleaner representation of the last item would be to build
FriendTemplateDecl nodes for these, storing their template parameters
etc, and to perform proper instantation of them like any other template
declaration. They can still be flagged as unsupported for the purpose of
access checking, etc.
This passed an asserts-enabled bootstrap for me, and the reduced test
case mentioned in the original review thread no longer causes issues,
likely fixed at somewhere amidst the 24k revisions that have elapsed.
llvm-svn: 130628
2011-05-01 00:51:33 +00:00
|
|
|
bool TemplateTypeParmDecl::isParameterPack() const {
|
2019-10-03 16:58:01 +00:00
|
|
|
return getTypeForDecl()->castAs<TemplateTypeParmType>()->isParameterPack();
|
Re-applies the patch first applied way back in r106099, with
accompanying fixes to make it work today.
The core of this patch is to provide a link from a TemplateTypeParmType
back to the TemplateTypeParmDecl node which declared it. This in turn
provides much more precise information about the type, where it came
from, and how it functions for AST consumers.
To make the patch work almost a year after its first attempt, it needed
serialization support, and it now retains the old getName() interface.
Finally, it requires us to not attempt to instantiate the type in an
unsupported friend decl -- specifically those coming from template
friend decls but which refer to a specific type through a dependent
name.
A cleaner representation of the last item would be to build
FriendTemplateDecl nodes for these, storing their template parameters
etc, and to perform proper instantation of them like any other template
declaration. They can still be flagged as unsupported for the purpose of
access checking, etc.
This passed an asserts-enabled bootstrap for me, and the reduced test
case mentioned in the original review thread no longer causes issues,
likely fixed at somewhere amidst the 24k revisions that have elapsed.
llvm-svn: 130628
2011-05-01 00:51:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 14:00:16 +02:00
|
|
|
void TemplateTypeParmDecl::setTypeConstraint(
|
|
|
|
ConceptReference *Loc, Expr *ImmediatelyDeclaredConstraint) {
|
2020-01-15 02:48:42 +02:00
|
|
|
assert(HasTypeConstraint &&
|
|
|
|
"HasTypeConstraint=true must be passed at construction in order to "
|
|
|
|
"call setTypeConstraint");
|
|
|
|
assert(!TypeConstraintInitialized &&
|
|
|
|
"TypeConstraint was already initialized!");
|
2023-08-02 14:00:16 +02:00
|
|
|
new (getTrailingObjects<TypeConstraint>())
|
|
|
|
TypeConstraint(Loc, ImmediatelyDeclaredConstraint);
|
2020-01-15 02:48:42 +02:00
|
|
|
TypeConstraintInitialized = true;
|
|
|
|
}
|
|
|
|
|
2009-02-04 19:02:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NonTypeTemplateParmDecl Method Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-06 04:19:16 +00:00
|
|
|
NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(
|
|
|
|
DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D,
|
2024-04-11 00:33:40 +00:00
|
|
|
unsigned P, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
|
2016-07-06 04:19:16 +00:00
|
|
|
ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos)
|
|
|
|
: DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
|
|
|
|
TemplateParmPosition(D, P), ParameterPack(true),
|
|
|
|
ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) {
|
|
|
|
if (!ExpandedTypes.empty() && !ExpandedTInfos.empty()) {
|
2015-08-06 20:26:32 +00:00
|
|
|
auto TypesAndInfos =
|
|
|
|
getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
|
2011-01-19 20:10:05 +00:00
|
|
|
for (unsigned I = 0; I != NumExpandedTypes; ++I) {
|
2015-08-06 20:26:32 +00:00
|
|
|
new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]);
|
|
|
|
TypesAndInfos[I].second = ExpandedTInfos[I];
|
2011-01-19 20:10:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 00:33:40 +00:00
|
|
|
NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create(
|
|
|
|
const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
|
|
|
|
SourceLocation IdLoc, unsigned D, unsigned P, const IdentifierInfo *Id,
|
|
|
|
QualType T, bool ParameterPack, TypeSourceInfo *TInfo) {
|
2020-01-22 02:03:05 +02:00
|
|
|
AutoType *AT =
|
2020-04-21 15:37:19 -04:00
|
|
|
C.getLangOpts().CPlusPlus20 ? T->getContainedAutoType() : nullptr;
|
2020-01-22 02:03:05 +02:00
|
|
|
return new (C, DC,
|
|
|
|
additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>,
|
|
|
|
Expr *>(0,
|
|
|
|
AT && AT->isConstrained() ? 1 : 0))
|
|
|
|
NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, ParameterPack,
|
|
|
|
TInfo);
|
2009-02-04 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 04:19:16 +00:00
|
|
|
NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create(
|
|
|
|
const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
|
2024-04-11 00:33:40 +00:00
|
|
|
SourceLocation IdLoc, unsigned D, unsigned P, const IdentifierInfo *Id,
|
2016-07-06 04:19:16 +00:00
|
|
|
QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
|
|
|
|
ArrayRef<TypeSourceInfo *> ExpandedTInfos) {
|
2020-01-22 02:03:05 +02:00
|
|
|
AutoType *AT = TInfo->getType()->getContainedAutoType();
|
2015-08-06 20:26:32 +00:00
|
|
|
return new (C, DC,
|
2020-01-22 02:03:05 +02:00
|
|
|
additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>,
|
|
|
|
Expr *>(
|
|
|
|
ExpandedTypes.size(), AT && AT->isConstrained() ? 1 : 0))
|
2015-08-06 20:26:32 +00:00
|
|
|
NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
|
2016-07-06 04:19:16 +00:00
|
|
|
ExpandedTypes, ExpandedTInfos);
|
2011-01-19 20:10:05 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 21:55:30 +00:00
|
|
|
NonTypeTemplateParmDecl *
|
2024-04-25 11:43:13 +08:00
|
|
|
NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID,
|
2020-01-22 02:03:05 +02:00
|
|
|
bool HasTypeConstraint) {
|
|
|
|
return new (C, ID, additionalSizeToAlloc<std::pair<QualType,
|
|
|
|
TypeSourceInfo *>,
|
|
|
|
Expr *>(0,
|
|
|
|
HasTypeConstraint ? 1 : 0))
|
|
|
|
NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
|
|
|
|
0, 0, nullptr, QualType(), false, nullptr);
|
2012-01-05 21:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NonTypeTemplateParmDecl *
|
2024-04-25 11:43:13 +08:00
|
|
|
NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID,
|
2020-01-22 02:03:05 +02:00
|
|
|
unsigned NumExpandedTypes,
|
|
|
|
bool HasTypeConstraint) {
|
2016-07-06 04:19:16 +00:00
|
|
|
auto *NTTP =
|
2022-12-03 11:13:41 -08:00
|
|
|
new (C, ID,
|
|
|
|
additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>, Expr *>(
|
|
|
|
NumExpandedTypes, HasTypeConstraint ? 1 : 0))
|
2016-07-06 04:19:16 +00:00
|
|
|
NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
|
2024-10-24 10:23:40 +01:00
|
|
|
0, 0, nullptr, QualType(), nullptr, {}, {});
|
2016-07-06 04:19:16 +00:00
|
|
|
NTTP->NumExpandedTypes = NumExpandedTypes;
|
|
|
|
return NTTP;
|
2012-01-05 21:55:30 +00:00
|
|
|
}
|
|
|
|
|
2011-02-09 01:13:10 +00:00
|
|
|
SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
|
2011-03-04 11:03:48 +00:00
|
|
|
if (hasDefaultArgument() && !defaultArgumentWasInherited())
|
2011-03-08 16:41:52 +00:00
|
|
|
return SourceRange(getOuterLocStart(),
|
2024-05-22 12:18:44 -03:00
|
|
|
getDefaultArgument().getSourceRange().getEnd());
|
2011-03-08 16:41:52 +00:00
|
|
|
return DeclaratorDecl::getSourceRange();
|
2011-02-09 01:13:10 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 19:49:53 +00:00
|
|
|
SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
|
2024-05-22 12:18:44 -03:00
|
|
|
return hasDefaultArgument() ? getDefaultArgument().getSourceRange().getBegin()
|
|
|
|
: SourceLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NonTypeTemplateParmDecl::setDefaultArgument(
|
|
|
|
const ASTContext &C, const TemplateArgumentLoc &DefArg) {
|
|
|
|
if (DefArg.getArgument().isNull())
|
|
|
|
DefaultArgument.set(nullptr);
|
|
|
|
else
|
|
|
|
DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg));
|
2009-02-10 19:49:53 +00:00
|
|
|
}
|
|
|
|
|
2009-02-04 19:02:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateTemplateParmDecl Method Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-11-29 22:39:22 +00:00
|
|
|
void TemplateTemplateParmDecl::anchor() {}
|
2011-12-20 02:48:34 +00:00
|
|
|
|
2012-09-07 02:06:42 +00:00
|
|
|
TemplateTemplateParmDecl::TemplateTemplateParmDecl(
|
|
|
|
DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
|
2024-04-11 13:20:05 -04:00
|
|
|
IdentifierInfo *Id, bool Typename, TemplateParameterList *Params,
|
2016-07-06 04:19:16 +00:00
|
|
|
ArrayRef<TemplateParameterList *> Expansions)
|
|
|
|
: TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
|
2024-04-11 13:20:05 -04:00
|
|
|
TemplateParmPosition(D, P), Typename(Typename), ParameterPack(true),
|
2016-07-06 04:19:16 +00:00
|
|
|
ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) {
|
|
|
|
if (!Expansions.empty())
|
|
|
|
std::uninitialized_copy(Expansions.begin(), Expansions.end(),
|
2015-08-06 20:26:32 +00:00
|
|
|
getTrailingObjects<TemplateParameterList *>());
|
2012-09-07 02:06:42 +00:00
|
|
|
}
|
|
|
|
|
2009-02-04 19:02:06 +00:00
|
|
|
TemplateTemplateParmDecl *
|
2011-01-12 09:06:06 +00:00
|
|
|
TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
|
2009-02-04 19:02:06 +00:00
|
|
|
SourceLocation L, unsigned D, unsigned P,
|
2011-01-05 15:48:55 +00:00
|
|
|
bool ParameterPack, IdentifierInfo *Id,
|
2024-04-11 13:20:05 -04:00
|
|
|
bool Typename, TemplateParameterList *Params) {
|
2013-11-22 09:01:48 +00:00
|
|
|
return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
|
2024-04-11 13:20:05 -04:00
|
|
|
Typename, Params);
|
2009-02-04 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 02:06:42 +00:00
|
|
|
TemplateTemplateParmDecl *
|
|
|
|
TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
|
|
|
|
SourceLocation L, unsigned D, unsigned P,
|
2024-04-11 13:20:05 -04:00
|
|
|
IdentifierInfo *Id, bool Typename,
|
2012-09-07 02:06:42 +00:00
|
|
|
TemplateParameterList *Params,
|
2013-01-12 19:30:44 +00:00
|
|
|
ArrayRef<TemplateParameterList *> Expansions) {
|
2015-08-06 20:26:32 +00:00
|
|
|
return new (C, DC,
|
|
|
|
additionalSizeToAlloc<TemplateParameterList *>(Expansions.size()))
|
2024-04-11 13:20:05 -04:00
|
|
|
TemplateTemplateParmDecl(DC, L, D, P, Id, Typename, Params, Expansions);
|
2012-09-07 02:06:42 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 21:55:30 +00:00
|
|
|
TemplateTemplateParmDecl *
|
2024-04-25 11:43:13 +08:00
|
|
|
TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
|
2014-05-12 05:36:57 +00:00
|
|
|
return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0,
|
2024-04-11 13:20:05 -04:00
|
|
|
false, nullptr, false, nullptr);
|
2012-01-05 21:55:30 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 02:06:42 +00:00
|
|
|
TemplateTemplateParmDecl *
|
2024-04-25 11:43:13 +08:00
|
|
|
TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID,
|
2012-09-07 02:06:42 +00:00
|
|
|
unsigned NumExpansions) {
|
2016-07-06 04:19:16 +00:00
|
|
|
auto *TTP =
|
|
|
|
new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions))
|
|
|
|
TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
|
2024-10-24 10:23:40 +01:00
|
|
|
false, nullptr, {});
|
2016-07-06 04:19:16 +00:00
|
|
|
TTP->NumExpandedParams = NumExpansions;
|
|
|
|
return TTP;
|
2012-09-07 02:06:42 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 20:16:32 +00:00
|
|
|
SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const {
|
|
|
|
return hasDefaultArgument() ? getDefaultArgument().getLocation()
|
|
|
|
: SourceLocation();
|
|
|
|
}
|
|
|
|
|
2015-06-10 00:29:03 +00:00
|
|
|
void TemplateTemplateParmDecl::setDefaultArgument(
|
|
|
|
const ASTContext &C, const TemplateArgumentLoc &DefArg) {
|
|
|
|
if (DefArg.getArgument().isNull())
|
|
|
|
DefaultArgument.set(nullptr);
|
|
|
|
else
|
|
|
|
DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg));
|
|
|
|
}
|
|
|
|
|
2009-05-11 23:53:27 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateArgumentList Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-07-03 21:17:51 +00:00
|
|
|
TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args)
|
2024-02-01 11:50:50 -05:00
|
|
|
: NumArguments(Args.size()) {
|
2016-07-03 21:17:51 +00:00
|
|
|
std::uninitialized_copy(Args.begin(), Args.end(),
|
2015-08-06 20:26:32 +00:00
|
|
|
getTrailingObjects<TemplateArgument>());
|
|
|
|
}
|
|
|
|
|
2010-11-07 23:05:16 +00:00
|
|
|
TemplateArgumentList *
|
|
|
|
TemplateArgumentList::CreateCopy(ASTContext &Context,
|
2016-07-03 21:17:51 +00:00
|
|
|
ArrayRef<TemplateArgument> Args) {
|
|
|
|
void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size()));
|
|
|
|
return new (Mem) TemplateArgumentList(Args);
|
2010-06-23 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 00:49:14 +00:00
|
|
|
FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create(
|
|
|
|
ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
|
2024-05-17 17:49:50 +04:00
|
|
|
TemplateSpecializationKind TSK, TemplateArgumentList *TemplateArgs,
|
2019-05-02 00:49:14 +00:00
|
|
|
const TemplateArgumentListInfo *TemplateArgsAsWritten, SourceLocation POI,
|
|
|
|
MemberSpecializationInfo *MSInfo) {
|
2014-05-12 05:36:57 +00:00
|
|
|
const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
|
2011-09-22 20:07:09 +00:00
|
|
|
if (TemplateArgsAsWritten)
|
|
|
|
ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
|
|
|
|
*TemplateArgsAsWritten);
|
|
|
|
|
2019-05-02 00:49:14 +00:00
|
|
|
void *Mem =
|
|
|
|
C.Allocate(totalSizeToAlloc<MemberSpecializationInfo *>(MSInfo ? 1 : 0));
|
|
|
|
return new (Mem) FunctionTemplateSpecializationInfo(
|
|
|
|
FD, Template, TSK, TemplateArgs, ArgsAsWritten, POI, MSInfo);
|
2011-09-22 20:07:09 +00: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 01:05:43 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ClassTemplateSpecializationDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-29 22:39:22 +00:00
|
|
|
|
2025-02-05 12:02:24 -03:00
|
|
|
ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(
|
|
|
|
ASTContext &Context, Kind DK, TagKind TK, DeclContext *DC,
|
|
|
|
SourceLocation StartLoc, SourceLocation IdLoc,
|
|
|
|
ClassTemplateDecl *SpecializedTemplate, ArrayRef<TemplateArgument> Args,
|
|
|
|
bool MatchedPackOnParmToNonPackOnArg,
|
|
|
|
ClassTemplateSpecializationDecl *PrevDecl)
|
2017-11-29 22:39:22 +00:00
|
|
|
: CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc,
|
|
|
|
SpecializedTemplate->getIdentifier(), PrevDecl),
|
2025-02-05 12:02:24 -03:00
|
|
|
SpecializedTemplate(SpecializedTemplate),
|
|
|
|
TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
|
|
|
|
SpecializationKind(TSK_Undeclared),
|
|
|
|
MatchedPackOnParmToNonPackOnArg(MatchedPackOnParmToNonPackOnArg) {
|
|
|
|
assert(DK == Kind::ClassTemplateSpecialization ||
|
|
|
|
MatchedPackOnParmToNonPackOnArg == 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 01:05:43 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
|
|
|
|
Kind DK)
|
2023-11-03 21:45:39 +04:00
|
|
|
: CXXRecordDecl(DK, TagTypeKind::Struct, C, nullptr, SourceLocation(),
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
SourceLocation(), nullptr, nullptr),
|
2017-11-29 22:39:22 +00:00
|
|
|
SpecializationKind(TSK_Undeclared) {}
|
2010-06-23 13:48:23 +00:00
|
|
|
|
2025-02-05 12:02:24 -03:00
|
|
|
ClassTemplateSpecializationDecl *ClassTemplateSpecializationDecl::Create(
|
|
|
|
ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation StartLoc,
|
|
|
|
SourceLocation IdLoc, ClassTemplateDecl *SpecializedTemplate,
|
|
|
|
ArrayRef<TemplateArgument> Args, bool MatchedPackOnParmToNonPackOnArg,
|
|
|
|
ClassTemplateSpecializationDecl *PrevDecl) {
|
|
|
|
auto *Result = new (Context, DC) ClassTemplateSpecializationDecl(
|
|
|
|
Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
|
|
|
|
SpecializedTemplate, Args, MatchedPackOnParmToNonPackOnArg, PrevDecl);
|
2018-08-01 20:48:16 +00:00
|
|
|
Result->setMayHaveOutOfDateDef(false);
|
Ensure that type definitions present in just-loaded modules are
visible.
The basic problem here is that a given translation unit can use
forward declarations to form pointers to a given type, say,
class X;
X *x;
and then import a module that includes a definition of X:
import XDef;
We will then fail when attempting to access a member of X, e.g.,
x->method()
because the AST reader did not know to look for a default of a class
named X within the new module.
This implementation is a bit of a C-centric hack, because the only
definitions that can have this property are enums, structs, unions,
Objective-C classes, and Objective-C protocols, and all of those are
either visible at the top-level or can't be defined later. Hence, we
can use the out-of-date-ness of the name and the identifier-update
mechanism to force the update.
In C++, we will not be so lucky, and will need a more advanced
solution, because the definitions could be in namespaces defined in
two different modules, e.g.,
// module 1
namespace N { struct X; }
// module 2
namespace N { struct X { /* ... */ }; }
One possible implementation here is for C++ to extend the information
associated with each identifier table to include the declaration IDs
of any definitions associated with that name, regardless of
context. We would have to eagerly load those definitions.
llvm-svn: 174794
2013-02-09 01:35:03 +00:00
|
|
|
|
2022-07-06 13:29:48 -05:00
|
|
|
// If the template decl is incomplete, copy the external lexical storage from
|
|
|
|
// the base template. This allows instantiations of incomplete types to
|
|
|
|
// complete using the external AST if the template's declaration came from an
|
|
|
|
// external AST.
|
|
|
|
if (!SpecializedTemplate->getTemplatedDecl()->isCompleteDefinition())
|
|
|
|
Result->setHasExternalLexicalStorage(
|
|
|
|
SpecializedTemplate->getTemplatedDecl()->hasExternalLexicalStorage());
|
|
|
|
|
2009-02-17 23:15:12 +00:00
|
|
|
Context.getTypeDeclType(Result, PrevDecl);
|
|
|
|
return Result;
|
Added ClassTemplateSpecializationDecl, which is a subclass of
CXXRecordDecl that is used to represent class template
specializations. These are canonical declarations that can refer to
either an actual class template specialization in the code, e.g.,
template<> class vector<bool> { };
or to a template instantiation. However, neither of these features is
actually implemented yet, so really we're just using (and uniqing) the
declarations to make sure that, e.g., A<int> is a different type from
A<float>. Note that we carefully distinguish between what the user
wrote in the source code (e.g., "A<FLOAT>") and the semantic entity it
represents (e.g., "A<float, int>"); the former is in the sugared Type,
the latter is an actual Decl.
llvm-svn: 64716
2009-02-17 01:05:43 +00:00
|
|
|
}
|
2009-05-31 09:31:02 +00:00
|
|
|
|
2010-06-23 13:48:23 +00:00
|
|
|
ClassTemplateSpecializationDecl *
|
2024-04-25 11:43:13 +08:00
|
|
|
ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
|
|
|
|
GlobalDeclID ID) {
|
2018-03-29 20:51:59 +00:00
|
|
|
auto *Result =
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization);
|
2018-08-01 20:48:16 +00:00
|
|
|
Result->setMayHaveOutOfDateDef(false);
|
Ensure that type definitions present in just-loaded modules are
visible.
The basic problem here is that a given translation unit can use
forward declarations to form pointers to a given type, say,
class X;
X *x;
and then import a module that includes a definition of X:
import XDef;
We will then fail when attempting to access a member of X, e.g.,
x->method()
because the AST reader did not know to look for a default of a class
named X within the new module.
This implementation is a bit of a C-centric hack, because the only
definitions that can have this property are enums, structs, unions,
Objective-C classes, and Objective-C protocols, and all of those are
either visible at the top-level or can't be defined later. Hence, we
can use the out-of-date-ness of the name and the identifier-update
mechanism to force the update.
In C++, we will not be so lucky, and will need a more advanced
solution, because the definitions could be in namespaces defined in
two different modules, e.g.,
// module 1
namespace N { struct X; }
// module 2
namespace N { struct X { /* ... */ }; }
One possible implementation here is for C++ to extend the information
associated with each identifier table to include the declaration IDs
of any definitions associated with that name, regardless of
context. We would have to eagerly load those definitions.
llvm-svn: 174794
2013-02-09 01:35:03 +00:00
|
|
|
return Result;
|
2010-06-23 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2013-02-22 15:46:01 +00:00
|
|
|
void ClassTemplateSpecializationDecl::getNameForDiagnostic(
|
|
|
|
raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
|
|
|
|
NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
|
2011-02-19 18:51:44 +00:00
|
|
|
|
2018-03-29 20:51:59 +00:00
|
|
|
const auto *PS = dyn_cast<ClassTemplatePartialSpecializationDecl>(this);
|
2016-12-24 04:09:05 +00:00
|
|
|
if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
|
|
|
|
PS ? PS->getTemplateArgsAsWritten() : nullptr) {
|
2020-11-11 13:04:35 -08:00
|
|
|
printTemplateArgumentList(
|
|
|
|
OS, ArgsAsWritten->arguments(), Policy,
|
|
|
|
getSpecializedTemplate()->getTemplateParameters());
|
2016-12-24 04:09:05 +00:00
|
|
|
} else {
|
|
|
|
const TemplateArgumentList &TemplateArgs = getTemplateArgs();
|
2020-11-11 13:04:35 -08:00
|
|
|
printTemplateArgumentList(
|
|
|
|
OS, TemplateArgs.asArray(), Policy,
|
|
|
|
getSpecializedTemplate()->getTemplateParameters());
|
2016-12-24 04:09:05 +00:00
|
|
|
}
|
2011-02-19 18:51:44 +00:00
|
|
|
}
|
|
|
|
|
2009-08-02 23:24:31 +00:00
|
|
|
ClassTemplateDecl *
|
2009-09-09 15:08:12 +00:00
|
|
|
ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
|
2018-03-29 20:51:59 +00:00
|
|
|
if (const auto *PartialSpec =
|
|
|
|
SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
|
2009-08-02 23:24:31 +00:00
|
|
|
return PartialSpec->PartialSpecialization->getSpecializedTemplate();
|
2024-11-24 07:28:20 -08:00
|
|
|
return cast<ClassTemplateDecl *>(SpecializedTemplate);
|
2009-08-02 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
2011-03-04 14:20:30 +00:00
|
|
|
SourceRange
|
|
|
|
ClassTemplateSpecializationDecl::getSourceRange() const {
|
2024-05-14 16:09:57 -04:00
|
|
|
switch (getSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ImplicitInstantiation: {
|
2011-10-03 20:34:03 +00:00
|
|
|
llvm::PointerUnion<ClassTemplateDecl *,
|
|
|
|
ClassTemplatePartialSpecializationDecl *>
|
2024-05-14 16:09:57 -04:00
|
|
|
Pattern = getSpecializedTemplateOrPartial();
|
|
|
|
assert(!Pattern.isNull() &&
|
|
|
|
"Class template specialization without pattern?");
|
|
|
|
if (const auto *CTPSD =
|
2025-01-25 01:15:38 -08:00
|
|
|
dyn_cast<ClassTemplatePartialSpecializationDecl *>(Pattern))
|
2024-05-14 16:09:57 -04:00
|
|
|
return CTPSD->getSourceRange();
|
2024-11-24 07:28:20 -08:00
|
|
|
return cast<ClassTemplateDecl *>(Pattern)->getSourceRange();
|
2024-05-14 16:09:57 -04:00
|
|
|
}
|
|
|
|
case TSK_ExplicitSpecialization: {
|
|
|
|
SourceRange Range = CXXRecordDecl::getSourceRange();
|
|
|
|
if (const ASTTemplateArgumentListInfo *Args = getTemplateArgsAsWritten();
|
|
|
|
!isThisDeclarationADefinition() && Args)
|
|
|
|
Range.setEnd(Args->getRAngleLoc());
|
|
|
|
return Range;
|
|
|
|
}
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
|
|
|
case TSK_ExplicitInstantiationDefinition: {
|
|
|
|
SourceRange Range = CXXRecordDecl::getSourceRange();
|
|
|
|
if (SourceLocation ExternKW = getExternKeywordLoc(); ExternKW.isValid())
|
|
|
|
Range.setBegin(ExternKW);
|
|
|
|
else if (SourceLocation TemplateKW = getTemplateKeywordLoc();
|
|
|
|
TemplateKW.isValid())
|
|
|
|
Range.setBegin(TemplateKW);
|
|
|
|
if (const ASTTemplateArgumentListInfo *Args = getTemplateArgsAsWritten())
|
|
|
|
Range.setEnd(Args->getRAngleLoc());
|
|
|
|
return Range;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("unhandled template specialization kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClassTemplateSpecializationDecl::setExternKeywordLoc(SourceLocation Loc) {
|
2025-01-25 14:05:01 -08:00
|
|
|
auto *Info = dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo);
|
2024-05-14 16:09:57 -04:00
|
|
|
if (!Info) {
|
|
|
|
// Don't allocate if the location is invalid.
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return;
|
|
|
|
Info = new (getASTContext()) ExplicitInstantiationInfo;
|
|
|
|
Info->TemplateArgsAsWritten = getTemplateArgsAsWritten();
|
|
|
|
ExplicitInfo = Info;
|
2011-10-03 20:34:03 +00:00
|
|
|
}
|
2024-05-14 16:09:57 -04:00
|
|
|
Info->ExternKeywordLoc = Loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClassTemplateSpecializationDecl::setTemplateKeywordLoc(
|
|
|
|
SourceLocation Loc) {
|
2025-01-25 14:05:01 -08:00
|
|
|
auto *Info = dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo);
|
2024-05-14 16:09:57 -04:00
|
|
|
if (!Info) {
|
|
|
|
// Don't allocate if the location is invalid.
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return;
|
|
|
|
Info = new (getASTContext()) ExplicitInstantiationInfo;
|
|
|
|
Info->TemplateArgsAsWritten = getTemplateArgsAsWritten();
|
|
|
|
ExplicitInfo = Info;
|
|
|
|
}
|
|
|
|
Info->TemplateKeywordLoc = Loc;
|
2011-03-04 14:20:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 21:25:49 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ConceptDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
ConceptDecl *ConceptDecl::Create(ASTContext &C, DeclContext *DC,
|
|
|
|
SourceLocation L, DeclarationName Name,
|
|
|
|
TemplateParameterList *Params,
|
|
|
|
Expr *ConstraintExpr) {
|
2021-09-24 22:18:54 +02:00
|
|
|
bool Invalid = AdoptTemplateParameterList(Params, DC);
|
|
|
|
auto *TD = new (C, DC) ConceptDecl(DC, L, Name, Params, ConstraintExpr);
|
|
|
|
if (Invalid)
|
|
|
|
TD->setInvalidDecl();
|
|
|
|
return TD;
|
2019-07-10 21:25:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 11:43:13 +08:00
|
|
|
ConceptDecl *ConceptDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
|
2019-07-10 21:25:49 +00:00
|
|
|
ConceptDecl *Result = new (C, ID) ConceptDecl(nullptr, SourceLocation(),
|
|
|
|
DeclarationName(),
|
|
|
|
nullptr, nullptr);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2022-10-24 12:20:36 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ImplicitConceptSpecializationDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
ImplicitConceptSpecializationDecl::ImplicitConceptSpecializationDecl(
|
|
|
|
DeclContext *DC, SourceLocation SL,
|
|
|
|
ArrayRef<TemplateArgument> ConvertedArgs)
|
|
|
|
: Decl(ImplicitConceptSpecialization, DC, SL),
|
|
|
|
NumTemplateArgs(ConvertedArgs.size()) {
|
|
|
|
setTemplateArguments(ConvertedArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImplicitConceptSpecializationDecl::ImplicitConceptSpecializationDecl(
|
|
|
|
EmptyShell Empty, unsigned NumTemplateArgs)
|
|
|
|
: Decl(ImplicitConceptSpecialization, Empty),
|
|
|
|
NumTemplateArgs(NumTemplateArgs) {}
|
|
|
|
|
|
|
|
ImplicitConceptSpecializationDecl *ImplicitConceptSpecializationDecl::Create(
|
|
|
|
const ASTContext &C, DeclContext *DC, SourceLocation SL,
|
|
|
|
ArrayRef<TemplateArgument> ConvertedArgs) {
|
|
|
|
return new (C, DC,
|
|
|
|
additionalSizeToAlloc<TemplateArgument>(ConvertedArgs.size()))
|
|
|
|
ImplicitConceptSpecializationDecl(DC, SL, ConvertedArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImplicitConceptSpecializationDecl *
|
|
|
|
ImplicitConceptSpecializationDecl::CreateDeserialized(
|
2024-04-25 11:43:13 +08:00
|
|
|
const ASTContext &C, GlobalDeclID ID, unsigned NumTemplateArgs) {
|
2022-10-24 12:20:36 -07:00
|
|
|
return new (C, ID, additionalSizeToAlloc<TemplateArgument>(NumTemplateArgs))
|
|
|
|
ImplicitConceptSpecializationDecl(EmptyShell{}, NumTemplateArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImplicitConceptSpecializationDecl::setTemplateArguments(
|
|
|
|
ArrayRef<TemplateArgument> Converted) {
|
|
|
|
assert(Converted.size() == NumTemplateArgs);
|
|
|
|
std::uninitialized_copy(Converted.begin(), Converted.end(),
|
|
|
|
getTrailingObjects<TemplateArgument>());
|
|
|
|
}
|
|
|
|
|
2009-05-31 09:31:02 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ClassTemplatePartialSpecializationDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-29 22:39:22 +00:00
|
|
|
void ClassTemplatePartialSpecializationDecl::anchor() {}
|
2011-12-20 02:48:34 +00:00
|
|
|
|
2024-05-14 16:09:57 -04:00
|
|
|
ClassTemplatePartialSpecializationDecl::ClassTemplatePartialSpecializationDecl(
|
|
|
|
ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation StartLoc,
|
|
|
|
SourceLocation IdLoc, TemplateParameterList *Params,
|
|
|
|
ClassTemplateDecl *SpecializedTemplate, ArrayRef<TemplateArgument> Args,
|
|
|
|
ClassTemplatePartialSpecializationDecl *PrevDecl)
|
|
|
|
: ClassTemplateSpecializationDecl(
|
|
|
|
Context, ClassTemplatePartialSpecialization, TK, DC, StartLoc, IdLoc,
|
2025-02-05 12:02:24 -03:00
|
|
|
// Tracking MatchedPackOnParmToNonPackOnArg for Partial
|
|
|
|
// Specializations is not needed.
|
|
|
|
SpecializedTemplate, Args, /*MatchedPackOnParmToNonPackOnArg=*/false,
|
|
|
|
PrevDecl),
|
2024-05-14 16:09:57 -04:00
|
|
|
TemplateParams(Params), InstantiatedFromMember(nullptr, false) {
|
2021-09-24 22:18:54 +02:00
|
|
|
if (AdoptTemplateParameterList(Params, this))
|
|
|
|
setInvalidDecl();
|
2011-03-04 17:52:15 +00:00
|
|
|
}
|
|
|
|
|
2009-05-31 09:31:02 +00:00
|
|
|
ClassTemplatePartialSpecializationDecl *
|
2024-05-14 16:09:57 -04:00
|
|
|
ClassTemplatePartialSpecializationDecl::Create(
|
|
|
|
ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation StartLoc,
|
|
|
|
SourceLocation IdLoc, TemplateParameterList *Params,
|
|
|
|
ClassTemplateDecl *SpecializedTemplate, ArrayRef<TemplateArgument> Args,
|
|
|
|
QualType CanonInjectedType,
|
|
|
|
ClassTemplatePartialSpecializationDecl *PrevDecl) {
|
|
|
|
auto *Result = new (Context, DC) ClassTemplatePartialSpecializationDecl(
|
|
|
|
Context, TK, DC, StartLoc, IdLoc, Params, SpecializedTemplate, Args,
|
|
|
|
PrevDecl);
|
2009-05-31 09:31:02 +00:00
|
|
|
Result->setSpecializationKind(TSK_ExplicitSpecialization);
|
2018-08-01 20:48:16 +00:00
|
|
|
Result->setMayHaveOutOfDateDef(false);
|
2010-03-10 03:28:59 +00:00
|
|
|
|
|
|
|
Context.getInjectedClassNameType(Result, CanonInjectedType);
|
2009-05-31 09:31:02 +00:00
|
|
|
return Result;
|
|
|
|
}
|
2009-09-16 22:47:08 +00:00
|
|
|
|
2010-06-23 13:48:23 +00:00
|
|
|
ClassTemplatePartialSpecializationDecl *
|
2012-01-05 21:55:30 +00:00
|
|
|
ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
|
2024-04-25 11:43:13 +08:00
|
|
|
GlobalDeclID ID) {
|
2018-03-29 20:51:59 +00:00
|
|
|
auto *Result = new (C, ID) ClassTemplatePartialSpecializationDecl(C);
|
2018-08-01 20:48:16 +00:00
|
|
|
Result->setMayHaveOutOfDateDef(false);
|
Ensure that type definitions present in just-loaded modules are
visible.
The basic problem here is that a given translation unit can use
forward declarations to form pointers to a given type, say,
class X;
X *x;
and then import a module that includes a definition of X:
import XDef;
We will then fail when attempting to access a member of X, e.g.,
x->method()
because the AST reader did not know to look for a default of a class
named X within the new module.
This implementation is a bit of a C-centric hack, because the only
definitions that can have this property are enums, structs, unions,
Objective-C classes, and Objective-C protocols, and all of those are
either visible at the top-level or can't be defined later. Hence, we
can use the out-of-date-ness of the name and the identifier-update
mechanism to force the update.
In C++, we will not be so lucky, and will need a more advanced
solution, because the definitions could be in namespaces defined in
two different modules, e.g.,
// module 1
namespace N { struct X; }
// module 2
namespace N { struct X { /* ... */ }; }
One possible implementation here is for C++ to extend the information
associated with each identifier table to include the declaration IDs
of any definitions associated with that name, regardless of
context. We would have to eagerly load those definitions.
llvm-svn: 174794
2013-02-09 01:35:03 +00:00
|
|
|
return Result;
|
2010-06-23 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 16:09:57 -04:00
|
|
|
SourceRange ClassTemplatePartialSpecializationDecl::getSourceRange() const {
|
|
|
|
if (const ClassTemplatePartialSpecializationDecl *MT =
|
|
|
|
getInstantiatedFromMember();
|
|
|
|
MT && !isMemberSpecialization())
|
|
|
|
return MT->getSourceRange();
|
|
|
|
SourceRange Range = ClassTemplateSpecializationDecl::getSourceRange();
|
|
|
|
if (const TemplateParameterList *TPL = getTemplateParameters();
|
|
|
|
TPL && !getNumTemplateParameterLists())
|
|
|
|
Range.setBegin(TPL->getTemplateLoc());
|
|
|
|
return Range;
|
|
|
|
}
|
|
|
|
|
2009-09-16 22:47:08 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FriendTemplateDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-11-29 22:39:22 +00:00
|
|
|
void FriendTemplateDecl::anchor() {}
|
2011-12-20 02:48:34 +00:00
|
|
|
|
2016-07-06 04:19:16 +00:00
|
|
|
FriendTemplateDecl *
|
|
|
|
FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC,
|
|
|
|
SourceLocation L,
|
|
|
|
MutableArrayRef<TemplateParameterList *> Params,
|
|
|
|
FriendUnion Friend, SourceLocation FLoc) {
|
2022-02-17 17:18:43 +01:00
|
|
|
TemplateParameterList **TPL = nullptr;
|
|
|
|
if (!Params.empty()) {
|
|
|
|
TPL = new (Context) TemplateParameterList *[Params.size()];
|
|
|
|
llvm::copy(Params, TPL);
|
|
|
|
}
|
|
|
|
return new (Context, DC)
|
|
|
|
FriendTemplateDecl(DC, L, TPL, Params.size(), Friend, FLoc);
|
2009-09-16 22:47:08 +00:00
|
|
|
}
|
2010-07-22 16:04:10 +00:00
|
|
|
|
2012-01-05 21:55:30 +00:00
|
|
|
FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
|
2024-04-25 11:43:13 +08:00
|
|
|
GlobalDeclID ID) {
|
2013-11-22 09:01:48 +00:00
|
|
|
return new (C, ID) FriendTemplateDecl(EmptyShell());
|
2010-07-22 16:04:10 +00:00
|
|
|
}
|
2011-05-05 21:57:07 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TypeAliasTemplateDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-09-24 22:18:54 +02:00
|
|
|
TypeAliasTemplateDecl *
|
|
|
|
TypeAliasTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
|
|
|
|
DeclarationName Name,
|
|
|
|
TemplateParameterList *Params, NamedDecl *Decl) {
|
|
|
|
bool Invalid = AdoptTemplateParameterList(Params, DC);
|
|
|
|
auto *TD = new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl);
|
|
|
|
if (Invalid)
|
|
|
|
TD->setInvalidDecl();
|
|
|
|
return TD;
|
2011-05-05 21:57:07 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 11:43:13 +08:00
|
|
|
TypeAliasTemplateDecl *
|
|
|
|
TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(),
|
2014-05-12 05:36:57 +00:00
|
|
|
DeclarationName(), nullptr, nullptr);
|
2011-05-05 21:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RedeclarableTemplateDecl::CommonBase *
|
2013-01-23 16:52:57 +00:00
|
|
|
TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
|
2018-03-29 20:51:59 +00:00
|
|
|
auto *CommonPtr = new (C) Common;
|
2017-02-14 05:37:36 +00:00
|
|
|
C.addDestruction(CommonPtr);
|
2011-05-05 21:57:07 +00:00
|
|
|
return CommonPtr;
|
|
|
|
}
|
|
|
|
|
2013-08-06 01:03:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// VarTemplateDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-08-13 02:02:26 +00:00
|
|
|
VarTemplateDecl *VarTemplateDecl::getDefinition() {
|
|
|
|
VarTemplateDecl *CurD = this;
|
|
|
|
while (CurD) {
|
|
|
|
if (CurD->isThisDeclarationADefinition())
|
|
|
|
return CurD;
|
|
|
|
CurD = CurD->getPreviousDecl();
|
|
|
|
}
|
2014-05-12 05:36:57 +00:00
|
|
|
return nullptr;
|
2013-08-13 02:02:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 01:03:05 +00:00
|
|
|
VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
|
|
|
|
SourceLocation L, DeclarationName Name,
|
|
|
|
TemplateParameterList *Params,
|
2014-01-16 23:39:20 +00:00
|
|
|
VarDecl *Decl) {
|
2021-09-24 22:18:54 +02:00
|
|
|
bool Invalid = AdoptTemplateParameterList(Params, DC);
|
|
|
|
auto *TD = new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl);
|
|
|
|
if (Invalid)
|
|
|
|
TD->setInvalidDecl();
|
|
|
|
return TD;
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 11:43:13 +08:00
|
|
|
VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
|
|
|
|
GlobalDeclID ID) {
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(),
|
|
|
|
DeclarationName(), nullptr, nullptr);
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
2024-12-11 09:40:47 +08:00
|
|
|
void VarTemplateDecl::LoadLazySpecializations(
|
|
|
|
bool OnlyPartial /*=false*/) const {
|
|
|
|
loadLazySpecializationsImpl(OnlyPartial);
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
|
|
|
|
VarTemplateDecl::getSpecializations() const {
|
|
|
|
LoadLazySpecializations();
|
|
|
|
return getCommonPtr()->Specializations;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
|
2020-10-19 09:44:29 +02:00
|
|
|
VarTemplateDecl::getPartialSpecializations() const {
|
2024-12-11 09:40:47 +08:00
|
|
|
LoadLazySpecializations(/*PartialOnly = */ true);
|
2013-08-06 01:03:05 +00:00
|
|
|
return getCommonPtr()->PartialSpecializations;
|
|
|
|
}
|
|
|
|
|
|
|
|
RedeclarableTemplateDecl::CommonBase *
|
|
|
|
VarTemplateDecl::newCommon(ASTContext &C) const {
|
2018-03-29 20:51:59 +00:00
|
|
|
auto *CommonPtr = new (C) Common;
|
2017-02-14 05:37:36 +00:00
|
|
|
C.addDestruction(CommonPtr);
|
2013-08-06 01:03:05 +00:00
|
|
|
return CommonPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
VarTemplateSpecializationDecl *
|
2014-06-26 04:58:53 +00:00
|
|
|
VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
|
|
|
|
void *&InsertPos) {
|
2024-12-11 09:40:47 +08:00
|
|
|
auto *Common = getCommonPtr();
|
|
|
|
return findSpecializationImpl(Common->Specializations, InsertPos, Args);
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
|
|
|
|
void *InsertPos) {
|
2024-12-11 09:40:47 +08:00
|
|
|
auto *Common = getCommonPtr();
|
|
|
|
addSpecializationImpl<VarTemplateDecl>(Common->Specializations, D, InsertPos);
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VarTemplatePartialSpecializationDecl *
|
2014-06-26 04:58:53 +00:00
|
|
|
VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
|
2019-12-23 08:37:35 +02:00
|
|
|
TemplateParameterList *TPL, void *&InsertPos) {
|
|
|
|
return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args,
|
|
|
|
TPL);
|
|
|
|
}
|
|
|
|
|
2023-03-30 11:42:14 -07:00
|
|
|
void VarTemplatePartialSpecializationDecl::Profile(
|
|
|
|
llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
|
|
|
|
TemplateParameterList *TPL, const ASTContext &Context) {
|
2019-12-23 08:37:35 +02:00
|
|
|
ID.AddInteger(TemplateArgs.size());
|
|
|
|
for (const TemplateArgument &TemplateArg : TemplateArgs)
|
|
|
|
TemplateArg.Profile(ID, Context);
|
2023-03-30 11:42:14 -07:00
|
|
|
TPL->Profile(ID, Context);
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VarTemplateDecl::AddPartialSpecialization(
|
|
|
|
VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
|
|
|
|
if (InsertPos)
|
|
|
|
getPartialSpecializations().InsertNode(D, InsertPos);
|
|
|
|
else {
|
|
|
|
VarTemplatePartialSpecializationDecl *Existing =
|
|
|
|
getPartialSpecializations().GetOrInsertNode(D);
|
|
|
|
(void)Existing;
|
|
|
|
assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ASTMutationListener *L = getASTMutationListener())
|
|
|
|
L->AddedCXXTemplateSpecialization(this, D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VarTemplateDecl::getPartialSpecializations(
|
2020-10-19 09:44:29 +02:00
|
|
|
SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const {
|
2013-08-06 01:03:05 +00:00
|
|
|
llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
|
|
|
|
getPartialSpecializations();
|
|
|
|
PS.clear();
|
2013-08-22 23:27:37 +00:00
|
|
|
PS.reserve(PartialSpecs.size());
|
2016-07-06 04:19:16 +00:00
|
|
|
for (VarTemplatePartialSpecializationDecl &P : PartialSpecs)
|
|
|
|
PS.push_back(P.getMostRecentDecl());
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VarTemplatePartialSpecializationDecl *
|
|
|
|
VarTemplateDecl::findPartialSpecInstantiatedFromMember(
|
|
|
|
VarTemplatePartialSpecializationDecl *D) {
|
|
|
|
Decl *DCanon = D->getCanonicalDecl();
|
2016-07-06 04:19:16 +00:00
|
|
|
for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
|
|
|
|
if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
|
|
|
|
return P.getMostRecentDecl();
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
2014-05-12 05:36:57 +00:00
|
|
|
return nullptr;
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// VarTemplateSpecializationDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-29 22:39:22 +00:00
|
|
|
|
2013-08-06 01:03:05 +00:00
|
|
|
VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
|
2013-08-06 01:03:05 +00:00
|
|
|
SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
|
2016-07-03 21:17:51 +00:00
|
|
|
TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args)
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
: VarDecl(DK, Context, DC, StartLoc, IdLoc,
|
|
|
|
SpecializedTemplate->getIdentifier(), T, TInfo, S),
|
2017-11-29 22:39:22 +00:00
|
|
|
SpecializedTemplate(SpecializedTemplate),
|
2016-07-03 21:17:51 +00:00
|
|
|
TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
|
2017-12-02 02:48:42 +00:00
|
|
|
SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
|
2013-08-06 01:03:05 +00:00
|
|
|
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK,
|
|
|
|
ASTContext &C)
|
|
|
|
: VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
|
2021-01-04 23:17:45 +01:00
|
|
|
QualType(), nullptr, SC_None),
|
2017-12-02 02:48:42 +00:00
|
|
|
SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
|
2013-08-06 01:03:05 +00:00
|
|
|
|
|
|
|
VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create(
|
|
|
|
ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
|
|
|
|
SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
|
2016-07-03 21:17:51 +00:00
|
|
|
TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) {
|
2013-11-22 09:01:48 +00:00
|
|
|
return new (Context, DC) VarTemplateSpecializationDecl(
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
VarTemplateSpecialization, Context, DC, StartLoc, IdLoc,
|
2016-07-03 21:17:51 +00:00
|
|
|
SpecializedTemplate, T, TInfo, S, Args);
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VarTemplateSpecializationDecl *
|
2024-04-25 11:43:13 +08:00
|
|
|
VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
|
|
|
|
GlobalDeclID ID) {
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
return new (C, ID)
|
|
|
|
VarTemplateSpecializationDecl(VarTemplateSpecialization, C);
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VarTemplateSpecializationDecl::getNameForDiagnostic(
|
|
|
|
raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
|
|
|
|
NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
|
|
|
|
|
2018-03-29 20:51:59 +00:00
|
|
|
const auto *PS = dyn_cast<VarTemplatePartialSpecializationDecl>(this);
|
2016-12-24 04:09:05 +00:00
|
|
|
if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
|
|
|
|
PS ? PS->getTemplateArgsAsWritten() : nullptr) {
|
2020-11-11 13:04:35 -08:00
|
|
|
printTemplateArgumentList(
|
|
|
|
OS, ArgsAsWritten->arguments(), Policy,
|
|
|
|
getSpecializedTemplate()->getTemplateParameters());
|
2016-12-24 04:09:05 +00:00
|
|
|
} else {
|
|
|
|
const TemplateArgumentList &TemplateArgs = getTemplateArgs();
|
2020-11-11 13:04:35 -08:00
|
|
|
printTemplateArgumentList(
|
|
|
|
OS, TemplateArgs.asArray(), Policy,
|
|
|
|
getSpecializedTemplate()->getTemplateParameters());
|
2016-12-24 04:09:05 +00:00
|
|
|
}
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
|
2018-03-29 20:51:59 +00:00
|
|
|
if (const auto *PartialSpec =
|
2013-08-06 01:03:05 +00:00
|
|
|
SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
|
|
|
|
return PartialSpec->PartialSpecialization->getSpecializedTemplate();
|
2024-11-24 07:28:20 -08:00
|
|
|
return cast<VarTemplateDecl *>(SpecializedTemplate);
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-27 10:13:20 +02:00
|
|
|
SourceRange VarTemplateSpecializationDecl::getSourceRange() const {
|
2024-05-14 16:09:57 -04:00
|
|
|
switch (getSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ImplicitInstantiation: {
|
|
|
|
llvm::PointerUnion<VarTemplateDecl *,
|
|
|
|
VarTemplatePartialSpecializationDecl *>
|
|
|
|
Pattern = getSpecializedTemplateOrPartial();
|
|
|
|
assert(!Pattern.isNull() &&
|
|
|
|
"Variable template specialization without pattern?");
|
|
|
|
if (const auto *VTPSD =
|
2025-01-26 01:34:41 -08:00
|
|
|
dyn_cast<VarTemplatePartialSpecializationDecl *>(Pattern))
|
2024-05-14 16:09:57 -04:00
|
|
|
return VTPSD->getSourceRange();
|
2024-11-24 07:28:20 -08:00
|
|
|
VarTemplateDecl *VTD = cast<VarTemplateDecl *>(Pattern);
|
2024-05-14 16:09:57 -04:00
|
|
|
if (hasInit()) {
|
|
|
|
if (VarTemplateDecl *Definition = VTD->getDefinition())
|
|
|
|
return Definition->getSourceRange();
|
|
|
|
}
|
|
|
|
return VTD->getCanonicalDecl()->getSourceRange();
|
|
|
|
}
|
|
|
|
case TSK_ExplicitSpecialization: {
|
|
|
|
SourceRange Range = VarDecl::getSourceRange();
|
|
|
|
if (const ASTTemplateArgumentListInfo *Args = getTemplateArgsAsWritten();
|
|
|
|
!hasInit() && Args)
|
|
|
|
Range.setEnd(Args->getRAngleLoc());
|
|
|
|
return Range;
|
|
|
|
}
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
|
|
|
case TSK_ExplicitInstantiationDefinition: {
|
|
|
|
SourceRange Range = VarDecl::getSourceRange();
|
|
|
|
if (SourceLocation ExternKW = getExternKeywordLoc(); ExternKW.isValid())
|
|
|
|
Range.setBegin(ExternKW);
|
|
|
|
else if (SourceLocation TemplateKW = getTemplateKeywordLoc();
|
|
|
|
TemplateKW.isValid())
|
|
|
|
Range.setBegin(TemplateKW);
|
|
|
|
if (const ASTTemplateArgumentListInfo *Args = getTemplateArgsAsWritten())
|
|
|
|
Range.setEnd(Args->getRAngleLoc());
|
|
|
|
return Range;
|
|
|
|
}
|
2023-03-27 10:13:20 +02:00
|
|
|
}
|
2024-05-14 16:09:57 -04:00
|
|
|
llvm_unreachable("unhandled template specialization kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
void VarTemplateSpecializationDecl::setExternKeywordLoc(SourceLocation Loc) {
|
2025-01-28 10:56:09 -08:00
|
|
|
auto *Info = dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo);
|
2024-05-14 16:09:57 -04:00
|
|
|
if (!Info) {
|
|
|
|
// Don't allocate if the location is invalid.
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return;
|
|
|
|
Info = new (getASTContext()) ExplicitInstantiationInfo;
|
|
|
|
Info->TemplateArgsAsWritten = getTemplateArgsAsWritten();
|
|
|
|
ExplicitInfo = Info;
|
|
|
|
}
|
|
|
|
Info->ExternKeywordLoc = Loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VarTemplateSpecializationDecl::setTemplateKeywordLoc(SourceLocation Loc) {
|
2025-01-28 10:56:09 -08:00
|
|
|
auto *Info = dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo);
|
2024-05-14 16:09:57 -04:00
|
|
|
if (!Info) {
|
|
|
|
// Don't allocate if the location is invalid.
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return;
|
|
|
|
Info = new (getASTContext()) ExplicitInstantiationInfo;
|
|
|
|
Info->TemplateArgsAsWritten = getTemplateArgsAsWritten();
|
|
|
|
ExplicitInfo = Info;
|
|
|
|
}
|
|
|
|
Info->TemplateKeywordLoc = Loc;
|
2023-03-27 10:13:20 +02:00
|
|
|
}
|
|
|
|
|
2013-08-06 01:03:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// VarTemplatePartialSpecializationDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-29 22:39:22 +00:00
|
|
|
|
2013-08-06 01:03:05 +00:00
|
|
|
void VarTemplatePartialSpecializationDecl::anchor() {}
|
|
|
|
|
|
|
|
VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
|
|
|
|
ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
|
|
|
|
SourceLocation IdLoc, TemplateParameterList *Params,
|
|
|
|
VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
|
2024-05-14 16:09:57 -04:00
|
|
|
StorageClass S, ArrayRef<TemplateArgument> Args)
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
: VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context,
|
2013-08-06 01:03:05 +00:00
|
|
|
DC, StartLoc, IdLoc, SpecializedTemplate, T,
|
2016-07-03 21:17:51 +00:00
|
|
|
TInfo, S, Args),
|
2024-05-14 16:09:57 -04:00
|
|
|
TemplateParams(Params), InstantiatedFromMember(nullptr, false) {
|
2021-09-24 22:18:54 +02:00
|
|
|
if (AdoptTemplateParameterList(Params, DC))
|
|
|
|
setInvalidDecl();
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VarTemplatePartialSpecializationDecl *
|
|
|
|
VarTemplatePartialSpecializationDecl::Create(
|
|
|
|
ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
|
|
|
|
SourceLocation IdLoc, TemplateParameterList *Params,
|
|
|
|
VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
|
2024-05-14 16:09:57 -04:00
|
|
|
StorageClass S, ArrayRef<TemplateArgument> Args) {
|
|
|
|
auto *Result = new (Context, DC) VarTemplatePartialSpecializationDecl(
|
|
|
|
Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo, S,
|
|
|
|
Args);
|
2013-08-06 01:03:05 +00:00
|
|
|
Result->setSpecializationKind(TSK_ExplicitSpecialization);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
VarTemplatePartialSpecializationDecl *
|
|
|
|
VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
|
2024-04-25 11:43:13 +08:00
|
|
|
GlobalDeclID ID) {
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-16 23:01:30 +00:00
|
|
|
return new (C, ID) VarTemplatePartialSpecializationDecl(C);
|
2013-08-06 01:03:05 +00:00
|
|
|
}
|
2015-11-04 03:40:30 +00:00
|
|
|
|
2023-03-27 10:13:20 +02:00
|
|
|
SourceRange VarTemplatePartialSpecializationDecl::getSourceRange() const {
|
2024-05-14 16:09:57 -04:00
|
|
|
if (const VarTemplatePartialSpecializationDecl *MT =
|
|
|
|
getInstantiatedFromMember();
|
|
|
|
MT && !isMemberSpecialization())
|
|
|
|
return MT->getSourceRange();
|
|
|
|
SourceRange Range = VarTemplateSpecializationDecl::getSourceRange();
|
|
|
|
if (const TemplateParameterList *TPL = getTemplateParameters();
|
|
|
|
TPL && !getNumTemplateParameterLists())
|
|
|
|
Range.setBegin(TPL->getTemplateLoc());
|
|
|
|
return Range;
|
2023-03-27 10:13:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-04 03:40:30 +00:00
|
|
|
static TemplateParameterList *
|
|
|
|
createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
|
|
|
|
// typename T
|
|
|
|
auto *T = TemplateTypeParmDecl::Create(
|
|
|
|
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0,
|
2020-01-15 02:48:42 +02:00
|
|
|
/*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
|
|
|
|
/*HasTypeConstraint=*/false);
|
2015-11-04 03:40:30 +00:00
|
|
|
T->setImplicit(true);
|
|
|
|
|
|
|
|
// T ...Ints
|
|
|
|
TypeSourceInfo *TI =
|
|
|
|
C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0));
|
|
|
|
auto *N = NonTypeTemplateParmDecl::Create(
|
|
|
|
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
|
|
|
|
/*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI);
|
|
|
|
N->setImplicit(true);
|
|
|
|
|
|
|
|
// <typename T, T ...Ints>
|
|
|
|
NamedDecl *P[2] = {T, N};
|
|
|
|
auto *TPL = TemplateParameterList::Create(
|
2016-07-30 22:33:34 +00:00
|
|
|
C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr);
|
2015-11-04 03:40:30 +00:00
|
|
|
|
|
|
|
// template <typename T, ...Ints> class IntSeq
|
|
|
|
auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create(
|
|
|
|
C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
|
2024-04-11 13:20:05 -04:00
|
|
|
/*ParameterPack=*/false, /*Id=*/nullptr, /*Typename=*/false, TPL);
|
2015-11-04 03:40:30 +00:00
|
|
|
TemplateTemplateParm->setImplicit(true);
|
|
|
|
|
|
|
|
// typename T
|
|
|
|
auto *TemplateTypeParm = TemplateTypeParmDecl::Create(
|
|
|
|
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
|
2020-01-15 02:48:42 +02:00
|
|
|
/*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
|
|
|
|
/*HasTypeConstraint=*/false);
|
2015-11-04 03:40:30 +00:00
|
|
|
TemplateTypeParm->setImplicit(true);
|
|
|
|
|
|
|
|
// T N
|
|
|
|
TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(
|
|
|
|
QualType(TemplateTypeParm->getTypeForDecl(), 0));
|
|
|
|
auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create(
|
|
|
|
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2,
|
|
|
|
/*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
|
|
|
|
NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm,
|
|
|
|
NonTypeTemplateParm};
|
|
|
|
|
|
|
|
// template <template <typename T, T ...Ints> class IntSeq, typename T, T N>
|
|
|
|
return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
|
2016-07-30 22:33:34 +00:00
|
|
|
Params, SourceLocation(), nullptr);
|
2015-11-04 03:40:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-01 01:24:09 +00:00
|
|
|
static TemplateParameterList *
|
|
|
|
createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) {
|
|
|
|
// std::size_t Index
|
|
|
|
TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType());
|
|
|
|
auto *Index = NonTypeTemplateParmDecl::Create(
|
|
|
|
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0,
|
|
|
|
/*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
|
|
|
|
|
|
|
|
// typename ...T
|
|
|
|
auto *Ts = TemplateTypeParmDecl::Create(
|
|
|
|
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
|
2020-01-15 02:48:42 +02:00
|
|
|
/*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true,
|
|
|
|
/*HasTypeConstraint=*/false);
|
2016-07-01 01:24:09 +00:00
|
|
|
Ts->setImplicit(true);
|
|
|
|
|
|
|
|
// template <std::size_t Index, typename ...T>
|
|
|
|
NamedDecl *Params[] = {Index, Ts};
|
|
|
|
return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
|
2023-01-06 16:56:23 +01:00
|
|
|
llvm::ArrayRef(Params), SourceLocation(),
|
|
|
|
nullptr);
|
2016-07-01 01:24:09 +00:00
|
|
|
}
|
|
|
|
|
2024-09-22 09:25:52 +02:00
|
|
|
static TemplateParameterList *createBuiltinCommonTypeList(const ASTContext &C,
|
|
|
|
DeclContext *DC) {
|
|
|
|
// class... Args
|
|
|
|
auto *Args =
|
|
|
|
TemplateTypeParmDecl::Create(C, DC, SourceLocation(), SourceLocation(),
|
|
|
|
/*Depth=*/1, /*Position=*/0, /*Id=*/nullptr,
|
|
|
|
/*Typename=*/false, /*ParameterPack=*/true);
|
|
|
|
|
|
|
|
// <class... Args>
|
|
|
|
auto *BaseTemplateList = TemplateParameterList::Create(
|
|
|
|
C, SourceLocation(), SourceLocation(), Args, SourceLocation(), nullptr);
|
|
|
|
|
|
|
|
// template <class... Args> class BaseTemplate
|
|
|
|
auto *BaseTemplate = TemplateTemplateParmDecl::Create(
|
|
|
|
C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
|
|
|
|
/*ParameterPack=*/false, /*Id=*/nullptr,
|
|
|
|
/*Typename=*/false, BaseTemplateList);
|
|
|
|
|
|
|
|
// class TypeMember
|
|
|
|
auto *TypeMember =
|
|
|
|
TemplateTypeParmDecl::Create(C, DC, SourceLocation(), SourceLocation(),
|
|
|
|
/*Depth=*/1, /*Position=*/0, /*Id=*/nullptr,
|
|
|
|
/*Typename=*/false, /*ParameterPack=*/false);
|
|
|
|
|
|
|
|
// <class TypeMember>
|
|
|
|
auto *HasTypeMemberList =
|
|
|
|
TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
|
|
|
|
TypeMember, SourceLocation(), nullptr);
|
|
|
|
|
|
|
|
// template <class TypeMember> class HasTypeMember
|
|
|
|
auto *HasTypeMember = TemplateTemplateParmDecl::Create(
|
|
|
|
C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/1,
|
|
|
|
/*ParameterPack=*/false, /*Id=*/nullptr,
|
|
|
|
/*Typename=*/false, HasTypeMemberList);
|
|
|
|
|
|
|
|
// class HasNoTypeMember
|
|
|
|
auto *HasNoTypeMember = TemplateTypeParmDecl::Create(
|
|
|
|
C, DC, {}, {}, /*Depth=*/0, /*Position=*/2, /*Id=*/nullptr,
|
|
|
|
/*Typename=*/false, /*ParameterPack=*/false);
|
|
|
|
|
|
|
|
// class... Ts
|
|
|
|
auto *Ts = TemplateTypeParmDecl::Create(
|
|
|
|
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/3,
|
|
|
|
/*Id=*/nullptr, /*Typename=*/false, /*ParameterPack=*/true);
|
|
|
|
|
|
|
|
// template <template <class... Args> class BaseTemplate,
|
|
|
|
// template <class TypeMember> class HasTypeMember, class HasNoTypeMember,
|
|
|
|
// class... Ts>
|
|
|
|
return TemplateParameterList::Create(
|
|
|
|
C, SourceLocation(), SourceLocation(),
|
|
|
|
{BaseTemplate, HasTypeMember, HasNoTypeMember, Ts}, SourceLocation(),
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
2015-11-04 03:40:30 +00:00
|
|
|
static TemplateParameterList *createBuiltinTemplateParameterList(
|
|
|
|
const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
|
|
|
|
switch (BTK) {
|
|
|
|
case BTK__make_integer_seq:
|
|
|
|
return createMakeIntegerSeqParameterList(C, DC);
|
2016-07-01 01:24:09 +00:00
|
|
|
case BTK__type_pack_element:
|
|
|
|
return createTypePackElementParameterList(C, DC);
|
2024-09-22 09:25:52 +02:00
|
|
|
case BTK__builtin_common_type:
|
|
|
|
return createBuiltinCommonTypeList(C, DC);
|
2015-11-04 03:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("unhandled BuiltinTemplateKind!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void BuiltinTemplateDecl::anchor() {}
|
|
|
|
|
|
|
|
BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC,
|
|
|
|
DeclarationName Name,
|
|
|
|
BuiltinTemplateKind BTK)
|
|
|
|
: TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name,
|
|
|
|
createBuiltinTemplateParameterList(C, DC, BTK)),
|
|
|
|
BTK(BTK) {}
|
2020-01-15 02:48:42 +02:00
|
|
|
|
2020-09-20 23:16:08 -07:00
|
|
|
TemplateParamObjectDecl *TemplateParamObjectDecl::Create(const ASTContext &C,
|
|
|
|
QualType T,
|
|
|
|
const APValue &V) {
|
|
|
|
DeclContext *DC = C.getTranslationUnitDecl();
|
|
|
|
auto *TPOD = new (C, DC) TemplateParamObjectDecl(DC, T, V);
|
|
|
|
C.addDestruction(&TPOD->Value);
|
|
|
|
return TPOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
TemplateParamObjectDecl *
|
2024-04-25 11:43:13 +08:00
|
|
|
TemplateParamObjectDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
|
2020-09-20 23:16:08 -07:00
|
|
|
auto *TPOD = new (C, ID) TemplateParamObjectDecl(nullptr, QualType(), APValue());
|
|
|
|
C.addDestruction(&TPOD->Value);
|
|
|
|
return TPOD;
|
|
|
|
}
|
|
|
|
|
2022-10-14 08:17:16 -04:00
|
|
|
void TemplateParamObjectDecl::printName(llvm::raw_ostream &OS,
|
|
|
|
const PrintingPolicy &Policy) const {
|
2020-09-20 23:16:08 -07:00
|
|
|
OS << "<template param ";
|
2022-10-14 08:17:16 -04:00
|
|
|
printAsExpr(OS, Policy);
|
2020-09-20 23:16:08 -07:00
|
|
|
OS << ">";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplateParamObjectDecl::printAsExpr(llvm::raw_ostream &OS) const {
|
2022-03-01 19:33:43 -06:00
|
|
|
printAsExpr(OS, getASTContext().getPrintingPolicy());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplateParamObjectDecl::printAsExpr(llvm::raw_ostream &OS,
|
|
|
|
const PrintingPolicy &Policy) const {
|
|
|
|
getType().getUnqualifiedType().print(OS, Policy);
|
|
|
|
printAsInit(OS, Policy);
|
2020-09-20 23:16:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void TemplateParamObjectDecl::printAsInit(llvm::raw_ostream &OS) const {
|
2022-03-01 19:33:43 -06:00
|
|
|
printAsInit(OS, getASTContext().getPrintingPolicy());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplateParamObjectDecl::printAsInit(llvm::raw_ostream &OS,
|
|
|
|
const PrintingPolicy &Policy) const {
|
|
|
|
getValue().printPretty(OS, Policy, getType(), &getASTContext());
|
2020-09-20 23:16:08 -07:00
|
|
|
}
|
2022-08-14 13:48:18 +02:00
|
|
|
|
|
|
|
TemplateParameterList *clang::getReplacedTemplateParameterList(Decl *D) {
|
|
|
|
switch (D->getKind()) {
|
2024-01-31 12:17:41 +08:00
|
|
|
case Decl::Kind::CXXRecord:
|
|
|
|
return cast<CXXRecordDecl>(D)
|
|
|
|
->getDescribedTemplate()
|
|
|
|
->getTemplateParameters();
|
2022-08-14 13:48:18 +02:00
|
|
|
case Decl::Kind::ClassTemplate:
|
|
|
|
return cast<ClassTemplateDecl>(D)->getTemplateParameters();
|
|
|
|
case Decl::Kind::ClassTemplateSpecialization: {
|
|
|
|
const auto *CTSD = cast<ClassTemplateSpecializationDecl>(D);
|
|
|
|
auto P = CTSD->getSpecializedTemplateOrPartial();
|
|
|
|
if (const auto *CTPSD =
|
2025-01-25 01:15:38 -08:00
|
|
|
dyn_cast<ClassTemplatePartialSpecializationDecl *>(P))
|
2022-08-14 13:48:18 +02:00
|
|
|
return CTPSD->getTemplateParameters();
|
|
|
|
return cast<ClassTemplateDecl *>(P)->getTemplateParameters();
|
|
|
|
}
|
|
|
|
case Decl::Kind::ClassTemplatePartialSpecialization:
|
|
|
|
return cast<ClassTemplatePartialSpecializationDecl>(D)
|
|
|
|
->getTemplateParameters();
|
|
|
|
case Decl::Kind::TypeAliasTemplate:
|
|
|
|
return cast<TypeAliasTemplateDecl>(D)->getTemplateParameters();
|
|
|
|
case Decl::Kind::BuiltinTemplate:
|
|
|
|
return cast<BuiltinTemplateDecl>(D)->getTemplateParameters();
|
|
|
|
case Decl::Kind::CXXDeductionGuide:
|
|
|
|
case Decl::Kind::CXXConversion:
|
|
|
|
case Decl::Kind::CXXConstructor:
|
|
|
|
case Decl::Kind::CXXDestructor:
|
|
|
|
case Decl::Kind::CXXMethod:
|
|
|
|
case Decl::Kind::Function:
|
|
|
|
return cast<FunctionDecl>(D)
|
|
|
|
->getTemplateSpecializationInfo()
|
|
|
|
->getTemplate()
|
|
|
|
->getTemplateParameters();
|
|
|
|
case Decl::Kind::FunctionTemplate:
|
|
|
|
return cast<FunctionTemplateDecl>(D)->getTemplateParameters();
|
|
|
|
case Decl::Kind::VarTemplate:
|
|
|
|
return cast<VarTemplateDecl>(D)->getTemplateParameters();
|
|
|
|
case Decl::Kind::VarTemplateSpecialization: {
|
|
|
|
const auto *VTSD = cast<VarTemplateSpecializationDecl>(D);
|
|
|
|
auto P = VTSD->getSpecializedTemplateOrPartial();
|
2025-01-25 14:05:11 -08:00
|
|
|
if (const auto *VTPSD = dyn_cast<VarTemplatePartialSpecializationDecl *>(P))
|
2022-08-14 13:48:18 +02:00
|
|
|
return VTPSD->getTemplateParameters();
|
|
|
|
return cast<VarTemplateDecl *>(P)->getTemplateParameters();
|
|
|
|
}
|
|
|
|
case Decl::Kind::VarTemplatePartialSpecialization:
|
|
|
|
return cast<VarTemplatePartialSpecializationDecl>(D)
|
|
|
|
->getTemplateParameters();
|
|
|
|
case Decl::Kind::TemplateTemplateParm:
|
|
|
|
return cast<TemplateTemplateParmDecl>(D)->getTemplateParameters();
|
|
|
|
case Decl::Kind::Concept:
|
|
|
|
return cast<ConceptDecl>(D)->getTemplateParameters();
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unhandled templated declaration kind");
|
|
|
|
}
|
|
|
|
}
|