python3kgae created this revision.
python3kgae added reviewers: pow2clk, beanz, bogner.
Herald added a subscriber: Anastasia.
Herald added a project: All.
python3kgae requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Support ConstantBuffer by define it like

  template<typename T>
  struct ConstantBuffer : public T {
  };


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136743

Files:
  clang/lib/Sema/HLSLExternalSemaSource.cpp
  clang/test/AST/HLSL/CBV.hlsl
  clang/test/SemaHLSL/BuiltIns/CBV.hlsl

Index: clang/test/SemaHLSL/BuiltIns/CBV.hlsl
===================================================================
--- /dev/null
+++ clang/test/SemaHLSL/BuiltIns/CBV.hlsl
@@ -0,0 +1,8 @@
+// RUN: not %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -fsyntax-only -verify %s 2>&1 | FileCheck %s
+
+// CHECK:error: 'error' diagnostics seen but not expected:
+// CHECK-NEXT:  (frontend): base specifier must name a class
+// CHECK:1 error generated
+
+// expected-note@+1 {{in instantiation of template class 'hlsl::ConstantBuffer<float>' requested here}}
+ConstantBuffer<float> CB;
Index: clang/test/AST/HLSL/CBV.hlsl
===================================================================
--- /dev/null
+++ clang/test/AST/HLSL/CBV.hlsl
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -fsyntax-only -ast-dump %s | FileCheck %s
+
+// CHECK:-ClassTemplateDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit ConstantBuffer
+// CHECK-NEXT:-TemplateTypeParmDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> class depth 0 index 0 element_type
+// CHECK-NEXT:-CXXRecordDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit class ConstantBuffer definition
+// CHECK:-public 'element_type'
+// CHECK-NEXT:-FinalAttr 0x{{[0-9a-f]+}} <<invalid sloc>> Implicit final
+// CHECK-NEXT:-HLSLResourceAttr 0x{{[0-9a-f]+}} <<invalid sloc>> Implicit CBuffer CBufferKind
+
+// CHECK:-CXXRecordDecl 0x{{[0-9a-f]+}} <col:1, col:8> col:8 implicit struct S
+// CHECK-NEXT:-FieldDecl 0x[[A:[0-9a-f]+]] <line:13:5, col:11> col:11 referenced a 'float'
+struct S {
+    float a;
+};
+
+// CHECK:VarDecl 0x[[CB:[0-9a-f]+]] <line:17:1, col:19> col:19 used CB 'ConstantBuffer<S>':'hlsl::ConstantBuffer<S>'
+ConstantBuffer<S> CB;
+
+float foo() {
+// CHECK:ReturnStmt 0x{{[0-9a-f]+}} <line:25:5, col:15>
+// CHECK-NEXT:-ImplicitCastExpr 0x{{[0-9a-f]+}} <col:12, col:15> 'float' <LValueToRValue>
+// CHECK-NEXT:-MemberExpr 0x{{[0-9a-f]+}} <col:12, col:15> 'float' lvalue .a 0x[[A]]
+// CHECK-NEXT:-ImplicitCastExpr 0x{{[0-9a-f]+}} <col:12> 'S' lvalue <UncheckedDerivedToBase (S)>
+// CHECK-NEXT:-DeclRefExpr 0x{{[0-9a-f]+}} <col:12> 'ConstantBuffer<S>':'hlsl::ConstantBuffer<S>' lvalue Var 0x[[CB]] 'CB' 'ConstantBuffer<S>':'hlsl::ConstantBuffer<S>'
+    return CB.a;
+}
Index: clang/lib/Sema/HLSLExternalSemaSource.cpp
===================================================================
--- clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -34,6 +34,7 @@
   ClassTemplateDecl *PrevTemplate = nullptr;
   NamespaceDecl *HLSLNamespace = nullptr;
   llvm::StringMap<FieldDecl *> Fields;
+  llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
 
   BuiltinTypeDeclBuilder(CXXRecordDecl *R) : Record(R) {
     Record->startDefinition();
@@ -306,6 +307,27 @@
     return *this;
   }
 
+  BuiltinTypeDeclBuilder &addBase(TypeDecl *Parent) {
+    if (Record->isCompleteDefinition())
+      return *this;
+    TypeSourceInfo *TInfo =
+        HLSLNamespace->getASTContext().getTrivialTypeSourceInfo(
+            QualType(Parent->getTypeForDecl(), 0));
+    CXXBaseSpecifier Base(SourceRange(), false, false,
+                          AccessSpecifier::AS_public, TInfo, SourceLocation());
+    Bases.emplace_back(&Base);
+    return *this;
+  }
+
+  BuiltinTypeDeclBuilder &setBases() {
+    if (Record->isCompleteDefinition())
+      return *this;
+
+    Record->setBases(Bases.data(), Bases.size());
+    Bases.clear();
+    return *this;
+  }
+
   TemplateParameterListBuilder addTemplateArgumentList();
 };
 
@@ -336,6 +358,13 @@
     return *this;
   }
 
+  TemplateParameterListBuilder &addTypeParameterAsBase(uint32_t I) {
+    if (Params.size() <= I)
+      return *this;
+    Builder.addBase(cast<TypeDecl>(Params[I]));
+    return *this;
+  }
+
   BuiltinTypeDeclBuilder &finalizeTemplateArgs() {
     if (Params.empty())
       return Builder;
@@ -466,6 +495,20 @@
                      .addHandleMember(AccessSpecifier::AS_public)
                      .completeDefinition()
                      .Record;
+  // Add CBV as
+  // template<typename T>
+  // struct ConstantBuffer : T {
+  // };
+  BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ConstantBuffer")
+      .startDefinition()
+      .addTemplateArgumentList()
+      .addTypeParameter("element_type")
+      .addTypeParameterAsBase(0)
+      .finalizeTemplateArgs()
+      .setBases()
+      .annotateResourceClass(HLSLResourceAttr::CBuffer,
+                             HLSLResourceAttr::CBufferKind)
+      .completeDefinition();
 }
 
 void HLSLExternalSemaSource::forwardDeclareHLSLTypes() {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to