Skip to content

Commit 3a97030

Browse files
joaosaffranmbcoder
authored andcommitted
[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 994303c commit 3a97030

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 {
@@ -1001,8 +1020,8 @@ bool isResourceDescriptorHeap(const Decl *D) {
10011020
}
10021021

10031022
bool isResourceDescriptorHeap(QualType T) {
1004-
const RecordType *RT = T->getAs<RecordType>();
1005-
return RT && RT->getDecl()->getName() == ".Resource";
1023+
const HLSLDynamicResourceAttr *Attr = getAttr<HLSLDynamicResourceAttr>(T);
1024+
return Attr && !Attr->getIsSampler();
10061025
}
10071026

10081027
bool isSamplerDescriptorHeap(const Decl *D) {
@@ -1011,8 +1030,8 @@ bool isSamplerDescriptorHeap(const Decl *D) {
10111030
}
10121031

10131032
bool isSamplerDescriptorHeap(QualType T) {
1014-
const RecordType *RT = T->getAs<RecordType>();
1015-
return RT && RT->getDecl()->getName() == ".Sampler";
1033+
const HLSLDynamicResourceAttr *Attr = getAttr<HLSLDynamicResourceAttr>(T);
1034+
return Attr && Attr->getIsSampler();
10161035
}
10171036

10181037
bool isAKindOfStructuredOrByteBuffer(QualType type) {

0 commit comments

Comments
 (0)