[clang] [HLSL] Resource initialization by constructors (PR #135120)
Ashley Coleman via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 11 12:44:08 PDT 2025
================
@@ -3179,6 +3207,67 @@ void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
}
}
+static bool initVarDeclWithCtor(Sema &S, VarDecl *VD,
+ MutableArrayRef<Expr *> Args) {
+ InitializedEntity Entity = InitializedEntity::InitializeVariable(VD);
+ InitializationKind Kind = InitializationKind::CreateDirect(
+ VD->getLocation(), SourceLocation(), SourceLocation());
+
+ InitializationSequence InitSeq(S, Entity, Kind, Args);
+ ExprResult Init = InitSeq.Perform(S, Entity, Kind, Args);
+
+ if (!Init.get())
+ return false;
+
+ VD->setInit(S.MaybeCreateExprWithCleanups(Init.get()));
+ VD->setInitStyle(VarDecl::CallInit);
+ S.CheckCompleteVariableDeclaration(VD);
+ return true;
+}
+
+static bool initGlobalResourceDecl(Sema &S, VarDecl *VD) {
+ HLSLResourceBindingAttr *RBA = VD->getAttr<HLSLResourceBindingAttr>();
+ if (!RBA)
+ // FIXME: add support for implicit binding (llvm/llvm-project#110722)
+ return false;
+
+ ASTContext &AST = S.getASTContext();
+ uint64_t UIntTySize = AST.getTypeSize(AST.UnsignedIntTy);
+ uint64_t IntTySize = AST.getTypeSize(AST.IntTy);
+ Expr *Args[] = {
+ IntegerLiteral::Create(AST, llvm::APInt(UIntTySize, RBA->getSlotNumber()),
+ AST.UnsignedIntTy, SourceLocation()),
+ IntegerLiteral::Create(AST,
+ llvm::APInt(UIntTySize, RBA->getSpaceNumber()),
+ AST.UnsignedIntTy, SourceLocation()),
+ IntegerLiteral::Create(AST, llvm::APInt(IntTySize, 1), AST.IntTy,
+ SourceLocation()),
+ IntegerLiteral::Create(AST, llvm::APInt(UIntTySize, 0), AST.UnsignedIntTy,
+ SourceLocation())};
+
+ return initVarDeclWithCtor(S, VD, Args);
+}
+
+// Returns true in the initialization has been handled;
+// Return false to let Clang handle the default initializaton.
----------------
V-FEXrt wrote:
nit: Aren't "we" clang? Does it make sense to just say
"Return false to use default initialization"?
https://github.com/llvm/llvm-project/pull/135120
More information about the cfe-commits
mailing list