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_ARRAY_H_ 16 #define SRC_SEM_ARRAY_H_ 17 18 #include <stdint.h> 19 #include <string> 20 21 #include "src/sem/node.h" 22 #include "src/sem/type.h" 23 24 // Forward declarations 25 namespace tint { 26 namespace ast { 27 class Array; 28 } // namespace ast 29 } // namespace tint 30 31 namespace tint { 32 namespace sem { 33 34 /// Array holds the semantic information for Array nodes. 35 class Array : public Castable<Array, Type> { 36 public: 37 /// Constructor 38 /// @param element the array element type 39 /// @param count the number of elements in the array. 0 represents a 40 /// runtime-sized array. 41 /// @param align the byte alignment of the array 42 /// @param size the byte size of the array 43 /// @param stride the number of bytes from the start of one element of the 44 /// array to the start of the next element 45 /// @param implicit_stride the number of bytes from the start of one element 46 /// of the array to the start of the next element, if there was no [[stride]] 47 /// decoration applied. 48 Array(Type const* element, 49 uint32_t count, 50 uint32_t align, 51 uint32_t size, 52 uint32_t stride, 53 uint32_t implicit_stride); 54 55 /// @return the array element type ElemType()56 Type const* ElemType() const { return element_; } 57 58 /// @returns the number of elements in the array. 0 represents a runtime-sized 59 /// array. Count()60 uint32_t Count() const { return count_; } 61 62 /// @returns the byte alignment of the array 63 /// @note this may differ from the alignment of a structure member of this 64 /// array type, if the member is annotated with the `[[align(n)]]` decoration. 65 uint32_t Align() const override; 66 67 /// @returns the byte size of the array 68 /// @note this may differ from the size of a structure member of this array 69 /// type, if the member is annotated with the `[[size(n)]]` decoration. 70 uint32_t Size() const override; 71 72 /// @returns the number of bytes from the start of one element of the 73 /// array to the start of the next element Stride()74 uint32_t Stride() const { return stride_; } 75 76 /// @returns the number of bytes from the start of one element of the 77 /// array to the start of the next element, if there was no [[stride]] 78 /// decoration applied ImplicitStride()79 uint32_t ImplicitStride() const { return implicit_stride_; } 80 81 /// @returns true if the value returned by Stride() matches the element's 82 /// natural stride IsStrideImplicit()83 bool IsStrideImplicit() const { return stride_ == implicit_stride_; } 84 85 /// @returns true if this array is runtime sized IsRuntimeSized()86 bool IsRuntimeSized() const { return count_ == 0; } 87 88 /// @returns true if constructible as per 89 /// https://gpuweb.github.io/gpuweb/wgsl/#constructible-types 90 bool IsConstructible() const override; 91 92 /// @returns the name for the type 93 std::string type_name() const override; 94 95 /// @param symbols the program's symbol table 96 /// @returns the name for this type that closely resembles how it would be 97 /// declared in WGSL. 98 std::string FriendlyName(const SymbolTable& symbols) const override; 99 100 private: 101 Type const* const element_; 102 const uint32_t count_; 103 const uint32_t align_; 104 const uint32_t size_; 105 const uint32_t stride_; 106 const uint32_t implicit_stride_; 107 const bool constructible_; 108 }; 109 110 } // namespace sem 111 } // namespace tint 112 113 #endif // SRC_SEM_ARRAY_H_ 114