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