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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
|
// slang-ast-type.h
#pragma once
#include "slang-ast-base.h"
#include "slang-ast-decl.h"
//
#include "slang-ast-type.h.fiddle"
FIDDLE()
namespace Slang
{
// Syntax class definitions for types.
// The type of a reference to an overloaded name
FIDDLE()
class OverloadGroupType : public Type
{
FIDDLE(...)
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
};
// The type of an initializer-list expression (before it has
// been coerced to some other type)
FIDDLE()
class InitializerListType : public Type
{
FIDDLE(...)
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
};
// The type of an expression that was erroneous
FIDDLE()
class ErrorType : public Type
{
FIDDLE(...)
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
};
// The bottom/empty type that has no values.
FIDDLE()
class BottomType : public Type
{
FIDDLE(...)
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
};
// A type that takes the form of a reference to some declaration
FIDDLE()
class DeclRefType : public Type
{
FIDDLE(...)
static Type* create(ASTBuilder* astBuilder, DeclRef<Decl> declRef);
DeclRef<Decl> getDeclRef() const { return DeclRef<Decl>(as<DeclRefBase>(getOperand(0))); }
DeclRefBase* getDeclRefBase() const { return as<DeclRefBase>(getOperand(0)); }
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
DeclRefType(DeclRefBase* declRefBase) { setOperands(declRefBase); }
};
template<typename T>
DeclRef<T> isDeclRefTypeOf(Val* type)
{
if (auto declRefType = as<DeclRefType>(type))
{
return declRefType->getDeclRef().template as<T>();
}
return DeclRef<T>();
}
bool isTypePack(Type* type);
bool isAbstractTypePack(Type* type);
// Base class for types that can be used in arithmetic expressions
FIDDLE(abstract)
class ArithmeticExpressionType : public DeclRefType
{
FIDDLE(...)
BasicExpressionType* getScalarType();
// Overrides should be public so base classes can access
BasicExpressionType* _getScalarTypeOverride();
};
FIDDLE()
class BasicExpressionType : public ArithmeticExpressionType
{
FIDDLE(...)
BaseType getBaseType() const;
// Overrides should be public so base classes can access
BasicExpressionType* _getScalarTypeOverride();
BasicExpressionType(DeclRefBase* inDeclRef) { setOperands(inDeclRef); }
};
// Base type for things that are built in to the compiler,
// and will usually have special behavior or a custom
// mapping to the IR level.
FIDDLE(abstract)
class BuiltinType : public DeclRefType
{
FIDDLE(...)
};
FIDDLE(abstract)
class DataLayoutType : public BuiltinType
{
FIDDLE(...)
};
FIDDLE()
class IBufferDataLayoutType : public BuiltinType
{
FIDDLE(...)
};
FIDDLE()
class DefaultDataLayoutType : public DataLayoutType
{
FIDDLE(...)
};
FIDDLE()
class DefaultPushConstantDataLayoutType : public DataLayoutType
{
FIDDLE(...)
};
FIDDLE()
class Std430DataLayoutType : public DataLayoutType
{
FIDDLE(...)
};
FIDDLE()
class Std140DataLayoutType : public DataLayoutType
{
FIDDLE(...)
};
FIDDLE()
class ScalarDataLayoutType : public DataLayoutType
{
FIDDLE(...)
};
FIDDLE()
class CDataLayoutType : public DataLayoutType
{
FIDDLE(...)
};
FIDDLE()
class FeedbackType : public BuiltinType
{
FIDDLE(...)
enum class Kind : uint8_t
{
MinMip, /// SAMPLER_FEEDBACK_MIN_MIP
MipRegionUsed, /// SAMPLER_FEEDBACK_MIP_REGION_USED
};
Kind getKind() const;
};
FIDDLE(abstract)
class TextureShapeType : public BuiltinType
{
FIDDLE(...)
};
FIDDLE()
class TextureShape1DType : public TextureShapeType
{
FIDDLE(...)
};
FIDDLE()
class TextureShape2DType : public TextureShapeType
{
FIDDLE(...)
};
FIDDLE()
class TextureShape3DType : public TextureShapeType
{
FIDDLE(...)
};
FIDDLE()
class TextureShapeCubeType : public TextureShapeType
{
FIDDLE(...)
};
FIDDLE()
class TextureShapeBufferType : public TextureShapeType
{
FIDDLE(...)
};
// Resources that contain "elements" that can be fetched
FIDDLE(abstract)
class ResourceType : public BuiltinType
{
FIDDLE(...)
bool isMultisample();
bool isArray();
bool isShadow();
bool isFeedback();
bool isCombined();
SlangResourceShape getBaseShape();
SlangResourceShape getShape();
SlangResourceAccess getAccess();
Type* getElementType();
void _toTextOverride(StringBuilder& out);
};
FIDDLE(abstract)
class TextureTypeBase : public ResourceType
{
FIDDLE(...)
Val* getSampleCount();
Val* getFormat();
};
FIDDLE()
class TextureType : public TextureTypeBase
{
FIDDLE(...)
};
// This is a base type for `image*` types, as they exist in GLSL
FIDDLE()
class GLSLImageType : public TextureTypeBase
{
FIDDLE(...)
};
FIDDLE()
class SubpassInputType : public BuiltinType
{
FIDDLE(...)
bool isMultisample();
Type* getElementType();
};
FIDDLE()
class SamplerStateType : public BuiltinType
{
FIDDLE(...)
// Returns flavor of sampler state of this type.
SamplerStateFlavor getFlavor() const;
};
// Other cases of generic types known to the compiler
FIDDLE()
class BuiltinGenericType : public BuiltinType
{
FIDDLE(...)
Type* getElementType() const;
};
// Types that behave like pointers, in that they can be
// dereferenced (implicitly) to access members defined
// in the element type.
FIDDLE(abstract)
class PointerLikeType : public BuiltinGenericType
{
FIDDLE(...)
};
FIDDLE()
class DynamicResourceType : public BuiltinType
{
FIDDLE(...)
};
// HLSL buffer-type resources
FIDDLE(abstract)
class HLSLStructuredBufferTypeBase : public BuiltinGenericType
{
FIDDLE(...)
};
FIDDLE()
class HLSLStructuredBufferType : public HLSLStructuredBufferTypeBase
{
FIDDLE(...)
};
FIDDLE()
class HLSLRWStructuredBufferType : public HLSLStructuredBufferTypeBase
{
FIDDLE(...)
};
FIDDLE()
class HLSLRasterizerOrderedStructuredBufferType : public HLSLStructuredBufferTypeBase
{
FIDDLE(...)
};
FIDDLE()
class UntypedBufferResourceType : public BuiltinType
{
FIDDLE(...)
};
FIDDLE()
class HLSLByteAddressBufferType : public UntypedBufferResourceType
{
FIDDLE(...)
};
FIDDLE()
class HLSLRWByteAddressBufferType : public UntypedBufferResourceType
{
FIDDLE(...)
};
FIDDLE()
class HLSLRasterizerOrderedByteAddressBufferType : public UntypedBufferResourceType
{
FIDDLE(...)
};
FIDDLE()
class RaytracingAccelerationStructureType : public UntypedBufferResourceType
{
FIDDLE(...)
};
FIDDLE()
class HLSLAppendStructuredBufferType : public HLSLStructuredBufferTypeBase
{
FIDDLE(...)
};
FIDDLE()
class HLSLConsumeStructuredBufferType : public HLSLStructuredBufferTypeBase
{
FIDDLE(...)
};
FIDDLE()
class GLSLAtomicUintType : public BuiltinType
{
FIDDLE(...)
};
FIDDLE()
class HLSLPatchType : public BuiltinType
{
FIDDLE(...)
Type* getElementType();
IntVal* getElementCount();
};
FIDDLE()
class HLSLInputPatchType : public HLSLPatchType
{
FIDDLE(...)
};
FIDDLE()
class HLSLOutputPatchType : public HLSLPatchType
{
FIDDLE(...)
};
// HLSL geometry shader output stream types
FIDDLE()
class HLSLStreamOutputType : public BuiltinGenericType
{
FIDDLE(...)
};
FIDDLE()
class HLSLPointStreamType : public HLSLStreamOutputType
{
FIDDLE(...)
};
FIDDLE()
class HLSLLineStreamType : public HLSLStreamOutputType
{
FIDDLE(...)
};
FIDDLE()
class HLSLTriangleStreamType : public HLSLStreamOutputType
{
FIDDLE(...)
};
// mesh shader output types
FIDDLE()
class MeshOutputType : public BuiltinGenericType
{
FIDDLE(...)
Type* getElementType();
IntVal* getMaxElementCount();
};
FIDDLE()
class VerticesType : public MeshOutputType
{
FIDDLE(...)
};
FIDDLE()
class IndicesType : public MeshOutputType
{
FIDDLE(...)
};
FIDDLE()
class PrimitivesType : public MeshOutputType
{
FIDDLE(...)
};
//
FIDDLE()
class GLSLInputAttachmentType : public BuiltinType
{
FIDDLE(...)
};
FIDDLE()
class DescriptorHandleType : public PointerLikeType
{
FIDDLE(...)
};
// Base class for types used when desugaring parameter block
// declarations, includeing HLSL `cbuffer` or GLSL `uniform` blocks.
FIDDLE(abstract)
class ParameterGroupType : public PointerLikeType
{
FIDDLE(...)
};
FIDDLE()
class UniformParameterGroupType : public ParameterGroupType
{
FIDDLE(...)
Type* getLayoutType();
};
FIDDLE()
class VaryingParameterGroupType : public ParameterGroupType
{
FIDDLE(...)
};
// type for HLSL `cbuffer` declarations, and `ConstantBuffer<T>`
// ALso used for GLSL `uniform` blocks.
FIDDLE()
class ConstantBufferType : public UniformParameterGroupType
{
FIDDLE(...)
};
// type for HLSL `tbuffer` declarations, and `TextureBuffer<T>`
FIDDLE()
class TextureBufferType : public UniformParameterGroupType
{
FIDDLE(...)
};
// type for GLSL `in` and `out` blocks
FIDDLE()
class GLSLInputParameterGroupType : public VaryingParameterGroupType
{
FIDDLE(...)
};
FIDDLE()
class GLSLOutputParameterGroupType : public VaryingParameterGroupType
{
FIDDLE(...)
};
// type for GLSL `buffer` blocks
FIDDLE()
class GLSLShaderStorageBufferType : public PointerLikeType
{
FIDDLE(...)
};
// type for Slang `ParameterBlock<T>` type
FIDDLE()
class ParameterBlockType : public UniformParameterGroupType
{
FIDDLE(...)
};
FIDDLE()
class ArrayExpressionType : public DeclRefType
{
FIDDLE(...)
bool isUnsized();
void _toTextOverride(StringBuilder& out);
Type* getElementType();
IntVal* getElementCount();
};
FIDDLE()
class AtomicType : public DeclRefType
{
FIDDLE(...)
Type* getElementType();
};
FIDDLE()
class CoopVectorExpressionType : public ArithmeticExpressionType
{
FIDDLE(...)
void _toTextOverride(StringBuilder& out);
BasicExpressionType* _getScalarTypeOverride();
Type* getElementType();
IntVal* getElementCount();
};
// The "type" of an expression that resolves to a type.
// For example, in the expression `float(2)` the sub-expression,
// `float` would have the type `TypeType(float)`.
FIDDLE()
class TypeType : public Type
{
FIDDLE(...)
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
Type* getType() { return as<Type>(getOperand(0)); }
TypeType(Type* type) { setOperands(type); }
};
// A differential pair type, e.g., `__DifferentialPair<T>`
FIDDLE()
class DifferentialPairType : public ArithmeticExpressionType
{
FIDDLE(...)
Type* getPrimalType();
};
FIDDLE()
class DifferentialPtrPairType : public ArithmeticExpressionType
{
FIDDLE(...)
Type* getPrimalRefType();
};
FIDDLE()
class DifferentiableType : public BuiltinType
{
FIDDLE(...)
};
FIDDLE()
class DifferentiablePtrType : public BuiltinType
{
FIDDLE(...)
};
FIDDLE()
class DefaultInitializableType : public BuiltinType
{
FIDDLE(...)
};
// A vector type, e.g., `vector<T,N>`
FIDDLE()
class VectorExpressionType : public ArithmeticExpressionType
{
FIDDLE(...)
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
BasicExpressionType* _getScalarTypeOverride();
Type* getElementType();
IntVal* getElementCount();
};
// A matrix type, e.g., `matrix<T,R,C,L>`
FIDDLE()
class MatrixExpressionType : public ArithmeticExpressionType
{
FIDDLE(...)
Type* getElementType();
IntVal* getRowCount();
IntVal* getColumnCount();
IntVal* getLayout();
Type* getRowType();
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
BasicExpressionType* _getScalarTypeOverride();
private:
Type* rowType = nullptr;
};
FIDDLE()
class TensorViewType : public BuiltinType
{
FIDDLE(...)
Type* getElementType();
};
// Base class for built in string types
FIDDLE(abstract)
class StringTypeBase : public BuiltinType
{
FIDDLE(...)
};
// The regular built-in `String` type
FIDDLE()
class StringType : public StringTypeBase
{
FIDDLE(...)
};
// The string type native to the target
FIDDLE()
class NativeStringType : public StringTypeBase
{
FIDDLE(...)
};
// The built-in `__Dynamic` type
FIDDLE()
class DynamicType : public BuiltinType
{
FIDDLE(...)
};
// Type built-in `__EnumType` type
FIDDLE()
class EnumTypeType : public BuiltinType
{
FIDDLE(...)
// TODO: provide accessors for the declaration, the "tag" type, etc.
};
// Base class for types that map down to
// simple pointers as part of code generation.
FIDDLE()
class PtrTypeBase : public BuiltinType
{
FIDDLE(...)
// Get the type of the pointed-to value.
Type* getValueType();
Val* getAccessQualifier();
Val* getAddressSpace();
std::optional<AccessQualifier> tryGetAccessQualifierValue();
};
FIDDLE()
class NoneType : public BuiltinType
{
FIDDLE(...)
};
FIDDLE()
class NullPtrType : public BuiltinType
{
FIDDLE(...)
};
// A true (user-visible) pointer type, e.g., `T*`
FIDDLE()
class PtrType : public PtrTypeBase
{
FIDDLE(...)
void _toTextOverride(StringBuilder& out);
};
/// A pointer-like type used to represent a parameter-passing mode.
///
FIDDLE()
class ParamPassingModeType : public PtrTypeBase
{
FIDDLE(...)
};
// A type that represents the behind-the-scenes
// logical pointer that is passed for an `out`
// or `in out` parameter
FIDDLE(abstract)
class OutParamTypeBase : public ParamPassingModeType
{
FIDDLE(...)
};
// The type for an output parameter, e.g., `out T`
FIDDLE()
class OutParamType : public OutParamTypeBase
{
FIDDLE(...)
void _toTextOverride(StringBuilder& out);
};
using OutType = OutParamType;
// The type for a mutable borrow input/output parameter, e.g., `in out T`
FIDDLE()
class BorrowInOutParamType : public OutParamTypeBase
{
FIDDLE(...)
void _toTextOverride(StringBuilder& out);
};
// The type for a by-reference parameter, e.g., `ref T`
FIDDLE()
class RefParamType : public ParamPassingModeType
{
FIDDLE(...)
void _toTextOverride(StringBuilder& out);
};
/// The type for a immutable borrow input parameter, e.g., `borrow T`
///
/// Note that, despite the modifier currently used to represent
/// this case in code, this is *not* comparable to the `ref`
/// parameter-passing mode, and is instead an input-only
/// equivalent of `inout`.
///
FIDDLE()
class BorrowInParamType : public ParamPassingModeType
{
FIDDLE(...)
void _toTextOverride(StringBuilder& out);
};
/// A reference type that is explicitly named somewhere in code (`Ref<T>`).
///
/// The explicit reference types are distinct from the
/// parameter-passing mode wrapper types like `RefParamType`.
/// An explicit reference type is a type that code written in
/// Slang is allowed to name (e.g., by having a function that
/// returns a `Ref<T>`), even if those uses may only occur
/// in the core module. In constrast, the parameter-passing
/// mode wrapper types should only ever be used as part of
/// the encoding of a `FuncType`.
///
FIDDLE()
class ExplicitRefType : public PtrTypeBase
{
FIDDLE(...)
void _toTextOverride(StringBuilder& out);
};
FIDDLE()
class OptionalType : public BuiltinType
{
FIDDLE(...)
Type* getValueType();
};
// A raw-pointer reference to an managed value.
FIDDLE()
class NativeRefType : public BuiltinType
{
FIDDLE(...)
Type* getValueType();
};
// A type alias of some kind (e.g., via `typedef`)
FIDDLE()
class NamedExpressionType : public Type
{
FIDDLE(...)
DeclRef<TypeDefDecl> getDeclRef() { return as<DeclRefBase>(getOperand(0)); }
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
NamedExpressionType(DeclRef<TypeDefDecl> inDeclRef) { setOperands(inDeclRef); }
};
// A function type is defined by its parameter types
// and its result type.
FIDDLE()
class FuncType : public Type
{
FIDDLE(...)
// Construct a unary function
FuncType(Type* paramType, Type* resultType, Type* errorType)
{
setOperands(paramType, resultType, errorType);
}
FuncType(ArrayView<Type*> parameters, Type* result, Type* error)
{
for (auto paramType : parameters)
m_operands.add(ValNodeOperand(paramType));
m_operands.add(ValNodeOperand(result));
m_operands.add(ValNodeOperand(error));
}
OperandView<Type> getParamTypes() { return OperandView<Type>(this, 0, getOperandCount() - 2); }
Index getParamCount() { return m_operands.getCount() - 2; }
/// Get the type of one of the function's parameters, by index.
///
/// The type returned by this function may include a wrapper
/// type around what the user-perceived type of the parameter
/// is. For example, if a parameter is declared as `out int a`
/// then this function would return a type coresponding to
/// `OutParam<int>`, using the hidden `OutParam<T>` type defined
/// in the core module.
///
/// Any code that calls this function should be conscious of
/// the possibility of encountering these wrappers, and handle
/// them accordingly.
///
Type* getParamTypeWithDirectionWrapper(Index index) { return as<Type>(getOperand(index)); }
/// Get the type of one of the function's parameters, by index.
///
/// The type returned by this funciton is the user-perceived
/// type of the parameter, and does not include any wrappers
/// that are introduced to indicate the parameter-passing mode.
/// For example, a parameter declared as `out int a` will simply
/// return the `int` type, the same as would be returned for
/// a parameter simply declared as `int a`.
///
/// Any code that calls this function should be conscious of
/// the possibility that the type returned may not fully
/// describe the contract for the given parameter, and should
/// make sure to consult `getParamDirection` as well, to get
/// a complete picture.
///
Type* getParamValueType(Index index);
/// Get the parameter-passing mode of one of the function's parameters, by index.
///
ParamPassingMode getParamDirection(Index index);
/// Combined information on the type and parameter-passing mode of a parameter.
///
struct ParamInfo
{
/// The parameter-passing mode used for the parameter.
ParamPassingMode direction = ParamPassingMode::In;
/// The user-perceived type of the parameter.
Type* type = nullptr;
};
/// Get combined information on the type and parameter-passing mode of a parameter.
///
ParamInfo getParamInfo(Index index)
{
ParamInfo info;
info.direction = getParamDirection(index);
info.type = getParamValueType(index);
return info;
}
/// Get the result type of this function.
///
/// This is the type that a call to the function evaluates to if
/// the function returns successfully.
///
/// A function that conceptually returns no value will have the `Unit`
/// type as its result type.
///
/// A type that can never return will have the bottom type `Never`
/// as its result type.
///
Type* getResultType() { return as<Type>(getOperand(m_operands.getCount() - 2)); }
/// Get the type of errors (if any) that this function can fail with.
///
/// Evaluation of a call to a function with this `FuncType` may fail
/// with an error of the corresponding error type.
///
/// A function that cannot fail with an error will have the bottom
/// type `Never` as its error type.
///
/// Note that a function that "never fails" at the type system level
/// may still fail in various ways that are perceivable to the user.
/// The error type of a function only refers to failure modes that
/// are being explicitly modeled using the Slang type system.
///
Type* getErrorType() { return as<Type>(getOperand(m_operands.getCount() - 1)); }
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
};
// A tuple is a product of its member types
FIDDLE()
class TupleType : public DeclRefType
{
FIDDLE(...)
Index getMemberCount() const;
Type* getMember(Index i) const;
Type* getTypePack() const;
};
FIDDLE()
class EachType : public Type
{
FIDDLE(...)
Type* getElementType() const { return as<Type>(getOperand(0)); }
DeclRefType* getElementDeclRefType() const { return as<DeclRefType>(getOperand(0)); }
EachType(Type* elementType) { m_operands.add(ValNodeOperand(elementType)); }
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
};
FIDDLE()
class ExpandType : public Type
{
FIDDLE(...)
Type* getPatternType() const { return as<Type>(getOperand(0)); }
Index getCapturedTypePackCount() { return getOperandCount() - 1; }
Type* getCapturedTypePack(Index i) { return as<Type>(getOperand(i + 1)); }
ExpandType(Type* patternType, ArrayView<Type*> capturedPacks)
{
m_operands.add(ValNodeOperand(patternType));
for (auto t : capturedPacks)
m_operands.add(ValNodeOperand(t));
}
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
};
// A concrete pack of types.
FIDDLE()
class ConcreteTypePack : public Type
{
FIDDLE(...)
ConcreteTypePack(ArrayView<Type*> types)
{
for (auto t : types)
m_operands.add(ValNodeOperand(t));
}
Index getTypeCount() { return getOperandCount(); }
Type* getElementType(Index i) { return as<Type>(getOperand(i)); }
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
};
// The "type" of an expression that names a generic declaration.
FIDDLE()
class GenericDeclRefType : public Type
{
FIDDLE(...)
DeclRef<GenericDecl> getDeclRef() const { return as<DeclRefBase>(getOperand(0)); }
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
GenericDeclRefType(DeclRef<GenericDecl> declRef) { setOperands(declRef); }
};
// The "type" of a reference to a module or namespace
FIDDLE()
class NamespaceType : public Type
{
FIDDLE(...)
DeclRef<NamespaceDeclBase> getDeclRef() const { return as<DeclRefBase>(getOperand(0)); }
NamespaceType(DeclRef<NamespaceDeclBase> inDeclRef) { setOperands(inDeclRef); }
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
};
// The concrete type for a value wrapped in an existential, accessible
// when the existential is "opened" in some context.
FIDDLE()
class ExtractExistentialType : public Type
{
FIDDLE(...)
DeclRef<VarDeclBase> getDeclRef() const { return as<DeclRefBase>(getOperand(0)); }
// A reference to the original interface this type is known
// to be a subtype of.
//
Type* getOriginalInterfaceType() { return as<Type>(getOperand(1)); }
DeclRef<InterfaceDecl> getOriginalInterfaceDeclRef() { return as<DeclRefBase>(getOperand(2)); }
ExtractExistentialType(
DeclRef<VarDeclBase> inDeclRef,
Type* inOriginalInterfaceType,
DeclRef<InterfaceDecl> inOriginalInterfaceDeclRef)
{
setOperands(inDeclRef, inOriginalInterfaceType, inOriginalInterfaceDeclRef);
}
// A cached decl-ref to the original interface's ThisType Decl, with
// a witness that refers to the type extracted here.
//
// This field is optional and can be filled in on-demand. It does *not*
// represent part of the logical value of this `Type`, and should not
// be serialized, included in hashes, etc.
//
DeclRef<ThisTypeDecl> cachedThisTypeDeclRef;
// A cached pointer to a witness that shows how this type is a subtype
// of `originalInterfaceType`.
//
SubtypeWitness* cachedSubtypeWitness = nullptr;
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
/// Get a witness that shows how this type is a subtype of `originalInterfaceType`.
///
/// This operation may create the witness on demand and cache it.
///
SubtypeWitness* getSubtypeWitness();
/// Get a decl-ref to the interface's ThisType decl, which represents a substitutable type
/// from which lookup can be performed.
///
/// This operation may create the decl-ref on demand and cache it.
///
DeclRef<ThisTypeDecl> getThisTypeDeclRef();
};
FIDDLE()
class ExistentialSpecializedType : public Type
{
FIDDLE(...)
Type* getBaseType() { return as<Type>(getOperand(0)); }
ExpandedSpecializationArg getArg(Index i)
{
ExpandedSpecializationArg arg;
arg.val = getOperand(i * 2 + 1);
arg.witness = getOperand(i * 2 + 2);
return arg;
}
Index getArgCount() { return (getOperandCount() - 1) / 2; }
ExistentialSpecializedType(Type* inBaseType, ExpandedSpecializationArgs const& inArgs)
{
m_operands.add(ValNodeOperand(inBaseType));
for (auto arg : inArgs)
{
m_operands.add(ValNodeOperand(arg.val));
m_operands.add(ValNodeOperand(arg.witness));
}
}
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
};
/// The type of `this` within a polymorphic declaration
FIDDLE()
class ThisType : public DeclRefType
{
FIDDLE(...)
ThisType(DeclRefBase* declRef)
: DeclRefType(declRef)
{
}
DeclRef<InterfaceDecl> getInterfaceDeclRef();
};
/// The type of `A & B` where `A` and `B` are types
///
/// A value `v` is of type `A & B` if it is both of type `A` and of type `B`.
FIDDLE()
class AndType : public Type
{
FIDDLE(...)
Type* getLeft() { return as<Type>(getOperand(0)); }
Type* getRight() { return as<Type>(getOperand(1)); }
AndType(Type* leftType, Type* rightType) { setOperands(leftType, rightType); }
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
};
FIDDLE()
class ModifiedType : public Type
{
FIDDLE(...)
Type* getBase() { return as<Type>(getOperand(0)); }
Index getModifierCount() { return getOperandCount() - 1; }
Val* getModifier(Index index) { return getOperand(index + 1); }
ModifiedType(Type* inBase, ArrayView<Val*> inModifiers)
{
m_operands.add(ValNodeOperand(inBase));
for (auto modifier : inModifiers)
m_operands.add(ValNodeOperand(modifier));
}
template<typename T>
T* findModifier()
{
for (Index i = 1; i < getOperandCount(); i++)
if (auto rs = as<T>(getOperand(i)))
return rs;
return nullptr;
}
// Overrides should be public so base classes can access
void _toTextOverride(StringBuilder& out);
Type* _createCanonicalTypeOverride();
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
};
Type* removeParamDirType(Type* type);
bool isNonCopyableType(Type* type);
} // namespace Slang
|