Skip to content

Commit 517dd5e

Browse files
authored
[NFC] Replacing .Resource and .Sampler with attribute check (#8487)
This patch cleans up a few places that use a string check to use an attribute check.
1 parent 35c1b99 commit 517dd5e

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

tools/clang/lib/SPIRV/AstTypeProbe.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ clang::DiagnosticBuilder emitError(const clang::ASTContext &astContext,
2323
clang::DiagnosticsEngine::Error, message);
2424
return astContext.getDiagnostics().Report(srcLoc, diagId);
2525
}
26+
27+
// Returns the attribute of the given type attached to the record declaration
28+
// behind \p type, or nullptr if there is none. Attributes live on the
29+
// declaration, so they cannot be retrieved with QualType::getAs (which only
30+
// navigates the clang::Type hierarchy).
31+
template <typename AttrType> AttrType *getAttr(clang::QualType type) {
32+
type = type.getCanonicalType();
33+
if (const clang::RecordType *RT = type->getAs<clang::RecordType>()) {
34+
if (const auto *Spec =
35+
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(
36+
RT->getDecl()))
37+
if (const auto *Template = clang::dyn_cast<clang::ClassTemplateDecl>(
38+
Spec->getSpecializedTemplate()))
39+
return Template->getTemplatedDecl()->getAttr<AttrType>();
40+
if (const auto *Decl = clang::dyn_cast<clang::CXXRecordDecl>(RT->getDecl()))
41+
return Decl->getAttr<AttrType>();
42+
}
43+
return nullptr;
44+
}
2645
} // namespace
2746

2847
namespace clang {
@@ -1021,8 +1040,8 @@ bool isResourceDescriptorHeap(const Decl *D) {
10211040
}
10221041

10231042
bool isResourceDescriptorHeap(QualType T) {
1024-
const RecordType *RT = T->getAs<RecordType>();
1025-
return RT && RT->getDecl()->getName() == ".Resource";
1043+
const HLSLDynamicResourceAttr *Attr = getAttr<HLSLDynamicResourceAttr>(T);
1044+
return Attr && !Attr->getIsSampler();
10261045
}
10271046

10281047
bool isSamplerDescriptorHeap(const Decl *D) {
@@ -1031,8 +1050,8 @@ bool isSamplerDescriptorHeap(const Decl *D) {
10311050
}
10321051

10331052
bool isSamplerDescriptorHeap(QualType T) {
1034-
const RecordType *RT = T->getAs<RecordType>();
1035-
return RT && RT->getDecl()->getName() == ".Sampler";
1053+
const HLSLDynamicResourceAttr *Attr = getAttr<HLSLDynamicResourceAttr>(T);
1054+
return Attr && Attr->getIsSampler();
10361055
}
10371056

10381057
bool isAKindOfStructuredOrByteBuffer(QualType type) {

0 commit comments

Comments
 (0)