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