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