================ @@ -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 ---------------- damyanp wrote:
Yeah, it spotted something questionable, but the suggested fix was worse! https://github.com/llvm/llvm-project/pull/112600 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits