Skip to content

Commit e190e73

Browse files
author
Erich Keane
committed
Better clean up DeclContextDesc uses, add a 'make' function to ensure constexpr construction
1 parent d409f95 commit e190e73

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,18 @@ namespace {
6868
/// Various utilities.
6969
class Util {
7070
public:
71-
using DeclContextDesc = std::pair<clang::Decl::Kind, StringRef>;
71+
using DeclContextDesc = std::pair<Decl::Kind, StringRef>;
72+
73+
template <size_t N>
74+
static constexpr DeclContextDesc MakeDeclContextDesc(Decl::Kind K,
75+
const char (&Str)[N]) {
76+
return DeclContextDesc{K, llvm::StringLiteral{Str}};
77+
}
78+
79+
static constexpr DeclContextDesc MakeDeclContextDesc(Decl::Kind K,
80+
llvm::StringRef SR) {
81+
return DeclContextDesc{K, SR};
82+
}
7283

7384
/// Checks whether given clang type is a full specialization of the SYCL
7485
/// accessor class.
@@ -4314,57 +4325,53 @@ bool Util::isSyclSamplerType(QualType Ty) { return isSyclType(Ty, "sampler"); }
43144325
bool Util::isSyclStreamType(QualType Ty) { return isSyclType(Ty, "stream"); }
43154326

43164327
bool Util::isSyclHalfType(QualType Ty) {
4317-
llvm::StringLiteral Name = "half";
43184328
std::array<DeclContextDesc, 5> Scopes = {
4319-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"},
4320-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"},
4321-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "detail"},
4322-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "half_impl"},
4323-
Util::DeclContextDesc{Decl::Kind::CXXRecord, Name}};
4329+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"),
4330+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"),
4331+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "detail"),
4332+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "half_impl"),
4333+
Util::MakeDeclContextDesc(Decl::Kind::CXXRecord, "half")};
43244334
return matchQualifiedTypeName(Ty, Scopes);
43254335
}
43264336

43274337
bool Util::isSyclSpecConstantType(QualType Ty) {
4328-
llvm::StringLiteral Name = "spec_constant";
43294338
std::array<DeclContextDesc, 5> Scopes = {
4330-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"},
4331-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"},
4332-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "ONEAPI"},
4333-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "experimental"},
4334-
Util::DeclContextDesc{Decl::Kind::ClassTemplateSpecialization, Name}};
4339+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"),
4340+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"),
4341+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "ONEAPI"),
4342+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "experimental"),
4343+
Util::MakeDeclContextDesc(Decl::Kind::ClassTemplateSpecialization,
4344+
"spec_constant")};
43354345
return matchQualifiedTypeName(Ty, Scopes);
43364346
}
43374347

43384348
bool Util::isSyclKernelHandlerType(QualType Ty) {
4339-
llvm::StringLiteral Name = "kernel_handler";
43404349
std::array<DeclContextDesc, 3> Scopes = {
4341-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"},
4342-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"},
4343-
Util::DeclContextDesc{Decl::Kind::CXXRecord, Name}};
4350+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"),
4351+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"),
4352+
Util::MakeDeclContextDesc(Decl::Kind::CXXRecord, "kernel_handler")};
43444353
return matchQualifiedTypeName(Ty, Scopes);
43454354
}
43464355

43474356
bool Util::isSyclBufferLocationType(QualType Ty) {
4348-
llvm::StringLiteral PropertyName = "buffer_location";
4349-
llvm::StringLiteral InstanceName = "instance";
43504357
std::array<DeclContextDesc, 6> Scopes = {
4351-
Util::DeclContextDesc{Decl::Kind::Namespace, "cl"},
4352-
Util::DeclContextDesc{Decl::Kind::Namespace, "sycl"},
4353-
Util::DeclContextDesc{Decl::Kind::Namespace, "INTEL"},
4354-
Util::DeclContextDesc{Decl::Kind::Namespace, "property"},
4355-
Util::DeclContextDesc{Decl::Kind::CXXRecord, PropertyName},
4356-
Util::DeclContextDesc{Decl::Kind::ClassTemplateSpecialization,
4357-
InstanceName}};
4358+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"),
4359+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"),
4360+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "INTEL"),
4361+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "property"),
4362+
Util::MakeDeclContextDesc(Decl::Kind::CXXRecord, "buffer_location"),
4363+
Util::MakeDeclContextDesc(Decl::Kind::ClassTemplateSpecialization,
4364+
"instance")};
43584365
return matchQualifiedTypeName(Ty, Scopes);
43594366
}
43604367

43614368
bool Util::isSyclType(QualType Ty, StringRef Name, bool Tmpl) {
43624369
Decl::Kind ClassDeclKind =
43634370
Tmpl ? Decl::Kind::ClassTemplateSpecialization : Decl::Kind::CXXRecord;
43644371
std::array<DeclContextDesc, 3> Scopes = {
4365-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"},
4366-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"},
4367-
Util::DeclContextDesc{ClassDeclKind, Name}};
4372+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"),
4373+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"),
4374+
Util::MakeDeclContextDesc(ClassDeclKind, Name)};
43684375
return matchQualifiedTypeName(Ty, Scopes);
43694376
}
43704377

@@ -4378,18 +4385,18 @@ bool Util::isSyclFunction(const FunctionDecl *FD, StringRef Name) {
43784385
return false;
43794386

43804387
std::array<DeclContextDesc, 2> Scopes = {
4381-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"},
4382-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"}};
4388+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"),
4389+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl")};
43834390
return matchContext(DC, Scopes);
43844391
}
43854392

43864393
bool Util::isAccessorPropertyListType(QualType Ty) {
4387-
llvm::StringLiteral Name = "accessor_property_list";
43884394
std::array<DeclContextDesc, 4> Scopes = {
4389-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"},
4390-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"},
4391-
Util::DeclContextDesc{clang::Decl::Kind::Namespace, "ONEAPI"},
4392-
Util::DeclContextDesc{Decl::Kind::ClassTemplateSpecialization, Name}};
4395+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"),
4396+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"),
4397+
Util::MakeDeclContextDesc(Decl::Kind::Namespace, "ONEAPI"),
4398+
Util::MakeDeclContextDesc(Decl::Kind::ClassTemplateSpecialization,
4399+
"accessor_property_list")};
43934400
return matchQualifiedTypeName(Ty, Scopes);
43944401
}
43954402

@@ -4401,17 +4408,17 @@ bool Util::matchContext(const DeclContext *Ctx,
44014408
StringRef Name = "";
44024409

44034410
for (const auto &Scope : llvm::reverse(Scopes)) {
4404-
clang::Decl::Kind DK = Ctx->getDeclKind();
4411+
Decl::Kind DK = Ctx->getDeclKind();
44054412
if (DK != Scope.first)
44064413
return false;
44074414

44084415
switch (DK) {
4409-
case clang::Decl::Kind::ClassTemplateSpecialization:
4416+
case Decl::Kind::ClassTemplateSpecialization:
44104417
// ClassTemplateSpecializationDecl inherits from CXXRecordDecl
4411-
case clang::Decl::Kind::CXXRecord:
4418+
case Decl::Kind::CXXRecord:
44124419
Name = cast<CXXRecordDecl>(Ctx)->getName();
44134420
break;
4414-
case clang::Decl::Kind::Namespace:
4421+
case Decl::Kind::Namespace:
44154422
Name = cast<NamespaceDecl>(Ctx)->getName();
44164423
break;
44174424
default:

0 commit comments

Comments
 (0)