[clang] [HLSL] Add implicit resource element type concepts to AST (PR #112600)

Chris B via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 17 16:38:01 PDT 2024


================
@@ -483,10 +555,106 @@ static BuiltinTypeDeclBuilder setupBufferType(CXXRecordDecl *Decl, Sema &S,
       .addDefaultHandleConstructor(S, RC);
 }
 
+BinaryOperator *getSizeOfLEQ16Expr(clang::ASTContext &context,
+                                   SourceLocation NameLoc,
+                                   TemplateTypeParmDecl *T) {
+  // Obtain the QualType for 'unsigned long'
+  clang::QualType unsignedLongType = context.UnsignedLongTy;
+
+  // Create a QualType that points to this TemplateTypeParmDecl
+  clang::QualType TType = context.getTypeDeclType(T);
+
+  // Create a TypeSourceInfo for the template type parameter 'T'
+  clang::TypeSourceInfo *TTypeSourceInfo =
+      context.getTrivialTypeSourceInfo(TType, NameLoc);
+
+  clang::UnaryExprOrTypeTraitExpr *sizeOfExpr = new (context)
+      clang::UnaryExprOrTypeTraitExpr(clang::UETT_SizeOf, TTypeSourceInfo,
+                                      unsignedLongType, NameLoc, NameLoc);
+
+  // Create an IntegerLiteral for the value '16' with size type
+  clang::QualType sizeType = context.getSizeType();
+  llvm::APInt sizeValue = llvm::APInt(context.getTypeSize(sizeType), 16);
+  clang::IntegerLiteral *sizeLiteral = new (context)
+      clang::IntegerLiteral(context, sizeValue, sizeType, NameLoc);
+
+  clang::QualType BoolTy = context.BoolTy;
+
+  clang::BinaryOperator *binaryOperator = clang::BinaryOperator::Create(
+      context, sizeOfExpr, // Left-hand side expression
+      sizeLiteral,         // Right-hand side expression
+      clang::BO_LE,        // Binary operator kind (<=)
+      BoolTy,              // Result type (bool)
+      clang::VK_LValue,    // Value kind
+      clang::OK_Ordinary,  // Object kind
+      NameLoc,             // Source location of operator
+      FPOptionsOverride());
+
+  return binaryOperator;
+}
+
+Expr *getTypedBufferConstraintExpr(Sema &S, SourceLocation NameLoc,
+                                   TemplateTypeParmDecl *T) {
+  clang::ASTContext &context = S.getASTContext();
+
+  // first get the "sizeof(T) <= 16" expression, as a binary operator
+  BinaryOperator *sizeOfLEQ16 = getSizeOfLEQ16Expr(context, NameLoc, T);
+  // TODO: add the '__builtin_hlsl_is_line_vector_layout_compatible' builtin
+  // and return a binary operator that evaluates the builtin on the given
+  // template type parameter 'T'
+  return sizeOfLEQ16;
+}
+
+ConceptDecl *getTypedBufferConceptDecl(Sema &S) {
+  DeclContext *DC = S.CurContext;
+  clang::ASTContext &context = S.getASTContext();
+  SourceLocation DeclLoc = SourceLocation();
+
+  IdentifierInfo &IsValidLineVectorII =
+      context.Idents.get("is_valid_line_vector");
+  IdentifierInfo &ElementTypeII = context.Idents.get("element_type");
+  clang::TemplateTypeParmDecl *T = clang::TemplateTypeParmDecl::Create(
+      context, context.getTranslationUnitDecl(), DeclLoc, DeclLoc,
+      /*depth=*/0,
+      /*position=*/0,
+      /*id=*/&ElementTypeII,
+      /*Typename=*/true,
+      /*ParameterPack=*/false);
+
+  T->setDeclContext(DC);
+  T->setReferenced();
+
+  // Create and Attach Template Parameter List to ConceptDecl
+  std::vector<NamedDecl *> TemplateParamsVec = {T};
+  llvm::ArrayRef<NamedDecl *> TemplateParams(TemplateParamsVec);
+
+  clang::TemplateParameterList *ConceptParams =
+      clang::TemplateParameterList::Create(context, DeclLoc, DeclLoc,
+                                           TemplateParams, DeclLoc, nullptr);
----------------
llvm-beanz wrote:

```suggestion
  clang::TemplateParameterList *ConceptParams =
      clang::TemplateParameterList::Create(context, DeclLoc, DeclLoc,
                                           {T}, DeclLoc, nullptr);
```

https://github.com/llvm/llvm-project/pull/112600


More information about the cfe-commits mailing list