forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathASTContextHLSL.cpp
More file actions
1667 lines (1480 loc) · 69 KB
/
ASTContextHLSL.cpp
File metadata and controls
1667 lines (1480 loc) · 69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--- ASTContextHLSL.cpp - HLSL support for AST nodes and operations ---===//
///////////////////////////////////////////////////////////////////////////////
// //
// ASTContextHLSL.cpp //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// This file implements the ASTContext interface for HLSL. //
// //
///////////////////////////////////////////////////////////////////////////////
#include "dxc/DXIL/DxilSemantic.h"
#include "dxc/HLSL/HLOperations.h"
#include "dxc/HlslIntrinsicOp.h"
#include "dxc/Support/Global.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExternalASTSource.h"
#include "clang/AST/HlslBuiltinTypeDeclBuilder.h"
#include "clang/AST/TypeLoc.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Sema/Overload.h"
#include "clang/Sema/Sema.h"
#include "clang/Sema/SemaDiagnostic.h"
using namespace clang;
using namespace hlsl;
static const int FirstTemplateDepth = 0;
static const int FirstParamPosition = 0;
static const bool ForConstFalse =
false; // a construct is targeting a const type
static const bool ForConstTrue =
true; // a construct is targeting a non-const type
static const bool ParameterPackFalse =
false; // template parameter is not an ellipsis.
static const bool TypenameFalse =
false; // 'typename' specified rather than 'class' for a template argument.
static const bool DelayTypeCreationTrue =
true; // delay type creation for a declaration
static const SourceLocation NoLoc; // no source location attribution available
static const bool InlineFalse = false; // namespace is not an inline namespace
static const bool InlineSpecifiedFalse =
false; // function was not specified as inline
static const bool ExplicitFalse =
false; // constructor was not specified as explicit
static const bool IsConstexprFalse = false; // function is not constexpr
static const bool VirtualFalse =
false; // whether the base class is declares 'virtual'
static const bool BaseClassFalse =
false; // whether the base class is declared as 'class' (vs. 'struct')
/// <summary>Names of HLSLScalarType enumeration values, in matching order to
/// HLSLScalarType.</summary>
const char *HLSLScalarTypeNames[] = {
"<unknown>", "bool", "int", "uint",
"dword", "half", "float", "double",
"min10float", "min16float", "min12int", "min16int",
"min16uint", "literal float", "literal int", "int16_t",
"int32_t", "int64_t", "uint16_t", "uint32_t",
"uint64_t", "float16_t", "float32_t", "float64_t",
"int8_t4_packed", "uint8_t4_packed"};
static_assert(HLSLScalarTypeCount == _countof(HLSLScalarTypeNames),
"otherwise scalar constants are not aligned");
static HLSLScalarType FindScalarTypeByName(const char *typeName,
const size_t typeLen,
const LangOptions &langOptions) {
// skipped HLSLScalarType: unknown, literal int, literal float
switch (typeLen) {
case 3: // int
if (typeName[0] == 'i') {
if (strncmp(typeName, "int", 3))
break;
return HLSLScalarType_int;
}
break;
case 4: // bool, uint, half
if (typeName[0] == 'b') {
if (strncmp(typeName, "bool", 4))
break;
return HLSLScalarType_bool;
} else if (typeName[0] == 'u') {
if (strncmp(typeName, "uint", 4))
break;
return HLSLScalarType_uint;
} else if (typeName[0] == 'h') {
if (strncmp(typeName, "half", 4))
break;
return HLSLScalarType_half;
}
break;
case 5: // dword, float
if (typeName[0] == 'd') {
if (strncmp(typeName, "dword", 5))
break;
return HLSLScalarType_dword;
} else if (typeName[0] == 'f') {
if (strncmp(typeName, "float", 5))
break;
return HLSLScalarType_float;
}
break;
case 6: // double
if (typeName[0] == 'd') {
if (strncmp(typeName, "double", 6))
break;
return HLSLScalarType_double;
}
break;
case 7: // int64_t
if (typeName[0] == 'i' && typeName[1] == 'n') {
if (typeName[3] == '6') {
if (strncmp(typeName, "int64_t", 7))
break;
return HLSLScalarType_int64;
}
}
break;
case 8: // min12int, min16int, uint64_t
if (typeName[0] == 'm' && typeName[1] == 'i') {
if (typeName[4] == '2') {
if (strncmp(typeName, "min12int", 8))
break;
return HLSLScalarType_int_min12;
} else if (typeName[4] == '6') {
if (strncmp(typeName, "min16int", 8))
break;
return HLSLScalarType_int_min16;
}
} else if (typeName[0] == 'u' && typeName[1] == 'i') {
if (typeName[4] == '6') {
if (strncmp(typeName, "uint64_t", 8))
break;
return HLSLScalarType_uint64;
}
}
break;
case 9: // min16uint
if (typeName[0] == 'm' && typeName[1] == 'i') {
if (strncmp(typeName, "min16uint", 9))
break;
return HLSLScalarType_uint_min16;
}
break;
case 10: // min10float, min16float
if (typeName[0] == 'm' && typeName[1] == 'i') {
if (typeName[4] == '0') {
if (strncmp(typeName, "min10float", 10))
break;
return HLSLScalarType_float_min10;
}
if (typeName[4] == '6') {
if (strncmp(typeName, "min16float", 10))
break;
return HLSLScalarType_float_min16;
}
}
break;
case 14: // int8_t4_packed
if (typeName[0] == 'i' && typeName[1] == 'n') {
if (strncmp(typeName, "int8_t4_packed", 14))
break;
return HLSLScalarType_int8_4packed;
}
break;
case 15: // uint8_t4_packed
if (typeName[0] == 'u' && typeName[1] == 'i') {
if (strncmp(typeName, "uint8_t4_packed", 15))
break;
return HLSLScalarType_uint8_4packed;
}
break;
default:
break;
}
// fixed width types (int16_t, uint16_t, int32_t, uint32_t, float16_t,
// float32_t, float64_t) are only supported in HLSL 2018
if (langOptions.HLSLVersion >= hlsl::LangStd::v2018) {
switch (typeLen) {
case 7: // int16_t, int32_t
if (typeName[0] == 'i' && typeName[1] == 'n') {
if (!langOptions.UseMinPrecision) {
if (typeName[3] == '1') {
if (strncmp(typeName, "int16_t", 7))
break;
return HLSLScalarType_int16;
}
}
if (typeName[3] == '3') {
if (strncmp(typeName, "int32_t", 7))
break;
return HLSLScalarType_int32;
}
}
break;
case 8: // uint16_t, uint32_t
if (!langOptions.UseMinPrecision) {
if (typeName[0] == 'u' && typeName[1] == 'i') {
if (typeName[4] == '1') {
if (strncmp(typeName, "uint16_t", 8))
break;
return HLSLScalarType_uint16;
}
}
}
if (typeName[4] == '3') {
if (strncmp(typeName, "uint32_t", 8))
break;
return HLSLScalarType_uint32;
}
break;
case 9: // float16_t, float32_t, float64_t
if (typeName[0] == 'f' && typeName[1] == 'l') {
if (!langOptions.UseMinPrecision) {
if (typeName[5] == '1') {
if (strncmp(typeName, "float16_t", 9))
break;
return HLSLScalarType_float16;
}
}
if (typeName[5] == '3') {
if (strncmp(typeName, "float32_t", 9))
break;
return HLSLScalarType_float32;
} else if (typeName[5] == '6') {
if (strncmp(typeName, "float64_t", 9))
break;
return HLSLScalarType_float64;
}
}
}
}
return HLSLScalarType_unknown;
}
/// <summary>Provides the primitive type for lowering matrix types to
/// IR.</summary>
static CanQualType GetHLSLObjectHandleType(ASTContext &context) {
return context.IntTy;
}
static void
AddSubscriptOperator(ASTContext &context, unsigned int templateDepth,
TemplateTypeParmDecl *elementTemplateParamDecl,
NonTypeTemplateParmDecl *colCountTemplateParamDecl,
QualType intType, CXXRecordDecl *templateRecordDecl,
ClassTemplateDecl *vectorTemplateDecl, bool forConst) {
QualType elementType = context.getTemplateTypeParmType(
templateDepth, 0, ParameterPackFalse, elementTemplateParamDecl);
Expr *sizeExpr = DeclRefExpr::Create(
context, NestedNameSpecifierLoc(), NoLoc, colCountTemplateParamDecl,
false,
DeclarationNameInfo(colCountTemplateParamDecl->getDeclName(), NoLoc),
intType, ExprValueKind::VK_RValue);
CXXRecordDecl *vecTemplateRecordDecl = vectorTemplateDecl->getTemplatedDecl();
const clang::Type *vecTy = vecTemplateRecordDecl->getTypeForDecl();
TemplateArgument templateArgs[2] = {TemplateArgument(elementType),
TemplateArgument(sizeExpr)};
TemplateName canonName =
context.getCanonicalTemplateName(TemplateName(vectorTemplateDecl));
QualType vectorType = context.getTemplateSpecializationType(
canonName, templateArgs, _countof(templateArgs), QualType(vecTy, 0));
vectorType = context.getLValueReferenceType(vectorType);
if (forConst)
vectorType = context.getConstType(vectorType);
QualType indexType = intType;
auto methodDecl = CreateObjectFunctionDeclarationWithParams(
context, templateRecordDecl, vectorType, ArrayRef<QualType>(indexType),
ArrayRef<StringRef>(StringRef("index")),
context.DeclarationNames.getCXXOperatorName(OO_Subscript), forConst);
methodDecl->addAttr(HLSLCXXOverloadAttr::CreateImplicit(context));
}
/// <summary>Adds up-front support for HLSL matrix types (just the template
/// declaration).</summary>
void hlsl::AddHLSLMatrixTemplate(ASTContext &context,
ClassTemplateDecl *vectorTemplateDecl,
ClassTemplateDecl **matrixTemplateDecl) {
DXASSERT_NOMSG(matrixTemplateDecl != nullptr);
DXASSERT_NOMSG(vectorTemplateDecl != nullptr);
// Create a matrix template declaration in translation unit scope.
// template<typename element, int row_count, int col_count> matrix { ... }
BuiltinTypeDeclBuilder typeDeclBuilder(context.getTranslationUnitDecl(),
"matrix");
TemplateTypeParmDecl *elementTemplateParamDecl =
typeDeclBuilder.addTypeTemplateParam("element",
(QualType)context.FloatTy);
NonTypeTemplateParmDecl *rowCountTemplateParamDecl =
typeDeclBuilder.addIntegerTemplateParam("row_count", context.IntTy, 4);
NonTypeTemplateParmDecl *colCountTemplateParamDecl =
typeDeclBuilder.addIntegerTemplateParam("col_count", context.IntTy, 4);
typeDeclBuilder.startDefinition();
CXXRecordDecl *templateRecordDecl = typeDeclBuilder.getRecordDecl();
ClassTemplateDecl *classTemplateDecl = typeDeclBuilder.getTemplateDecl();
// Add an 'h' field to hold the handle.
// The type is vector<element, col>[row].
QualType elementType = context.getTemplateTypeParmType(
/*templateDepth*/ 0, 0, ParameterPackFalse, elementTemplateParamDecl);
Expr *sizeExpr = DeclRefExpr::Create(
context, NestedNameSpecifierLoc(), NoLoc, rowCountTemplateParamDecl,
false,
DeclarationNameInfo(rowCountTemplateParamDecl->getDeclName(), NoLoc),
context.IntTy, ExprValueKind::VK_RValue);
Expr *rowSizeExpr = DeclRefExpr::Create(
context, NestedNameSpecifierLoc(), NoLoc, colCountTemplateParamDecl,
false,
DeclarationNameInfo(colCountTemplateParamDecl->getDeclName(), NoLoc),
context.IntTy, ExprValueKind::VK_RValue);
QualType vectorType = context.getDependentSizedExtVectorType(
elementType, rowSizeExpr, SourceLocation());
QualType vectorArrayType = context.getDependentSizedArrayType(
vectorType, sizeExpr, ArrayType::Normal, 0, SourceRange());
typeDeclBuilder.addField("h", vectorArrayType);
typeDeclBuilder.getRecordDecl()->addAttr(
HLSLMatrixAttr::CreateImplicit(context));
// Add an operator[]. The operator ranges from zero to rowcount-1, and returns
// a vector of colcount elements.
const unsigned int templateDepth = 0;
AddSubscriptOperator(context, templateDepth, elementTemplateParamDecl,
colCountTemplateParamDecl, context.UnsignedIntTy,
templateRecordDecl, vectorTemplateDecl, ForConstFalse);
AddSubscriptOperator(context, templateDepth, elementTemplateParamDecl,
colCountTemplateParamDecl, context.UnsignedIntTy,
templateRecordDecl, vectorTemplateDecl, ForConstTrue);
typeDeclBuilder.completeDefinition();
*matrixTemplateDecl = classTemplateDecl;
}
static void AddHLSLVectorSubscriptAttr(Decl *D, ASTContext &context) {
StringRef group = GetHLOpcodeGroupName(HLOpcodeGroup::HLSubscript);
D->addAttr(HLSLIntrinsicAttr::CreateImplicit(
context, group, "",
static_cast<unsigned>(HLSubscriptOpcode::VectorSubscript)));
D->addAttr(HLSLCXXOverloadAttr::CreateImplicit(context));
}
/// <summary>Adds up-front support for HLSL vector types (just the template
/// declaration).</summary>
void hlsl::AddHLSLVectorTemplate(ASTContext &context,
ClassTemplateDecl **vectorTemplateDecl) {
DXASSERT_NOMSG(vectorTemplateDecl != nullptr);
// Create a vector template declaration in translation unit scope.
// template<typename element, int element_count> vector { ... }
BuiltinTypeDeclBuilder typeDeclBuilder(context.getTranslationUnitDecl(),
"vector");
TemplateTypeParmDecl *elementTemplateParamDecl =
typeDeclBuilder.addTypeTemplateParam("element",
(QualType)context.FloatTy);
NonTypeTemplateParmDecl *colCountTemplateParamDecl =
typeDeclBuilder.addIntegerTemplateParam("element_count", context.IntTy,
4);
typeDeclBuilder.startDefinition();
CXXRecordDecl *templateRecordDecl = typeDeclBuilder.getRecordDecl();
ClassTemplateDecl *classTemplateDecl = typeDeclBuilder.getTemplateDecl();
Expr *vecSizeExpr = DeclRefExpr::Create(
context, NestedNameSpecifierLoc(), NoLoc, colCountTemplateParamDecl,
false,
DeclarationNameInfo(colCountTemplateParamDecl->getDeclName(), NoLoc),
context.IntTy, ExprValueKind::VK_RValue);
const unsigned int templateDepth = 0;
QualType resultType = context.getTemplateTypeParmType(
templateDepth, 0, ParameterPackFalse, elementTemplateParamDecl);
QualType vectorType = context.getDependentSizedExtVectorType(
resultType, vecSizeExpr, SourceLocation());
// Add an 'h' field to hold the handle.
typeDeclBuilder.addField("h", vectorType);
typeDeclBuilder.getRecordDecl()->addAttr(
HLSLVectorAttr::CreateImplicit(context));
// Add an operator[]. The operator ranges from zero to colcount-1, and returns
// a scalar.
// ForConstTrue:
QualType refResultType =
context.getConstType(context.getLValueReferenceType(resultType));
CXXMethodDecl *functionDecl = CreateObjectFunctionDeclarationWithParams(
context, templateRecordDecl, refResultType,
ArrayRef<QualType>(context.UnsignedIntTy),
ArrayRef<StringRef>(StringRef("index")),
context.DeclarationNames.getCXXOperatorName(OO_Subscript), ForConstTrue);
AddHLSLVectorSubscriptAttr(functionDecl, context);
// ForConstFalse:
resultType = context.getLValueReferenceType(resultType);
functionDecl = CreateObjectFunctionDeclarationWithParams(
context, templateRecordDecl, resultType,
ArrayRef<QualType>(context.UnsignedIntTy),
ArrayRef<StringRef>(StringRef("index")),
context.DeclarationNames.getCXXOperatorName(OO_Subscript), ForConstFalse);
AddHLSLVectorSubscriptAttr(functionDecl, context);
typeDeclBuilder.completeDefinition();
*vectorTemplateDecl = classTemplateDecl;
}
static void AddRecordAccessMethod(clang::ASTContext &Ctx,
clang::CXXRecordDecl *RD,
clang::QualType ReturnTy,
bool IsGetOrSubscript, bool IsConst,
bool IsArray) {
DeclarationName DeclName =
IsGetOrSubscript ? DeclarationName(&Ctx.Idents.get("Get"))
: Ctx.DeclarationNames.getCXXOperatorName(OO_Subscript);
if (IsConst)
ReturnTy.addConst();
ReturnTy = Ctx.getLValueReferenceType(ReturnTy);
QualType ArgTypes[] = {Ctx.UnsignedIntTy};
ArrayRef<QualType> Types = IsArray ? ArgTypes : ArrayRef<QualType>();
StringRef ArgNames[] = {"Index"};
ArrayRef<StringRef> Names = IsArray ? ArgNames : ArrayRef<StringRef>();
CXXMethodDecl *MethodDecl = CreateObjectFunctionDeclarationWithParams(
Ctx, RD, ReturnTy, Types, Names, DeclName, IsConst);
if (IsGetOrSubscript && IsArray) {
ParmVarDecl *IndexParam = MethodDecl->getParamDecl(0);
Expr *ConstantZero = IntegerLiteral::Create(
Ctx, llvm::APInt(Ctx.getIntWidth(Ctx.UnsignedIntTy), 0),
Ctx.UnsignedIntTy, NoLoc);
IndexParam->setDefaultArg(ConstantZero);
}
StringRef OpcodeGroup = GetHLOpcodeGroupName(HLOpcodeGroup::HLSubscript);
unsigned Opcode = static_cast<unsigned>(HLSubscriptOpcode::DefaultSubscript);
MethodDecl->addAttr(
HLSLIntrinsicAttr::CreateImplicit(Ctx, OpcodeGroup, "", Opcode));
MethodDecl->addAttr(HLSLCXXOverloadAttr::CreateImplicit(Ctx));
}
static void AddRecordGetMethods(clang::ASTContext &Ctx,
clang::CXXRecordDecl *RD,
clang::QualType ReturnTy, bool IsConstOnly,
bool IsArray) {
if (!IsConstOnly)
AddRecordAccessMethod(Ctx, RD, ReturnTy, true, false, IsArray);
AddRecordAccessMethod(Ctx, RD, ReturnTy, true, true, IsArray);
}
static void AddRecordSubscriptAccess(clang::ASTContext &Ctx,
clang::CXXRecordDecl *RD,
clang::QualType ReturnTy,
bool IsConstOnly) {
if (!IsConstOnly)
AddRecordAccessMethod(Ctx, RD, ReturnTy, false, false, true);
AddRecordAccessMethod(Ctx, RD, ReturnTy, false, true, true);
}
/// <summary>Adds up-front support for HLSL *NodeOutputRecords template
/// types.</summary>
void hlsl::AddHLSLNodeOutputRecordTemplate(
ASTContext &context, DXIL::NodeIOKind Type,
ClassTemplateDecl **outputRecordTemplateDecl,
bool isCompleteType /*= true*/) {
DXASSERT_NOMSG(outputRecordTemplateDecl != nullptr);
StringRef templateName = HLSLNodeObjectAttr::ConvertRecordTypeToStr(Type);
// Create a *NodeOutputRecords template declaration in translation unit scope.
BuiltinTypeDeclBuilder typeDeclBuilder(context.getTranslationUnitDecl(),
templateName,
TagDecl::TagKind::TTK_Struct);
TemplateTypeParmDecl *outputTemplateParamDecl =
typeDeclBuilder.addTypeTemplateParam("recordType");
typeDeclBuilder.startDefinition();
ClassTemplateDecl *classTemplateDecl = typeDeclBuilder.getTemplateDecl();
// Add an 'h' field to hold the handle.
typeDeclBuilder.addField("h", GetHLSLObjectHandleType(context));
typeDeclBuilder.getRecordDecl()->addAttr(
HLSLNodeObjectAttr::CreateImplicit(context, Type));
QualType elementType = context.getTemplateTypeParmType(
0, 0, ParameterPackFalse, outputTemplateParamDecl);
CXXRecordDecl *record = typeDeclBuilder.getRecordDecl();
// Subscript operator is required for Node Array Types.
AddRecordSubscriptAccess(context, record, elementType, false);
AddRecordGetMethods(context, record, elementType, false, true);
if (isCompleteType)
typeDeclBuilder.completeDefinition();
*outputRecordTemplateDecl = classTemplateDecl;
}
/// <summary>
/// Adds a new record type in the specified context with the given name. The
/// record type will have a handle field.
/// </summary>
CXXRecordDecl *
hlsl::DeclareRecordTypeWithHandleAndNoMemberFunctions(ASTContext &context,
StringRef name) {
BuiltinTypeDeclBuilder typeDeclBuilder(context.getTranslationUnitDecl(), name,
TagDecl::TagKind::TTK_Struct);
typeDeclBuilder.startDefinition();
typeDeclBuilder.addField("h", GetHLSLObjectHandleType(context));
typeDeclBuilder.completeDefinition();
return typeDeclBuilder.getRecordDecl();
}
/// <summary>
/// Adds a new record type in the specified context with the given name. The
/// record type will have a handle field.
/// </summary>
CXXRecordDecl *
hlsl::DeclareRecordTypeWithHandle(ASTContext &context, StringRef name,
bool isCompleteType /*= true */,
InheritableAttr *Attr) {
BuiltinTypeDeclBuilder typeDeclBuilder(context.getTranslationUnitDecl(), name,
TagDecl::TagKind::TTK_Struct);
typeDeclBuilder.startDefinition();
typeDeclBuilder.addField("h", GetHLSLObjectHandleType(context));
if (Attr)
typeDeclBuilder.getRecordDecl()->addAttr(Attr);
if (isCompleteType)
return typeDeclBuilder.completeDefinition();
return typeDeclBuilder.getRecordDecl();
}
AvailabilityAttr *ConstructAvailabilityAttribute(clang::ASTContext &context,
VersionTuple Introduced) {
AvailabilityAttr *AAttr = AvailabilityAttr::CreateImplicit(
context, &context.Idents.get(""), clang::VersionTuple(6, 9),
clang::VersionTuple(), clang::VersionTuple(), false, "");
return AAttr;
}
// creates a global static constant unsigned integer with value.
// equivalent to: static const uint name = val;
static void AddConstUInt(clang::ASTContext &context, DeclContext *DC,
StringRef name, unsigned val,
AvailabilityAttr *AAttr = nullptr) {
IdentifierInfo &Id = context.Idents.get(name, tok::TokenKind::identifier);
QualType type = context.getConstType(context.UnsignedIntTy);
VarDecl *varDecl = VarDecl::Create(context, DC, NoLoc, NoLoc, &Id, type,
context.getTrivialTypeSourceInfo(type),
clang::StorageClass::SC_Static);
Expr *exprVal = IntegerLiteral::Create(
context, llvm::APInt(context.getIntWidth(type), val), type, NoLoc);
varDecl->setInit(exprVal);
varDecl->setImplicit(true);
if (AAttr)
varDecl->addAttr(AAttr);
DC->addDecl(varDecl);
}
static void AddConstUInt(clang::ASTContext &context, StringRef name,
unsigned val) {
AddConstUInt(context, context.getTranslationUnitDecl(), name, val);
}
// Adds a top-level enum with the given enumerants.
struct Enumerant {
StringRef name;
unsigned value;
AvailabilityAttr *avail = nullptr;
};
static void AddTypedefPseudoEnum(ASTContext &context, StringRef name,
ArrayRef<Enumerant> enumerants) {
DeclContext *curDC = context.getTranslationUnitDecl();
// typedef uint <name>;
IdentifierInfo &enumId = context.Idents.get(name, tok::TokenKind::identifier);
TypeSourceInfo *uintTypeSource =
context.getTrivialTypeSourceInfo(context.UnsignedIntTy, NoLoc);
TypedefDecl *enumDecl = TypedefDecl::Create(context, curDC, NoLoc, NoLoc,
&enumId, uintTypeSource);
curDC->addDecl(enumDecl);
enumDecl->setImplicit(true);
// static const uint <enumerant.name> = <enumerant.value>;
for (const Enumerant &enumerant : enumerants) {
AddConstUInt(context, curDC, enumerant.name, enumerant.value,
enumerant.avail);
}
}
/// <summary> Adds all constants and enums for ray tracing </summary>
void hlsl::AddRaytracingConstants(ASTContext &context) {
// Create aversion tuple for availability attributes
// for the RAYQUERY_FLAG enum
VersionTuple VT69 = VersionTuple(6, 9);
AddTypedefPseudoEnum(
context, "RAY_FLAG",
{{"RAY_FLAG_NONE", (unsigned)DXIL::RayFlag::None},
{"RAY_FLAG_FORCE_OPAQUE", (unsigned)DXIL::RayFlag::ForceOpaque},
{"RAY_FLAG_FORCE_NON_OPAQUE", (unsigned)DXIL::RayFlag::ForceNonOpaque},
{"RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH",
(unsigned)DXIL::RayFlag::AcceptFirstHitAndEndSearch},
{"RAY_FLAG_SKIP_CLOSEST_HIT_SHADER",
(unsigned)DXIL::RayFlag::SkipClosestHitShader},
{"RAY_FLAG_CULL_BACK_FACING_TRIANGLES",
(unsigned)DXIL::RayFlag::CullBackFacingTriangles},
{"RAY_FLAG_CULL_FRONT_FACING_TRIANGLES",
(unsigned)DXIL::RayFlag::CullFrontFacingTriangles},
{"RAY_FLAG_CULL_OPAQUE", (unsigned)DXIL::RayFlag::CullOpaque},
{"RAY_FLAG_CULL_NON_OPAQUE", (unsigned)DXIL::RayFlag::CullNonOpaque},
{"RAY_FLAG_SKIP_TRIANGLES", (unsigned)DXIL::RayFlag::SkipTriangles},
{"RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES",
(unsigned)DXIL::RayFlag::SkipProceduralPrimitives},
{"RAY_FLAG_FORCE_OMM_2_STATE", (unsigned)DXIL::RayFlag::ForceOMM2State,
ConstructAvailabilityAttribute(context, VT69)}});
AddTypedefPseudoEnum(
context, "RAYQUERY_FLAG",
{{"RAYQUERY_FLAG_NONE", (unsigned)DXIL::RayQueryFlag::None},
{"RAYQUERY_FLAG_ALLOW_OPACITY_MICROMAPS",
(unsigned)DXIL::RayQueryFlag::AllowOpacityMicromaps,
ConstructAvailabilityAttribute(context, VT69)}});
AddTypedefPseudoEnum(
context, "COMMITTED_STATUS",
{{"COMMITTED_NOTHING", (unsigned)DXIL::CommittedStatus::CommittedNothing},
{"COMMITTED_TRIANGLE_HIT",
(unsigned)DXIL::CommittedStatus::CommittedTriangleHit},
{"COMMITTED_PROCEDURAL_PRIMITIVE_HIT",
(unsigned)DXIL::CommittedStatus::CommittedProceduralPrimitiveHit}});
AddTypedefPseudoEnum(
context, "CANDIDATE_TYPE",
{{"CANDIDATE_NON_OPAQUE_TRIANGLE",
(unsigned)DXIL::CandidateType::CandidateNonOpaqueTriangle},
{"CANDIDATE_PROCEDURAL_PRIMITIVE",
(unsigned)DXIL::CandidateType::CandidateProceduralPrimitive}});
// static const uint HIT_KIND_* = *;
AddConstUInt(context, StringRef("HIT_KIND_NONE"),
(unsigned)DXIL::HitKind::None);
AddConstUInt(context, StringRef("HIT_KIND_TRIANGLE_FRONT_FACE"),
(unsigned)DXIL::HitKind::TriangleFrontFace);
AddConstUInt(context, StringRef("HIT_KIND_TRIANGLE_BACK_FACE"),
(unsigned)DXIL::HitKind::TriangleBackFace);
AddConstUInt(
context,
StringRef(
"STATE_OBJECT_FLAGS_ALLOW_LOCAL_DEPENDENCIES_ON_EXTERNAL_DEFINITONS"),
(unsigned)
DXIL::StateObjectFlags::AllowLocalDependenciesOnExternalDefinitions);
AddConstUInt(
context,
StringRef("STATE_OBJECT_FLAGS_ALLOW_EXTERNAL_DEPENDENCIES_ON_LOCAL_"
"DEFINITIONS"),
(unsigned)
DXIL::StateObjectFlags::AllowExternalDependenciesOnLocalDefinitions);
// The above "_FLAGS_" was a typo, leaving in to avoid breaking anyone.
// Supposed to be _FLAG_ below.
AddConstUInt(
context,
StringRef(
"STATE_OBJECT_FLAG_ALLOW_LOCAL_DEPENDENCIES_ON_EXTERNAL_DEFINITONS"),
(unsigned)
DXIL::StateObjectFlags::AllowLocalDependenciesOnExternalDefinitions);
AddConstUInt(
context,
StringRef(
"STATE_OBJECT_FLAG_ALLOW_EXTERNAL_DEPENDENCIES_ON_LOCAL_DEFINITIONS"),
(unsigned)
DXIL::StateObjectFlags::AllowExternalDependenciesOnLocalDefinitions);
AddConstUInt(context,
StringRef("STATE_OBJECT_FLAG_ALLOW_STATE_OBJECT_ADDITIONS"),
(unsigned)DXIL::StateObjectFlags::AllowStateObjectAdditions);
AddConstUInt(context, StringRef("RAYTRACING_PIPELINE_FLAG_NONE"),
(unsigned)DXIL::RaytracingPipelineFlags::None);
AddConstUInt(context, StringRef("RAYTRACING_PIPELINE_FLAG_SKIP_TRIANGLES"),
(unsigned)DXIL::RaytracingPipelineFlags::SkipTriangles);
AddConstUInt(
context, StringRef("RAYTRACING_PIPELINE_FLAG_SKIP_PROCEDURAL_PRIMITIVES"),
(unsigned)DXIL::RaytracingPipelineFlags::SkipProceduralPrimitives);
AddConstUInt(context, context.getTranslationUnitDecl(),
StringRef("RAYTRACING_PIPELINE_FLAG_ALLOW_OPACITY_MICROMAPS"),
(unsigned)DXIL::RaytracingPipelineFlags::AllowOpacityMicromaps,
ConstructAvailabilityAttribute(context, VT69));
}
/// <summary> Adds all constants and enums for sampler feedback </summary>
void hlsl::AddSamplerFeedbackConstants(ASTContext &context) {
AddConstUInt(context, StringRef("SAMPLER_FEEDBACK_MIN_MIP"),
(unsigned)DXIL::SamplerFeedbackType::MinMip);
AddConstUInt(context, StringRef("SAMPLER_FEEDBACK_MIP_REGION_USED"),
(unsigned)DXIL::SamplerFeedbackType::MipRegionUsed);
}
/// <summary> Adds all enums for Barrier intrinsic</summary>
void hlsl::AddBarrierConstants(ASTContext &context) {
AddTypedefPseudoEnum(
context, "MEMORY_TYPE_FLAG",
{{"UAV_MEMORY", (unsigned)DXIL::MemoryTypeFlag::UavMemory},
{"GROUP_SHARED_MEMORY",
(unsigned)DXIL::MemoryTypeFlag::GroupSharedMemory},
{"NODE_INPUT_MEMORY", (unsigned)DXIL::MemoryTypeFlag::NodeInputMemory},
{"NODE_OUTPUT_MEMORY", (unsigned)DXIL::MemoryTypeFlag::NodeOutputMemory},
{"ALL_MEMORY", (unsigned)DXIL::MemoryTypeFlag::AllMemory}});
AddTypedefPseudoEnum(
context, "BARRIER_SEMANTIC_FLAG",
{{"GROUP_SYNC", (unsigned)DXIL::BarrierSemanticFlag::GroupSync},
{"GROUP_SCOPE", (unsigned)DXIL::BarrierSemanticFlag::GroupScope},
{"DEVICE_SCOPE", (unsigned)DXIL::BarrierSemanticFlag::DeviceScope}});
}
static Expr *IntConstantAsBoolExpr(clang::Sema &sema, uint64_t value) {
return sema
.ImpCastExprToType(sema.ActOnIntegerConstant(NoLoc, value).get(),
sema.getASTContext().BoolTy, CK_IntegralToBoolean)
.get();
}
static CXXRecordDecl *CreateStdStructWithStaticBool(clang::ASTContext &context,
NamespaceDecl *stdNamespace,
IdentifierInfo &trueTypeId,
IdentifierInfo &valueId,
Expr *trueExpression) {
// struct true_type { static const bool value = true; }
TypeSourceInfo *boolTypeSource =
context.getTrivialTypeSourceInfo(context.BoolTy.withConst());
CXXRecordDecl *trueTypeDecl = CXXRecordDecl::Create(
context, TagTypeKind::TTK_Struct, stdNamespace, NoLoc, NoLoc, &trueTypeId,
nullptr, DelayTypeCreationTrue);
// static fields are variables in the AST
VarDecl *trueValueDecl =
VarDecl::Create(context, trueTypeDecl, NoLoc, NoLoc, &valueId,
context.BoolTy.withConst(), boolTypeSource, SC_Static);
trueValueDecl->setInit(trueExpression);
trueValueDecl->setConstexpr(true);
trueValueDecl->setAccess(AS_public);
trueTypeDecl->setLexicalDeclContext(stdNamespace);
trueTypeDecl->startDefinition();
trueTypeDecl->addDecl(trueValueDecl);
trueTypeDecl->completeDefinition();
stdNamespace->addDecl(trueTypeDecl);
return trueTypeDecl;
}
static void DefineRecordWithBase(CXXRecordDecl *decl,
DeclContext *lexicalContext,
const CXXBaseSpecifier *base) {
decl->setLexicalDeclContext(lexicalContext);
decl->startDefinition();
decl->setBases(&base, 1);
decl->completeDefinition();
lexicalContext->addDecl(decl);
}
static void SetPartialExplicitSpecialization(
ClassTemplateDecl *templateDecl,
ClassTemplatePartialSpecializationDecl *specializationDecl) {
specializationDecl->setSpecializationKind(TSK_ExplicitSpecialization);
templateDecl->AddPartialSpecialization(specializationDecl, nullptr);
}
static void CreateIsEqualSpecialization(
ASTContext &context, ClassTemplateDecl *templateDecl,
TemplateName &templateName, DeclContext *lexicalContext,
const CXXBaseSpecifier *base, TemplateParameterList *templateParamList,
TemplateArgument (&templateArgs)[2]) {
QualType specializationCanonType = context.getTemplateSpecializationType(
templateName, templateArgs, _countof(templateArgs));
TemplateArgumentListInfo templateArgsListInfo =
TemplateArgumentListInfo(NoLoc, NoLoc);
templateArgsListInfo.addArgument(TemplateArgumentLoc(
templateArgs[0],
context.getTrivialTypeSourceInfo(templateArgs[0].getAsType())));
templateArgsListInfo.addArgument(TemplateArgumentLoc(
templateArgs[1],
context.getTrivialTypeSourceInfo(templateArgs[1].getAsType())));
ClassTemplatePartialSpecializationDecl *specializationDecl =
ClassTemplatePartialSpecializationDecl::Create(
context, TTK_Struct, lexicalContext, NoLoc, NoLoc, templateParamList,
templateDecl, templateArgs, _countof(templateArgs),
templateArgsListInfo, specializationCanonType, nullptr);
context.getTagDeclType(specializationDecl); // Fault this in now.
DefineRecordWithBase(specializationDecl, lexicalContext, base);
SetPartialExplicitSpecialization(templateDecl, specializationDecl);
}
/// <summary>Adds the implementation for std::is_equal.</summary>
void hlsl::AddStdIsEqualImplementation(clang::ASTContext &context,
clang::Sema &sema) {
// The goal is to support std::is_same<T, T>::value for testing purposes, in a
// manner that can evolve into a compliant feature in the future.
//
// The definitions necessary are as follows (all in the std namespace).
// template <class T, T v>
// struct integral_constant {
// typedef T value_type;
// static const value_type value = v;
// operator value_type() { return value; }
// };
//
// typedef integral_constant<bool, true> true_type;
// typedef integral_constant<bool, false> false_type;
//
// template<typename T, typename U> struct is_same : public false_type {};
// template<typename T> struct is_same<T, T> : public
// true_type{};
//
// We instead use these simpler definitions for true_type and false_type.
// struct false_type { static const bool value = false; };
// struct true_type { static const bool value = true; };
DeclContext *tuContext = context.getTranslationUnitDecl();
IdentifierInfo &stdId =
context.Idents.get(StringRef("std"), tok::TokenKind::identifier);
IdentifierInfo &trueTypeId =
context.Idents.get(StringRef("true_type"), tok::TokenKind::identifier);
IdentifierInfo &falseTypeId =
context.Idents.get(StringRef("false_type"), tok::TokenKind::identifier);
IdentifierInfo &valueId =
context.Idents.get(StringRef("value"), tok::TokenKind::identifier);
IdentifierInfo &isSameId =
context.Idents.get(StringRef("is_same"), tok::TokenKind::identifier);
IdentifierInfo &tId =
context.Idents.get(StringRef("T"), tok::TokenKind::identifier);
IdentifierInfo &vId =
context.Idents.get(StringRef("V"), tok::TokenKind::identifier);
Expr *trueExpression = IntConstantAsBoolExpr(sema, 1);
Expr *falseExpression = IntConstantAsBoolExpr(sema, 0);
// namespace std
NamespaceDecl *stdNamespace = NamespaceDecl::Create(
context, tuContext, InlineFalse, NoLoc, NoLoc, &stdId, nullptr);
CXXRecordDecl *trueTypeDecl = CreateStdStructWithStaticBool(
context, stdNamespace, trueTypeId, valueId, trueExpression);
CXXRecordDecl *falseTypeDecl = CreateStdStructWithStaticBool(
context, stdNamespace, falseTypeId, valueId, falseExpression);
// template<typename T, typename U> struct is_same : public false_type {};
CXXRecordDecl *isSameFalseRecordDecl =
CXXRecordDecl::Create(context, TagTypeKind::TTK_Struct, stdNamespace,
NoLoc, NoLoc, &isSameId, nullptr, false);
TemplateTypeParmDecl *tParam = TemplateTypeParmDecl::Create(
context, stdNamespace, NoLoc, NoLoc, FirstTemplateDepth,
FirstParamPosition, &tId, TypenameFalse, ParameterPackFalse);
TemplateTypeParmDecl *uParam = TemplateTypeParmDecl::Create(
context, stdNamespace, NoLoc, NoLoc, FirstTemplateDepth,
FirstParamPosition + 1, &vId, TypenameFalse, ParameterPackFalse);
NamedDecl *falseParams[] = {tParam, uParam};
TemplateParameterList *falseParamList = TemplateParameterList::Create(
context, NoLoc, NoLoc, falseParams, _countof(falseParams), NoLoc);
ClassTemplateDecl *isSameFalseTemplateDecl = ClassTemplateDecl::Create(
context, stdNamespace, NoLoc, DeclarationName(&isSameId), falseParamList,
isSameFalseRecordDecl, nullptr);
context.getTagDeclType(isSameFalseRecordDecl); // Fault this in now.
CXXBaseSpecifier *falseBase = new (context) CXXBaseSpecifier(
SourceRange(), VirtualFalse, BaseClassFalse, AS_public,
context.getTrivialTypeSourceInfo(context.getTypeDeclType(falseTypeDecl)),
NoLoc);
isSameFalseRecordDecl->setDescribedClassTemplate(isSameFalseTemplateDecl);
isSameFalseTemplateDecl->setLexicalDeclContext(stdNamespace);
DefineRecordWithBase(isSameFalseRecordDecl, stdNamespace, falseBase);
// is_same for 'true' is a specialization of is_same for 'false', taking a
// single T, where both T will match
// template<typename T> struct is_same<T, T> : public true_type{};
TemplateName tn = TemplateName(isSameFalseTemplateDecl);
NamedDecl *trueParams[] = {tParam};
TemplateParameterList *trueParamList = TemplateParameterList::Create(
context, NoLoc, NoLoc, trueParams, _countof(trueParams), NoLoc);
CXXBaseSpecifier *trueBase = new (context) CXXBaseSpecifier(
SourceRange(), VirtualFalse, BaseClassFalse, AS_public,
context.getTrivialTypeSourceInfo(context.getTypeDeclType(trueTypeDecl)),
NoLoc);
TemplateArgument ta = TemplateArgument(
context.getCanonicalType(context.getTypeDeclType(tParam)));
TemplateArgument isSameTrueTemplateArgs[] = {ta, ta};
CreateIsEqualSpecialization(context, isSameFalseTemplateDecl, tn,
stdNamespace, trueBase, trueParamList,
isSameTrueTemplateArgs);
stdNamespace->addDecl(isSameFalseTemplateDecl);
stdNamespace->setImplicit(true);
tuContext->addDecl(stdNamespace);
// This could be a parameter if ever needed.
const bool SupportExtensions = true;
// Consider right-hand const and right-hand ref to be true for is_same:
// template<typename T> struct is_same<T, const T> : public true_type{};
// template<typename T> struct is_same<T, T&> : public true_type{};
if (SupportExtensions) {
TemplateArgument trueConstArg = TemplateArgument(
context.getCanonicalType(context.getTypeDeclType(tParam)).withConst());
TemplateArgument isSameTrueConstTemplateArgs[] = {ta, trueConstArg};
CreateIsEqualSpecialization(context, isSameFalseTemplateDecl, tn,
stdNamespace, trueBase, trueParamList,
isSameTrueConstTemplateArgs);
TemplateArgument trueRefArg =
TemplateArgument(context.getLValueReferenceType(
context.getCanonicalType(context.getTypeDeclType(tParam))));
TemplateArgument isSameTrueRefTemplateArgs[] = {ta, trueRefArg};
CreateIsEqualSpecialization(context, isSameFalseTemplateDecl, tn,
stdNamespace, trueBase, trueParamList,
isSameTrueRefTemplateArgs);
}
}
/// <summary>
/// Adds a new template type in the specified context with the given name. The
/// record type will have a handle field.
/// </summary>
/// <parm name="context">AST context to which template will be added.</param>
/// <parm name="typeName">Name of template to create.</param>
/// <parm name="templateArgCount">Number of template arguments (one or
/// two).</param> <parm name="defaultTypeArgValue">If assigned, the default
/// argument for the element template.</param>
CXXRecordDecl *hlsl::DeclareTemplateTypeWithHandle(
ASTContext &context, StringRef name, uint8_t templateArgCount,
TypeSourceInfo *defaultTypeArgValue, InheritableAttr *Attr) {
return DeclareTemplateTypeWithHandleInDeclContext(
context, context.getTranslationUnitDecl(), name, templateArgCount,
defaultTypeArgValue, Attr);
}
CXXRecordDecl *hlsl::DeclareTemplateTypeWithHandleInDeclContext(
ASTContext &context, DeclContext *declContext, StringRef name,
uint8_t templateArgCount, TypeSourceInfo *defaultTypeArgValue,
InheritableAttr *Attr) {
DXASSERT(templateArgCount != 0,
"otherwise caller should be creating a class or struct");
DXASSERT(templateArgCount <= 2, "otherwise the function needs to be updated "
"for a different template pattern");
// Create an object template declaration in translation unit scope.
// templateArgCount=1: template<typename element> typeName { ... }
// templateArgCount=2: template<typename element, int count> typeName { ... }
BuiltinTypeDeclBuilder typeDeclBuilder(declContext, name);
TemplateTypeParmDecl *elementTemplateParamDecl =
typeDeclBuilder.addTypeTemplateParam("element", defaultTypeArgValue);
NonTypeTemplateParmDecl *countTemplateParamDecl = nullptr;
if (templateArgCount > 1)
countTemplateParamDecl =
typeDeclBuilder.addIntegerTemplateParam("count", context.IntTy, 0);
typeDeclBuilder.startDefinition();
CXXRecordDecl *templateRecordDecl = typeDeclBuilder.getRecordDecl();
// Add an 'h' field to hold the handle.
QualType elementType = context.getTemplateTypeParmType(
/*templateDepth*/ 0, 0, ParameterPackFalse, elementTemplateParamDecl);
// Only need array type for inputpatch and outputpatch.
if (Attr && isa<HLSLTessPatchAttr>(Attr)) {
DXASSERT(templateArgCount == 2, "Tess patches need 2 template params");
Expr *countExpr = DeclRefExpr::Create(
context, NestedNameSpecifierLoc(), NoLoc, countTemplateParamDecl, false,
DeclarationNameInfo(countTemplateParamDecl->getDeclName(), NoLoc),
context.IntTy, ExprValueKind::VK_RValue);
elementType = context.getDependentSizedArrayType(
elementType, countExpr, ArrayType::ArraySizeModifier::Normal, 0,
SourceRange());
// InputPatch and OutputPatch also have a "Length" static const member for
// the number of control points
IdentifierInfo &lengthId =
context.Idents.get(StringRef("Length"), tok::TokenKind::identifier);
TypeSourceInfo *lengthTypeSource =
context.getTrivialTypeSourceInfo(context.IntTy.withConst());
VarDecl *lengthValueDecl =