1 // Copyright 2020 The Tint Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SRC_SEM_TYPE_H_ 16 #define SRC_SEM_TYPE_H_ 17 18 #include <string> 19 20 #include "src/sem/node.h" 21 22 namespace tint { 23 24 // Forward declarations 25 class ProgramBuilder; 26 class SymbolTable; 27 28 namespace sem { 29 30 /// Supported memory layouts for calculating sizes 31 enum class MemoryLayout { kUniformBuffer, kStorageBuffer }; 32 33 /// Base class for a type in the system 34 class Type : public Castable<Type, Node> { 35 public: 36 /// Move constructor 37 Type(Type&&); 38 ~Type() override; 39 40 /// @returns the name for this type. The type name is unique over all types. 41 virtual std::string type_name() const = 0; 42 43 /// @param symbols the program's symbol table 44 /// @returns the name for this type that closely resembles how it would be 45 /// declared in WGSL. 46 virtual std::string FriendlyName(const SymbolTable& symbols) const = 0; 47 48 /// @returns the inner most pointee type if this is a pointer, `this` 49 /// otherwise 50 const Type* UnwrapPtr() const; 51 52 /// @returns the inner type if this is a reference, `this` otherwise 53 const Type* UnwrapRef() const; 54 55 /// @returns the size in bytes of the type. This may include tail padding. 56 /// @note opaque types will return a size of 0. 57 virtual uint32_t Size() const; 58 59 /// @returns the alignment in bytes of the type. This may include tail 60 /// padding. 61 /// @note opaque types will return a size of 0. 62 virtual uint32_t Align() const; 63 64 /// @returns true if constructible as per 65 /// https://gpuweb.github.io/gpuweb/wgsl/#constructible-types 66 virtual bool IsConstructible() const; 67 68 /// @returns true if this type is a scalar 69 bool is_scalar() const; 70 /// @returns true if this type is a numeric scalar 71 bool is_numeric_scalar() const; 72 /// @returns true if this type is a float scalar 73 bool is_float_scalar() const; 74 /// @returns true if this type is a float matrix 75 bool is_float_matrix() const; 76 /// @returns true if this type is a float vector 77 bool is_float_vector() const; 78 /// @returns true if this type is a float scalar or vector 79 bool is_float_scalar_or_vector() const; 80 /// @returns true if this type is a float scalar or vector or matrix 81 bool is_float_scalar_or_vector_or_matrix() const; 82 /// @returns true if this type is an integer scalar 83 bool is_integer_scalar() const; 84 /// @returns true if this type is a signed integer scalar 85 bool is_signed_integer_scalar() const; 86 /// @returns true if this type is an unsigned integer scalar 87 bool is_unsigned_integer_scalar() const; 88 /// @returns true if this type is a signed integer vector 89 bool is_signed_integer_vector() const; 90 /// @returns true if this type is an unsigned vector 91 bool is_unsigned_integer_vector() const; 92 /// @returns true if this type is an unsigned scalar or vector 93 bool is_unsigned_scalar_or_vector() const; 94 /// @returns true if this type is a signed scalar or vector 95 bool is_signed_scalar_or_vector() const; 96 /// @returns true if this type is an integer scalar or vector 97 bool is_integer_scalar_or_vector() const; 98 /// @returns true if this type is a boolean vector 99 bool is_bool_vector() const; 100 /// @returns true if this type is boolean scalar or vector 101 bool is_bool_scalar_or_vector() const; 102 /// @returns true if this type is a numeric vector 103 bool is_numeric_vector() const; 104 /// @returns true if this type is a vector of scalar type 105 bool is_scalar_vector() const; 106 /// @returns true if this type is a numeric scale or vector 107 bool is_numeric_scalar_or_vector() const; 108 /// @returns true if this type is a handle type 109 bool is_handle() const; 110 111 protected: 112 Type(); 113 }; 114 115 } // namespace sem 116 } // namespace tint 117 118 #endif // SRC_SEM_TYPE_H_ 119