wgsl-types 0.3.2

Type-checking of WGSL types and builtins
Documentation
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
//! Type-checking of operators.

use crate::{
    Error,
    conv::{Convert, convert_ty},
    syntax::{AddressSpace, BinaryOperator, UnaryOperator},
    ty::{Ty, Type},
};

type E = Error;

/// Compute the return type of a unary operator expression.
pub fn type_unary_op(operator: UnaryOperator, operand: &Type) -> Result<Type, E> {
    match operator {
        UnaryOperator::LogicalNegation => operand.op_not(),
        UnaryOperator::Negation => operand.op_neg(),
        UnaryOperator::BitwiseComplement => operand.op_bitnot(),
        UnaryOperator::AddressOf => operand.op_ref(),
        UnaryOperator::Indirection => operand.op_deref(),
    }
}

/// Compute the return type of a binary operator expression.
pub fn type_binary_op(op: BinaryOperator, lhs: &Type, rhs: &Type) -> Result<Type, E> {
    match op {
        BinaryOperator::ShortCircuitOr => lhs.op_or(rhs),
        BinaryOperator::ShortCircuitAnd => lhs.op_and(rhs),
        BinaryOperator::Addition => lhs.op_add(rhs),
        BinaryOperator::Subtraction => lhs.op_sub(rhs),
        BinaryOperator::Multiplication => lhs.op_mul(rhs),
        BinaryOperator::Division => lhs.op_div(rhs),
        BinaryOperator::Remainder => lhs.op_rem(rhs),
        BinaryOperator::Equality => lhs.op_eq(rhs),
        BinaryOperator::Inequality => lhs.op_ne(rhs),
        BinaryOperator::LessThan => lhs.op_lt(rhs),
        BinaryOperator::LessThanEqual => lhs.op_le(rhs),
        BinaryOperator::GreaterThan => lhs.op_gt(rhs),
        BinaryOperator::GreaterThanEqual => lhs.op_ge(rhs),
        BinaryOperator::BitwiseOr => lhs.op_bitor(rhs),
        BinaryOperator::BitwiseAnd => lhs.op_bitand(rhs),
        BinaryOperator::BitwiseXor => lhs.op_bitxor(rhs),
        BinaryOperator::ShiftLeft => lhs.op_shl(rhs),
        BinaryOperator::ShiftRight => lhs.op_shr(rhs),
    }
}

// -------------------
// LOGICAL EXPRESSIONS
// -------------------
// reference: https://www.w3.org/TR/WGSL/#logical-expr

impl Type {
    pub fn op_not(&self) -> Result<Self, E> {
        match self {
            Self::Bool | Self::Vec(_, _) => Ok(self.clone()),
            _ => Err(E::Unary(UnaryOperator::LogicalNegation, self.clone())),
        }
    }
    pub fn op_or(&self, rhs: &Type) -> Result<Self, E> {
        match (self, rhs) {
            (Type::Bool, Type::Bool) => Ok(Type::Bool),
            _ => Err(E::Binary(
                BinaryOperator::ShortCircuitOr,
                self.ty(),
                rhs.ty(),
            )),
        }
    }
    pub fn op_and(&self, rhs: &Type) -> Result<Self, E> {
        match (self, rhs) {
            (Type::Bool, Type::Bool) => Ok(Type::Bool),
            _ => Err(E::Binary(
                BinaryOperator::ShortCircuitAnd,
                self.ty(),
                rhs.ty(),
            )),
        }
    }
}

// ----------------------
// ARITHMETIC EXPRESSIONS
// ----------------------
// reference: https://www.w3.org/TR/WGSL/#arithmetic-expr

impl Type {
    /// Valid operands:
    /// * `-S`, S: scalar
    pub fn op_neg(&self) -> Result<Self, E> {
        if self.is_scalar() {
            Ok(self.clone())
        } else {
            Err(E::Unary(UnaryOperator::Negation, self.ty()))
        }
    }

    /// Valid operands:
    /// * `T + T`, T: scalar or vec
    /// * `S + V` or `V + S`, S: scalar, V: `vec<S>`
    /// * `M + M`, M: mat
    pub fn op_add(&self, rhs: &Type) -> Result<Self, E> {
        let err = || E::Binary(BinaryOperator::Addition, self.ty(), rhs.ty());
        match (self, rhs) {
            (lhs, rhs) if lhs.is_scalar() && rhs.is_scalar() || lhs.is_vec() && rhs.is_vec() => {
                let ty = convert_ty(self, rhs).ok_or_else(err)?;
                Ok(ty.clone())
            }
            (scalar_ty, Type::Vec(n, vec_ty)) | (Type::Vec(n, vec_ty), scalar_ty)
                if scalar_ty.is_scalar() =>
            {
                let inner_ty = convert_ty(scalar_ty, vec_ty).ok_or_else(err)?;
                Ok(Type::Vec(*n, Box::new(inner_ty.clone())))
            }
            (Type::Mat(c1, r1, lhs), Type::Mat(c2, r2, rhs)) if c1 == c2 && r1 == r2 => {
                let inner_ty = convert_ty(lhs, rhs).ok_or_else(err)?;
                Ok(Type::Mat(*c1, *c2, Box::new(inner_ty.clone())))
            }
            _ => Err(err()),
        }
    }

    /// Valid operands:
    /// * `T - T`, T: scalar or vec
    /// * `S - V` or `V - S`, S: scalar, V: `vec<S>`
    /// * `M - M`, M: mat
    pub fn op_sub(&self, rhs: &Type) -> Result<Self, E> {
        let err = || E::Binary(BinaryOperator::Subtraction, self.ty(), rhs.ty());
        match (self, rhs) {
            (lhs, rhs) if lhs.is_scalar() && rhs.is_scalar() || lhs.is_vec() && rhs.is_vec() => {
                let ty = convert_ty(self, rhs).ok_or_else(err)?;
                Ok(ty.clone())
            }
            (scalar_ty, Type::Vec(n, vec_ty)) | (Type::Vec(n, vec_ty), scalar_ty)
                if scalar_ty.is_scalar() =>
            {
                let inner_ty = convert_ty(scalar_ty, vec_ty).ok_or_else(err)?;
                Ok(Type::Vec(*n, Box::new(inner_ty.clone())))
            }
            (Type::Mat(c1, r1, lhs), Type::Mat(c2, r2, rhs)) if c1 == c2 && r1 == r2 => {
                let inner_ty = convert_ty(lhs, rhs).ok_or_else(err)?;
                Ok(Type::Mat(*c1, *c2, Box::new(inner_ty.clone())))
            }
            _ => Err(err()),
        }
    }

    /// Valid operands:
    /// * `T * T`, T: scalar or vec
    /// * `S * V` or `V * S`, S: scalar, V: `vec<S>`
    /// * `S * M` or `M * S`, S: float, M: `mat<S>`
    /// * `V * M` or `M * V`, S: float, V: `vec<S>`, M: `mat<S>`
    /// * `M1 * M1`, M1: `matKxR`, M2: `matCxK`
    pub fn op_mul(&self, rhs: &Type) -> Result<Self, E> {
        let err = || E::Binary(BinaryOperator::Multiplication, self.ty(), rhs.ty());
        match (self, rhs) {
            (lhs, rhs) if lhs.is_scalar() && rhs.is_scalar() || lhs.is_vec() && rhs.is_vec() => {
                let ty = convert_ty(self, rhs).ok_or_else(err)?;
                Ok(ty.clone())
            }
            (scalar_ty, Type::Vec(n, vec_ty)) | (Type::Vec(n, vec_ty), scalar_ty)
                if scalar_ty.is_scalar() =>
            {
                let inner_ty = convert_ty(scalar_ty, vec_ty).ok_or_else(err)?;
                Ok(Type::Vec(*n, Box::new(inner_ty.clone())))
            }
            (scalar_ty, Type::Mat(c, r, mat_ty)) | (Type::Mat(c, r, mat_ty), scalar_ty)
                if scalar_ty.is_scalar() =>
            {
                let inner_ty = convert_ty(scalar_ty, mat_ty).ok_or_else(err)?;
                Ok(Type::Mat(*c, *r, Box::new(inner_ty.clone())))
            }
            (Type::Vec(n1, vec_ty), Type::Mat(n2, n, mat_ty))
            | (Type::Mat(n, n1, mat_ty), Type::Vec(n2, vec_ty))
                if n1 == n2 =>
            {
                let inner_ty = convert_ty(vec_ty, mat_ty).ok_or_else(err)?;
                Ok(Type::Vec(*n, Box::new(inner_ty.clone())))
            }
            (Type::Mat(k1, r, lhs), Type::Mat(c, k2, rhs)) if k1 == k2 => {
                let inner_ty = convert_ty(lhs, rhs).ok_or_else(err)?;
                Ok(Type::Mat(*c, *r, Box::new(inner_ty.clone())))
            }
            _ => Err(err()),
        }
    }

    /// Valid operands:
    /// * `T / T`, T: scalar or vec
    /// * `S / V` or `V / S`, S: scalar, V: `vec<S>`
    pub fn op_div(&self, rhs: &Type) -> Result<Self, E> {
        let err = || E::Binary(BinaryOperator::Division, self.ty(), rhs.ty());
        match (self, rhs) {
            (lhs, rhs) if lhs.is_scalar() && rhs.is_scalar() || lhs.is_vec() && rhs.is_vec() => {
                let ty = convert_ty(self, rhs).ok_or_else(err)?;
                Ok(ty.clone())
            }
            (scalar_ty, Type::Vec(n, vec_ty)) | (Type::Vec(n, vec_ty), scalar_ty)
                if scalar_ty.is_scalar() =>
            {
                let inner_ty = convert_ty(scalar_ty, vec_ty).ok_or_else(err)?;
                Ok(Type::Vec(*n, Box::new(inner_ty.clone())))
            }
            _ => Err(err()),
        }
    }

    /// Valid operands:
    /// * `T % T`, T: scalar or vec
    /// * `S % V` or `V % S`, S: scalar, V: `vec<S>`
    pub fn op_rem(&self, rhs: &Type) -> Result<Self, E> {
        let err = || E::Binary(BinaryOperator::Remainder, self.ty(), rhs.ty());
        match (self, rhs) {
            (lhs, rhs) if lhs.is_scalar() && rhs.is_scalar() || lhs.is_vec() && rhs.is_vec() => {
                let ty = convert_ty(self, rhs).ok_or_else(err)?;
                Ok(ty.clone())
            }
            (scalar_ty, Type::Vec(n, vec_ty)) | (Type::Vec(n, vec_ty), scalar_ty)
                if scalar_ty.is_scalar() =>
            {
                let inner_ty = convert_ty(scalar_ty, vec_ty).ok_or_else(err)?;
                Ok(Type::Vec(*n, Box::new(inner_ty.clone())))
            }
            _ => Err(err()),
        }
    }
}

// ----------------------
// COMPARISON EXPRESSIONS
// ----------------------
// reference: https://www.w3.org/TR/WGSL/#comparison-expr

impl Type {
    /// Valid operands:
    /// * `T == T`, T: scalar or vec
    pub fn op_eq(&self, rhs: &Type) -> Result<Type, E> {
        let err = || E::Binary(BinaryOperator::Equality, self.ty(), rhs.ty());
        match convert_ty(self, rhs).ok_or_else(err)? {
            ty if ty.is_scalar() => Ok(Type::Bool),
            Type::Vec(n, _) => Ok(Type::Vec(*n, Box::new(Type::Bool))),
            _ => Err(err()),
        }
    }
    /// Valid operands:
    /// * `T != T`, T: scalar or vec
    pub fn op_ne(&self, rhs: &Type) -> Result<Type, E> {
        let err = || E::Binary(BinaryOperator::Inequality, self.ty(), rhs.ty());
        match convert_ty(self, rhs).ok_or_else(err)? {
            ty if ty.is_scalar() => Ok(Type::Bool),
            Type::Vec(n, _) => Ok(Type::Vec(*n, Box::new(Type::Bool))),
            _ => Err(err()),
        }
    }
    /// Valid operands:
    /// * `T < T`, T: scalar or vec
    pub fn op_lt(&self, rhs: &Type) -> Result<Type, E> {
        let err = || E::Binary(BinaryOperator::LessThan, self.ty(), rhs.ty());
        match convert_ty(self, rhs).ok_or_else(err)? {
            ty if ty.is_scalar() => Ok(Type::Bool),
            Type::Vec(n, _) => Ok(Type::Vec(*n, Box::new(Type::Bool))),
            _ => Err(err()),
        }
    }
    /// Valid operands:
    /// * `T <= T`, T: scalar or vec
    pub fn op_le(&self, rhs: &Type) -> Result<Type, E> {
        let err = || E::Binary(BinaryOperator::LessThanEqual, self.ty(), rhs.ty());
        match convert_ty(self, rhs).ok_or_else(err)? {
            ty if ty.is_scalar() => Ok(Type::Bool),
            Type::Vec(n, _) => Ok(Type::Vec(*n, Box::new(Type::Bool))),
            _ => Err(err()),
        }
    }
    /// Valid operands:
    /// * `T > T`, T: scalar or vec
    pub fn op_gt(&self, rhs: &Type) -> Result<Type, E> {
        let err = || E::Binary(BinaryOperator::GreaterThan, self.ty(), rhs.ty());
        match convert_ty(self, rhs).ok_or_else(err)? {
            ty if ty.is_scalar() => Ok(Type::Bool),
            Type::Vec(n, _) => Ok(Type::Vec(*n, Box::new(Type::Bool))),
            _ => Err(err()),
        }
    }
    /// Valid operands:
    /// * `T >= T`, T: scalar or vec
    pub fn op_ge(&self, rhs: &Type) -> Result<Type, E> {
        let err = || E::Binary(BinaryOperator::GreaterThanEqual, self.ty(), rhs.ty());
        match convert_ty(self, rhs).ok_or_else(err)? {
            ty if ty.is_scalar() => Ok(Type::Bool),
            Type::Vec(n, _) => Ok(Type::Vec(*n, Box::new(Type::Bool))),
            _ => Err(err()),
        }
    }
}

// ---------------
// BIT EXPRESSIONS
// ---------------
// reference: https://www.w3.org/TR/WGSL/#bit-expr

impl Type {
    /// Valid operands:
    /// * `~T`, I: integer, T: I or `vec<I>`
    pub fn op_bitnot(&self) -> Result<Type, E> {
        match self {
            ty if ty.is_integer() => Ok(self.clone()),
            Type::Vec(_, ty) if ty.is_integer() => Ok(self.clone()),
            _ => Err(E::Unary(UnaryOperator::BitwiseComplement, self.ty())),
        }
    }

    /// Note: this is both the "bitwise OR" and "logical OR" operator.
    ///
    /// Valid operands:
    /// * `T | T`, T: integer, or `vec<integer>` (bitwise OR)
    /// * `V | V`, V: `vec<bool>` (logical OR)
    pub fn op_bitor(&self, rhs: &Type) -> Result<Type, E> {
        let err = || E::Binary(BinaryOperator::BitwiseOr, self.ty(), rhs.ty());
        match convert_ty(self, rhs).ok_or_else(err)? {
            ty if ty.is_integer() => Ok(self.clone()),
            Type::Vec(_, ty) if ty.is_integer() => Ok(self.clone()),
            Type::Vec(_, ty) if ty.is_bool() => Ok(self.clone()),
            _ => Err(err()),
        }
    }

    /// Note: this is both the "bitwise AND" and "logical AND" operator.
    ///
    /// Valid operands:
    /// * `T & T`, T: integer or `vec<integer>` (bitwise AND)
    /// * `V & V`, V: `vec<bool>` (logical AND)
    pub fn op_bitand(&self, rhs: &Type) -> Result<Type, E> {
        let err = || E::Binary(BinaryOperator::BitwiseAnd, self.ty(), rhs.ty());
        match convert_ty(self, rhs).ok_or_else(err)? {
            ty if ty.is_integer() => Ok(self.clone()),
            Type::Vec(_, ty) if ty.is_integer() => Ok(self.clone()),
            Type::Vec(_, ty) if ty.is_bool() => Ok(self.clone()),
            _ => Err(err()),
        }
    }

    /// Valid operands:
    /// * `T ^ T`, T: integer or `vec<integer>`
    pub fn op_bitxor(&self, rhs: &Type) -> Result<Type, E> {
        let err = || E::Binary(BinaryOperator::BitwiseXor, self.ty(), rhs.ty());
        match convert_ty(self, rhs).ok_or_else(err)? {
            ty if ty.is_integer() => Ok(self.clone()),
            Type::Vec(_, ty) if ty.is_integer() => Ok(self.clone()),
            _ => Err(err()),
        }
    }

    /// Valid operands:
    /// * `integer << u32`
    /// * `vec<integer> << vec<u32>`
    pub fn op_shl(&self, rhs: &Type) -> Result<Type, E> {
        let err = || E::Binary(BinaryOperator::ShiftLeft, self.ty(), rhs.ty());
        let rhs = rhs.convert_inner_to(&Type::U32).ok_or_else(err)?;
        match (self, rhs) {
            (lhs, Type::U32) if lhs.is_integer() => Ok(lhs.clone()),
            (lhs, Type::Vec(_, _)) => Ok(lhs.clone()),
            _ => Err(err()),
        }
    }

    /// Valid operands:
    /// * `integer >> u32`
    /// * `vec<integer> >> vec<u32>`
    pub fn op_shr(&self, rhs: &Type) -> Result<Type, E> {
        let err = || E::Binary(BinaryOperator::ShiftRight, self.ty(), rhs.ty());
        let rhs = rhs.convert_inner_to(&Type::U32).ok_or_else(err)?;
        match (self, rhs) {
            (lhs, Type::U32) if lhs.is_integer() => Ok(lhs.clone()),
            (lhs, Type::Vec(_, _)) => Ok(lhs.clone()),
            _ => Err(err()),
        }
    }
}

// -------------------
// POINTER EXPRESSIONS
// -------------------
// reference: https://www.w3.org/TR/WGSL/#address-of-expr
// reference: https://www.w3.org/TR/WGSL/#indirection-expr

impl Type {
    pub fn op_ref(&self) -> Result<Type, E> {
        match self {
            Type::Ref(a_s, ty, a_m) => {
                if *a_s == AddressSpace::Handle {
                    // "It is a shader-creation error if AS is the handle address space."
                    Err(E::PtrHandle)
                } else if false {
                    // TODO: We do not yet have enough information to check this:
                    // "It is a shader-creation error if r is a reference to a vector component."
                    Err(E::PtrVecComp)
                } else {
                    Ok(Type::Ptr(*a_s, ty.clone(), *a_m))
                }
            }
            _ => Err(E::Unary(UnaryOperator::AddressOf, self.ty())),
        }
    }

    pub fn op_deref(&self) -> Result<Type, E> {
        match self {
            Type::Ptr(a_s, ty, a_m) => Ok(Type::Ref(*a_s, ty.clone(), *a_m)),
            _ => Err(E::Unary(UnaryOperator::Indirection, self.ty())),
        }
    }
}