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