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