Skip to content

Commit d6903da

Browse files
committed
[Clang] Add __common_type builtin
1 parent b634e05 commit d6903da

File tree

16 files changed

+404
-6
lines changed

16 files changed

+404
-6
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
399399
/// The identifier '__type_pack_element'.
400400
mutable IdentifierInfo *TypePackElementName = nullptr;
401401

402+
/// The identifier '__common_type'.
403+
mutable IdentifierInfo *CommonTypeName = nullptr;
404+
402405
QualType ObjCConstantStringType;
403406
mutable RecordDecl *CFConstantStringTagDecl = nullptr;
404407
mutable TypedefDecl *CFConstantStringTypeDecl = nullptr;
@@ -606,6 +609,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
606609
mutable ExternCContextDecl *ExternCContext = nullptr;
607610
mutable BuiltinTemplateDecl *MakeIntegerSeqDecl = nullptr;
608611
mutable BuiltinTemplateDecl *TypePackElementDecl = nullptr;
612+
mutable BuiltinTemplateDecl *CommonTypeDecl = nullptr;
609613

610614
/// The associated SourceManager object.
611615
SourceManager &SourceMgr;
@@ -1107,6 +1111,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
11071111
ExternCContextDecl *getExternCContextDecl() const;
11081112
BuiltinTemplateDecl *getMakeIntegerSeqDecl() const;
11091113
BuiltinTemplateDecl *getTypePackElementDecl() const;
1114+
BuiltinTemplateDecl *getCommonTypeDecl() const;
11101115

11111116
// Builtin Types.
11121117
CanQualType VoidTy;
@@ -1984,6 +1989,12 @@ class ASTContext : public RefCountedBase<ASTContext> {
19841989
return TypePackElementName;
19851990
}
19861991

1992+
IdentifierInfo *getCommonTypeName() const {
1993+
if (!CommonTypeName)
1994+
CommonTypeName = &Idents.get("__common_type");
1995+
return CommonTypeName;
1996+
}
1997+
19871998
/// Retrieve the Objective-C "instancetype" type, if already known;
19881999
/// otherwise, returns a NULL type;
19892000
QualType getObjCInstanceType() {

clang/include/clang/AST/DeclID.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,16 @@ enum PredefinedDeclIDs {
8484

8585
/// The internal '__type_pack_element' template.
8686
PREDEF_DECL_TYPE_PACK_ELEMENT_ID = 17,
87+
88+
/// The internal '__common_type' template.
89+
PREDEF_DECL_COMMON_TYPE_ID = 18,
8790
};
8891

8992
/// The number of declaration IDs that are predefined.
9093
///
9194
/// For more information about predefined declarations, see the
9295
/// \c PredefinedDeclIDs type and the PREDEF_DECL_*_ID constants.
93-
const unsigned int NUM_PREDEF_DECL_IDS = 18;
96+
const unsigned int NUM_PREDEF_DECL_IDS = 19;
9497

9598
/// GlobalDeclID means DeclID in the current ASTContext and LocalDeclID means
9699
/// DeclID specific to a certain ModuleFile. Specially, in ASTWriter, the

clang/include/clang/Basic/Builtins.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,10 @@ enum BuiltinTemplateKind : int {
309309
BTK__make_integer_seq,
310310

311311
/// This names the __type_pack_element BuiltinTemplateDecl.
312-
BTK__type_pack_element
312+
BTK__type_pack_element,
313+
314+
/// This names the __common_type BuiltinTemplateDecl.
315+
BTK__common_type,
313316
};
314317

315318
} // end namespace clang

clang/include/clang/Sema/Sema.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,10 @@ class Sema final : public SemaBase {
22822282
/// Check to see if a given expression could have '.c_str()' called on it.
22832283
bool hasCStrMethod(const Expr *E);
22842284

2285+
// Check whether a type member 'Type::Name' exists, and if yes, return the
2286+
// type. If there is no type, the QualType is null
2287+
QualType getTypeMember(StringRef Name, QualType Type);
2288+
22852289
/// Diagnose pointers that are always non-null.
22862290
/// \param E the expression containing the pointer
22872291
/// \param NullKind NPCK_NotNull if E is a cast to bool, otherwise, E is

clang/lib/AST/ASTContext.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,13 @@ ASTContext::getTypePackElementDecl() const {
11701170
return TypePackElementDecl;
11711171
}
11721172

1173+
BuiltinTemplateDecl *ASTContext::getCommonTypeDecl() const {
1174+
if (!CommonTypeDecl)
1175+
CommonTypeDecl =
1176+
buildBuiltinTemplateDecl(BTK__common_type, getCommonTypeName());
1177+
return CommonTypeDecl;
1178+
}
1179+
11731180
RecordDecl *ASTContext::buildImplicitRecord(StringRef Name,
11741181
RecordDecl::TagKind TK) const {
11751182
SourceLocation Loc;

clang/lib/AST/ASTImporter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5408,6 +5408,9 @@ ExpectedDecl ASTNodeImporter::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
54085408
case BuiltinTemplateKind::BTK__type_pack_element:
54095409
ToD = Importer.getToContext().getTypePackElementDecl();
54105410
break;
5411+
case BuiltinTemplateKind::BTK__common_type:
5412+
ToD = Importer.getToContext().getCommonTypeDecl();
5413+
break;
54115414
}
54125415
assert(ToD && "BuiltinTemplateDecl of unsupported kind!");
54135416
Importer.MapImported(D, ToD);

clang/lib/AST/DeclTemplate.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,13 +1599,66 @@ createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) {
15991599
nullptr);
16001600
}
16011601

1602+
static TemplateParameterList *createCommonTypeList(const ASTContext &C,
1603+
DeclContext *DC) {
1604+
// class... Args
1605+
auto *Args = TemplateTypeParmDecl::Create(
1606+
C, DC, {}, {}, /*Depth=*/1, /*Position=*/0, /*Id=*/nullptr,
1607+
/*Typename=*/false, /*ParameterPack=*/true);
1608+
Args->setImplicit();
1609+
1610+
// <class... Args>
1611+
auto *BaseTemplateList =
1612+
TemplateParameterList::Create(C, {}, {}, Args, {}, nullptr);
1613+
1614+
// template <class... Args> class BaseTemplate
1615+
auto *BaseTemplate = TemplateTemplateParmDecl::Create(
1616+
C, DC, {}, /*Depth=*/0, /*Position=*/0, /*ParameterPack=*/false, {},
1617+
/*Typename=*/false, BaseTemplateList);
1618+
1619+
// class TypeMember
1620+
auto *TypeMember = TemplateTypeParmDecl::Create(
1621+
C, DC, {}, {}, /*Depth=*/1, /*Position=*/0, /*Id=*/nullptr,
1622+
/*Typename=*/false, /*ParameterPack=*/false);
1623+
1624+
// <class TypeMember>
1625+
auto *HasTypeMemberList =
1626+
TemplateParameterList::Create(C, {}, {}, TypeMember, {}, nullptr);
1627+
1628+
// template <class TypeMember> class HasTypeMember
1629+
auto *HasTypeMember =
1630+
TemplateTemplateParmDecl::Create(C, DC, {}, /*Depth=*/0, /*Position=*/1,
1631+
/*ParameterPack=*/false, {},
1632+
/*Typename=*/false, HasTypeMemberList);
1633+
1634+
// class HasNoTypeMember
1635+
auto *HasNoTypeMember = TemplateTypeParmDecl::Create(
1636+
C, DC, {}, {}, /*Depth=*/0, /*Position=*/2, /*Id=*/nullptr,
1637+
/*Typename=*/false, /*ParameterPack=*/false);
1638+
1639+
// class... Ts
1640+
auto *Ts = TemplateTypeParmDecl::Create(
1641+
C, DC, {}, {}, /*Depth=*/0, /*Position=*/3,
1642+
/*Id=*/nullptr, /*Typename=*/false, /*ParameterPack=*/true);
1643+
Ts->setImplicit();
1644+
1645+
// template <template <class... Args> class BaseTemplate,
1646+
// template <class TypeMember> class HasTypeMember, class HasNoTypeMember,
1647+
// class... Ts>
1648+
return TemplateParameterList::Create(
1649+
C, {}, {}, {BaseTemplate, HasTypeMember, HasNoTypeMember, Ts}, {},
1650+
nullptr);
1651+
}
1652+
16021653
static TemplateParameterList *createBuiltinTemplateParameterList(
16031654
const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
16041655
switch (BTK) {
16051656
case BTK__make_integer_seq:
16061657
return createMakeIntegerSeqParameterList(C, DC);
16071658
case BTK__type_pack_element:
16081659
return createTypePackElementParameterList(C, DC);
1660+
case BTK__common_type:
1661+
return createCommonTypeList(C, DC);
16091662
}
16101663

16111664
llvm_unreachable("unhandled BuiltinTemplateKind!");

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
18221822
// Report builtin templates as being builtins.
18231823
.Case("__make_integer_seq", getLangOpts().CPlusPlus)
18241824
.Case("__type_pack_element", getLangOpts().CPlusPlus)
1825+
.Case("__common_type", getLangOpts().CPlusPlus)
18251826
// Likewise for some builtin preprocessor macros.
18261827
// FIXME: This is inconsistent; we usually suggest detecting
18271828
// builtin macros via #ifdef. Don't add more cases here.

clang/lib/Sema/SemaChecking.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6844,6 +6844,14 @@ CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) {
68446844
return Results;
68456845
}
68466846

6847+
QualType Sema::getTypeMember(StringRef Name, QualType Type) {
6848+
auto Results = CXXRecordMembersNamed<TypeDecl>(Name, *this, Type);
6849+
assert(Results.size() <= 1);
6850+
if (Results.empty())
6851+
return {};
6852+
return Context.getTypeDeclType(*Results.begin());
6853+
}
6854+
68476855
/// Check if we could call '.c_str()' on an object.
68486856
///
68496857
/// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't

clang/lib/Sema/SemaLookup.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,10 @@ bool Sema::LookupBuiltin(LookupResult &R) {
932932
R.addDecl(getASTContext().getTypePackElementDecl());
933933
return true;
934934
}
935+
if (II == getASTContext().getCommonTypeName()) {
936+
R.addDecl(getASTContext().getCommonTypeDecl());
937+
return true;
938+
}
935939
}
936940

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

0 commit comments

Comments
 (0)