aboutsummaryrefslogtreecommitdiff
path: root/clang/lib
diff options
context:
space:
mode:
authorNikolas Klauser <nikolasklauser@berlin.de>2025-02-26 16:01:14 +0100
committerGitHub <noreply@github.com>2025-02-26 16:01:14 +0100
commit8dd8e5f7d692cc43f4322f04034f5c472381aa43 (patch)
tree410484b1a4eac639bb0cd92c78200f6a893de748 /clang/lib
parentdefe43bbffb0d25ec468f0e54b20548ec192ff90 (diff)
downloadllvm-8dd8e5f7d692cc43f4322f04034f5c472381aa43.zip
llvm-8dd8e5f7d692cc43f4322f04034f5c472381aa43.tar.gz
llvm-8dd8e5f7d692cc43f4322f04034f5c472381aa43.tar.bz2
[Clang] Add BuiltinTemplates.td to generate code for builtin templates (#123736)
This makes it significantly easier to add new builtin templates, since you only have to modify two places instead of a dozen or so. The `BuiltinTemplates.td` could also be extended to generate documentation from it in the future.
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/AST/ASTContext.cpp30
-rw-r--r--clang/lib/AST/ASTImporter.cpp12
-rw-r--r--clang/lib/AST/DeclTemplate.cpp133
-rw-r--r--clang/lib/Lex/PPMacroExpansion.cpp7
-rw-r--r--clang/lib/Sema/SemaLookup.cpp18
-rw-r--r--clang/lib/Serialization/ASTReader.cpp22
-rw-r--r--clang/lib/Serialization/ASTWriter.cpp8
7 files changed, 32 insertions, 198 deletions
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 4a79131..fce1c34 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -1198,28 +1198,14 @@ ASTContext::buildBuiltinTemplateDecl(BuiltinTemplateKind BTK,
return BuiltinTemplate;
}
-BuiltinTemplateDecl *
-ASTContext::getMakeIntegerSeqDecl() const {
- if (!MakeIntegerSeqDecl)
- MakeIntegerSeqDecl = buildBuiltinTemplateDecl(BTK__make_integer_seq,
- getMakeIntegerSeqName());
- return MakeIntegerSeqDecl;
-}
-
-BuiltinTemplateDecl *
-ASTContext::getTypePackElementDecl() const {
- if (!TypePackElementDecl)
- TypePackElementDecl = buildBuiltinTemplateDecl(BTK__type_pack_element,
- getTypePackElementName());
- return TypePackElementDecl;
-}
-
-BuiltinTemplateDecl *ASTContext::getBuiltinCommonTypeDecl() const {
- if (!BuiltinCommonTypeDecl)
- BuiltinCommonTypeDecl = buildBuiltinTemplateDecl(
- BTK__builtin_common_type, getBuiltinCommonTypeName());
- return BuiltinCommonTypeDecl;
-}
+#define BuiltinTemplate(BTName) \
+ BuiltinTemplateDecl *ASTContext::get##BTName##Decl() const { \
+ if (!Decl##BTName) \
+ Decl##BTName = \
+ buildBuiltinTemplateDecl(BTK##BTName, get##BTName##Name()); \
+ return Decl##BTName; \
+ }
+#include "clang/Basic/BuiltinTemplates.inc"
RecordDecl *ASTContext::buildImplicitRecord(StringRef Name,
RecordDecl::TagKind TK) const {
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 43da76e..8218048 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -5465,15 +5465,11 @@ ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
ExpectedDecl ASTNodeImporter::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
Decl* ToD = nullptr;
switch (D->getBuiltinTemplateKind()) {
- case BuiltinTemplateKind::BTK__make_integer_seq:
- ToD = Importer.getToContext().getMakeIntegerSeqDecl();
- break;
- case BuiltinTemplateKind::BTK__type_pack_element:
- ToD = Importer.getToContext().getTypePackElementDecl();
- break;
- case BuiltinTemplateKind::BTK__builtin_common_type:
- ToD = Importer.getToContext().getBuiltinCommonTypeDecl();
+#define BuiltinTemplate(BTName) \
+ case BuiltinTemplateKind::BTK##BTName: \
+ ToD = Importer.getToContext().get##BTName##Decl(); \
break;
+#include "clang/Basic/BuiltinTemplates.inc"
}
assert(ToD && "BuiltinTemplateDecl of unsupported kind!");
Importer.MapImported(D, ToD);
diff --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp
index 63caf04..b8fe19c 100644
--- a/clang/lib/AST/DeclTemplate.cpp
+++ b/clang/lib/AST/DeclTemplate.cpp
@@ -1581,140 +1581,11 @@ SourceRange VarTemplatePartialSpecializationDecl::getSourceRange() const {
return Range;
}
-static TemplateParameterList *
-createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
- // typename T
- auto *T = TemplateTypeParmDecl::Create(
- C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0,
- /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
- /*HasTypeConstraint=*/false);
- 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(
- C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr);
-
- // template <typename T, ...Ints> class IntSeq
- auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create(
- C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
- /*ParameterPack=*/false, /*Id=*/nullptr, /*Typename=*/false, TPL);
- TemplateTemplateParm->setImplicit(true);
-
- // typename T
- auto *TemplateTypeParm = TemplateTypeParmDecl::Create(
- C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
- /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
- /*HasTypeConstraint=*/false);
- 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(),
- Params, SourceLocation(), nullptr);
-}
-
-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,
- /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true,
- /*HasTypeConstraint=*/false);
- Ts->setImplicit(true);
-
- // template <std::size_t Index, typename ...T>
- NamedDecl *Params[] = {Index, Ts};
- return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
- llvm::ArrayRef(Params), SourceLocation(),
- nullptr);
-}
-
-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);
-}
-
static TemplateParameterList *createBuiltinTemplateParameterList(
const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
switch (BTK) {
- case BTK__make_integer_seq:
- return createMakeIntegerSeqParameterList(C, DC);
- case BTK__type_pack_element:
- return createTypePackElementParameterList(C, DC);
- case BTK__builtin_common_type:
- return createBuiltinCommonTypeList(C, DC);
+#define CREATE_BUILTIN_TEMPLATE_PARAMETER_LIST
+#include "clang/Basic/BuiltinTemplates.inc"
}
llvm_unreachable("unhandled BuiltinTemplateKind!");
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index 944966a..8e35d56 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1831,10 +1831,9 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
return true;
} else {
return llvm::StringSwitch<bool>(II->getName())
- // Report builtin templates as being builtins.
- .Case("__make_integer_seq", getLangOpts().CPlusPlus)
- .Case("__type_pack_element", getLangOpts().CPlusPlus)
- .Case("__builtin_common_type", getLangOpts().CPlusPlus)
+ // Report builtin templates as being builtins.
+#define BuiltinTemplate(BTName) .Case(#BTName, getLangOpts().CPlusPlus)
+#include "clang/Basic/BuiltinTemplates.inc"
// Likewise for some builtin preprocessor macros.
// FIXME: This is inconsistent; we usually suggest detecting
// builtin macros via #ifdef. Don't add more cases here.
diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index 0f5b742..aecf8ed 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -924,18 +924,12 @@ bool Sema::LookupBuiltin(LookupResult &R) {
IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo();
if (II) {
if (getLangOpts().CPlusPlus && NameKind == Sema::LookupOrdinaryName) {
- if (II == getASTContext().getMakeIntegerSeqName()) {
- R.addDecl(getASTContext().getMakeIntegerSeqDecl());
- return true;
- }
- if (II == getASTContext().getTypePackElementName()) {
- R.addDecl(getASTContext().getTypePackElementDecl());
- return true;
- }
- if (II == getASTContext().getBuiltinCommonTypeName()) {
- R.addDecl(getASTContext().getBuiltinCommonTypeDecl());
- return true;
- }
+#define BuiltinTemplate(BIName) \
+ if (II == getASTContext().get##BIName##Name()) { \
+ R.addDecl(getASTContext().get##BIName##Decl()); \
+ return true; \
+ }
+#include "clang/Basic/BuiltinTemplates.inc"
}
// Check if this is an OpenCL Builtin, and if so, insert its overloads.
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index 4a40df6..294e8e0 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -8114,12 +8114,6 @@ Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
NewLoaded = Context.getExternCContextDecl();
break;
- case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
- if (Context.MakeIntegerSeqDecl)
- return Context.MakeIntegerSeqDecl;
- NewLoaded = Context.getMakeIntegerSeqDecl();
- break;
-
case PREDEF_DECL_CF_CONSTANT_STRING_ID:
if (Context.CFConstantStringTypeDecl)
return Context.CFConstantStringTypeDecl;
@@ -8132,17 +8126,13 @@ Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
NewLoaded = Context.getCFConstantStringTagDecl();
break;
- case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
- if (Context.TypePackElementDecl)
- return Context.TypePackElementDecl;
- NewLoaded = Context.getTypePackElementDecl();
- break;
-
- case PREDEF_DECL_COMMON_TYPE_ID:
- if (Context.BuiltinCommonTypeDecl)
- return Context.BuiltinCommonTypeDecl;
- NewLoaded = Context.getBuiltinCommonTypeDecl();
+#define BuiltinTemplate(BTName) \
+ case PREDEF_DECL##BTName##_ID: \
+ if (Context.Decl##BTName) \
+ return Context.Decl##BTName; \
+ NewLoaded = Context.get##BTName##Decl(); \
break;
+#include "clang/Basic/BuiltinTemplates.inc"
case NUM_PREDEF_DECL_IDS:
llvm_unreachable("Invalid decl ID");
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index 79b777c..a01e72f 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -5539,15 +5539,13 @@ void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
RegisterPredefDecl(Context.MSGuidTagDecl,
PREDEF_DECL_BUILTIN_MS_GUID_ID);
RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
- RegisterPredefDecl(Context.MakeIntegerSeqDecl,
- PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
RegisterPredefDecl(Context.CFConstantStringTypeDecl,
PREDEF_DECL_CF_CONSTANT_STRING_ID);
RegisterPredefDecl(Context.CFConstantStringTagDecl,
PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
- RegisterPredefDecl(Context.TypePackElementDecl,
- PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
- RegisterPredefDecl(Context.BuiltinCommonTypeDecl, PREDEF_DECL_COMMON_TYPE_ID);
+#define BuiltinTemplate(BTName) \
+ RegisterPredefDecl(Context.Decl##BTName, PREDEF_DECL##BTName##_ID);
+#include "clang/Basic/BuiltinTemplates.inc"
const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();