Skip to content

[HLSL] Add helpers to simplify HLSL resource type declarations. NFC #73967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/include/clang/Sema/HLSLExternalSemaSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class HLSLExternalSemaSource : public ExternalSemaSource {

void defineHLSLVectorAlias();
void defineTrivialHLSLTypes();
void forwardDeclareHLSLTypes();
void defineHLSLTypesWithForwardDeclarations();

void completeBufferType(CXXRecordDecl *Record);
void onCompletion(CXXRecordDecl *Record, CompletionFunction Fn);

public:
~HLSLExternalSemaSource() override;
Expand Down
54 changes: 34 additions & 20 deletions clang/lib/Sema/HLSLExternalSemaSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ struct BuiltinTypeDeclBuilder {
}

TemplateParameterListBuilder addTemplateArgumentList();
BuiltinTypeDeclBuilder &addSimpleTemplateParams(ArrayRef<StringRef> Names);
};

struct TemplateParameterListBuilder {
Expand Down Expand Up @@ -360,11 +361,19 @@ struct TemplateParameterListBuilder {
return Builder;
}
};
} // namespace

TemplateParameterListBuilder BuiltinTypeDeclBuilder::addTemplateArgumentList() {
return TemplateParameterListBuilder(*this);
}
} // namespace

BuiltinTypeDeclBuilder &
BuiltinTypeDeclBuilder::addSimpleTemplateParams(ArrayRef<StringRef> Names) {
TemplateParameterListBuilder Builder = this->addTemplateArgumentList();
for (StringRef Name : Names)
Builder.addTypeParameter(Name);
return Builder.finalizeTemplateArgs();
}

HLSLExternalSemaSource::~HLSLExternalSemaSource() {}

Expand All @@ -390,7 +399,7 @@ void HLSLExternalSemaSource::InitializeSema(Sema &S) {
// Force external decls in the HLSL namespace to load from the PCH.
(void)HLSLNamespace->getCanonicalDecl()->decls_begin();
defineTrivialHLSLTypes();
forwardDeclareHLSLTypes();
defineHLSLTypesWithForwardDeclarations();

// This adds a `using namespace hlsl` directive. In DXC, we don't put HLSL's
// built in types inside a namespace, but we are planning to change that in
Expand Down Expand Up @@ -467,18 +476,32 @@ void HLSLExternalSemaSource::defineTrivialHLSLTypes() {
.Record;
}

void HLSLExternalSemaSource::forwardDeclareHLSLTypes() {
/// Set up common members and attributes for buffer types
static BuiltinTypeDeclBuilder setupBufferType(CXXRecordDecl *Decl, Sema &S,
ResourceClass RC,
ResourceKind RK) {
return BuiltinTypeDeclBuilder(Decl)
.addHandleMember()
.addDefaultHandleConstructor(S, RC)
.annotateResourceClass(RC, RK);
}

void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
CXXRecordDecl *Decl;
Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWBuffer")
.addTemplateArgumentList()
.addTypeParameter("element_type")
.finalizeTemplateArgs()
.addSimpleTemplateParams({"element_type"})
.Record;
if (!Decl->isCompleteDefinition())
Completions.insert(
std::make_pair(Decl->getCanonicalDecl(),
std::bind(&HLSLExternalSemaSource::completeBufferType,
this, std::placeholders::_1)));
onCompletion(Decl, [this](CXXRecordDecl *Decl) {
setupBufferType(Decl, *SemaPtr, ResourceClass::UAV,
ResourceKind::TypedBuffer)
.addArraySubscriptOperators()
.completeDefinition();
});
}

void HLSLExternalSemaSource::onCompletion(CXXRecordDecl *Record,
CompletionFunction Fn) {
Completions.insert(std::make_pair(Record->getCanonicalDecl(), Fn));
}

void HLSLExternalSemaSource::CompleteType(TagDecl *Tag) {
Expand All @@ -496,12 +519,3 @@ void HLSLExternalSemaSource::CompleteType(TagDecl *Tag) {
return;
It->second(Record);
}

void HLSLExternalSemaSource::completeBufferType(CXXRecordDecl *Record) {
BuiltinTypeDeclBuilder(Record)
.addHandleMember()
.addDefaultHandleConstructor(*SemaPtr, ResourceClass::UAV)
.addArraySubscriptOperators()
.annotateResourceClass(ResourceClass::UAV, ResourceKind::TypedBuffer)
.completeDefinition();
}