[clang] [HLSL] Add implicit resource element type concepts to AST (PR #112600)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 16 13:45:41 PDT 2024
================
@@ -483,10 +554,109 @@ 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
+ llvm::ArrayRef<NamedDecl *> TemplateParams = {T};
+ clang::TemplateParameterList *ConceptParams =
+ clang::TemplateParameterList::Create(context, DeclLoc, DeclLoc,
+ TemplateParams, DeclLoc, nullptr);
+
+ DeclarationName DeclName = DeclarationName(&IsValidLineVectorII);
+ Expr *ConstraintExpr = getTypedBufferConstraintExpr(S, DeclLoc, T);
+
+ // Create a ConceptDecl
+ clang::ConceptDecl *conceptDecl = clang::ConceptDecl::Create(
+ context,
+ context.getTranslationUnitDecl(), // DeclContext
+ DeclLoc, // Source location of start of concept
+ DeclName, // Source location of end of concept
+ ConceptParams, // Template type parameter
+ ConstraintExpr // Expression defining the concept
----------------
graphite-app[bot] wrote:
The `DeclName` parameter is incorrectly used as the location in the `ConceptDecl::Create` call. To fix this, replace `DeclName` with `DeclLoc` for the location parameter, and use `DeclName` as the name parameter. Here's the corrected code:
```cpp
clang::ConceptDecl *conceptDecl = clang::ConceptDecl::Create(
context,
context.getTranslationUnitDecl(), // DeclContext
DeclLoc, // Source location of start of concept
DeclLoc, // Source location of end of concept
DeclName, // Name of the concept
ConceptParams, // Template type parameter
ConstraintExpr // Expression defining the concept
);
```
This change ensures that the concept declaration is created with the correct location and name parameters.
*Spotted by [Graphite Reviewer](https://app.graphite.dev/graphite-reviewer/?org=llvm&ref=ai-review-comment)*<i class='graphite__hidden'><br /><br />Is this helpful? React 👍 or 👎 to let us know.</i>
https://github.com/llvm/llvm-project/pull/112600
More information about the cfe-commits
mailing list