Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 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_PROGRAM_BUILDER_H_ |
| 16 | #define SRC_PROGRAM_BUILDER_H_ |
| 17 | |
| 18 | #include <string> |
| 19 | #include <utility> |
| 20 | |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 21 | #include "src/ast/alias.h" |
Ben Clayton | 7241a50 | 2021-04-22 14:32:53 +0000 | [diff] [blame] | 22 | #include "src/ast/array.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 23 | #include "src/ast/array_accessor_expression.h" |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 24 | #include "src/ast/assignment_statement.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 25 | #include "src/ast/binary_expression.h" |
Ben Clayton | a922e65 | 2021-04-21 13:37:22 +0000 | [diff] [blame] | 26 | #include "src/ast/bool.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 27 | #include "src/ast/bool_literal.h" |
| 28 | #include "src/ast/call_expression.h" |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 29 | #include "src/ast/case_statement.h" |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 30 | #include "src/ast/depth_texture.h" |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 31 | #include "src/ast/external_texture.h" |
Ben Clayton | a922e65 | 2021-04-21 13:37:22 +0000 | [diff] [blame] | 32 | #include "src/ast/f32.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 33 | #include "src/ast/float_literal.h" |
Ben Clayton | a922e65 | 2021-04-21 13:37:22 +0000 | [diff] [blame] | 34 | #include "src/ast/i32.h" |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 35 | #include "src/ast/if_statement.h" |
| 36 | #include "src/ast/loop_statement.h" |
Ben Clayton | fdb91fd | 2021-04-22 14:30:53 +0000 | [diff] [blame] | 37 | #include "src/ast/matrix.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 38 | #include "src/ast/member_accessor_expression.h" |
| 39 | #include "src/ast/module.h" |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 40 | #include "src/ast/multisampled_texture.h" |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 41 | #include "src/ast/override_decoration.h" |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 42 | #include "src/ast/pointer.h" |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 43 | #include "src/ast/return_statement.h" |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 44 | #include "src/ast/sampled_texture.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 45 | #include "src/ast/scalar_constructor_expression.h" |
| 46 | #include "src/ast/sint_literal.h" |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 47 | #include "src/ast/stage_decoration.h" |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 48 | #include "src/ast/storage_texture.h" |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 49 | #include "src/ast/stride_decoration.h" |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 50 | #include "src/ast/struct_member_align_decoration.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 51 | #include "src/ast/struct_member_offset_decoration.h" |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 52 | #include "src/ast/struct_member_size_decoration.h" |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 53 | #include "src/ast/switch_statement.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 54 | #include "src/ast/type_constructor_expression.h" |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 55 | #include "src/ast/type_name.h" |
Ben Clayton | a922e65 | 2021-04-21 13:37:22 +0000 | [diff] [blame] | 56 | #include "src/ast/u32.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 57 | #include "src/ast/uint_literal.h" |
Ben Clayton | fe0910f | 2021-05-17 15:51:47 +0000 | [diff] [blame] | 58 | #include "src/ast/unary_op_expression.h" |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 59 | #include "src/ast/variable_decl_statement.h" |
Ben Clayton | e30ffeb | 2021-04-22 14:24:43 +0000 | [diff] [blame] | 60 | #include "src/ast/vector.h" |
Ben Clayton | a922e65 | 2021-04-21 13:37:22 +0000 | [diff] [blame] | 61 | #include "src/ast/void.h" |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 62 | #include "src/ast/workgroup_decoration.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 63 | #include "src/program.h" |
Ben Clayton | e6995de | 2021-04-13 23:27:27 +0000 | [diff] [blame] | 64 | #include "src/program_id.h" |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 65 | #include "src/sem/array.h" |
Antonio Maiorano | aea9c68 | 2021-04-19 22:54:43 +0000 | [diff] [blame] | 66 | #include "src/sem/bool_type.h" |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 67 | #include "src/sem/depth_texture_type.h" |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 68 | #include "src/sem/external_texture_type.h" |
Antonio Maiorano | aea9c68 | 2021-04-19 22:54:43 +0000 | [diff] [blame] | 69 | #include "src/sem/f32_type.h" |
| 70 | #include "src/sem/i32_type.h" |
| 71 | #include "src/sem/matrix_type.h" |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 72 | #include "src/sem/multisampled_texture_type.h" |
Antonio Maiorano | aea9c68 | 2021-04-19 22:54:43 +0000 | [diff] [blame] | 73 | #include "src/sem/pointer_type.h" |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 74 | #include "src/sem/sampled_texture_type.h" |
| 75 | #include "src/sem/storage_texture_type.h" |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 76 | #include "src/sem/struct.h" |
Antonio Maiorano | aea9c68 | 2021-04-19 22:54:43 +0000 | [diff] [blame] | 77 | #include "src/sem/u32_type.h" |
| 78 | #include "src/sem/vector_type.h" |
| 79 | #include "src/sem/void_type.h" |
Ben Clayton | a922e65 | 2021-04-21 13:37:22 +0000 | [diff] [blame] | 80 | #include "src/typepair.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 81 | |
| 82 | namespace tint { |
| 83 | |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 84 | // Forward declarations |
| 85 | namespace ast { |
| 86 | class VariableDeclStatement; |
| 87 | } // namespace ast |
| 88 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 89 | class CloneContext; |
| 90 | |
| 91 | /// ProgramBuilder is a mutable builder for a Program. |
| 92 | /// To construct a Program, populate the builder and then `std::move` it to a |
| 93 | /// Program. |
| 94 | class ProgramBuilder { |
| 95 | public: |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 96 | /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node> |
| 97 | using ASTNodeAllocator = BlockAllocator<ast::Node>; |
| 98 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 99 | /// SemNodeAllocator is an alias to BlockAllocator<sem::Node> |
| 100 | using SemNodeAllocator = BlockAllocator<sem::Node>; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 101 | |
| 102 | /// `i32` is a type alias to `int`. |
| 103 | /// Useful for passing to template methods such as `vec2<i32>()` to imitate |
| 104 | /// WGSL syntax. |
| 105 | /// Note: this is intentionally not aliased to uint32_t as we want integer |
| 106 | /// literals passed to the builder to match WGSL's integer literal types. |
| 107 | using i32 = decltype(1); |
| 108 | /// `u32` is a type alias to `unsigned int`. |
| 109 | /// Useful for passing to template methods such as `vec2<u32>()` to imitate |
| 110 | /// WGSL syntax. |
| 111 | /// Note: this is intentionally not aliased to uint32_t as we want integer |
| 112 | /// literals passed to the builder to match WGSL's integer literal types. |
| 113 | using u32 = decltype(1u); |
| 114 | /// `f32` is a type alias to `float` |
| 115 | /// Useful for passing to template methods such as `vec2<f32>()` to imitate |
| 116 | /// WGSL syntax. |
| 117 | using f32 = float; |
| 118 | |
| 119 | /// Constructor |
| 120 | ProgramBuilder(); |
| 121 | |
| 122 | /// Move constructor |
| 123 | /// @param rhs the builder to move |
| 124 | ProgramBuilder(ProgramBuilder&& rhs); |
| 125 | |
| 126 | /// Destructor |
| 127 | virtual ~ProgramBuilder(); |
| 128 | |
| 129 | /// Move assignment operator |
| 130 | /// @param rhs the builder to move |
| 131 | /// @return this builder |
| 132 | ProgramBuilder& operator=(ProgramBuilder&& rhs); |
| 133 | |
Ben Clayton | e43c830 | 2021-01-29 11:59:32 +0000 | [diff] [blame] | 134 | /// Wrap returns a new ProgramBuilder wrapping the Program `program` without |
| 135 | /// making a deep clone of the Program contents. |
| 136 | /// ProgramBuilder returned by Wrap() is intended to temporarily extend an |
| 137 | /// existing immutable program. |
| 138 | /// As the returned ProgramBuilder wraps `program`, `program` must not be |
| 139 | /// destructed or assigned while using the returned ProgramBuilder. |
| 140 | /// TODO(bclayton) - Evaluate whether there are safer alternatives to this |
| 141 | /// function. See crbug.com/tint/460. |
| 142 | /// @param program the immutable Program to wrap |
| 143 | /// @return the ProgramBuilder that wraps `program` |
| 144 | static ProgramBuilder Wrap(const Program* program); |
| 145 | |
Ben Clayton | e6995de | 2021-04-13 23:27:27 +0000 | [diff] [blame] | 146 | /// @returns the unique identifier for this program |
| 147 | ProgramID ID() const { return id_; } |
| 148 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 149 | /// @returns a reference to the program's types |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 150 | sem::Manager& Types() { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 151 | AssertNotMoved(); |
| 152 | return types_; |
| 153 | } |
| 154 | |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 155 | /// @returns a reference to the program's types |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 156 | const sem::Manager& Types() const { |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 157 | AssertNotMoved(); |
| 158 | return types_; |
| 159 | } |
| 160 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 161 | /// @returns a reference to the program's AST nodes storage |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 162 | ASTNodeAllocator& ASTNodes() { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 163 | AssertNotMoved(); |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 164 | return ast_nodes_; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 167 | /// @returns a reference to the program's AST nodes storage |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 168 | const ASTNodeAllocator& ASTNodes() const { |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 169 | AssertNotMoved(); |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 170 | return ast_nodes_; |
| 171 | } |
| 172 | |
| 173 | /// @returns a reference to the program's semantic nodes storage |
| 174 | SemNodeAllocator& SemNodes() { |
| 175 | AssertNotMoved(); |
| 176 | return sem_nodes_; |
| 177 | } |
| 178 | |
| 179 | /// @returns a reference to the program's semantic nodes storage |
| 180 | const SemNodeAllocator& SemNodes() const { |
| 181 | AssertNotMoved(); |
| 182 | return sem_nodes_; |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 185 | /// @returns a reference to the program's AST root Module |
| 186 | ast::Module& AST() { |
| 187 | AssertNotMoved(); |
| 188 | return *ast_; |
| 189 | } |
| 190 | |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 191 | /// @returns a reference to the program's AST root Module |
| 192 | const ast::Module& AST() const { |
| 193 | AssertNotMoved(); |
| 194 | return *ast_; |
| 195 | } |
| 196 | |
| 197 | /// @returns a reference to the program's semantic info |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 198 | sem::Info& Sem() { |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 199 | AssertNotMoved(); |
| 200 | return sem_; |
| 201 | } |
| 202 | |
| 203 | /// @returns a reference to the program's semantic info |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 204 | const sem::Info& Sem() const { |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 205 | AssertNotMoved(); |
| 206 | return sem_; |
| 207 | } |
| 208 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 209 | /// @returns a reference to the program's SymbolTable |
| 210 | SymbolTable& Symbols() { |
| 211 | AssertNotMoved(); |
| 212 | return symbols_; |
| 213 | } |
| 214 | |
Ben Clayton | 708dc2d | 2021-01-29 11:22:40 +0000 | [diff] [blame] | 215 | /// @returns a reference to the program's SymbolTable |
| 216 | const SymbolTable& Symbols() const { |
| 217 | AssertNotMoved(); |
| 218 | return symbols_; |
| 219 | } |
| 220 | |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 221 | /// @returns a reference to the program's diagnostics |
| 222 | diag::List& Diagnostics() { |
| 223 | AssertNotMoved(); |
| 224 | return diagnostics_; |
| 225 | } |
| 226 | |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 227 | /// @returns a reference to the program's diagnostics |
| 228 | const diag::List& Diagnostics() const { |
| 229 | AssertNotMoved(); |
| 230 | return diagnostics_; |
| 231 | } |
| 232 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 233 | /// Controls whether the Resolver will be run on the program when it is built. |
Ben Clayton | dd69ac3 | 2021-01-27 19:23:55 +0000 | [diff] [blame] | 234 | /// @param enable the new flag value (defaults to true) |
| 235 | void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; } |
| 236 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 237 | /// @return true if the Resolver will be run on the program when it is |
Ben Clayton | dd69ac3 | 2021-01-27 19:23:55 +0000 | [diff] [blame] | 238 | /// built. |
| 239 | bool ResolveOnBuild() const { return resolve_on_build_; } |
| 240 | |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 241 | /// @returns true if the program has no error diagnostics and is not missing |
| 242 | /// information |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 243 | bool IsValid() const; |
| 244 | |
Ben Clayton | 708dc2d | 2021-01-29 11:22:40 +0000 | [diff] [blame] | 245 | /// Writes a representation of the node to the output stream |
| 246 | /// @note unlike str(), to_str() does not automatically demangle the string. |
| 247 | /// @param node the AST node |
| 248 | /// @param out the stream to write to |
| 249 | /// @param indent number of spaces to indent the node when writing |
| 250 | void to_str(const ast::Node* node, std::ostream& out, size_t indent) const { |
| 251 | node->to_str(Sem(), out, indent); |
| 252 | } |
| 253 | |
| 254 | /// Returns a demangled, string representation of `node`. |
| 255 | /// @param node the AST node |
| 256 | /// @returns a string representation of the node |
| 257 | std::string str(const ast::Node* node) const; |
| 258 | |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 259 | /// Creates a new ast::Node owned by the ProgramBuilder. When the |
| 260 | /// ProgramBuilder is destructed, the ast::Node will also be destructed. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 261 | /// @param source the Source of the node |
| 262 | /// @param args the arguments to pass to the type constructor |
| 263 | /// @returns the node pointer |
| 264 | template <typename T, typename... ARGS> |
| 265 | traits::EnableIfIsType<T, ast::Node>* create(const Source& source, |
| 266 | ARGS&&... args) { |
| 267 | AssertNotMoved(); |
Ben Clayton | e6995de | 2021-04-13 23:27:27 +0000 | [diff] [blame] | 268 | return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 271 | /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current |
| 272 | /// Source as set by the last call to SetSource() as the only argument to the |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 273 | /// constructor. |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 274 | /// When the ProgramBuilder is destructed, the ast::Node will also be |
| 275 | /// destructed. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 276 | /// @returns the node pointer |
| 277 | template <typename T> |
| 278 | traits::EnableIfIsType<T, ast::Node>* create() { |
| 279 | AssertNotMoved(); |
Ben Clayton | e6995de | 2021-04-13 23:27:27 +0000 | [diff] [blame] | 280 | return ast_nodes_.Create<T>(id_, source_); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 283 | /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current |
| 284 | /// Source as set by the last call to SetSource() as the first argument to the |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 285 | /// constructor. |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 286 | /// When the ProgramBuilder is destructed, the ast::Node will also be |
| 287 | /// destructed. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 288 | /// @param arg0 the first arguments to pass to the type constructor |
| 289 | /// @param args the remaining arguments to pass to the type constructor |
| 290 | /// @returns the node pointer |
| 291 | template <typename T, typename ARG0, typename... ARGS> |
| 292 | traits::EnableIf</* T is ast::Node and ARG0 is not Source */ |
| 293 | traits::IsTypeOrDerived<T, ast::Node>::value && |
| 294 | !traits::IsTypeOrDerived<ARG0, Source>::value, |
| 295 | T>* |
| 296 | create(ARG0&& arg0, ARGS&&... args) { |
| 297 | AssertNotMoved(); |
Ben Clayton | e6995de | 2021-04-13 23:27:27 +0000 | [diff] [blame] | 298 | return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0), |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 299 | std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 302 | /// Creates a new sem::Node owned by the ProgramBuilder. |
| 303 | /// When the ProgramBuilder is destructed, the sem::Node will also be |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 304 | /// destructed. |
| 305 | /// @param args the arguments to pass to the type constructor |
| 306 | /// @returns the node pointer |
| 307 | template <typename T, typename... ARGS> |
Ben Clayton | 58750ea | 2021-05-06 15:52:33 +0000 | [diff] [blame] | 308 | traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value && |
| 309 | !traits::IsTypeOrDerived<T, sem::Type>::value, |
| 310 | T>* |
| 311 | create(ARGS&&... args) { |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 312 | AssertNotMoved(); |
| 313 | return sem_nodes_.Create<T>(std::forward<ARGS>(args)...); |
| 314 | } |
| 315 | |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 316 | /// Creates a new sem::Type owned by the ProgramBuilder. |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 317 | /// When the ProgramBuilder is destructed, owned ProgramBuilder and the |
| 318 | /// returned`Type` will also be destructed. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 319 | /// Types are unique (de-aliased), and so calling create() for the same `T` |
| 320 | /// and arguments will return the same pointer. |
| 321 | /// @warning Use this method to acquire a type only if all of its type |
| 322 | /// information is provided in the constructor arguments `args`.<br> |
| 323 | /// If the type requires additional configuration after construction that |
| 324 | /// affect its fundamental type, build the type with `std::make_unique`, make |
| 325 | /// any necessary alterations and then call unique_type() instead. |
| 326 | /// @param args the arguments to pass to the type constructor |
| 327 | /// @returns the de-aliased type pointer |
| 328 | template <typename T, typename... ARGS> |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 329 | traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) { |
| 330 | static_assert(std::is_base_of<sem::Type, T>::value, |
| 331 | "T does not derive from sem::Type"); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 332 | AssertNotMoved(); |
| 333 | return types_.Get<T>(std::forward<ARGS>(args)...); |
| 334 | } |
| 335 | |
| 336 | /// Marks this builder as moved, preventing any further use of the builder. |
| 337 | void MarkAsMoved(); |
| 338 | |
| 339 | ////////////////////////////////////////////////////////////////////////////// |
| 340 | // TypesBuilder |
| 341 | ////////////////////////////////////////////////////////////////////////////// |
| 342 | |
| 343 | /// TypesBuilder holds basic `tint` types and methods for constructing |
| 344 | /// complex types. |
| 345 | class TypesBuilder { |
| 346 | public: |
| 347 | /// Constructor |
| 348 | /// @param builder the program builder |
| 349 | explicit TypesBuilder(ProgramBuilder* builder); |
| 350 | |
| 351 | /// @return the tint AST type for the C type `T`. |
| 352 | template <typename T> |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 353 | typ::Type Of() const { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 354 | return CToAST<T>::get(this); |
| 355 | } |
| 356 | |
| 357 | /// @returns a boolean type |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 358 | typ::Bool bool_() const { return builder->create<ast::Bool>(); } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 359 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 360 | /// @param source the Source of the node |
| 361 | /// @returns a boolean type |
| 362 | typ::Bool bool_(const Source& source) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 363 | return builder->create<ast::Bool>(source); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 366 | /// @returns a f32 type |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 367 | typ::F32 f32() const { return builder->create<ast::F32>(); } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 368 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 369 | /// @param source the Source of the node |
| 370 | /// @returns a f32 type |
| 371 | typ::F32 f32(const Source& source) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 372 | return builder->create<ast::F32>(source); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 375 | /// @returns a i32 type |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 376 | typ::I32 i32() const { return builder->create<ast::I32>(); } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 377 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 378 | /// @param source the Source of the node |
| 379 | /// @returns a i32 type |
| 380 | typ::I32 i32(const Source& source) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 381 | return builder->create<ast::I32>(source); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 384 | /// @returns a u32 type |
Ben Clayton | 58dec17 | 2021-05-19 17:47:11 +0000 | [diff] [blame] | 385 | typ::U32 u32() const { return builder->create<ast::U32>(); } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 386 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 387 | /// @param source the Source of the node |
| 388 | /// @returns a u32 type |
| 389 | typ::U32 u32(const Source& source) const { |
Ben Clayton | 58dec17 | 2021-05-19 17:47:11 +0000 | [diff] [blame] | 390 | return builder->create<ast::U32>(source); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 393 | /// @returns a void type |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 394 | typ::Void void_() const { return builder->create<ast::Void>(); } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 395 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 396 | /// @param source the Source of the node |
| 397 | /// @returns a void type |
| 398 | typ::Void void_(const Source& source) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 399 | return builder->create<ast::Void>(source); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 402 | /// @param type vector subtype |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 403 | /// @param n vector width in elements |
| 404 | /// @return the tint AST type for a `n`-element vector of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 405 | typ::Vector vec(ast::Type* type, uint32_t n) const { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 406 | type = MaybeCreateTypename(type); |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 407 | return builder->create<ast::Vector>(type, n); |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 410 | /// @param source the Source of the node |
| 411 | /// @param type vector subtype |
| 412 | /// @param n vector width in elements |
| 413 | /// @return the tint AST type for a `n`-element vector of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 414 | typ::Vector vec(const Source& source, ast::Type* type, uint32_t n) const { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 415 | type = MaybeCreateTypename(type); |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 416 | return builder->create<ast::Vector>(source, type, n); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 419 | /// @param type vector subtype |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 420 | /// @return the tint AST type for a 2-element vector of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 421 | typ::Vector vec2(ast::Type* type) const { return vec(type, 2u); } |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 422 | |
| 423 | /// @param type vector subtype |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 424 | /// @return the tint AST type for a 3-element vector of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 425 | typ::Vector vec3(ast::Type* type) const { return vec(type, 3u); } |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 426 | |
| 427 | /// @param type vector subtype |
| 428 | /// @return the tint AST type for a 4-element vector of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 429 | typ::Vector vec4(ast::Type* type) const { return vec(type, 4u); } |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 430 | |
| 431 | /// @param n vector width in elements |
| 432 | /// @return the tint AST type for a `n`-element vector of `type`. |
| 433 | template <typename T> |
| 434 | typ::Vector vec(uint32_t n) const { |
| 435 | return vec(Of<T>(), n); |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 438 | /// @return the tint AST type for a 2-element vector of the C type `T`. |
| 439 | template <typename T> |
Ben Clayton | e30ffeb | 2021-04-22 14:24:43 +0000 | [diff] [blame] | 440 | typ::Vector vec2() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 441 | return vec2(Of<T>()); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /// @return the tint AST type for a 3-element vector of the C type `T`. |
| 445 | template <typename T> |
Ben Clayton | e30ffeb | 2021-04-22 14:24:43 +0000 | [diff] [blame] | 446 | typ::Vector vec3() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 447 | return vec3(Of<T>()); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /// @return the tint AST type for a 4-element vector of the C type `T`. |
| 451 | template <typename T> |
Ben Clayton | e30ffeb | 2021-04-22 14:24:43 +0000 | [diff] [blame] | 452 | typ::Vector vec4() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 453 | return vec4(Of<T>()); |
| 454 | } |
| 455 | |
| 456 | /// @param type matrix subtype |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 457 | /// @param columns number of columns for the matrix |
| 458 | /// @param rows number of rows for the matrix |
| 459 | /// @return the tint AST type for a matrix of `type` |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 460 | typ::Matrix mat(ast::Type* type, uint32_t columns, uint32_t rows) const { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 461 | type = MaybeCreateTypename(type); |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 462 | return builder->create<ast::Matrix>(type, rows, columns); |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 465 | /// @param source the Source of the node |
| 466 | /// @param type matrix subtype |
| 467 | /// @param columns number of columns for the matrix |
| 468 | /// @param rows number of rows for the matrix |
| 469 | /// @return the tint AST type for a matrix of `type` |
| 470 | typ::Matrix mat(const Source& source, |
| 471 | typ::Type type, |
| 472 | uint32_t columns, |
| 473 | uint32_t rows) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 474 | return builder->create<ast::Matrix>(source, type, rows, columns); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 477 | /// @param type matrix subtype |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 478 | /// @return the tint AST type for a 2x3 matrix of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 479 | typ::Matrix mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); } |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 480 | |
| 481 | /// @param type matrix subtype |
| 482 | /// @return the tint AST type for a 2x3 matrix of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 483 | typ::Matrix mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); } |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 484 | |
| 485 | /// @param type matrix subtype |
| 486 | /// @return the tint AST type for a 2x4 matrix of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 487 | typ::Matrix mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); } |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 488 | |
| 489 | /// @param type matrix subtype |
| 490 | /// @return the tint AST type for a 3x2 matrix of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 491 | typ::Matrix mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); } |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 492 | |
| 493 | /// @param type matrix subtype |
| 494 | /// @return the tint AST type for a 3x3 matrix of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 495 | typ::Matrix mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); } |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 496 | |
| 497 | /// @param type matrix subtype |
| 498 | /// @return the tint AST type for a 3x4 matrix of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 499 | typ::Matrix mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); } |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 500 | |
| 501 | /// @param type matrix subtype |
| 502 | /// @return the tint AST type for a 4x2 matrix of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 503 | typ::Matrix mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); } |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 504 | |
| 505 | /// @param type matrix subtype |
| 506 | /// @return the tint AST type for a 4x3 matrix of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 507 | typ::Matrix mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); } |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 508 | |
| 509 | /// @param type matrix subtype |
| 510 | /// @return the tint AST type for a 4x4 matrix of `type`. |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 511 | typ::Matrix mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 512 | |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 513 | /// @param columns number of columns for the matrix |
| 514 | /// @param rows number of rows for the matrix |
| 515 | /// @return the tint AST type for a matrix of `type` |
| 516 | template <typename T> |
| 517 | typ::Matrix mat(uint32_t columns, uint32_t rows) const { |
| 518 | return mat(Of<T>(), columns, rows); |
| 519 | } |
| 520 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 521 | /// @return the tint AST type for a 2x3 matrix of the C type `T`. |
| 522 | template <typename T> |
Ben Clayton | fdb91fd | 2021-04-22 14:30:53 +0000 | [diff] [blame] | 523 | typ::Matrix mat2x2() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 524 | return mat2x2(Of<T>()); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /// @return the tint AST type for a 2x3 matrix of the C type `T`. |
| 528 | template <typename T> |
Ben Clayton | fdb91fd | 2021-04-22 14:30:53 +0000 | [diff] [blame] | 529 | typ::Matrix mat2x3() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 530 | return mat2x3(Of<T>()); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /// @return the tint AST type for a 2x4 matrix of the C type `T`. |
| 534 | template <typename T> |
Ben Clayton | fdb91fd | 2021-04-22 14:30:53 +0000 | [diff] [blame] | 535 | typ::Matrix mat2x4() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 536 | return mat2x4(Of<T>()); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /// @return the tint AST type for a 3x2 matrix of the C type `T`. |
| 540 | template <typename T> |
Ben Clayton | fdb91fd | 2021-04-22 14:30:53 +0000 | [diff] [blame] | 541 | typ::Matrix mat3x2() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 542 | return mat3x2(Of<T>()); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /// @return the tint AST type for a 3x3 matrix of the C type `T`. |
| 546 | template <typename T> |
Ben Clayton | fdb91fd | 2021-04-22 14:30:53 +0000 | [diff] [blame] | 547 | typ::Matrix mat3x3() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 548 | return mat3x3(Of<T>()); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /// @return the tint AST type for a 3x4 matrix of the C type `T`. |
| 552 | template <typename T> |
Ben Clayton | fdb91fd | 2021-04-22 14:30:53 +0000 | [diff] [blame] | 553 | typ::Matrix mat3x4() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 554 | return mat3x4(Of<T>()); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /// @return the tint AST type for a 4x2 matrix of the C type `T`. |
| 558 | template <typename T> |
Ben Clayton | fdb91fd | 2021-04-22 14:30:53 +0000 | [diff] [blame] | 559 | typ::Matrix mat4x2() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 560 | return mat4x2(Of<T>()); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /// @return the tint AST type for a 4x3 matrix of the C type `T`. |
| 564 | template <typename T> |
Ben Clayton | fdb91fd | 2021-04-22 14:30:53 +0000 | [diff] [blame] | 565 | typ::Matrix mat4x3() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 566 | return mat4x3(Of<T>()); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /// @return the tint AST type for a 4x4 matrix of the C type `T`. |
| 570 | template <typename T> |
Ben Clayton | fdb91fd | 2021-04-22 14:30:53 +0000 | [diff] [blame] | 571 | typ::Matrix mat4x4() const { |
Antonio Maiorano | 2543686 | 2021-04-13 13:32:33 +0000 | [diff] [blame] | 572 | return mat4x4(Of<T>()); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /// @param subtype the array element type |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 576 | /// @param n the array size. 0 represents a runtime-array |
| 577 | /// @param decos the optional decorations for the array |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 578 | /// @return the tint AST type for a array of size `n` of type `T` |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 579 | ast::Array* array(ast::Type* subtype, |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 580 | uint32_t n = 0, |
| 581 | ast::DecorationList decos = {}) const { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 582 | subtype = MaybeCreateTypename(subtype); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 583 | return builder->create<ast::Array>(subtype, n, decos); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /// @param source the Source of the node |
| 587 | /// @param subtype the array element type |
| 588 | /// @param n the array size. 0 represents a runtime-array |
| 589 | /// @param decos the optional decorations for the array |
| 590 | /// @return the tint AST type for a array of size `n` of type `T` |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 591 | ast::Array* array(const Source& source, |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 592 | ast::Type* subtype, |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 593 | uint32_t n = 0, |
| 594 | ast::DecorationList decos = {}) const { |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 595 | subtype = MaybeCreateTypename(subtype); |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 596 | return builder->create<ast::Array>(source, subtype, n, decos); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 599 | /// @param subtype the array element type |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 600 | /// @param n the array size. 0 represents a runtime-array |
Ben Clayton | cbbe576 | 2021-05-10 17:25:21 +0000 | [diff] [blame] | 601 | /// @param stride the array stride. 0 represents implicit stride |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 602 | /// @return the tint AST type for a array of size `n` of type `T` |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 603 | ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 604 | subtype = MaybeCreateTypename(subtype); |
Ben Clayton | cbbe576 | 2021-05-10 17:25:21 +0000 | [diff] [blame] | 605 | ast::DecorationList decos; |
| 606 | if (stride) { |
| 607 | decos.emplace_back(builder->create<ast::StrideDecoration>(stride)); |
| 608 | } |
| 609 | return array(subtype, n, std::move(decos)); |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 612 | /// @param source the Source of the node |
| 613 | /// @param subtype the array element type |
| 614 | /// @param n the array size. 0 represents a runtime-array |
Ben Clayton | cbbe576 | 2021-05-10 17:25:21 +0000 | [diff] [blame] | 615 | /// @param stride the array stride. 0 represents implicit stride |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 616 | /// @return the tint AST type for a array of size `n` of type `T` |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 617 | ast::Array* array(const Source& source, |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 618 | ast::Type* subtype, |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 619 | uint32_t n, |
| 620 | uint32_t stride) const { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 621 | subtype = MaybeCreateTypename(subtype); |
Ben Clayton | cbbe576 | 2021-05-10 17:25:21 +0000 | [diff] [blame] | 622 | ast::DecorationList decos; |
| 623 | if (stride) { |
| 624 | decos.emplace_back(builder->create<ast::StrideDecoration>(stride)); |
| 625 | } |
| 626 | return array(source, subtype, n, std::move(decos)); |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 629 | /// @return the tint AST type for an array of size `N` of type `T` |
| 630 | template <typename T, int N = 0> |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 631 | ast::Array* array() const { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 632 | return array(Of<T>(), N); |
| 633 | } |
| 634 | |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 635 | /// @param stride the array stride |
| 636 | /// @return the tint AST type for an array of size `N` of type `T` |
| 637 | template <typename T, int N = 0> |
Ben Clayton | 4cd5eea | 2021-05-07 20:58:34 +0000 | [diff] [blame] | 638 | ast::Array* array(uint32_t stride) const { |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 639 | return array(Of<T>(), N, stride); |
| 640 | } |
Ben Clayton | 4db10dd | 2021-04-16 08:47:14 +0000 | [diff] [blame] | 641 | |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 642 | /// Creates a type name |
| 643 | /// @param name the name |
| 644 | /// @returns the type name |
| 645 | template <typename NAME> |
| 646 | ast::TypeName* type_name(NAME&& name) const { |
| 647 | return builder->create<ast::TypeName>( |
| 648 | builder->Sym(std::forward<NAME>(name))); |
| 649 | } |
| 650 | |
| 651 | /// Creates a type name |
| 652 | /// @param source the Source of the node |
| 653 | /// @param name the name |
| 654 | /// @returns the type name |
| 655 | template <typename NAME> |
| 656 | ast::TypeName* type_name(const Source& source, NAME&& name) const { |
| 657 | return builder->create<ast::TypeName>( |
| 658 | source, builder->Sym(std::forward<NAME>(name))); |
| 659 | } |
| 660 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 661 | /// Creates an alias type |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 662 | /// @param name the alias name |
| 663 | /// @param type the alias type |
| 664 | /// @returns the alias pointer |
Ben Clayton | 1d618b1 | 2021-04-09 16:19:48 +0000 | [diff] [blame] | 665 | template <typename NAME> |
Ben Clayton | a34fa0e | 2021-05-10 17:38:01 +0000 | [diff] [blame] | 666 | ast::Alias* alias(NAME&& name, typ::Type type) const { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 667 | type = MaybeCreateTypename(type); |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 668 | auto sym = builder->Sym(std::forward<NAME>(name)); |
Ben Clayton | a34fa0e | 2021-05-10 17:38:01 +0000 | [diff] [blame] | 669 | return builder->create<ast::Alias>(sym, type); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 672 | /// Creates an alias type |
| 673 | /// @param source the Source of the node |
| 674 | /// @param name the alias name |
| 675 | /// @param type the alias type |
| 676 | /// @returns the alias pointer |
| 677 | template <typename NAME> |
Ben Clayton | a34fa0e | 2021-05-10 17:38:01 +0000 | [diff] [blame] | 678 | ast::Alias* alias(const Source& source, NAME&& name, typ::Type type) const { |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 679 | type = MaybeCreateTypename(type); |
| 680 | auto sym = builder->Sym(std::forward<NAME>(name)); |
Ben Clayton | a34fa0e | 2021-05-10 17:38:01 +0000 | [diff] [blame] | 681 | return builder->create<ast::Alias>(source, sym, type); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Ben Clayton | 4db10dd | 2021-04-16 08:47:14 +0000 | [diff] [blame] | 684 | /// Creates an access control qualifier type |
| 685 | /// @param access the access control |
| 686 | /// @param type the inner type |
| 687 | /// @returns the access control qualifier type |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 688 | ast::AccessControl* access(ast::AccessControl::Access access, |
| 689 | const ast::Type* type) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 690 | type = MaybeCreateTypename(type); |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 691 | return type ? builder->create<ast::AccessControl>(access, type) : nullptr; |
Ben Clayton | 4db10dd | 2021-04-16 08:47:14 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 694 | /// Creates an access control qualifier type |
| 695 | /// @param source the Source of the node |
| 696 | /// @param access the access control |
| 697 | /// @param type the inner type |
| 698 | /// @returns the access control qualifier type |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 699 | ast::AccessControl* access(const Source& source, |
| 700 | ast::AccessControl::Access access, |
| 701 | const ast::Type* type) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 702 | type = MaybeCreateTypename(type); |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 703 | return type ? builder->create<ast::AccessControl>(source, access, type) |
| 704 | : nullptr; |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Antonio Maiorano | 39a65a1 | 2021-03-31 12:46:52 +0000 | [diff] [blame] | 707 | /// @param type the type of the pointer |
| 708 | /// @param storage_class the storage class of the pointer |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 709 | /// @return the pointer to `type` with the given ast::StorageClass |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 710 | typ::Pointer pointer(ast::Type* type, |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 711 | ast::StorageClass storage_class) const { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 712 | type = MaybeCreateTypename(type); |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 713 | return builder->create<ast::Pointer>(type, storage_class); |
Antonio Maiorano | 39a65a1 | 2021-03-31 12:46:52 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 716 | /// @param source the Source of the node |
| 717 | /// @param type the type of the pointer |
| 718 | /// @param storage_class the storage class of the pointer |
| 719 | /// @return the pointer to `type` with the given ast::StorageClass |
| 720 | typ::Pointer pointer(const Source& source, |
| 721 | typ::Type type, |
| 722 | ast::StorageClass storage_class) const { |
| 723 | type = MaybeCreateTypename(type); |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 724 | return builder->create<ast::Pointer>(source, type, storage_class); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 727 | /// @param storage_class the storage class of the pointer |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 728 | /// @return the pointer to type `T` with the given ast::StorageClass. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 729 | template <typename T> |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 730 | typ::Pointer pointer(ast::StorageClass storage_class) const { |
Antonio Maiorano | 39a65a1 | 2021-03-31 12:46:52 +0000 | [diff] [blame] | 731 | return pointer(Of<T>(), storage_class); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 734 | /// @param kind the kind of sampler |
| 735 | /// @returns the sampler |
| 736 | typ::Sampler sampler(ast::SamplerKind kind) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 737 | return builder->create<ast::Sampler>(kind); |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 740 | /// @param source the Source of the node |
| 741 | /// @param kind the kind of sampler |
| 742 | /// @returns the sampler |
| 743 | typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 744 | return builder->create<ast::Sampler>(source, kind); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 747 | /// @param dims the dimensionality of the texture |
| 748 | /// @returns the depth texture |
| 749 | typ::DepthTexture depth_texture(ast::TextureDimension dims) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 750 | return builder->create<ast::DepthTexture>(dims); |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 753 | /// @param source the Source of the node |
| 754 | /// @param dims the dimensionality of the texture |
| 755 | /// @returns the depth texture |
| 756 | typ::DepthTexture depth_texture(const Source& source, |
| 757 | ast::TextureDimension dims) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 758 | return builder->create<ast::DepthTexture>(source, dims); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 761 | /// @param dims the dimensionality of the texture |
| 762 | /// @param subtype the texture subtype. |
| 763 | /// @returns the sampled texture |
| 764 | typ::SampledTexture sampled_texture(ast::TextureDimension dims, |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 765 | ast::Type* subtype) const { |
| 766 | return builder->create<ast::SampledTexture>(dims, subtype); |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 769 | /// @param source the Source of the node |
| 770 | /// @param dims the dimensionality of the texture |
| 771 | /// @param subtype the texture subtype. |
| 772 | /// @returns the sampled texture |
| 773 | typ::SampledTexture sampled_texture(const Source& source, |
| 774 | ast::TextureDimension dims, |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 775 | ast::Type* subtype) const { |
| 776 | return builder->create<ast::SampledTexture>(source, dims, subtype); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 779 | /// @param dims the dimensionality of the texture |
| 780 | /// @param subtype the texture subtype. |
| 781 | /// @returns the multisampled texture |
| 782 | typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims, |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 783 | ast::Type* subtype) const { |
| 784 | return builder->create<ast::MultisampledTexture>(dims, subtype); |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 787 | /// @param source the Source of the node |
| 788 | /// @param dims the dimensionality of the texture |
| 789 | /// @param subtype the texture subtype. |
| 790 | /// @returns the multisampled texture |
| 791 | typ::MultisampledTexture multisampled_texture(const Source& source, |
| 792 | ast::TextureDimension dims, |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 793 | ast::Type* subtype) const { |
| 794 | return builder->create<ast::MultisampledTexture>(source, dims, subtype); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Ben Clayton | e204f27 | 2021-04-22 14:40:23 +0000 | [diff] [blame] | 797 | /// @param dims the dimensionality of the texture |
| 798 | /// @param format the image format of the texture |
| 799 | /// @returns the storage texture |
| 800 | typ::StorageTexture storage_texture(ast::TextureDimension dims, |
| 801 | ast::ImageFormat format) const { |
Ben Clayton | 55ecfc4 | 2021-05-20 14:42:28 +0000 | [diff] [blame] | 802 | auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder); |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 803 | return builder->create<ast::StorageTexture>(dims, format, subtype); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 806 | /// @param source the Source of the node |
| 807 | /// @param dims the dimensionality of the texture |
| 808 | /// @param format the image format of the texture |
| 809 | /// @returns the storage texture |
| 810 | typ::StorageTexture storage_texture(const Source& source, |
| 811 | ast::TextureDimension dims, |
| 812 | ast::ImageFormat format) const { |
Ben Clayton | 55ecfc4 | 2021-05-20 14:42:28 +0000 | [diff] [blame] | 813 | auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder); |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 814 | return builder->create<ast::StorageTexture>(source, dims, format, |
| 815 | subtype); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Brandon Jones | 7b25769 | 2021-05-17 17:40:17 +0000 | [diff] [blame] | 818 | /// @returns the external texture |
| 819 | typ::ExternalTexture external_texture() const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 820 | return builder->create<ast::ExternalTexture>(); |
Brandon Jones | 7b25769 | 2021-05-17 17:40:17 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 823 | /// @param source the Source of the node |
| 824 | /// @returns the external texture |
| 825 | typ::ExternalTexture external_texture(const Source& source) const { |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 826 | return builder->create<ast::ExternalTexture>(source); |
Antonio Maiorano | 4b16a16 | 2021-04-27 17:32:37 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 829 | /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers. |
| 830 | /// If ty is a ast::Struct or ast::Alias, the returned type is an |
| 831 | /// ast::TypeName of the given type's name, otherwise type is returned. |
| 832 | /// @param type the type |
| 833 | /// @return either type or a pointer to a new ast::TypeName |
| 834 | ast::Type* MaybeCreateTypename(ast::Type* type) const; |
| 835 | |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 836 | /// If ty is a ast::Struct or ast::Alias, the returned type is an |
| 837 | /// ast::TypeName of the given type's name, otherwise type is returned. |
| 838 | /// @param type the type |
| 839 | /// @return either type or a pointer to a new ast::TypeName |
Ben Clayton | 19b0319 | 2021-05-20 15:04:08 +0000 | [diff] [blame^] | 840 | const ast::Type* MaybeCreateTypename(const ast::Type* type) const; |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 841 | |
Ben Clayton | f5f311e | 2021-04-28 14:31:23 +0000 | [diff] [blame] | 842 | /// The ProgramBuilder |
| 843 | ProgramBuilder* const builder; |
| 844 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 845 | private: |
| 846 | /// CToAST<T> is specialized for various `T` types and each specialization |
| 847 | /// contains a single static `get()` method for obtaining the corresponding |
| 848 | /// AST type for the C type `T`. |
| 849 | /// `get()` has the signature: |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 850 | /// `static typ::Type get(Types* t)` |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 851 | template <typename T> |
| 852 | struct CToAST {}; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 853 | }; |
| 854 | |
| 855 | ////////////////////////////////////////////////////////////////////////////// |
| 856 | // AST helper methods |
| 857 | ////////////////////////////////////////////////////////////////////////////// |
| 858 | |
James Price | 65ae64d | 2021-04-29 12:59:14 +0000 | [diff] [blame] | 859 | /// @return a new unnamed symbol |
| 860 | Symbol Sym() { return Symbols().New(); } |
| 861 | |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 862 | /// @param name the symbol string |
| 863 | /// @return a Symbol with the given name |
| 864 | Symbol Sym(const std::string& name) { return Symbols().Register(name); } |
| 865 | |
| 866 | /// @param sym the symbol |
| 867 | /// @return `sym` |
| 868 | Symbol Sym(Symbol sym) { return sym; } |
| 869 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 870 | /// @param expr the expression |
| 871 | /// @return expr |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 872 | template <typename T> |
| 873 | traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) { |
| 874 | return expr; |
| 875 | } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 876 | |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 877 | /// Passthrough for nullptr |
| 878 | /// @return nullptr |
| 879 | ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; } |
| 880 | |
Ben Clayton | fe0910f | 2021-05-17 15:51:47 +0000 | [diff] [blame] | 881 | /// @param source the source information |
| 882 | /// @param symbol the identifier symbol |
| 883 | /// @return an ast::IdentifierExpression with the given symbol |
| 884 | ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) { |
| 885 | return create<ast::IdentifierExpression>(source, symbol); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 888 | /// @param symbol the identifier symbol |
| 889 | /// @return an ast::IdentifierExpression with the given symbol |
| 890 | ast::IdentifierExpression* Expr(Symbol symbol) { |
| 891 | return create<ast::IdentifierExpression>(symbol); |
| 892 | } |
| 893 | |
Ben Clayton | fe0910f | 2021-05-17 15:51:47 +0000 | [diff] [blame] | 894 | /// @param source the source information |
| 895 | /// @param variable the AST variable |
| 896 | /// @return an ast::IdentifierExpression with the variable's symbol |
| 897 | ast::IdentifierExpression* Expr(const Source& source, |
| 898 | ast::Variable* variable) { |
| 899 | return create<ast::IdentifierExpression>(source, variable->symbol()); |
| 900 | } |
| 901 | |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 902 | /// @param variable the AST variable |
| 903 | /// @return an ast::IdentifierExpression with the variable's symbol |
| 904 | ast::IdentifierExpression* Expr(ast::Variable* variable) { |
| 905 | return create<ast::IdentifierExpression>(variable->symbol()); |
| 906 | } |
| 907 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 908 | /// @param source the source information |
| 909 | /// @param name the identifier name |
| 910 | /// @return an ast::IdentifierExpression with the given name |
Ben Clayton | fe0910f | 2021-05-17 15:51:47 +0000 | [diff] [blame] | 911 | ast::IdentifierExpression* Expr(const Source& source, const char* name) { |
| 912 | return create<ast::IdentifierExpression>(source, Symbols().Register(name)); |
| 913 | } |
| 914 | |
| 915 | /// @param name the identifier name |
| 916 | /// @return an ast::IdentifierExpression with the given name |
| 917 | ast::IdentifierExpression* Expr(const char* name) { |
| 918 | return create<ast::IdentifierExpression>(Symbols().Register(name)); |
| 919 | } |
| 920 | |
| 921 | /// @param source the source information |
| 922 | /// @param name the identifier name |
| 923 | /// @return an ast::IdentifierExpression with the given name |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 924 | ast::IdentifierExpression* Expr(const Source& source, |
| 925 | const std::string& name) { |
| 926 | return create<ast::IdentifierExpression>(source, Symbols().Register(name)); |
| 927 | } |
| 928 | |
| 929 | /// @param name the identifier name |
| 930 | /// @return an ast::IdentifierExpression with the given name |
Ben Clayton | fe0910f | 2021-05-17 15:51:47 +0000 | [diff] [blame] | 931 | ast::IdentifierExpression* Expr(const std::string& name) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 932 | return create<ast::IdentifierExpression>(Symbols().Register(name)); |
| 933 | } |
| 934 | |
Ben Clayton | fe0910f | 2021-05-17 15:51:47 +0000 | [diff] [blame] | 935 | /// @param source the source information |
| 936 | /// @param value the boolean value |
| 937 | /// @return a Scalar constructor for the given value |
| 938 | ast::ScalarConstructorExpression* Expr(const Source& source, bool value) { |
| 939 | return create<ast::ScalarConstructorExpression>(source, Literal(value)); |
| 940 | } |
| 941 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 942 | /// @param value the boolean value |
| 943 | /// @return a Scalar constructor for the given value |
| 944 | ast::ScalarConstructorExpression* Expr(bool value) { |
| 945 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 946 | } |
| 947 | |
Ben Clayton | fe0910f | 2021-05-17 15:51:47 +0000 | [diff] [blame] | 948 | /// @param source the source information |
| 949 | /// @param value the float value |
| 950 | /// @return a Scalar constructor for the given value |
| 951 | ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) { |
| 952 | return create<ast::ScalarConstructorExpression>(source, Literal(value)); |
| 953 | } |
| 954 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 955 | /// @param value the float value |
| 956 | /// @return a Scalar constructor for the given value |
| 957 | ast::ScalarConstructorExpression* Expr(f32 value) { |
| 958 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 959 | } |
| 960 | |
Ben Clayton | fe0910f | 2021-05-17 15:51:47 +0000 | [diff] [blame] | 961 | /// @param source the source information |
| 962 | /// @param value the integer value |
| 963 | /// @return a Scalar constructor for the given value |
| 964 | ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) { |
| 965 | return create<ast::ScalarConstructorExpression>(source, Literal(value)); |
| 966 | } |
| 967 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 968 | /// @param value the integer value |
| 969 | /// @return a Scalar constructor for the given value |
| 970 | ast::ScalarConstructorExpression* Expr(i32 value) { |
| 971 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 972 | } |
| 973 | |
Ben Clayton | fe0910f | 2021-05-17 15:51:47 +0000 | [diff] [blame] | 974 | /// @param source the source information |
| 975 | /// @param value the unsigned int value |
| 976 | /// @return a Scalar constructor for the given value |
| 977 | ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) { |
| 978 | return create<ast::ScalarConstructorExpression>(source, Literal(value)); |
| 979 | } |
| 980 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 981 | /// @param value the unsigned int value |
| 982 | /// @return a Scalar constructor for the given value |
| 983 | ast::ScalarConstructorExpression* Expr(u32 value) { |
| 984 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 985 | } |
| 986 | |
| 987 | /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to |
| 988 | /// `list`. |
| 989 | /// @param list the list to append too |
| 990 | /// @param arg the arg to create |
| 991 | template <typename ARG> |
| 992 | void Append(ast::ExpressionList& list, ARG&& arg) { |
| 993 | list.emplace_back(Expr(std::forward<ARG>(arg))); |
| 994 | } |
| 995 | |
| 996 | /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`, |
| 997 | /// then appends them to `list`. |
| 998 | /// @param list the list to append too |
| 999 | /// @param arg0 the first argument |
| 1000 | /// @param args the rest of the arguments |
| 1001 | template <typename ARG0, typename... ARGS> |
| 1002 | void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) { |
| 1003 | Append(list, std::forward<ARG0>(arg0)); |
| 1004 | Append(list, std::forward<ARGS>(args)...); |
| 1005 | } |
| 1006 | |
| 1007 | /// @return an empty list of expressions |
| 1008 | ast::ExpressionList ExprList() { return {}; } |
| 1009 | |
| 1010 | /// @param args the list of expressions |
| 1011 | /// @return the list of expressions converted to `ast::Expression`s using |
| 1012 | /// `Expr()`, |
| 1013 | template <typename... ARGS> |
| 1014 | ast::ExpressionList ExprList(ARGS&&... args) { |
| 1015 | ast::ExpressionList list; |
| 1016 | list.reserve(sizeof...(args)); |
| 1017 | Append(list, std::forward<ARGS>(args)...); |
| 1018 | return list; |
| 1019 | } |
| 1020 | |
| 1021 | /// @param list the list of expressions |
| 1022 | /// @return `list` |
| 1023 | ast::ExpressionList ExprList(ast::ExpressionList list) { return list; } |
| 1024 | |
| 1025 | /// @param val the boolan value |
| 1026 | /// @return a boolean literal with the given value |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 1027 | ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1028 | |
| 1029 | /// @param val the float value |
| 1030 | /// @return a float literal with the given value |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 1031 | ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1032 | |
| 1033 | /// @param val the unsigned int value |
| 1034 | /// @return a ast::UintLiteral with the given value |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 1035 | ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1036 | |
| 1037 | /// @param val the integer value |
| 1038 | /// @return the ast::SintLiteral with the given value |
Ben Clayton | 109b18f | 2021-04-28 13:50:43 +0000 | [diff] [blame] | 1039 | ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1040 | |
| 1041 | /// @param args the arguments for the type constructor |
| 1042 | /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values |
| 1043 | /// of `args` converted to `ast::Expression`s using `Expr()` |
| 1044 | template <typename T, typename... ARGS> |
| 1045 | ast::TypeConstructorExpression* Construct(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1046 | return Construct(ty.Of<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | /// @param type the type to construct |
| 1050 | /// @param args the arguments for the constructor |
| 1051 | /// @return an `ast::TypeConstructorExpression` of `type` constructed with the |
| 1052 | /// values `args`. |
| 1053 | template <typename... ARGS> |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1054 | ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1055 | type = ty.MaybeCreateTypename(type); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1056 | return create<ast::TypeConstructorExpression>( |
| 1057 | type, ExprList(std::forward<ARGS>(args)...)); |
| 1058 | } |
| 1059 | |
Antonio Maiorano | 39a65a1 | 2021-03-31 12:46:52 +0000 | [diff] [blame] | 1060 | /// Creates a constructor expression that constructs an object of |
| 1061 | /// `type` filled with `elem_value`. For example, |
| 1062 | /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a |
| 1063 | /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values. |
| 1064 | /// @param type the type to construct |
| 1065 | /// @param elem_value the initial or element value (for vec and mat) to |
| 1066 | /// construct with |
| 1067 | /// @return the constructor expression |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1068 | ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type, |
Antonio Maiorano | 39a65a1 | 2021-03-31 12:46:52 +0000 | [diff] [blame] | 1069 | int elem_value = 0); |
| 1070 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1071 | /// @param args the arguments for the vector constructor |
| 1072 | /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type |
| 1073 | /// `T`, constructed with the values `args`. |
| 1074 | template <typename T, typename... ARGS> |
| 1075 | ast::TypeConstructorExpression* vec2(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1076 | return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /// @param args the arguments for the vector constructor |
| 1080 | /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type |
| 1081 | /// `T`, constructed with the values `args`. |
| 1082 | template <typename T, typename... ARGS> |
| 1083 | ast::TypeConstructorExpression* vec3(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1084 | return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /// @param args the arguments for the vector constructor |
| 1088 | /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type |
| 1089 | /// `T`, constructed with the values `args`. |
| 1090 | template <typename T, typename... ARGS> |
| 1091 | ast::TypeConstructorExpression* vec4(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1092 | return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /// @param args the arguments for the matrix constructor |
| 1096 | /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type |
| 1097 | /// `T`, constructed with the values `args`. |
| 1098 | template <typename T, typename... ARGS> |
| 1099 | ast::TypeConstructorExpression* mat2x2(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1100 | return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /// @param args the arguments for the matrix constructor |
| 1104 | /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type |
| 1105 | /// `T`, constructed with the values `args`. |
| 1106 | template <typename T, typename... ARGS> |
| 1107 | ast::TypeConstructorExpression* mat2x3(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1108 | return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /// @param args the arguments for the matrix constructor |
| 1112 | /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type |
| 1113 | /// `T`, constructed with the values `args`. |
| 1114 | template <typename T, typename... ARGS> |
| 1115 | ast::TypeConstructorExpression* mat2x4(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1116 | return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | /// @param args the arguments for the matrix constructor |
| 1120 | /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type |
| 1121 | /// `T`, constructed with the values `args`. |
| 1122 | template <typename T, typename... ARGS> |
| 1123 | ast::TypeConstructorExpression* mat3x2(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1124 | return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | /// @param args the arguments for the matrix constructor |
| 1128 | /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type |
| 1129 | /// `T`, constructed with the values `args`. |
| 1130 | template <typename T, typename... ARGS> |
| 1131 | ast::TypeConstructorExpression* mat3x3(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1132 | return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | /// @param args the arguments for the matrix constructor |
| 1136 | /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type |
| 1137 | /// `T`, constructed with the values `args`. |
| 1138 | template <typename T, typename... ARGS> |
| 1139 | ast::TypeConstructorExpression* mat3x4(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1140 | return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /// @param args the arguments for the matrix constructor |
| 1144 | /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type |
| 1145 | /// `T`, constructed with the values `args`. |
| 1146 | template <typename T, typename... ARGS> |
| 1147 | ast::TypeConstructorExpression* mat4x2(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1148 | return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /// @param args the arguments for the matrix constructor |
| 1152 | /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type |
| 1153 | /// `T`, constructed with the values `args`. |
| 1154 | template <typename T, typename... ARGS> |
| 1155 | ast::TypeConstructorExpression* mat4x3(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1156 | return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | /// @param args the arguments for the matrix constructor |
| 1160 | /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type |
| 1161 | /// `T`, constructed with the values `args`. |
| 1162 | template <typename T, typename... ARGS> |
| 1163 | ast::TypeConstructorExpression* mat4x4(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1164 | return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | /// @param args the arguments for the array constructor |
| 1168 | /// @return an `ast::TypeConstructorExpression` of an array with element type |
| 1169 | /// `T`, constructed with the values `args`. |
| 1170 | template <typename T, int N = 0, typename... ARGS> |
| 1171 | ast::TypeConstructorExpression* array(ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1172 | return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /// @param subtype the array element type |
| 1176 | /// @param n the array size. 0 represents a runtime-array. |
| 1177 | /// @param args the arguments for the array constructor |
| 1178 | /// @return an `ast::TypeConstructorExpression` of an array with element type |
| 1179 | /// `subtype`, constructed with the values `args`. |
| 1180 | template <typename... ARGS> |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1181 | ast::TypeConstructorExpression* array(ast::Type* subtype, |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1182 | uint32_t n, |
| 1183 | ARGS&&... args) { |
Ben Clayton | 0f88b31 | 2021-05-04 17:36:31 +0000 | [diff] [blame] | 1184 | return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /// @param name the variable name |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1188 | /// @param type the variable type |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 1189 | /// @param storage the variable storage class |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1190 | /// @param constructor constructor expression |
| 1191 | /// @param decorations variable decorations |
| 1192 | /// @returns a `ast::Variable` with the given name, storage and type |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1193 | template <typename NAME> |
| 1194 | ast::Variable* Var(NAME&& name, |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 1195 | const ast::Type* type, |
Ben Clayton | f6c84e4 | 2021-05-12 21:08:22 +0000 | [diff] [blame] | 1196 | ast::StorageClass storage = ast::StorageClass::kNone, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 1197 | ast::Expression* constructor = nullptr, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 1198 | ast::DecorationList decorations = {}) { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 1199 | type = ty.MaybeCreateTypename(type); |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1200 | return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type, |
| 1201 | false, constructor, decorations); |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 1202 | } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1203 | |
| 1204 | /// @param source the variable source |
| 1205 | /// @param name the variable name |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1206 | /// @param type the variable type |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 1207 | /// @param storage the variable storage class |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1208 | /// @param constructor constructor expression |
| 1209 | /// @param decorations variable decorations |
| 1210 | /// @returns a `ast::Variable` with the given name, storage and type |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1211 | template <typename NAME> |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1212 | ast::Variable* Var(const Source& source, |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1213 | NAME&& name, |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 1214 | const ast::Type* type, |
Ben Clayton | f6c84e4 | 2021-05-12 21:08:22 +0000 | [diff] [blame] | 1215 | ast::StorageClass storage = ast::StorageClass::kNone, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 1216 | ast::Expression* constructor = nullptr, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 1217 | ast::DecorationList decorations = {}) { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 1218 | type = ty.MaybeCreateTypename(type); |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1219 | return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 1220 | type, false, constructor, decorations); |
| 1221 | } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1222 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1223 | /// @param name the variable name |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1224 | /// @param type the variable type |
Antonio Maiorano | e0448d3 | 2021-05-10 20:42:56 +0000 | [diff] [blame] | 1225 | /// @param constructor constructor expression |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1226 | /// @param decorations optional variable decorations |
James Price | daf1f1c | 2021-04-08 22:15:48 +0000 | [diff] [blame] | 1227 | /// @returns a constant `ast::Variable` with the given name and type |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1228 | template <typename NAME> |
| 1229 | ast::Variable* Const(NAME&& name, |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1230 | ast::Type* type, |
Antonio Maiorano | e0448d3 | 2021-05-10 20:42:56 +0000 | [diff] [blame] | 1231 | ast::Expression* constructor, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 1232 | ast::DecorationList decorations = {}) { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 1233 | type = ty.MaybeCreateTypename(type); |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1234 | return create<ast::Variable>(Sym(std::forward<NAME>(name)), |
Ben Clayton | 81a29fe | 2021-02-17 00:26:52 +0000 | [diff] [blame] | 1235 | ast::StorageClass::kNone, type, true, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 1236 | constructor, decorations); |
| 1237 | } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1238 | |
| 1239 | /// @param source the variable source |
| 1240 | /// @param name the variable name |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1241 | /// @param type the variable type |
Antonio Maiorano | e0448d3 | 2021-05-10 20:42:56 +0000 | [diff] [blame] | 1242 | /// @param constructor constructor expression |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1243 | /// @param decorations optional variable decorations |
James Price | daf1f1c | 2021-04-08 22:15:48 +0000 | [diff] [blame] | 1244 | /// @returns a constant `ast::Variable` with the given name and type |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1245 | template <typename NAME> |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1246 | ast::Variable* Const(const Source& source, |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1247 | NAME&& name, |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1248 | ast::Type* type, |
Antonio Maiorano | e0448d3 | 2021-05-10 20:42:56 +0000 | [diff] [blame] | 1249 | ast::Expression* constructor, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 1250 | ast::DecorationList decorations = {}) { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 1251 | type = ty.MaybeCreateTypename(type); |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1252 | return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), |
Ben Clayton | 81a29fe | 2021-02-17 00:26:52 +0000 | [diff] [blame] | 1253 | ast::StorageClass::kNone, type, true, |
| 1254 | constructor, decorations); |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 1255 | } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1256 | |
James Price | daf1f1c | 2021-04-08 22:15:48 +0000 | [diff] [blame] | 1257 | /// @param name the parameter name |
| 1258 | /// @param type the parameter type |
| 1259 | /// @param decorations optional parameter decorations |
| 1260 | /// @returns a constant `ast::Variable` with the given name and type |
| 1261 | template <typename NAME> |
| 1262 | ast::Variable* Param(NAME&& name, |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1263 | ast::Type* type, |
James Price | daf1f1c | 2021-04-08 22:15:48 +0000 | [diff] [blame] | 1264 | ast::DecorationList decorations = {}) { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 1265 | type = ty.MaybeCreateTypename(type); |
James Price | daf1f1c | 2021-04-08 22:15:48 +0000 | [diff] [blame] | 1266 | return create<ast::Variable>(Sym(std::forward<NAME>(name)), |
| 1267 | ast::StorageClass::kNone, type, true, nullptr, |
| 1268 | decorations); |
| 1269 | } |
| 1270 | |
| 1271 | /// @param source the parameter source |
| 1272 | /// @param name the parameter name |
| 1273 | /// @param type the parameter type |
| 1274 | /// @param decorations optional parameter decorations |
| 1275 | /// @returns a constant `ast::Variable` with the given name and type |
| 1276 | template <typename NAME> |
| 1277 | ast::Variable* Param(const Source& source, |
| 1278 | NAME&& name, |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1279 | ast::Type* type, |
James Price | daf1f1c | 2021-04-08 22:15:48 +0000 | [diff] [blame] | 1280 | ast::DecorationList decorations = {}) { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 1281 | type = ty.MaybeCreateTypename(type); |
James Price | daf1f1c | 2021-04-08 22:15:48 +0000 | [diff] [blame] | 1282 | return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), |
| 1283 | ast::StorageClass::kNone, type, true, nullptr, |
| 1284 | decorations); |
| 1285 | } |
| 1286 | |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1287 | /// @param name the variable name |
| 1288 | /// @param type the variable type |
| 1289 | /// @param storage the variable storage class |
| 1290 | /// @param constructor constructor expression |
| 1291 | /// @param decorations variable decorations |
| 1292 | /// @returns a new `ast::Variable`, which is automatically registered as a |
| 1293 | /// global variable with the ast::Module. |
| 1294 | template <typename NAME> |
| 1295 | ast::Variable* Global(NAME&& name, |
Antonio Maiorano | dc4e6c1 | 2021-05-14 17:51:13 +0000 | [diff] [blame] | 1296 | const ast::Type* type, |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1297 | ast::StorageClass storage, |
| 1298 | ast::Expression* constructor = nullptr, |
| 1299 | ast::DecorationList decorations = {}) { |
| 1300 | auto* var = |
| 1301 | Var(std::forward<NAME>(name), type, storage, constructor, decorations); |
| 1302 | AST().AddGlobalVariable(var); |
| 1303 | return var; |
| 1304 | } |
| 1305 | |
| 1306 | /// @param source the variable source |
| 1307 | /// @param name the variable name |
| 1308 | /// @param type the variable type |
| 1309 | /// @param storage the variable storage class |
| 1310 | /// @param constructor constructor expression |
| 1311 | /// @param decorations variable decorations |
| 1312 | /// @returns a new `ast::Variable`, which is automatically registered as a |
| 1313 | /// global variable with the ast::Module. |
| 1314 | template <typename NAME> |
| 1315 | ast::Variable* Global(const Source& source, |
| 1316 | NAME&& name, |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1317 | ast::Type* type, |
Ben Clayton | 4270834 | 2021-04-21 17:55:12 +0000 | [diff] [blame] | 1318 | ast::StorageClass storage, |
| 1319 | ast::Expression* constructor = nullptr, |
| 1320 | ast::DecorationList decorations = {}) { |
| 1321 | auto* var = Var(source, std::forward<NAME>(name), type, storage, |
| 1322 | constructor, decorations); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1323 | AST().AddGlobalVariable(var); |
| 1324 | return var; |
| 1325 | } |
| 1326 | |
Antonio Maiorano | e0448d3 | 2021-05-10 20:42:56 +0000 | [diff] [blame] | 1327 | /// @param name the variable name |
| 1328 | /// @param type the variable type |
| 1329 | /// @param constructor constructor expression |
| 1330 | /// @param decorations optional variable decorations |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1331 | /// @returns a const `ast::Variable` constructed by calling Var() with the |
| 1332 | /// arguments of `args`, which is automatically registered as a global |
| 1333 | /// variable with the ast::Module. |
Antonio Maiorano | e0448d3 | 2021-05-10 20:42:56 +0000 | [diff] [blame] | 1334 | template <typename NAME> |
| 1335 | ast::Variable* GlobalConst(NAME&& name, |
| 1336 | typ::Type type, |
| 1337 | ast::Expression* constructor, |
| 1338 | ast::DecorationList decorations = {}) { |
| 1339 | auto* var = Const(std::forward<NAME>(name), type, constructor, |
| 1340 | std::move(decorations)); |
| 1341 | AST().AddGlobalVariable(var); |
| 1342 | return var; |
| 1343 | } |
| 1344 | |
| 1345 | /// @param source the variable source |
| 1346 | /// @param name the variable name |
| 1347 | /// @param type the variable type |
| 1348 | /// @param constructor constructor expression |
| 1349 | /// @param decorations optional variable decorations |
| 1350 | /// @returns a const `ast::Variable` constructed by calling Var() with the |
| 1351 | /// arguments of `args`, which is automatically registered as a global |
| 1352 | /// variable with the ast::Module. |
| 1353 | template <typename NAME> |
| 1354 | ast::Variable* GlobalConst(const Source& source, |
| 1355 | NAME&& name, |
| 1356 | typ::Type type, |
| 1357 | ast::Expression* constructor, |
| 1358 | ast::DecorationList decorations = {}) { |
| 1359 | auto* var = Const(source, std::forward<NAME>(name), type, constructor, |
| 1360 | std::move(decorations)); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1361 | AST().AddGlobalVariable(var); |
| 1362 | return var; |
| 1363 | } |
| 1364 | |
Ben Clayton | fe0910f | 2021-05-17 15:51:47 +0000 | [diff] [blame] | 1365 | /// @param source the source information |
| 1366 | /// @param expr the expression to take the address of |
| 1367 | /// @return an ast::UnaryOpExpression that takes the address of `expr` |
| 1368 | template <typename EXPR> |
| 1369 | ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) { |
| 1370 | return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf, |
| 1371 | Expr(std::forward<EXPR>(expr))); |
| 1372 | } |
| 1373 | |
| 1374 | /// @param expr the expression to take the address of |
| 1375 | /// @return an ast::UnaryOpExpression that takes the address of `expr` |
| 1376 | template <typename EXPR> |
| 1377 | ast::UnaryOpExpression* AddressOf(EXPR&& expr) { |
| 1378 | return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf, |
| 1379 | Expr(std::forward<EXPR>(expr))); |
| 1380 | } |
| 1381 | |
| 1382 | /// @param source the source information |
| 1383 | /// @param expr the expression to perform an indirection on |
| 1384 | /// @return an ast::UnaryOpExpression that dereferences the pointer `expr` |
| 1385 | template <typename EXPR> |
| 1386 | ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) { |
| 1387 | return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection, |
| 1388 | Expr(std::forward<EXPR>(expr))); |
| 1389 | } |
| 1390 | |
| 1391 | /// @param expr the expression to perform an indirection on |
| 1392 | /// @return an ast::UnaryOpExpression that dereferences the pointer `expr` |
| 1393 | template <typename EXPR> |
| 1394 | ast::UnaryOpExpression* Deref(EXPR&& expr) { |
| 1395 | return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection, |
| 1396 | Expr(std::forward<EXPR>(expr))); |
| 1397 | } |
| 1398 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1399 | /// @param func the function name |
| 1400 | /// @param args the function call arguments |
| 1401 | /// @returns a `ast::CallExpression` to the function `func`, with the |
| 1402 | /// arguments of `args` converted to `ast::Expression`s using `Expr()`. |
| 1403 | template <typename NAME, typename... ARGS> |
| 1404 | ast::CallExpression* Call(NAME&& func, ARGS&&... args) { |
| 1405 | return create<ast::CallExpression>(Expr(func), |
| 1406 | ExprList(std::forward<ARGS>(args)...)); |
| 1407 | } |
| 1408 | |
| 1409 | /// @param lhs the left hand argument to the addition operation |
| 1410 | /// @param rhs the right hand argument to the addition operation |
| 1411 | /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs` |
| 1412 | template <typename LHS, typename RHS> |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 1413 | ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1414 | return create<ast::BinaryExpression>(ast::BinaryOp::kAdd, |
| 1415 | Expr(std::forward<LHS>(lhs)), |
| 1416 | Expr(std::forward<RHS>(rhs))); |
| 1417 | } |
| 1418 | |
| 1419 | /// @param lhs the left hand argument to the subtraction operation |
| 1420 | /// @param rhs the right hand argument to the subtraction operation |
| 1421 | /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs` |
| 1422 | template <typename LHS, typename RHS> |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 1423 | ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1424 | return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract, |
| 1425 | Expr(std::forward<LHS>(lhs)), |
| 1426 | Expr(std::forward<RHS>(rhs))); |
| 1427 | } |
| 1428 | |
| 1429 | /// @param lhs the left hand argument to the multiplication operation |
| 1430 | /// @param rhs the right hand argument to the multiplication operation |
| 1431 | /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs` |
| 1432 | template <typename LHS, typename RHS> |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 1433 | ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1434 | return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply, |
| 1435 | Expr(std::forward<LHS>(lhs)), |
| 1436 | Expr(std::forward<RHS>(rhs))); |
| 1437 | } |
| 1438 | |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 1439 | /// @param source the source information |
| 1440 | /// @param lhs the left hand argument to the multiplication operation |
| 1441 | /// @param rhs the right hand argument to the multiplication operation |
| 1442 | /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs` |
| 1443 | template <typename LHS, typename RHS> |
| 1444 | ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) { |
| 1445 | return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply, |
| 1446 | Expr(std::forward<LHS>(lhs)), |
| 1447 | Expr(std::forward<RHS>(rhs))); |
| 1448 | } |
| 1449 | |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1450 | /// @param lhs the left hand argument to the division operation |
| 1451 | /// @param rhs the right hand argument to the division operation |
| 1452 | /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs` |
| 1453 | template <typename LHS, typename RHS> |
| 1454 | ast::Expression* Div(LHS&& lhs, RHS&& rhs) { |
| 1455 | return create<ast::BinaryExpression>(ast::BinaryOp::kDivide, |
| 1456 | Expr(std::forward<LHS>(lhs)), |
| 1457 | Expr(std::forward<RHS>(rhs))); |
| 1458 | } |
| 1459 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1460 | /// @param arr the array argument for the array accessor expression |
| 1461 | /// @param idx the index argument for the array accessor expression |
| 1462 | /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx` |
| 1463 | template <typename ARR, typename IDX> |
Antonio Maiorano | 61dabab | 2021-04-01 19:58:37 +0000 | [diff] [blame] | 1464 | ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1465 | return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)), |
| 1466 | Expr(std::forward<IDX>(idx))); |
| 1467 | } |
| 1468 | |
| 1469 | /// @param obj the object for the member accessor expression |
| 1470 | /// @param idx the index argument for the array accessor expression |
| 1471 | /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx` |
| 1472 | template <typename OBJ, typename IDX> |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 1473 | ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1474 | return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)), |
| 1475 | Expr(std::forward<IDX>(idx))); |
| 1476 | } |
| 1477 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 1478 | /// Creates a ast::StructMemberOffsetDecoration |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1479 | /// @param val the offset value |
| 1480 | /// @returns the offset decoration pointer |
| 1481 | ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) { |
| 1482 | return create<ast::StructMemberOffsetDecoration>(source_, val); |
| 1483 | } |
| 1484 | |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 1485 | /// Creates a ast::StructMemberSizeDecoration |
| 1486 | /// @param source the source information |
| 1487 | /// @param val the size value |
| 1488 | /// @returns the size decoration pointer |
Antonio Maiorano | 101f463 | 2021-04-07 20:35:11 +0000 | [diff] [blame] | 1489 | ast::StructMemberSizeDecoration* MemberSize(const Source& source, |
| 1490 | uint32_t val) { |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 1491 | return create<ast::StructMemberSizeDecoration>(source, val); |
| 1492 | } |
| 1493 | |
| 1494 | /// Creates a ast::StructMemberSizeDecoration |
| 1495 | /// @param val the size value |
| 1496 | /// @returns the size decoration pointer |
| 1497 | ast::StructMemberSizeDecoration* MemberSize(uint32_t val) { |
| 1498 | return create<ast::StructMemberSizeDecoration>(source_, val); |
| 1499 | } |
| 1500 | |
| 1501 | /// Creates a ast::StructMemberAlignDecoration |
| 1502 | /// @param source the source information |
| 1503 | /// @param val the align value |
| 1504 | /// @returns the align decoration pointer |
Antonio Maiorano | 101f463 | 2021-04-07 20:35:11 +0000 | [diff] [blame] | 1505 | ast::StructMemberAlignDecoration* MemberAlign(const Source& source, |
| 1506 | uint32_t val) { |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 1507 | return create<ast::StructMemberAlignDecoration>(source, val); |
| 1508 | } |
| 1509 | |
| 1510 | /// Creates a ast::StructMemberAlignDecoration |
| 1511 | /// @param val the align value |
| 1512 | /// @returns the align decoration pointer |
| 1513 | ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) { |
| 1514 | return create<ast::StructMemberAlignDecoration>(source_, val); |
| 1515 | } |
| 1516 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 1517 | /// Creates an ast::Function and registers it with the ast::Module. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1518 | /// @param source the source information |
| 1519 | /// @param name the function name |
| 1520 | /// @param params the function parameters |
| 1521 | /// @param type the function return type |
| 1522 | /// @param body the function body |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1523 | /// @param decorations the optional function decorations |
| 1524 | /// @param return_type_decorations the optional function return type |
| 1525 | /// decorations |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1526 | /// @returns the function pointer |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1527 | template <typename NAME> |
Antonio Maiorano | 101f463 | 2021-04-07 20:35:11 +0000 | [diff] [blame] | 1528 | ast::Function* Func(const Source& source, |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1529 | NAME&& name, |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1530 | ast::VariableList params, |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1531 | ast::Type* type, |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1532 | ast::StatementList body, |
Ben Clayton | 4f154a8 | 2021-04-06 18:15:17 +0000 | [diff] [blame] | 1533 | ast::DecorationList decorations = {}, |
James Price | feecbe0 | 2021-03-15 17:01:34 +0000 | [diff] [blame] | 1534 | ast::DecorationList return_type_decorations = {}) { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 1535 | type = ty.MaybeCreateTypename(type); |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1536 | auto* func = |
| 1537 | create<ast::Function>(source, Sym(std::forward<NAME>(name)), params, |
| 1538 | type, create<ast::BlockStatement>(body), |
| 1539 | decorations, return_type_decorations); |
James Price | 3eaa450 | 2021-02-09 21:26:21 +0000 | [diff] [blame] | 1540 | AST().AddFunction(func); |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 1541 | return func; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 1544 | /// Creates an ast::Function and registers it with the ast::Module. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1545 | /// @param name the function name |
| 1546 | /// @param params the function parameters |
| 1547 | /// @param type the function return type |
| 1548 | /// @param body the function body |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1549 | /// @param decorations the optional function decorations |
| 1550 | /// @param return_type_decorations the optional function return type |
| 1551 | /// decorations |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1552 | /// @returns the function pointer |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1553 | template <typename NAME> |
| 1554 | ast::Function* Func(NAME&& name, |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1555 | ast::VariableList params, |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1556 | ast::Type* type, |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1557 | ast::StatementList body, |
Ben Clayton | 4f154a8 | 2021-04-06 18:15:17 +0000 | [diff] [blame] | 1558 | ast::DecorationList decorations = {}, |
James Price | feecbe0 | 2021-03-15 17:01:34 +0000 | [diff] [blame] | 1559 | ast::DecorationList return_type_decorations = {}) { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 1560 | type = ty.MaybeCreateTypename(type); |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1561 | auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params, |
| 1562 | type, create<ast::BlockStatement>(body), |
James Price | feecbe0 | 2021-03-15 17:01:34 +0000 | [diff] [blame] | 1563 | decorations, return_type_decorations); |
James Price | 3eaa450 | 2021-02-09 21:26:21 +0000 | [diff] [blame] | 1564 | AST().AddFunction(func); |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 1565 | return func; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Ben Clayton | 49668bb | 2021-04-17 05:32:01 +0000 | [diff] [blame] | 1568 | /// Creates an ast::ReturnStatement with no return value |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame] | 1569 | /// @param source the source information |
| 1570 | /// @returns the return statement pointer |
| 1571 | ast::ReturnStatement* Return(const Source& source) { |
| 1572 | return create<ast::ReturnStatement>(source); |
| 1573 | } |
| 1574 | |
| 1575 | /// Creates an ast::ReturnStatement with no return value |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1576 | /// @returns the return statement pointer |
Ben Clayton | 49668bb | 2021-04-17 05:32:01 +0000 | [diff] [blame] | 1577 | ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); } |
| 1578 | |
| 1579 | /// Creates an ast::ReturnStatement with the given return value |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame] | 1580 | /// @param source the source information |
| 1581 | /// @param val the return value |
| 1582 | /// @returns the return statement pointer |
| 1583 | template <typename EXPR> |
| 1584 | ast::ReturnStatement* Return(const Source& source, EXPR&& val) { |
| 1585 | return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val))); |
| 1586 | } |
| 1587 | |
| 1588 | /// Creates an ast::ReturnStatement with the given return value |
Ben Clayton | 49668bb | 2021-04-17 05:32:01 +0000 | [diff] [blame] | 1589 | /// @param val the return value |
| 1590 | /// @returns the return statement pointer |
| 1591 | template <typename EXPR> |
| 1592 | ast::ReturnStatement* Return(EXPR&& val) { |
| 1593 | return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val))); |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1596 | /// Creates a ast::Struct registering it with the AST().ConstructedTypes(). |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 1597 | /// @param source the source information |
| 1598 | /// @param name the struct name |
| 1599 | /// @param members the struct members |
| 1600 | /// @param decorations the optional struct decorations |
| 1601 | /// @returns the struct type |
Ben Clayton | 1d618b1 | 2021-04-09 16:19:48 +0000 | [diff] [blame] | 1602 | template <typename NAME> |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1603 | ast::Struct* Structure(const Source& source, |
| 1604 | NAME&& name, |
| 1605 | ast::StructMemberList members, |
| 1606 | ast::DecorationList decorations = {}) { |
Ben Clayton | 8a8d26b | 2021-04-20 15:04:21 +0000 | [diff] [blame] | 1607 | auto sym = Sym(std::forward<NAME>(name)); |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1608 | auto* type = create<ast::Struct>(source, sym, std::move(members), |
Ben Clayton | 8a8d26b | 2021-04-20 15:04:21 +0000 | [diff] [blame] | 1609 | std::move(decorations)); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 1610 | AST().AddConstructedType(type); |
| 1611 | return type; |
| 1612 | } |
| 1613 | |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1614 | /// Creates a ast::Struct registering it with the AST().ConstructedTypes(). |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 1615 | /// @param name the struct name |
| 1616 | /// @param members the struct members |
| 1617 | /// @param decorations the optional struct decorations |
| 1618 | /// @returns the struct type |
Ben Clayton | 1d618b1 | 2021-04-09 16:19:48 +0000 | [diff] [blame] | 1619 | template <typename NAME> |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1620 | ast::Struct* Structure(NAME&& name, |
| 1621 | ast::StructMemberList members, |
| 1622 | ast::DecorationList decorations = {}) { |
Ben Clayton | 8a8d26b | 2021-04-20 15:04:21 +0000 | [diff] [blame] | 1623 | auto sym = Sym(std::forward<NAME>(name)); |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 1624 | auto* type = |
Ben Clayton | 8a8d26b | 2021-04-20 15:04:21 +0000 | [diff] [blame] | 1625 | create<ast::Struct>(sym, std::move(members), std::move(decorations)); |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 1626 | AST().AddConstructedType(type); |
| 1627 | return type; |
| 1628 | } |
| 1629 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 1630 | /// Creates a ast::StructMember |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1631 | /// @param source the source information |
| 1632 | /// @param name the struct member name |
| 1633 | /// @param type the struct member type |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 1634 | /// @param decorations the optional struct member decorations |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1635 | /// @returns the struct member pointer |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1636 | template <typename NAME> |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1637 | ast::StructMember* Member(const Source& source, |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1638 | NAME&& name, |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1639 | ast::Type* type, |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 1640 | ast::DecorationList decorations = {}) { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 1641 | type = ty.MaybeCreateTypename(type); |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1642 | return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)), |
| 1643 | type, std::move(decorations)); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 1646 | /// Creates a ast::StructMember |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1647 | /// @param name the struct member name |
| 1648 | /// @param type the struct member type |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 1649 | /// @param decorations the optional struct member decorations |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1650 | /// @returns the struct member pointer |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1651 | template <typename NAME> |
| 1652 | ast::StructMember* Member(NAME&& name, |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1653 | ast::Type* type, |
Ben Clayton | d614dd5 | 2021-03-15 10:43:11 +0000 | [diff] [blame] | 1654 | ast::DecorationList decorations = {}) { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 1655 | type = ty.MaybeCreateTypename(type); |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1656 | return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)), |
| 1657 | type, std::move(decorations)); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 1660 | /// Creates a ast::StructMember with the given byte offset |
| 1661 | /// @param offset the offset to use in the StructMemberOffsetDecoration |
| 1662 | /// @param name the struct member name |
| 1663 | /// @param type the struct member type |
| 1664 | /// @returns the struct member pointer |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1665 | template <typename NAME> |
Ben Clayton | 02ebf0d | 2021-05-05 09:09:41 +0000 | [diff] [blame] | 1666 | ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 1667 | type = ty.MaybeCreateTypename(type); |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 1668 | return create<ast::StructMember>( |
Ben Clayton | 1b8d9f2 | 2021-04-07 11:16:01 +0000 | [diff] [blame] | 1669 | source_, Sym(std::forward<NAME>(name)), type, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 1670 | ast::DecorationList{ |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 1671 | create<ast::StructMemberOffsetDecoration>(offset), |
| 1672 | }); |
| 1673 | } |
| 1674 | |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 1675 | /// Creates a ast::BlockStatement with input statements |
| 1676 | /// @param statements statements of block |
| 1677 | /// @returns the block statement pointer |
| 1678 | template <typename... Statements> |
| 1679 | ast::BlockStatement* Block(Statements&&... statements) { |
| 1680 | return create<ast::BlockStatement>( |
| 1681 | ast::StatementList{std::forward<Statements>(statements)...}); |
| 1682 | } |
| 1683 | |
| 1684 | /// Creates a ast::ElseStatement with input condition and body |
| 1685 | /// @param condition the else condition expression |
| 1686 | /// @param body the else body |
| 1687 | /// @returns the else statement pointer |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 1688 | template <typename CONDITION> |
| 1689 | ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) { |
| 1690 | return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)), |
| 1691 | body); |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | /// Creates a ast::IfStatement with input condition, body, and optional |
| 1695 | /// variadic else statements |
| 1696 | /// @param condition the if statement condition expression |
| 1697 | /// @param body the if statement body |
| 1698 | /// @param elseStatements optional variadic else statements |
| 1699 | /// @returns the if statement pointer |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 1700 | template <typename CONDITION, typename... ELSE_STATEMENTS> |
| 1701 | ast::IfStatement* If(CONDITION&& condition, |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 1702 | ast::BlockStatement* body, |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 1703 | ELSE_STATEMENTS&&... elseStatements) { |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 1704 | return create<ast::IfStatement>( |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 1705 | Expr(std::forward<CONDITION>(condition)), body, |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 1706 | ast::ElseStatementList{ |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 1707 | std::forward<ELSE_STATEMENTS>(elseStatements)...}); |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | /// Creates a ast::AssignmentStatement with input lhs and rhs expressions |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame] | 1711 | /// @param source the source information |
| 1712 | /// @param lhs the left hand side expression initializer |
| 1713 | /// @param rhs the right hand side expression initializer |
| 1714 | /// @returns the assignment statement pointer |
| 1715 | template <typename LhsExpressionInit, typename RhsExpressionInit> |
| 1716 | ast::AssignmentStatement* Assign(const Source& source, |
| 1717 | LhsExpressionInit&& lhs, |
| 1718 | RhsExpressionInit&& rhs) { |
| 1719 | return create<ast::AssignmentStatement>( |
| 1720 | source, Expr(std::forward<LhsExpressionInit>(lhs)), |
| 1721 | Expr(std::forward<RhsExpressionInit>(rhs))); |
| 1722 | } |
| 1723 | |
| 1724 | /// Creates a ast::AssignmentStatement with input lhs and rhs expressions |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 1725 | /// @param lhs the left hand side expression initializer |
| 1726 | /// @param rhs the right hand side expression initializer |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 1727 | /// @returns the assignment statement pointer |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 1728 | template <typename LhsExpressionInit, typename RhsExpressionInit> |
| 1729 | ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs, |
| 1730 | RhsExpressionInit&& rhs) { |
| 1731 | return create<ast::AssignmentStatement>( |
| 1732 | Expr(std::forward<LhsExpressionInit>(lhs)), |
| 1733 | Expr(std::forward<RhsExpressionInit>(rhs))); |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | /// Creates a ast::LoopStatement with input body and optional continuing |
| 1737 | /// @param body the loop body |
| 1738 | /// @param continuing the optional continuing block |
| 1739 | /// @returns the loop statement pointer |
| 1740 | ast::LoopStatement* Loop(ast::BlockStatement* body, |
| 1741 | ast::BlockStatement* continuing = nullptr) { |
| 1742 | return create<ast::LoopStatement>(body, continuing); |
| 1743 | } |
| 1744 | |
| 1745 | /// Creates a ast::VariableDeclStatement for the input variable |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame] | 1746 | /// @param source the source information |
| 1747 | /// @param var the variable to wrap in a decl statement |
| 1748 | /// @returns the variable decl statement pointer |
| 1749 | ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) { |
| 1750 | return create<ast::VariableDeclStatement>(source, var); |
| 1751 | } |
| 1752 | |
| 1753 | /// Creates a ast::VariableDeclStatement for the input variable |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 1754 | /// @param var the variable to wrap in a decl statement |
| 1755 | /// @returns the variable decl statement pointer |
| 1756 | ast::VariableDeclStatement* Decl(ast::Variable* var) { |
| 1757 | return create<ast::VariableDeclStatement>(var); |
| 1758 | } |
| 1759 | |
Antonio Maiorano | cea744d | 2021-03-25 12:55:27 +0000 | [diff] [blame] | 1760 | /// Creates a ast::SwitchStatement with input expression and cases |
| 1761 | /// @param condition the condition expression initializer |
| 1762 | /// @param cases case statements |
| 1763 | /// @returns the switch statement pointer |
| 1764 | template <typename ExpressionInit, typename... Cases> |
| 1765 | ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) { |
| 1766 | return create<ast::SwitchStatement>( |
| 1767 | Expr(std::forward<ExpressionInit>(condition)), |
| 1768 | ast::CaseStatementList{std::forward<Cases>(cases)...}); |
| 1769 | } |
| 1770 | |
| 1771 | /// Creates a ast::CaseStatement with input list of selectors, and body |
| 1772 | /// @param selectors list of selectors |
| 1773 | /// @param body the case body |
| 1774 | /// @returns the case statement pointer |
| 1775 | ast::CaseStatement* Case(ast::CaseSelectorList selectors, |
| 1776 | ast::BlockStatement* body = nullptr) { |
| 1777 | return create<ast::CaseStatement>(std::move(selectors), |
| 1778 | body ? body : Block()); |
| 1779 | } |
| 1780 | |
| 1781 | /// Convenient overload that takes a single selector |
| 1782 | /// @param selector a single case selector |
| 1783 | /// @param body the case body |
| 1784 | /// @returns the case statement pointer |
| 1785 | ast::CaseStatement* Case(ast::IntLiteral* selector, |
| 1786 | ast::BlockStatement* body = nullptr) { |
| 1787 | return Case(ast::CaseSelectorList{selector}, body); |
| 1788 | } |
| 1789 | |
| 1790 | /// Convenience function that creates a 'default' ast::CaseStatement |
| 1791 | /// @param body the case body |
| 1792 | /// @returns the case statement pointer |
| 1793 | ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) { |
| 1794 | return Case(ast::CaseSelectorList{}, body); |
| 1795 | } |
| 1796 | |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1797 | /// Creates an ast::BuiltinDecoration |
| 1798 | /// @param source the source information |
| 1799 | /// @param builtin the builtin value |
| 1800 | /// @returns the builtin decoration pointer |
Antonio Maiorano | 101f463 | 2021-04-07 20:35:11 +0000 | [diff] [blame] | 1801 | ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1802 | return create<ast::BuiltinDecoration>(source, builtin); |
| 1803 | } |
| 1804 | |
| 1805 | /// Creates an ast::BuiltinDecoration |
| 1806 | /// @param builtin the builtin value |
| 1807 | /// @returns the builtin decoration pointer |
| 1808 | ast::BuiltinDecoration* Builtin(ast::Builtin builtin) { |
| 1809 | return create<ast::BuiltinDecoration>(source_, builtin); |
| 1810 | } |
| 1811 | |
| 1812 | /// Creates an ast::LocationDecoration |
| 1813 | /// @param source the source information |
| 1814 | /// @param location the location value |
| 1815 | /// @returns the location decoration pointer |
Antonio Maiorano | 101f463 | 2021-04-07 20:35:11 +0000 | [diff] [blame] | 1816 | ast::LocationDecoration* Location(const Source& source, uint32_t location) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1817 | return create<ast::LocationDecoration>(source, location); |
| 1818 | } |
| 1819 | |
| 1820 | /// Creates an ast::LocationDecoration |
| 1821 | /// @param location the location value |
| 1822 | /// @returns the location decoration pointer |
| 1823 | ast::LocationDecoration* Location(uint32_t location) { |
| 1824 | return create<ast::LocationDecoration>(source_, location); |
| 1825 | } |
| 1826 | |
James Price | f2f3bfc | 2021-05-13 20:32:32 +0000 | [diff] [blame] | 1827 | /// Creates an ast::OverrideDecoration with a specific constant ID |
| 1828 | /// @param source the source information |
| 1829 | /// @param id the id value |
| 1830 | /// @returns the override decoration pointer |
| 1831 | ast::OverrideDecoration* Override(const Source& source, uint32_t id) { |
| 1832 | return create<ast::OverrideDecoration>(source, id); |
| 1833 | } |
| 1834 | |
| 1835 | /// Creates an ast::OverrideDecoration with a specific constant ID |
| 1836 | /// @param id the optional id value |
| 1837 | /// @returns the override decoration pointer |
| 1838 | ast::OverrideDecoration* Override(uint32_t id) { |
| 1839 | return Override(source_, id); |
| 1840 | } |
| 1841 | |
| 1842 | /// Creates an ast::OverrideDecoration without a constant ID |
| 1843 | /// @param source the source information |
| 1844 | /// @returns the override decoration pointer |
| 1845 | ast::OverrideDecoration* Override(const Source& source) { |
| 1846 | return create<ast::OverrideDecoration>(source); |
| 1847 | } |
| 1848 | |
| 1849 | /// Creates an ast::OverrideDecoration without a constant ID |
| 1850 | /// @returns the override decoration pointer |
| 1851 | ast::OverrideDecoration* Override() { return Override(source_); } |
| 1852 | |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1853 | /// Creates an ast::StageDecoration |
| 1854 | /// @param source the source information |
| 1855 | /// @param stage the pipeline stage |
| 1856 | /// @returns the stage decoration pointer |
Antonio Maiorano | 101f463 | 2021-04-07 20:35:11 +0000 | [diff] [blame] | 1857 | ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) { |
James Price | 68f558f | 2021-04-06 15:51:47 +0000 | [diff] [blame] | 1858 | return create<ast::StageDecoration>(source, stage); |
| 1859 | } |
| 1860 | |
| 1861 | /// Creates an ast::StageDecoration |
| 1862 | /// @param stage the pipeline stage |
| 1863 | /// @returns the stage decoration pointer |
| 1864 | ast::StageDecoration* Stage(ast::PipelineStage stage) { |
| 1865 | return create<ast::StageDecoration>(source_, stage); |
| 1866 | } |
| 1867 | |
James Price | 70f80bb | 2021-05-19 13:40:08 +0000 | [diff] [blame] | 1868 | /// Creates an ast::WorkgroupDecoration |
| 1869 | /// @param x the x dimension expression |
| 1870 | /// @returns the workgroup decoration pointer |
| 1871 | template <typename EXPR_X> |
| 1872 | ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) { |
| 1873 | return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr); |
| 1874 | } |
| 1875 | |
| 1876 | /// Creates an ast::WorkgroupDecoration |
| 1877 | /// @param x the x dimension expression |
| 1878 | /// @param y the y dimension expression |
| 1879 | /// @returns the workgroup decoration pointer |
| 1880 | template <typename EXPR_X, typename EXPR_Y> |
| 1881 | ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) { |
| 1882 | return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y), |
| 1883 | nullptr); |
| 1884 | } |
| 1885 | |
| 1886 | /// Creates an ast::WorkgroupDecoration |
| 1887 | /// @param x the x dimension expression |
| 1888 | /// @param y the y dimension expression |
| 1889 | /// @param z the z dimension expression |
| 1890 | /// @returns the workgroup decoration pointer |
| 1891 | template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z> |
| 1892 | ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) { |
| 1893 | return create<ast::WorkgroupDecoration>( |
| 1894 | source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)), |
| 1895 | Expr(std::forward<EXPR_Z>(z))); |
| 1896 | } |
| 1897 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1898 | /// Sets the current builder source to `src` |
| 1899 | /// @param src the Source used for future create() calls |
| 1900 | void SetSource(const Source& src) { |
| 1901 | AssertNotMoved(); |
| 1902 | source_ = src; |
| 1903 | } |
| 1904 | |
| 1905 | /// Sets the current builder source to `loc` |
| 1906 | /// @param loc the Source used for future create() calls |
| 1907 | void SetSource(const Source::Location& loc) { |
| 1908 | AssertNotMoved(); |
| 1909 | source_ = Source(loc); |
| 1910 | } |
| 1911 | |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 1912 | /// Helper for returning the resolved semantic type of the expression `expr`. |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1913 | /// @note As the Resolver is run when the Program is built, this will only be |
| 1914 | /// useful for the Resolver itself and tests that use their own Resolver. |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 1915 | /// @param expr the AST expression |
| 1916 | /// @return the resolved semantic type for the expression, or nullptr if the |
| 1917 | /// expression has no resolved type. |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 1918 | sem::Type* TypeOf(const ast::Expression* expr) const; |
| 1919 | |
Ben Clayton | fe0910f | 2021-05-17 15:51:47 +0000 | [diff] [blame] | 1920 | /// Helper for returning the resolved semantic type of the variable `var`. |
| 1921 | /// @note As the Resolver is run when the Program is built, this will only be |
| 1922 | /// useful for the Resolver itself and tests that use their own Resolver. |
| 1923 | /// @param var the AST variable |
| 1924 | /// @return the resolved semantic type for the variable, or nullptr if the |
| 1925 | /// variable has no resolved type. |
| 1926 | sem::Type* TypeOf(const ast::Variable* var) const; |
| 1927 | |
Ben Clayton | fbec46f | 2021-04-30 20:20:19 +0000 | [diff] [blame] | 1928 | /// Helper for returning the resolved semantic type of the AST type `type`. |
| 1929 | /// @note As the Resolver is run when the Program is built, this will only be |
| 1930 | /// useful for the Resolver itself and tests that use their own Resolver. |
| 1931 | /// @param expr the AST type |
| 1932 | /// @return the resolved semantic type for the type, or nullptr if the type |
| 1933 | /// has no resolved type. |
| 1934 | const sem::Type* TypeOf(const ast::Type* expr) const; |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 1935 | |
Ben Clayton | 169512e | 2021-04-17 05:52:11 +0000 | [diff] [blame] | 1936 | /// Wraps the ast::Literal in a statement. This is used by tests that |
| 1937 | /// construct a partial AST and require the Resolver to reach these |
| 1938 | /// nodes. |
| 1939 | /// @param lit the ast::Literal to be wrapped by an ast::Statement |
| 1940 | /// @return the ast::Statement that wraps the ast::Statement |
| 1941 | ast::Statement* WrapInStatement(ast::Literal* lit); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1942 | /// Wraps the ast::Expression in a statement. This is used by tests that |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1943 | /// construct a partial AST and require the Resolver to reach these |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1944 | /// nodes. |
| 1945 | /// @param expr the ast::Expression to be wrapped by an ast::Statement |
| 1946 | /// @return the ast::Statement that wraps the ast::Expression |
| 1947 | ast::Statement* WrapInStatement(ast::Expression* expr); |
| 1948 | /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1949 | /// tests that construct a partial AST and require the Resolver to reach |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1950 | /// these nodes. |
| 1951 | /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement |
| 1952 | /// @return the ast::VariableDeclStatement that wraps the ast::Variable |
| 1953 | ast::VariableDeclStatement* WrapInStatement(ast::Variable* v); |
| 1954 | /// Returns the statement argument. Used as a passthrough-overload by |
| 1955 | /// WrapInFunction(). |
| 1956 | /// @param stmt the ast::Statement |
| 1957 | /// @return `stmt` |
| 1958 | ast::Statement* WrapInStatement(ast::Statement* stmt); |
| 1959 | /// Wraps the list of arguments in a simple function so that each is reachable |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1960 | /// by the Resolver. |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1961 | /// @param args a mix of ast::Expression, ast::Statement, ast::Variables. |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1962 | /// @returns the function |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1963 | template <typename... ARGS> |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1964 | ast::Function* WrapInFunction(ARGS&&... args) { |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1965 | ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...}; |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1966 | return WrapInFunction(std::move(stmts)); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1967 | } |
| 1968 | /// @param stmts a list of ast::Statement that will be wrapped by a function, |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1969 | /// so that each statement is reachable by the Resolver. |
Antonio Maiorano | 03c01b5 | 2021-03-19 14:04:51 +0000 | [diff] [blame] | 1970 | /// @returns the function |
| 1971 | ast::Function* WrapInFunction(ast::StatementList stmts); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1972 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1973 | /// The builder types |
Ben Clayton | c7ca766 | 2021-02-17 16:23:52 +0000 | [diff] [blame] | 1974 | TypesBuilder const ty{this}; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1975 | |
| 1976 | protected: |
| 1977 | /// Asserts that the builder has not been moved. |
| 1978 | void AssertNotMoved() const; |
| 1979 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1980 | private: |
Ben Clayton | e6995de | 2021-04-13 23:27:27 +0000 | [diff] [blame] | 1981 | ProgramID id_; |
Antonio Maiorano | 3751fd2 | 2021-04-19 22:51:23 +0000 | [diff] [blame] | 1982 | sem::Manager types_; |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 1983 | ASTNodeAllocator ast_nodes_; |
| 1984 | SemNodeAllocator sem_nodes_; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1985 | ast::Module* ast_; |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame] | 1986 | sem::Info sem_; |
Ben Clayton | 13ef87c | 2021-04-15 18:20:03 +0000 | [diff] [blame] | 1987 | SymbolTable symbols_{id_}; |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 1988 | diag::List diagnostics_; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1989 | |
| 1990 | /// The source to use when creating AST nodes without providing a Source as |
| 1991 | /// the first argument. |
| 1992 | Source source_; |
| 1993 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1994 | /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the |
Ben Clayton | dd69ac3 | 2021-01-27 19:23:55 +0000 | [diff] [blame] | 1995 | /// program when built. |
| 1996 | bool resolve_on_build_ = true; |
| 1997 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1998 | /// Set by MarkAsMoved(). Once set, no methods may be called on this builder. |
| 1999 | bool moved_ = false; |
| 2000 | }; |
| 2001 | |
| 2002 | //! @cond Doxygen_Suppress |
| 2003 | // Various template specializations for ProgramBuilder::TypesBuilder::CToAST. |
| 2004 | template <> |
| 2005 | struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 2006 | static typ::Type get(const ProgramBuilder::TypesBuilder* t) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 2007 | return t->i32(); |
| 2008 | } |
| 2009 | }; |
| 2010 | template <> |
| 2011 | struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 2012 | static typ::Type get(const ProgramBuilder::TypesBuilder* t) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 2013 | return t->u32(); |
| 2014 | } |
| 2015 | }; |
| 2016 | template <> |
| 2017 | struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 2018 | static typ::Type get(const ProgramBuilder::TypesBuilder* t) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 2019 | return t->f32(); |
| 2020 | } |
| 2021 | }; |
| 2022 | template <> |
| 2023 | struct ProgramBuilder::TypesBuilder::CToAST<bool> { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 2024 | static typ::Type get(const ProgramBuilder::TypesBuilder* t) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 2025 | return t->bool_(); |
| 2026 | } |
| 2027 | }; |
| 2028 | template <> |
| 2029 | struct ProgramBuilder::TypesBuilder::CToAST<void> { |
Ben Clayton | 3b3cf5f | 2021-04-23 15:41:34 +0000 | [diff] [blame] | 2030 | static typ::Type get(const ProgramBuilder::TypesBuilder* t) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 2031 | return t->void_(); |
| 2032 | } |
| 2033 | }; |
| 2034 | //! @endcond |
| 2035 | |
Ben Clayton | 917b14b | 2021-04-19 16:50:23 +0000 | [diff] [blame] | 2036 | /// @param builder the ProgramBuilder |
| 2037 | /// @returns the ProgramID of the ProgramBuilder |
| 2038 | inline ProgramID ProgramIDOf(const ProgramBuilder* builder) { |
| 2039 | return builder->ID(); |
| 2040 | } |
| 2041 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 2042 | } // namespace tint |
| 2043 | |
| 2044 | #endif // SRC_PROGRAM_BUILDER_H_ |