summaryrefslogtreecommitdiff
path: root/source/slang/type-defs.h
blob: 2318c693306697f24401263bf00a5d3da608b164 (plain)
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
// type-defs.h

// Syntax class definitions for types.

// The type of a reference to an overloaded name
SYNTAX_CLASS(OverloadGroupType, Type)
RAW(
public:
    virtual String ToString() override;

protected:
    virtual bool EqualsImpl(Type * type) override;
    virtual RefPtr<Type> CreateCanonicalType() override;
    virtual int GetHashCode() override;
)
END_SYNTAX_CLASS()

// The type of an initializer-list expression (before it has
// been coerced to some other type)
SYNTAX_CLASS(InitializerListType, Type)
RAW(
    virtual String ToString() override;

protected:
    virtual bool EqualsImpl(Type * type) override;
    virtual RefPtr<Type> CreateCanonicalType() override;
    virtual int GetHashCode() override;
)
END_SYNTAX_CLASS()

// The type of an expression that was erroneous
SYNTAX_CLASS(ErrorType, Type)
RAW(
public:
    virtual String ToString() override;

protected:
    virtual bool EqualsImpl(Type * type) override;
    virtual RefPtr<Val> SubstituteImpl(SubstitutionSet subst, int* ioDiff) override;
    virtual RefPtr<Type> CreateCanonicalType() override;
    virtual int GetHashCode() override;
)
END_SYNTAX_CLASS()

// A type that takes the form of a reference to some declaration
SYNTAX_CLASS(DeclRefType, Type)
    DECL_FIELD(DeclRef<Decl>, declRef)

RAW(
    virtual String ToString() override;
    virtual RefPtr<Val> SubstituteImpl(SubstitutionSet subst, int* ioDiff) override;

    static DeclRefType* Create(
        Session*        session,
        DeclRef<Decl>   declRef);

    DeclRefType()
    {}
    DeclRefType(
        DeclRef<Decl> declRef)
        : declRef(declRef)
    {}
protected:
    virtual int GetHashCode() override;
    virtual bool EqualsImpl(Type * type) override;
    virtual RefPtr<Type> CreateCanonicalType() override;
)
END_SYNTAX_CLASS()

// Base class for types that can be used in arithmetic expressions
ABSTRACT_SYNTAX_CLASS(ArithmeticExpressionType, DeclRefType)
RAW(
public:
    virtual BasicExpressionType* GetScalarType() = 0;
)
END_SYNTAX_CLASS()

SYNTAX_CLASS(BasicExpressionType, ArithmeticExpressionType)

    FIELD(BaseType, baseType)

RAW(
    BasicExpressionType() {}
    BasicExpressionType(
        Slang::BaseType baseType)
        : baseType(baseType)
    {}
protected:
    virtual BasicExpressionType* GetScalarType() override;
    virtual bool EqualsImpl(Type * type) override;
    virtual RefPtr<Type> CreateCanonicalType() override;
)
END_SYNTAX_CLASS()

// 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.
ABSTRACT_SYNTAX_CLASS(BuiltinType, DeclRefType)
END_SYNTAX_CLASS()

// Resources that contain "elements" that can be fetched
ABSTRACT_SYNTAX_CLASS(ResourceType, BuiltinType)
    // The type that results from fetching an element from this resource
    SYNTAX_FIELD(RefPtr<Type>, elementType)

    // Shape and access level information for this resource type
    FIELD(TextureFlavor, flavor)

    RAW(
        TextureFlavor::Shape GetBaseShape()
        {
            return flavor.GetBaseShape();
        }
        bool isMultisample() { return flavor.isMultisample(); }
        bool isArray() { return flavor.isArray(); }
        SlangResourceShape getShape() const { return flavor.getShape(); }
        SlangResourceAccess getAccess() { return flavor.getAccess(); }

    )
END_SYNTAX_CLASS()

ABSTRACT_SYNTAX_CLASS(TextureTypeBase, ResourceType)
RAW(
    TextureTypeBase()
    {}
    TextureTypeBase(
        TextureFlavor flavor,
        RefPtr<Type> elementType)
    {
        this->elementType = elementType;
        this->flavor = flavor;
    }
)
END_SYNTAX_CLASS()

SYNTAX_CLASS(TextureType, TextureTypeBase)
RAW(
    TextureType()
    {}
    TextureType(
        TextureFlavor flavor,
        RefPtr<Type> elementType)
        : TextureTypeBase(flavor, elementType)
    {}
)
END_SYNTAX_CLASS()

// This is a base type for texture/sampler pairs,
// as they exist in, e.g., GLSL
SYNTAX_CLASS(TextureSamplerType, TextureTypeBase)
RAW(
    TextureSamplerType()
    {}
    TextureSamplerType(
        TextureFlavor flavor,
        RefPtr<Type> elementType)
        : TextureTypeBase(flavor, elementType)
    {}
)
END_SYNTAX_CLASS()

// This is a base type for `image*` types, as they exist in GLSL
SYNTAX_CLASS(GLSLImageType, TextureTypeBase)
RAW(
    GLSLImageType()
    {}
    GLSLImageType(
        TextureFlavor flavor,
        RefPtr<Type> elementType)
        : TextureTypeBase(flavor, elementType)
    {}
)
END_SYNTAX_CLASS()

SYNTAX_CLASS(SamplerStateType, BuiltinType)
    // What flavor of sampler state is this
    FIELD(SamplerStateFlavor, flavor)
END_SYNTAX_CLASS()

// Other cases of generic types known to the compiler
SYNTAX_CLASS(BuiltinGenericType, BuiltinType)
    SYNTAX_FIELD(RefPtr<Type>, elementType)

    RAW(Type* getElementType() { return elementType; })
END_SYNTAX_CLASS()

// Types that behave like pointers, in that they can be
// dereferenced (implicitly) to access members defined
// in the element type.
SIMPLE_SYNTAX_CLASS(PointerLikeType, BuiltinGenericType)

// HLSL buffer-type resources

SIMPLE_SYNTAX_CLASS(HLSLStructuredBufferTypeBase, BuiltinGenericType)
SIMPLE_SYNTAX_CLASS(HLSLStructuredBufferType, HLSLStructuredBufferTypeBase)
SIMPLE_SYNTAX_CLASS(HLSLRWStructuredBufferType, HLSLStructuredBufferTypeBase)
SIMPLE_SYNTAX_CLASS(HLSLRasterizerOrderedStructuredBufferType, HLSLStructuredBufferTypeBase)

SIMPLE_SYNTAX_CLASS(UntypedBufferResourceType, BuiltinType)
SIMPLE_SYNTAX_CLASS(HLSLByteAddressBufferType, UntypedBufferResourceType)
SIMPLE_SYNTAX_CLASS(HLSLRWByteAddressBufferType, UntypedBufferResourceType)
SIMPLE_SYNTAX_CLASS(HLSLRasterizerOrderedByteAddressBufferType, UntypedBufferResourceType)
SIMPLE_SYNTAX_CLASS(RaytracingAccelerationStructureType, UntypedBufferResourceType)

SIMPLE_SYNTAX_CLASS(HLSLAppendStructuredBufferType, HLSLStructuredBufferTypeBase)
SIMPLE_SYNTAX_CLASS(HLSLConsumeStructuredBufferType, HLSLStructuredBufferTypeBase)

SYNTAX_CLASS(HLSLPatchType, BuiltinType)
RAW(
    Type* getElementType();
    IntVal*         getElementCount();
)
END_SYNTAX_CLASS()

SIMPLE_SYNTAX_CLASS(HLSLInputPatchType, HLSLPatchType)
SIMPLE_SYNTAX_CLASS(HLSLOutputPatchType, HLSLPatchType)

// HLSL geometry shader output stream types

SIMPLE_SYNTAX_CLASS(HLSLStreamOutputType, BuiltinGenericType)
SIMPLE_SYNTAX_CLASS(HLSLPointStreamType, HLSLStreamOutputType)
SIMPLE_SYNTAX_CLASS(HLSLLineStreamType, HLSLStreamOutputType)
SIMPLE_SYNTAX_CLASS(HLSLTriangleStreamType, HLSLStreamOutputType)

//
SIMPLE_SYNTAX_CLASS(GLSLInputAttachmentType, BuiltinType)

// Base class for types used when desugaring parameter block
// declarations, includeing HLSL `cbuffer` or GLSL `uniform` blocks.
SIMPLE_SYNTAX_CLASS(ParameterGroupType, PointerLikeType)

SIMPLE_SYNTAX_CLASS(UniformParameterGroupType, ParameterGroupType)
SIMPLE_SYNTAX_CLASS(VaryingParameterGroupType, ParameterGroupType)

// type for HLSL `cbuffer` declarations, and `ConstantBuffer<T>`
// ALso used for GLSL `uniform` blocks.
SIMPLE_SYNTAX_CLASS(ConstantBufferType, UniformParameterGroupType)

// type for HLSL `tbuffer` declarations, and `TextureBuffer<T>`
SIMPLE_SYNTAX_CLASS(TextureBufferType, UniformParameterGroupType)

// type for GLSL `in` and `out` blocks
SIMPLE_SYNTAX_CLASS(GLSLInputParameterGroupType, VaryingParameterGroupType)
SIMPLE_SYNTAX_CLASS(GLSLOutputParameterGroupType, VaryingParameterGroupType)

// type for GLLSL `buffer` blocks
SIMPLE_SYNTAX_CLASS(GLSLShaderStorageBufferType, UniformParameterGroupType)

// type for Slang `ParameterBlock<T>` type
SIMPLE_SYNTAX_CLASS(ParameterBlockType, UniformParameterGroupType)

SYNTAX_CLASS(ArrayExpressionType, Type)
    SYNTAX_FIELD(RefPtr<Type>, baseType)
    SYNTAX_FIELD(RefPtr<IntVal>, ArrayLength)

RAW(
    virtual Slang::String ToString() override;

protected:
    virtual bool EqualsImpl(Type * type) override;
    virtual RefPtr<Type> CreateCanonicalType() override;
    virtual RefPtr<Val> SubstituteImpl(SubstitutionSet subst, int* ioDiff) override;
    virtual int GetHashCode() override;
    )
END_SYNTAX_CLASS()

// 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)`.
SYNTAX_CLASS(TypeType, Type)
    // The type that this is the type of...
    SYNTAX_FIELD(RefPtr<Type>, type)

RAW(
public:
    TypeType()
    {}
    TypeType(RefPtr<Type> type)
        : type(type)
    {}

    virtual String ToString() override;

protected:
    virtual bool EqualsImpl(Type * type) override;
    virtual RefPtr<Type> CreateCanonicalType() override;
    virtual int GetHashCode() override;
)
END_SYNTAX_CLASS()

// A vector type, e.g., `vector<T,N>`
SYNTAX_CLASS(VectorExpressionType, ArithmeticExpressionType)

    // The type of vector elements.
    // As an invariant, this should be a basic type or an alias.
    SYNTAX_FIELD(RefPtr<Type>, elementType)

    // The number of elements
    SYNTAX_FIELD(RefPtr<IntVal>, elementCount)

RAW(
    virtual String ToString() override;

protected:
    virtual BasicExpressionType* GetScalarType() override;
)
END_SYNTAX_CLASS()

// A matrix type, e.g., `matrix<T,R,C>`
SYNTAX_CLASS(MatrixExpressionType, ArithmeticExpressionType)
RAW(

    Type* getElementType();
    IntVal*         getRowCount();
    IntVal*         getColumnCount();

    RefPtr<Type> getRowType();

    virtual String ToString() override;

protected:
    virtual BasicExpressionType* GetScalarType() override;

private:
    RefPtr<Type> mRowType;
)
END_SYNTAX_CLASS()

// The built-in `String` type
SIMPLE_SYNTAX_CLASS(StringType, BuiltinType)

// Type built-in `__EnumType` type
SYNTAX_CLASS(EnumTypeType, BuiltinType)

// TODO: provide accessors for the declaration, the "tag" type, etc.

END_SYNTAX_CLASS()

// Base class for types that map down to
// simple pointers as part of code generation.
SYNTAX_CLASS(PtrTypeBase, BuiltinType)
RAW(
    // Get the type of the pointed-to value.
    Type*   getValueType();
)
END_SYNTAX_CLASS()

// A true (user-visible) pointer type, e.g., `T*`
SYNTAX_CLASS(PtrType, PtrTypeBase)
END_SYNTAX_CLASS()

// A type that represents the behind-the-scenes
// logical pointer that is passed for an `out`
// or `in out` parameter
SYNTAX_CLASS(OutTypeBase, PtrTypeBase)
END_SYNTAX_CLASS()

// The type for an `out` parameter, e.g., `out T`
SYNTAX_CLASS(OutType, OutTypeBase)
END_SYNTAX_CLASS()

// The type for an `in out` parameter, e.g., `in out T`
SYNTAX_CLASS(InOutType, OutTypeBase)
END_SYNTAX_CLASS()

// The type for an `ref` parameter, e.g., `ref T`
SYNTAX_CLASS(RefType, PtrTypeBase)
END_SYNTAX_CLASS()

// A type alias of some kind (e.g., via `typedef`)
SYNTAX_CLASS(NamedExpressionType, Type)
DECL_FIELD(DeclRef<TypeDefDecl>, declRef)

RAW(
    RefPtr<Type> innerType;
    NamedExpressionType()
    {}
    NamedExpressionType(
        DeclRef<TypeDefDecl> declRef)
        : declRef(declRef)
    {}


    virtual String ToString() override;

protected:
    virtual bool EqualsImpl(Type * type) override;
    virtual RefPtr<Type> CreateCanonicalType() override;
    virtual int GetHashCode() override;
)
END_SYNTAX_CLASS()

// A function type is defined by its parameter types
// and its result type.
SYNTAX_CLASS(FuncType, Type)

    // TODO: We may want to preserve parameter names
    // in the list here, just so that we can print
    // out friendly names when printing a function
    // type, even if they don't affect the actual
    // semantic type underneath.

    FIELD(List<RefPtr<Type>>, paramTypes)
    FIELD(RefPtr<Type>, resultType)
RAW(
    FuncType()
    {}

    UInt getParamCount() { return paramTypes.Count(); }
    Type* getParamType(UInt index) { return paramTypes[index]; }
    Type* getResultType() { return resultType; }

    virtual String ToString() override;
protected:
    virtual RefPtr<Val> SubstituteImpl(SubstitutionSet subst, int* ioDiff) override;
    virtual bool EqualsImpl(Type * type) override;
    virtual RefPtr<Type> CreateCanonicalType() override;
    virtual int GetHashCode() override;
)
END_SYNTAX_CLASS()

// The "type" of an expression that names a generic declaration.
SYNTAX_CLASS(GenericDeclRefType, Type)

    DECL_FIELD(DeclRef<GenericDecl>, declRef)

    RAW(
    GenericDeclRefType()
    {}
    GenericDeclRefType(
        DeclRef<GenericDecl> declRef)
        : declRef(declRef)
    {}


    DeclRef<GenericDecl> const& GetDeclRef() const { return declRef; }

    virtual String ToString() override;

protected:
    virtual bool EqualsImpl(Type * type) override;
    virtual int GetHashCode() override;
    virtual RefPtr<Type> CreateCanonicalType() override;
)
END_SYNTAX_CLASS()

// The concrete type for a value wrapped in an existential, accessible
// when the existential is "opened" in some context.
SYNTAX_CLASS(ExtractExistentialType, Type)
RAW(
    DeclRef<VarDeclBase> declRef;

    virtual String ToString() override;
    virtual bool EqualsImpl(Type * type) override;
    virtual int GetHashCode() override;
    virtual RefPtr<Type> CreateCanonicalType() override;
    virtual RefPtr<Val> SubstituteImpl(SubstitutionSet subst, int* ioDiff) override;
)
END_SYNTAX_CLASS()

    /// A tagged union of zero or more other types.
SYNTAX_CLASS(TaggedUnionType, Type)
RAW(
        /// The distinct "cases" the tagged union can store.
        ///
        /// For each type in this array, the array index is the
        /// tag value for that case.
        ///
    List<RefPtr<Type>> caseTypes;

    virtual String ToString() override;
    virtual bool EqualsImpl(Type * type) override;
    virtual int GetHashCode() override;
    virtual RefPtr<Type> CreateCanonicalType() override;
    virtual RefPtr<Val> SubstituteImpl(SubstitutionSet subst, int* ioDiff) override;
)
END_SYNTAX_CLASS()