Skip to content

Commit 586dd4e

Browse files
committed
[Clang] Add BuiltinTemplates.td to generate code for builtin templates
1 parent c25bd6e commit 586dd4e

16 files changed

+269
-234
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
410410
/// The identifier 'NSCopying'.
411411
IdentifierInfo *NSCopyingName = nullptr;
412412

413-
/// The identifier '__make_integer_seq'.
414-
mutable IdentifierInfo *MakeIntegerSeqName = nullptr;
415-
416-
/// The identifier '__type_pack_element'.
417-
mutable IdentifierInfo *TypePackElementName = nullptr;
413+
#define BuiltinTemplate(BTName) mutable IdentifierInfo *Name##BTName = nullptr;
414+
#include "clang/Basic/BuiltinTemplates.inc"
418415

419416
/// The identifier '__builtin_common_type'.
420417
mutable IdentifierInfo *BuiltinCommonTypeName = nullptr;
@@ -624,9 +621,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
624621

625622
TranslationUnitDecl *TUDecl = nullptr;
626623
mutable ExternCContextDecl *ExternCContext = nullptr;
627-
mutable BuiltinTemplateDecl *MakeIntegerSeqDecl = nullptr;
628-
mutable BuiltinTemplateDecl *TypePackElementDecl = nullptr;
629-
mutable BuiltinTemplateDecl *BuiltinCommonTypeDecl = nullptr;
624+
625+
#define BuiltinTemplate(Name) mutable BuiltinTemplateDecl *Decl##Name = nullptr;
626+
#include "clang/Basic/BuiltinTemplates.inc"
630627

631628
/// The associated SourceManager object.
632629
SourceManager &SourceMgr;
@@ -1152,9 +1149,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
11521149
}
11531150

11541151
ExternCContextDecl *getExternCContextDecl() const;
1155-
BuiltinTemplateDecl *getMakeIntegerSeqDecl() const;
1156-
BuiltinTemplateDecl *getTypePackElementDecl() const;
1157-
BuiltinTemplateDecl *getBuiltinCommonTypeDecl() const;
1152+
1153+
#define BuiltinTemplate(Name) BuiltinTemplateDecl *get##Name##Decl() const;
1154+
#include "clang/Basic/BuiltinTemplates.inc"
11581155

11591156
// Builtin Types.
11601157
CanQualType VoidTy;
@@ -2054,17 +2051,13 @@ class ASTContext : public RefCountedBase<ASTContext> {
20542051
return BoolName;
20552052
}
20562053

2057-
IdentifierInfo *getMakeIntegerSeqName() const {
2058-
if (!MakeIntegerSeqName)
2059-
MakeIntegerSeqName = &Idents.get("__make_integer_seq");
2060-
return MakeIntegerSeqName;
2061-
}
2062-
2063-
IdentifierInfo *getTypePackElementName() const {
2064-
if (!TypePackElementName)
2065-
TypePackElementName = &Idents.get("__type_pack_element");
2066-
return TypePackElementName;
2054+
#define BuiltinTemplate(BTName) \
2055+
IdentifierInfo *get##BTName##Name() const { \
2056+
if (!Name##BTName) \
2057+
Name##BTName = &Idents.get(#BTName); \
2058+
return Name##BTName; \
20672059
}
2060+
#include "clang/Basic/BuiltinTemplates.inc"
20682061

20692062
IdentifierInfo *getBuiltinCommonTypeName() const {
20702063
if (!BuiltinCommonTypeName)

clang/include/clang/AST/DeclID.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,14 @@ enum PredefinedDeclIDs {
7171
/// The extern "C" context.
7272
PREDEF_DECL_EXTERN_C_CONTEXT_ID,
7373

74-
/// The internal '__make_integer_seq' template.
75-
PREDEF_DECL_MAKE_INTEGER_SEQ_ID,
76-
7774
/// The internal '__NSConstantString' typedef.
7875
PREDEF_DECL_CF_CONSTANT_STRING_ID,
7976

8077
/// The internal '__NSConstantString' tag type.
8178
PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID,
8279

83-
/// The internal '__type_pack_element' template.
84-
PREDEF_DECL_TYPE_PACK_ELEMENT_ID,
85-
86-
/// The internal '__builtin_common_type' template.
87-
PREDEF_DECL_COMMON_TYPE_ID,
80+
#define BuiltinTemplate(Name) PREDEF_DECL##Name##_ID,
81+
#include "clang/Basic/BuiltinTemplates.inc"
8882

8983
/// The number of declaration IDs that are predefined.
9084
NUM_PREDEF_DECL_IDS
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===--- BuiltinTemplates.td - Clang builtin template aliases ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
class BuiltinTemplate<string prototype> {
10+
string Prototype = prototype;
11+
}
12+
13+
def __make_integer_seq : BuiltinTemplate<
14+
"template <template <class T, T... Ints> class IntSeq, class T, T N>">;
15+
16+
def __type_pack_element : BuiltinTemplate<
17+
"template <size_t, class... T>">;
18+
19+
def __builtin_common_type : BuiltinTemplate<
20+
"template <template <class... Args> class BaseTemplate,"
21+
" template <class TypeMember> class HasTypeMember,"
22+
" class HasNoTypeMember,"
23+
" class... Ts>">;

clang/include/clang/Basic/Builtins.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,8 @@ bool evaluateRequiredTargetFeatures(
306306

307307
/// Kinds of BuiltinTemplateDecl.
308308
enum BuiltinTemplateKind : int {
309-
/// This names the __make_integer_seq BuiltinTemplateDecl.
310-
BTK__make_integer_seq,
311-
312-
/// This names the __type_pack_element BuiltinTemplateDecl.
313-
BTK__type_pack_element,
314-
315-
/// This names the __builtin_common_type BuiltinTemplateDecl.
316-
BTK__builtin_common_type,
309+
#define BuiltinTemplate(Name) BTK##Name,
310+
#include "clang/Basic/BuiltinTemplates.inc"
317311
};
318312

319313
} // end namespace clang

clang/include/clang/Basic/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ clang_tablegen(BuiltinsX86_64.inc -gen-clang-builtins
7777
SOURCE BuiltinsX86_64.td
7878
TARGET ClangBuiltinsX86_64)
7979

80+
clang_tablegen(BuiltinTemplates.inc -gen-clang-builtin-templates
81+
SOURCE BuiltinTemplates.td
82+
TARGET ClangBuiltinTemplates)
83+
8084
# ARM NEON and MVE
8185
clang_tablegen(arm_neon.inc -gen-arm-neon-sema
8286
SOURCE arm_neon.td

clang/lib/AST/ASTContext.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,28 +1195,14 @@ ASTContext::buildBuiltinTemplateDecl(BuiltinTemplateKind BTK,
11951195
return BuiltinTemplate;
11961196
}
11971197

1198-
BuiltinTemplateDecl *
1199-
ASTContext::getMakeIntegerSeqDecl() const {
1200-
if (!MakeIntegerSeqDecl)
1201-
MakeIntegerSeqDecl = buildBuiltinTemplateDecl(BTK__make_integer_seq,
1202-
getMakeIntegerSeqName());
1203-
return MakeIntegerSeqDecl;
1204-
}
1205-
1206-
BuiltinTemplateDecl *
1207-
ASTContext::getTypePackElementDecl() const {
1208-
if (!TypePackElementDecl)
1209-
TypePackElementDecl = buildBuiltinTemplateDecl(BTK__type_pack_element,
1210-
getTypePackElementName());
1211-
return TypePackElementDecl;
1212-
}
1213-
1214-
BuiltinTemplateDecl *ASTContext::getBuiltinCommonTypeDecl() const {
1215-
if (!BuiltinCommonTypeDecl)
1216-
BuiltinCommonTypeDecl = buildBuiltinTemplateDecl(
1217-
BTK__builtin_common_type, getBuiltinCommonTypeName());
1218-
return BuiltinCommonTypeDecl;
1219-
}
1198+
#define BuiltinTemplate(BTName) \
1199+
BuiltinTemplateDecl *ASTContext::get##BTName##Decl() const { \
1200+
if (!Decl##BTName) \
1201+
Decl##BTName = \
1202+
buildBuiltinTemplateDecl(BTK##BTName, get##BTName##Name()); \
1203+
return Decl##BTName; \
1204+
}
1205+
#include "clang/Basic/BuiltinTemplates.inc"
12201206

12211207
RecordDecl *ASTContext::buildImplicitRecord(StringRef Name,
12221208
RecordDecl::TagKind TK) const {

clang/lib/AST/ASTImporter.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5461,15 +5461,11 @@ ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
54615461
ExpectedDecl ASTNodeImporter::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
54625462
Decl* ToD = nullptr;
54635463
switch (D->getBuiltinTemplateKind()) {
5464-
case BuiltinTemplateKind::BTK__make_integer_seq:
5465-
ToD = Importer.getToContext().getMakeIntegerSeqDecl();
5466-
break;
5467-
case BuiltinTemplateKind::BTK__type_pack_element:
5468-
ToD = Importer.getToContext().getTypePackElementDecl();
5469-
break;
5470-
case BuiltinTemplateKind::BTK__builtin_common_type:
5471-
ToD = Importer.getToContext().getBuiltinCommonTypeDecl();
5464+
#define BuiltinTemplate(Name) \
5465+
case BuiltinTemplateKind::BTK##Name: \
5466+
ToD = Importer.getToContext().get##Name##Decl(); \
54725467
break;
5468+
#include "clang/Basic/BuiltinTemplates.inc"
54735469
}
54745470
assert(ToD && "BuiltinTemplateDecl of unsupported kind!");
54755471
Importer.MapImported(D, ToD);

clang/lib/AST/DeclTemplate.cpp

Lines changed: 2 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,140 +1571,11 @@ SourceRange VarTemplatePartialSpecializationDecl::getSourceRange() const {
15711571
return Range;
15721572
}
15731573

1574-
static TemplateParameterList *
1575-
createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
1576-
// typename T
1577-
auto *T = TemplateTypeParmDecl::Create(
1578-
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0,
1579-
/*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
1580-
/*HasTypeConstraint=*/false);
1581-
T->setImplicit(true);
1582-
1583-
// T ...Ints
1584-
TypeSourceInfo *TI =
1585-
C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0));
1586-
auto *N = NonTypeTemplateParmDecl::Create(
1587-
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1588-
/*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI);
1589-
N->setImplicit(true);
1590-
1591-
// <typename T, T ...Ints>
1592-
NamedDecl *P[2] = {T, N};
1593-
auto *TPL = TemplateParameterList::Create(
1594-
C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr);
1595-
1596-
// template <typename T, ...Ints> class IntSeq
1597-
auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create(
1598-
C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
1599-
/*ParameterPack=*/false, /*Id=*/nullptr, /*Typename=*/false, TPL);
1600-
TemplateTemplateParm->setImplicit(true);
1601-
1602-
// typename T
1603-
auto *TemplateTypeParm = TemplateTypeParmDecl::Create(
1604-
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1605-
/*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
1606-
/*HasTypeConstraint=*/false);
1607-
TemplateTypeParm->setImplicit(true);
1608-
1609-
// T N
1610-
TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(
1611-
QualType(TemplateTypeParm->getTypeForDecl(), 0));
1612-
auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create(
1613-
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2,
1614-
/*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1615-
NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm,
1616-
NonTypeTemplateParm};
1617-
1618-
// template <template <typename T, T ...Ints> class IntSeq, typename T, T N>
1619-
return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
1620-
Params, SourceLocation(), nullptr);
1621-
}
1622-
1623-
static TemplateParameterList *
1624-
createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) {
1625-
// std::size_t Index
1626-
TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType());
1627-
auto *Index = NonTypeTemplateParmDecl::Create(
1628-
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0,
1629-
/*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1630-
1631-
// typename ...T
1632-
auto *Ts = TemplateTypeParmDecl::Create(
1633-
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1634-
/*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true,
1635-
/*HasTypeConstraint=*/false);
1636-
Ts->setImplicit(true);
1637-
1638-
// template <std::size_t Index, typename ...T>
1639-
NamedDecl *Params[] = {Index, Ts};
1640-
return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
1641-
llvm::ArrayRef(Params), SourceLocation(),
1642-
nullptr);
1643-
}
1644-
1645-
static TemplateParameterList *createBuiltinCommonTypeList(const ASTContext &C,
1646-
DeclContext *DC) {
1647-
// class... Args
1648-
auto *Args =
1649-
TemplateTypeParmDecl::Create(C, DC, SourceLocation(), SourceLocation(),
1650-
/*Depth=*/1, /*Position=*/0, /*Id=*/nullptr,
1651-
/*Typename=*/false, /*ParameterPack=*/true);
1652-
1653-
// <class... Args>
1654-
auto *BaseTemplateList = TemplateParameterList::Create(
1655-
C, SourceLocation(), SourceLocation(), Args, SourceLocation(), nullptr);
1656-
1657-
// template <class... Args> class BaseTemplate
1658-
auto *BaseTemplate = TemplateTemplateParmDecl::Create(
1659-
C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
1660-
/*ParameterPack=*/false, /*Id=*/nullptr,
1661-
/*Typename=*/false, BaseTemplateList);
1662-
1663-
// class TypeMember
1664-
auto *TypeMember =
1665-
TemplateTypeParmDecl::Create(C, DC, SourceLocation(), SourceLocation(),
1666-
/*Depth=*/1, /*Position=*/0, /*Id=*/nullptr,
1667-
/*Typename=*/false, /*ParameterPack=*/false);
1668-
1669-
// <class TypeMember>
1670-
auto *HasTypeMemberList =
1671-
TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
1672-
TypeMember, SourceLocation(), nullptr);
1673-
1674-
// template <class TypeMember> class HasTypeMember
1675-
auto *HasTypeMember = TemplateTemplateParmDecl::Create(
1676-
C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/1,
1677-
/*ParameterPack=*/false, /*Id=*/nullptr,
1678-
/*Typename=*/false, HasTypeMemberList);
1679-
1680-
// class HasNoTypeMember
1681-
auto *HasNoTypeMember = TemplateTypeParmDecl::Create(
1682-
C, DC, {}, {}, /*Depth=*/0, /*Position=*/2, /*Id=*/nullptr,
1683-
/*Typename=*/false, /*ParameterPack=*/false);
1684-
1685-
// class... Ts
1686-
auto *Ts = TemplateTypeParmDecl::Create(
1687-
C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/3,
1688-
/*Id=*/nullptr, /*Typename=*/false, /*ParameterPack=*/true);
1689-
1690-
// template <template <class... Args> class BaseTemplate,
1691-
// template <class TypeMember> class HasTypeMember, class HasNoTypeMember,
1692-
// class... Ts>
1693-
return TemplateParameterList::Create(
1694-
C, SourceLocation(), SourceLocation(),
1695-
{BaseTemplate, HasTypeMember, HasNoTypeMember, Ts}, SourceLocation(),
1696-
nullptr);
1697-
}
1698-
16991574
static TemplateParameterList *createBuiltinTemplateParameterList(
17001575
const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
17011576
switch (BTK) {
1702-
case BTK__make_integer_seq:
1703-
return createMakeIntegerSeqParameterList(C, DC);
1704-
case BTK__type_pack_element:
1705-
return createTypePackElementParameterList(C, DC);
1706-
case BTK__builtin_common_type:
1707-
return createBuiltinCommonTypeList(C, DC);
1577+
#define CREATE_BUILTIN_TEMPLATE_PARAMETER_LIST
1578+
#include "clang/Basic/BuiltinTemplates.inc"
17081579
}
17091580

17101581
llvm_unreachable("unhandled BuiltinTemplateKind!");

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,9 +1831,8 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
18311831
} else {
18321832
return llvm::StringSwitch<bool>(II->getName())
18331833
// Report builtin templates as being builtins.
1834-
.Case("__make_integer_seq", getLangOpts().CPlusPlus)
1835-
.Case("__type_pack_element", getLangOpts().CPlusPlus)
1836-
.Case("__builtin_common_type", getLangOpts().CPlusPlus)
1834+
#define BuiltinTemplate(BTName) .Case(#BTName, getLangOpts().CPlusPlus)
1835+
#include "clang/Basic/BuiltinTemplates.inc"
18371836
// Likewise for some builtin preprocessor macros.
18381837
// FIXME: This is inconsistent; we usually suggest detecting
18391838
// builtin macros via #ifdef. Don't add more cases here.

clang/lib/Sema/SemaLookup.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -924,18 +924,12 @@ bool Sema::LookupBuiltin(LookupResult &R) {
924924
IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo();
925925
if (II) {
926926
if (getLangOpts().CPlusPlus && NameKind == Sema::LookupOrdinaryName) {
927-
if (II == getASTContext().getMakeIntegerSeqName()) {
928-
R.addDecl(getASTContext().getMakeIntegerSeqDecl());
929-
return true;
930-
}
931-
if (II == getASTContext().getTypePackElementName()) {
932-
R.addDecl(getASTContext().getTypePackElementDecl());
933-
return true;
934-
}
935-
if (II == getASTContext().getBuiltinCommonTypeName()) {
936-
R.addDecl(getASTContext().getBuiltinCommonTypeDecl());
937-
return true;
938-
}
927+
#define BuiltinTemplate(BIName) \
928+
if (II == getASTContext().get##BIName##Name()) { \
929+
R.addDecl(getASTContext().get##BIName##Decl()); \
930+
return true; \
931+
}
932+
#include "clang/Basic/BuiltinTemplates.inc"
939933
}
940934

941935
// Check if this is an OpenCL Builtin, and if so, insert its overloads.

0 commit comments

Comments
 (0)