1 // Copyright 2016-2021 Doug Moen
2 // Licensed under the Apache License, version 2.0
3 // See accompanying file LICENSE or https://www.apache.org/licenses/LICENSE-2.0
4 
5 #ifndef LIBCURV_TYPE_H
6 #define LIBCURV_TYPE_H
7 
8 #include <libcurv/value.h>
9 
10 namespace curv {
11 
12 // A plex type is either a scalar type (Bool, Num)
13 // or it is one of the list types that are treated specially
14 // by GPU shader languages (the WGSL and SPIR-V type systems).
15 // Plex types are the argument and result types of SPIR-V primitive functions.
16 enum class Plex_Type : short
17 {
18     missing,    // flag value: not a plex type
19     Bool,
20     Bool2,
21     Bool3,
22     Bool4,
23     Bool32,
24     Bool2x32,
25     Bool3x32,
26     Bool4x32,
27     Num,
28     Vec2,
29     Vec3,
30     Vec4,
31     Mat2,
32     Mat3,
33     Mat4
34 };
35 
36 extern const char* glsl_plex_type_name[];
37 
38 struct Type : public Ref_Value
39 {
40     Plex_Type plex_type_;
TypeType41     Type(int st, Plex_Type pt) : Ref_Value(ty_type, st), plex_type_(pt) {}
42     static Shared<const Type> Error;
43     static Shared<const Type> Bool;
44     static Shared<const Type> Bool32;
45     static Shared<const Type> Num;
46 
47     static bool equal(const Type&, const Type&);
48     unsigned rank() const;
49 
50     Shared<const Type> plex_array_base() const;
51     unsigned plex_array_rank() const;
52     unsigned plex_array_dim(unsigned) const;
53 };
54 
55 struct Error_Type : public Type
56 {
Error_TypeError_Type57     Error_Type() : Type(sty_error_type, Plex_Type::missing) {}
58     virtual void print_repr(std::ostream&) const override;
59 };
60 
61 struct Bool_Type : public Type
62 {
Bool_TypeBool_Type63     Bool_Type() : Type(sty_bool_type, Plex_Type::Bool) {}
64     virtual void print_repr(std::ostream&) const override;
65 };
66 
67 struct Num_Type : public Type
68 {
Num_TypeNum_Type69     Num_Type() : Type(sty_num_type, Plex_Type::Num) {}
70     virtual void print_repr(std::ostream&) const override;
71 };
72 
73 struct List_Type : public Type
74 {
75     unsigned count_;
76     Shared<const Type> elem_type_;
List_TypeList_Type77     List_Type(unsigned c, Shared<const Type> et)
78     :
79         Type(sty_list_type, make_plex_type(c, et)),
80         count_(c),
81         elem_type_(et)
82     {}
83     static Plex_Type make_plex_type(unsigned, Shared<const Type>);
84     virtual void print_repr(std::ostream&) const override;
85 };
86 
87 } // namespace curv
88 #endif // header guard
89