1 // Copyright 2021 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_CONSTANT_H_ 16 #define SRC_SEM_CONSTANT_H_ 17 18 #include <vector> 19 20 #include "src/program_builder.h" 21 #include "src/sem/type.h" 22 23 namespace tint { 24 namespace sem { 25 26 /// A Constant is compile-time known expression value, expressed as a flattened 27 /// list of scalar values. Value may be of a scalar or vector type. 28 class Constant { 29 using i32 = ProgramBuilder::i32; 30 using u32 = ProgramBuilder::u32; 31 using f32 = ProgramBuilder::f32; 32 33 public: 34 /// Scalar holds a single constant scalar value, as a union of an i32, u32, 35 /// f32 or boolean. 36 union Scalar { 37 /// The scalar value as a i32 38 int32_t i32; 39 /// The scalar value as a u32 40 uint32_t u32; 41 /// The scalar value as a f32 42 float f32; 43 /// The scalar value as a bool 44 bool bool_; 45 46 /// Constructs the scalar with the i32 value `v` 47 /// @param v the value of the Scalar Scalar(ProgramBuilder::i32 v)48 Scalar(ProgramBuilder::i32 v) : i32(v) {} // NOLINT 49 50 /// Constructs the scalar with the u32 value `v` 51 /// @param v the value of the Scalar Scalar(ProgramBuilder::u32 v)52 Scalar(ProgramBuilder::u32 v) : u32(v) {} // NOLINT 53 54 /// Constructs the scalar with the f32 value `v` 55 /// @param v the value of the Scalar Scalar(ProgramBuilder::f32 v)56 Scalar(ProgramBuilder::f32 v) : f32(v) {} // NOLINT 57 58 /// Constructs the scalar with the bool value `v` 59 /// @param v the value of the Scalar Scalar(bool v)60 Scalar(bool v) : bool_(v) {} // NOLINT 61 }; 62 63 /// Scalars is a list of scalar values 64 using Scalars = std::vector<Scalar>; 65 66 /// Constructs an invalid Constant 67 Constant(); 68 69 /// Constructs a Constant of the given type and element values 70 /// @param ty the Constant type 71 /// @param els the Constant element values 72 Constant(const Type* ty, Scalars els); 73 74 /// Copy constructor 75 Constant(const Constant&); 76 77 /// Destructor 78 ~Constant(); 79 80 /// Copy assignment 81 /// @param other the Constant to copy 82 /// @returns this Constant 83 Constant& operator=(const Constant& other); 84 85 /// @returns true if the Constant has been initialized IsValid()86 bool IsValid() const { return type_ != nullptr; } 87 88 /// @return true if the Constant has been initialized 89 operator bool() const { return IsValid(); } 90 91 /// @returns the type of the Constant Type()92 const sem::Type* Type() const { return type_; } 93 94 /// @returns the element type of the Constant ElementType()95 const sem::Type* ElementType() const { return elem_type_; } 96 97 /// @returns the constant's scalar elements Elements()98 const Scalars& Elements() const { return elems_; } 99 100 /// Calls `func(s)` with s being the current scalar value at `index`. 101 /// `func` is typically a lambda of the form '[](auto&& s)'. 102 /// @param index the index of the scalar value 103 /// @param func a function with signature `T(S)` 104 /// @return the value returned by func. 105 template <typename Func> WithScalarAt(size_t index,Func && func)106 auto WithScalarAt(size_t index, Func&& func) const { 107 auto* elem_type = ElementType(); 108 if (elem_type->Is<I32>()) { 109 return func(elems_[index].i32); 110 } 111 if (elem_type->Is<U32>()) { 112 return func(elems_[index].u32); 113 } 114 if (elem_type->Is<F32>()) { 115 return func(elems_[index].f32); 116 } 117 if (elem_type->Is<Bool>()) { 118 return func(elems_[index].bool_); 119 } 120 diag::List diags; 121 TINT_UNREACHABLE(Semantic, diags) 122 << "invalid scalar type " << type_->type_name(); 123 return func(~0); 124 } 125 126 private: 127 const sem::Type* type_ = nullptr; 128 const sem::Type* elem_type_ = nullptr; 129 Scalars elems_; 130 }; 131 132 } // namespace sem 133 } // namespace tint 134 135 #endif // SRC_SEM_CONSTANT_H_ 136