Joshua Batista c50ef30cce
[ParserHLSL] Attempt to parse HLSL annotations on Field Decls. (#96346)
`MaybeParseHLSLAnnotations` should be run on Field Decls instead of just
assuming that any colon after a field decl is a bitfield. In the case
that HLSL is the language, the code after the colon may be an
annotation. This PR gives the parser a chance to parse the subsequent
text as if it was an HLSL annotation.

The burden of parsing is now on the HLSL parser, but the actual work
needs to be done in handling every case of an hlsl annotation on a field
decl. SV_DispatchThreadID was straightforward enough to implement in
this PR, and tests have been added that the annotation appears as an
attribute in the AST.

Previously, the `hlsl_annotations_on_struct_members.hlsl` test would
result in an error shown below on the line that declares variable `a` in
struct Eg9:
error: use of undeclared identifier
     'SV_DispatchThreadID'
This is because the annotation is parsed as if it was a c++ bit field,
and an identifier
that represents an integer is expected, but not found.

This test ensures that hlsl annotations are parsed when parsing struct
decls.
This test not only ensures we make progress by moving the validation
error from the realm of
C++ and expecting bitfields, to HLSL and a specialized error for the
recognized annotation, but also
validates that the parser does parse the annotation and adds an
attribute to the field decl in the AST.

Fixes https://github.com/llvm/llvm-project/issues/57889
2024-06-28 01:53:54 -07:00

31 lines
1.1 KiB
HLSL

// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -ast-dump -x hlsl -o - %s | FileCheck %s
struct MyBitFields {
// CHECK: FieldDecl 0x{{[0-9a-f]+}} <line:9:3, col:25> col:16 referenced field1 'unsigned int'
// CHECK:-ConstantExpr 0x{{[0-9a-f]+}} <col:25> 'int'
// CHECK:-value: Int 3
// CHECK:-IntegerLiteral 0x{{[0-9a-f]+}} <col:25> 'int' 3
unsigned int field1 : 3; // 3 bits for field1
// CHECK:FieldDecl 0x{{[0-9a-f]+}} <line:15:3, col:25> col:16 referenced field2 'unsigned int'
// CHECK:-ConstantExpr 0x{{[0-9a-f]+}} <col:25> 'int'
// CHECK:-value: Int 4
// CHECK:-IntegerLiteral 0x{{[0-9a-f]+}} <col:25> 'int' 4
unsigned int field2 : 4; // 4 bits for field2
// CHECK:FieldDecl 0x{{[0-9a-f]+}} <line:21:3, col:16> col:7 field3 'int'
// CHECK:-ConstantExpr 0x{{[0-9a-f]+}} <col:16> 'int'
// CHECK:-value: Int 5
// CHECK:-IntegerLiteral 0x{{[0-9a-f]+}} <col:16> 'int' 5
int field3 : 5; // 5 bits for field3 (signed)
};
[numthreads(1,1,1)]
void main() {
MyBitFields m;
m.field1 = 4;
m.field2 = m.field1*2;
}