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" |
| 22 | #include "src/ast/binary_expression.h" |
| 23 | #include "src/ast/bool_literal.h" |
| 24 | #include "src/ast/call_expression.h" |
| 25 | #include "src/ast/expression.h" |
| 26 | #include "src/ast/float_literal.h" |
| 27 | #include "src/ast/identifier_expression.h" |
| 28 | #include "src/ast/member_accessor_expression.h" |
| 29 | #include "src/ast/module.h" |
| 30 | #include "src/ast/scalar_constructor_expression.h" |
| 31 | #include "src/ast/sint_literal.h" |
| 32 | #include "src/ast/struct.h" |
| 33 | #include "src/ast/struct_member.h" |
| 34 | #include "src/ast/struct_member_offset_decoration.h" |
| 35 | #include "src/ast/type_constructor_expression.h" |
| 36 | #include "src/ast/uint_literal.h" |
| 37 | #include "src/ast/variable.h" |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 38 | #include "src/diagnostic/diagnostic.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 39 | #include "src/program.h" |
| 40 | #include "src/symbol_table.h" |
| 41 | #include "src/type/alias_type.h" |
| 42 | #include "src/type/array_type.h" |
| 43 | #include "src/type/bool_type.h" |
| 44 | #include "src/type/f32_type.h" |
| 45 | #include "src/type/i32_type.h" |
| 46 | #include "src/type/matrix_type.h" |
| 47 | #include "src/type/pointer_type.h" |
| 48 | #include "src/type/struct_type.h" |
| 49 | #include "src/type/type_manager.h" |
| 50 | #include "src/type/u32_type.h" |
| 51 | #include "src/type/vector_type.h" |
| 52 | #include "src/type/void_type.h" |
| 53 | |
| 54 | namespace tint { |
| 55 | |
| 56 | class CloneContext; |
| 57 | |
| 58 | /// ProgramBuilder is a mutable builder for a Program. |
| 59 | /// To construct a Program, populate the builder and then `std::move` it to a |
| 60 | /// Program. |
| 61 | class ProgramBuilder { |
| 62 | public: |
| 63 | /// ASTNodes is an alias to BlockAllocator<ast::Node> |
| 64 | using ASTNodes = BlockAllocator<ast::Node>; |
| 65 | |
| 66 | /// `i32` is a type alias to `int`. |
| 67 | /// Useful for passing to template methods such as `vec2<i32>()` to imitate |
| 68 | /// WGSL syntax. |
| 69 | /// Note: this is intentionally not aliased to uint32_t as we want integer |
| 70 | /// literals passed to the builder to match WGSL's integer literal types. |
| 71 | using i32 = decltype(1); |
| 72 | /// `u32` is a type alias to `unsigned int`. |
| 73 | /// Useful for passing to template methods such as `vec2<u32>()` to imitate |
| 74 | /// WGSL syntax. |
| 75 | /// Note: this is intentionally not aliased to uint32_t as we want integer |
| 76 | /// literals passed to the builder to match WGSL's integer literal types. |
| 77 | using u32 = decltype(1u); |
| 78 | /// `f32` is a type alias to `float` |
| 79 | /// Useful for passing to template methods such as `vec2<f32>()` to imitate |
| 80 | /// WGSL syntax. |
| 81 | using f32 = float; |
| 82 | |
| 83 | /// Constructor |
| 84 | ProgramBuilder(); |
| 85 | |
| 86 | /// Move constructor |
| 87 | /// @param rhs the builder to move |
| 88 | ProgramBuilder(ProgramBuilder&& rhs); |
| 89 | |
| 90 | /// Destructor |
| 91 | virtual ~ProgramBuilder(); |
| 92 | |
| 93 | /// Move assignment operator |
| 94 | /// @param rhs the builder to move |
| 95 | /// @return this builder |
| 96 | ProgramBuilder& operator=(ProgramBuilder&& rhs); |
| 97 | |
| 98 | /// @returns a reference to the program's types |
| 99 | type::Manager& Types() { |
| 100 | AssertNotMoved(); |
| 101 | return types_; |
| 102 | } |
| 103 | |
| 104 | /// @returns a reference to the program's AST nodes storage |
| 105 | ASTNodes& Nodes() { |
| 106 | AssertNotMoved(); |
| 107 | return nodes_; |
| 108 | } |
| 109 | |
| 110 | /// @returns a reference to the program's AST root Module |
| 111 | ast::Module& AST() { |
| 112 | AssertNotMoved(); |
| 113 | return *ast_; |
| 114 | } |
| 115 | |
| 116 | /// @returns a reference to the program's SymbolTable |
| 117 | SymbolTable& Symbols() { |
| 118 | AssertNotMoved(); |
| 119 | return symbols_; |
| 120 | } |
| 121 | |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 122 | /// @returns a reference to the program's diagnostics |
| 123 | diag::List& Diagnostics() { |
| 124 | AssertNotMoved(); |
| 125 | return diagnostics_; |
| 126 | } |
| 127 | |
Ben Clayton | dd69ac3 | 2021-01-27 19:23:55 +0000 | [diff] [blame^] | 128 | /// Controls whether the TypeDeterminer will be run on the program when it is |
| 129 | /// built. |
| 130 | /// @param enable the new flag value (defaults to true) |
| 131 | void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; } |
| 132 | |
| 133 | /// @return true if the TypeDeterminer will be run on the program when it is |
| 134 | /// built. |
| 135 | bool ResolveOnBuild() const { return resolve_on_build_; } |
| 136 | |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 137 | /// @returns true if the program has no error diagnostics and is not missing |
| 138 | /// information |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 139 | bool IsValid() const; |
| 140 | |
| 141 | /// creates a new ast::Node owned by the Module. When the Module is |
| 142 | /// destructed, the ast::Node will also be destructed. |
| 143 | /// @param source the Source of the node |
| 144 | /// @param args the arguments to pass to the type constructor |
| 145 | /// @returns the node pointer |
| 146 | template <typename T, typename... ARGS> |
| 147 | traits::EnableIfIsType<T, ast::Node>* create(const Source& source, |
| 148 | ARGS&&... args) { |
| 149 | AssertNotMoved(); |
| 150 | return nodes_.Create<T>(source, std::forward<ARGS>(args)...); |
| 151 | } |
| 152 | |
| 153 | /// creates a new ast::Node owned by the Module, injecting the current Source |
| 154 | /// as set by the last call to SetSource() as the only argument to the |
| 155 | /// constructor. |
| 156 | /// When the Module is destructed, the ast::Node will also be destructed. |
| 157 | /// @returns the node pointer |
| 158 | template <typename T> |
| 159 | traits::EnableIfIsType<T, ast::Node>* create() { |
| 160 | AssertNotMoved(); |
| 161 | return nodes_.Create<T>(source_); |
| 162 | } |
| 163 | |
| 164 | /// creates a new ast::Node owned by the Module, injecting the current Source |
| 165 | /// as set by the last call to SetSource() as the first argument to the |
| 166 | /// constructor. |
| 167 | /// When the Module is destructed, the ast::Node will also be destructed. |
| 168 | /// @param arg0 the first arguments to pass to the type constructor |
| 169 | /// @param args the remaining arguments to pass to the type constructor |
| 170 | /// @returns the node pointer |
| 171 | template <typename T, typename ARG0, typename... ARGS> |
| 172 | traits::EnableIf</* T is ast::Node and ARG0 is not Source */ |
| 173 | traits::IsTypeOrDerived<T, ast::Node>::value && |
| 174 | !traits::IsTypeOrDerived<ARG0, Source>::value, |
| 175 | T>* |
| 176 | create(ARG0&& arg0, ARGS&&... args) { |
| 177 | AssertNotMoved(); |
| 178 | return nodes_.Create<T>(source_, std::forward<ARG0>(arg0), |
| 179 | std::forward<ARGS>(args)...); |
| 180 | } |
| 181 | |
| 182 | /// creates a new type::Type owned by the Module. |
| 183 | /// When the Module is destructed, owned Module and the returned |
| 184 | /// `Type` will also be destructed. |
| 185 | /// Types are unique (de-aliased), and so calling create() for the same `T` |
| 186 | /// and arguments will return the same pointer. |
| 187 | /// @warning Use this method to acquire a type only if all of its type |
| 188 | /// information is provided in the constructor arguments `args`.<br> |
| 189 | /// If the type requires additional configuration after construction that |
| 190 | /// affect its fundamental type, build the type with `std::make_unique`, make |
| 191 | /// any necessary alterations and then call unique_type() instead. |
| 192 | /// @param args the arguments to pass to the type constructor |
| 193 | /// @returns the de-aliased type pointer |
| 194 | template <typename T, typename... ARGS> |
| 195 | traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) { |
| 196 | static_assert(std::is_base_of<type::Type, T>::value, |
| 197 | "T does not derive from type::Type"); |
| 198 | AssertNotMoved(); |
| 199 | return types_.Get<T>(std::forward<ARGS>(args)...); |
| 200 | } |
| 201 | |
| 202 | /// Marks this builder as moved, preventing any further use of the builder. |
| 203 | void MarkAsMoved(); |
| 204 | |
| 205 | ////////////////////////////////////////////////////////////////////////////// |
| 206 | // TypesBuilder |
| 207 | ////////////////////////////////////////////////////////////////////////////// |
| 208 | |
| 209 | /// TypesBuilder holds basic `tint` types and methods for constructing |
| 210 | /// complex types. |
| 211 | class TypesBuilder { |
| 212 | public: |
| 213 | /// Constructor |
| 214 | /// @param builder the program builder |
| 215 | explicit TypesBuilder(ProgramBuilder* builder); |
| 216 | |
| 217 | /// @return the tint AST type for the C type `T`. |
| 218 | template <typename T> |
| 219 | type::Type* Of() const { |
| 220 | return CToAST<T>::get(this); |
| 221 | } |
| 222 | |
| 223 | /// @returns a boolean type |
| 224 | type::Bool* bool_() const { return builder->create<type::Bool>(); } |
| 225 | |
| 226 | /// @returns a f32 type |
| 227 | type::F32* f32() const { return builder->create<type::F32>(); } |
| 228 | |
| 229 | /// @returns a i32 type |
| 230 | type::I32* i32() const { return builder->create<type::I32>(); } |
| 231 | |
| 232 | /// @returns a u32 type |
| 233 | type::U32* u32() const { return builder->create<type::U32>(); } |
| 234 | |
| 235 | /// @returns a void type |
| 236 | type::Void* void_() const { return builder->create<type::Void>(); } |
| 237 | |
| 238 | /// @return the tint AST type for a 2-element vector of the C type `T`. |
| 239 | template <typename T> |
| 240 | type::Vector* vec2() const { |
| 241 | return builder->create<type::Vector>(Of<T>(), 2); |
| 242 | } |
| 243 | |
| 244 | /// @return the tint AST type for a 3-element vector of the C type `T`. |
| 245 | template <typename T> |
| 246 | type::Vector* vec3() const { |
| 247 | return builder->create<type::Vector>(Of<T>(), 3); |
| 248 | } |
| 249 | |
| 250 | /// @return the tint AST type for a 4-element vector of the C type `T`. |
| 251 | template <typename T> |
| 252 | type::Type* vec4() const { |
| 253 | return builder->create<type::Vector>(Of<T>(), 4); |
| 254 | } |
| 255 | |
| 256 | /// @return the tint AST type for a 2x3 matrix of the C type `T`. |
| 257 | template <typename T> |
| 258 | type::Matrix* mat2x2() const { |
| 259 | return builder->create<type::Matrix>(Of<T>(), 2, 2); |
| 260 | } |
| 261 | |
| 262 | /// @return the tint AST type for a 2x3 matrix of the C type `T`. |
| 263 | template <typename T> |
| 264 | type::Matrix* mat2x3() const { |
| 265 | return builder->create<type::Matrix>(Of<T>(), 3, 2); |
| 266 | } |
| 267 | |
| 268 | /// @return the tint AST type for a 2x4 matrix of the C type `T`. |
| 269 | template <typename T> |
| 270 | type::Matrix* mat2x4() const { |
| 271 | return builder->create<type::Matrix>(Of<T>(), 4, 2); |
| 272 | } |
| 273 | |
| 274 | /// @return the tint AST type for a 3x2 matrix of the C type `T`. |
| 275 | template <typename T> |
| 276 | type::Matrix* mat3x2() const { |
| 277 | return builder->create<type::Matrix>(Of<T>(), 2, 3); |
| 278 | } |
| 279 | |
| 280 | /// @return the tint AST type for a 3x3 matrix of the C type `T`. |
| 281 | template <typename T> |
| 282 | type::Matrix* mat3x3() const { |
| 283 | return builder->create<type::Matrix>(Of<T>(), 3, 3); |
| 284 | } |
| 285 | |
| 286 | /// @return the tint AST type for a 3x4 matrix of the C type `T`. |
| 287 | template <typename T> |
| 288 | type::Matrix* mat3x4() const { |
| 289 | return builder->create<type::Matrix>(Of<T>(), 4, 3); |
| 290 | } |
| 291 | |
| 292 | /// @return the tint AST type for a 4x2 matrix of the C type `T`. |
| 293 | template <typename T> |
| 294 | type::Matrix* mat4x2() const { |
| 295 | return builder->create<type::Matrix>(Of<T>(), 2, 4); |
| 296 | } |
| 297 | |
| 298 | /// @return the tint AST type for a 4x3 matrix of the C type `T`. |
| 299 | template <typename T> |
| 300 | type::Matrix* mat4x3() const { |
| 301 | return builder->create<type::Matrix>(Of<T>(), 3, 4); |
| 302 | } |
| 303 | |
| 304 | /// @return the tint AST type for a 4x4 matrix of the C type `T`. |
| 305 | template <typename T> |
| 306 | type::Matrix* mat4x4() const { |
| 307 | return builder->create<type::Matrix>(Of<T>(), 4, 4); |
| 308 | } |
| 309 | |
| 310 | /// @param subtype the array element type |
| 311 | /// @param n the array size. 0 represents a runtime-array. |
| 312 | /// @return the tint AST type for a array of size `n` of type `T` |
| 313 | type::Array* array(type::Type* subtype, uint32_t n) const { |
| 314 | return builder->create<type::Array>(subtype, n, |
| 315 | ast::ArrayDecorationList{}); |
| 316 | } |
| 317 | |
| 318 | /// @return the tint AST type for an array of size `N` of type `T` |
| 319 | template <typename T, int N = 0> |
| 320 | type::Array* array() const { |
| 321 | return array(Of<T>(), N); |
| 322 | } |
| 323 | |
| 324 | /// creates an alias type |
| 325 | /// @param name the alias name |
| 326 | /// @param type the alias type |
| 327 | /// @returns the alias pointer |
| 328 | type::Alias* alias(const std::string& name, type::Type* type) const { |
| 329 | return builder->create<type::Alias>(builder->Symbols().Register(name), |
| 330 | type); |
| 331 | } |
| 332 | |
| 333 | /// @return the tint AST pointer to type `T` with the given |
| 334 | /// ast::StorageClass. |
| 335 | /// @param storage_class the storage class of the pointer |
| 336 | template <typename T> |
| 337 | type::Pointer* pointer(ast::StorageClass storage_class) const { |
| 338 | return builder->create<type::Pointer>(Of<T>(), storage_class); |
| 339 | } |
| 340 | |
| 341 | /// @param name the struct name |
| 342 | /// @param impl the struct implementation |
| 343 | /// @returns a struct pointer |
| 344 | type::Struct* struct_(const std::string& name, ast::Struct* impl) const { |
| 345 | return builder->create<type::Struct>(builder->Symbols().Register(name), |
| 346 | impl); |
| 347 | } |
| 348 | |
| 349 | private: |
| 350 | /// CToAST<T> is specialized for various `T` types and each specialization |
| 351 | /// contains a single static `get()` method for obtaining the corresponding |
| 352 | /// AST type for the C type `T`. |
| 353 | /// `get()` has the signature: |
| 354 | /// `static type::Type* get(Types* t)` |
| 355 | template <typename T> |
| 356 | struct CToAST {}; |
| 357 | |
| 358 | ProgramBuilder* builder; |
| 359 | }; |
| 360 | |
| 361 | ////////////////////////////////////////////////////////////////////////////// |
| 362 | // AST helper methods |
| 363 | ////////////////////////////////////////////////////////////////////////////// |
| 364 | |
| 365 | /// @param expr the expression |
| 366 | /// @return expr |
| 367 | ast::Expression* Expr(ast::Expression* expr) { return expr; } |
| 368 | |
| 369 | /// @param name the identifier name |
| 370 | /// @return an ast::IdentifierExpression with the given name |
| 371 | ast::IdentifierExpression* Expr(const std::string& name) { |
| 372 | return create<ast::IdentifierExpression>(Symbols().Register(name)); |
| 373 | } |
| 374 | |
| 375 | /// @param source the source information |
| 376 | /// @param name the identifier name |
| 377 | /// @return an ast::IdentifierExpression with the given name |
| 378 | ast::IdentifierExpression* Expr(const Source& source, |
| 379 | const std::string& name) { |
| 380 | return create<ast::IdentifierExpression>(source, Symbols().Register(name)); |
| 381 | } |
| 382 | |
| 383 | /// @param name the identifier name |
| 384 | /// @return an ast::IdentifierExpression with the given name |
| 385 | ast::IdentifierExpression* Expr(const char* name) { |
| 386 | return create<ast::IdentifierExpression>(Symbols().Register(name)); |
| 387 | } |
| 388 | |
| 389 | /// @param value the boolean value |
| 390 | /// @return a Scalar constructor for the given value |
| 391 | ast::ScalarConstructorExpression* Expr(bool value) { |
| 392 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 393 | } |
| 394 | |
| 395 | /// @param value the float value |
| 396 | /// @return a Scalar constructor for the given value |
| 397 | ast::ScalarConstructorExpression* Expr(f32 value) { |
| 398 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 399 | } |
| 400 | |
| 401 | /// @param value the integer value |
| 402 | /// @return a Scalar constructor for the given value |
| 403 | ast::ScalarConstructorExpression* Expr(i32 value) { |
| 404 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 405 | } |
| 406 | |
| 407 | /// @param value the unsigned int value |
| 408 | /// @return a Scalar constructor for the given value |
| 409 | ast::ScalarConstructorExpression* Expr(u32 value) { |
| 410 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 411 | } |
| 412 | |
| 413 | /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to |
| 414 | /// `list`. |
| 415 | /// @param list the list to append too |
| 416 | /// @param arg the arg to create |
| 417 | template <typename ARG> |
| 418 | void Append(ast::ExpressionList& list, ARG&& arg) { |
| 419 | list.emplace_back(Expr(std::forward<ARG>(arg))); |
| 420 | } |
| 421 | |
| 422 | /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`, |
| 423 | /// then appends them to `list`. |
| 424 | /// @param list the list to append too |
| 425 | /// @param arg0 the first argument |
| 426 | /// @param args the rest of the arguments |
| 427 | template <typename ARG0, typename... ARGS> |
| 428 | void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) { |
| 429 | Append(list, std::forward<ARG0>(arg0)); |
| 430 | Append(list, std::forward<ARGS>(args)...); |
| 431 | } |
| 432 | |
| 433 | /// @return an empty list of expressions |
| 434 | ast::ExpressionList ExprList() { return {}; } |
| 435 | |
| 436 | /// @param args the list of expressions |
| 437 | /// @return the list of expressions converted to `ast::Expression`s using |
| 438 | /// `Expr()`, |
| 439 | template <typename... ARGS> |
| 440 | ast::ExpressionList ExprList(ARGS&&... args) { |
| 441 | ast::ExpressionList list; |
| 442 | list.reserve(sizeof...(args)); |
| 443 | Append(list, std::forward<ARGS>(args)...); |
| 444 | return list; |
| 445 | } |
| 446 | |
| 447 | /// @param list the list of expressions |
| 448 | /// @return `list` |
| 449 | ast::ExpressionList ExprList(ast::ExpressionList list) { return list; } |
| 450 | |
| 451 | /// @param val the boolan value |
| 452 | /// @return a boolean literal with the given value |
| 453 | ast::BoolLiteral* Literal(bool val) { |
| 454 | return create<ast::BoolLiteral>(ty.bool_(), val); |
| 455 | } |
| 456 | |
| 457 | /// @param val the float value |
| 458 | /// @return a float literal with the given value |
| 459 | ast::FloatLiteral* Literal(f32 val) { |
| 460 | return create<ast::FloatLiteral>(ty.f32(), val); |
| 461 | } |
| 462 | |
| 463 | /// @param val the unsigned int value |
| 464 | /// @return a ast::UintLiteral with the given value |
| 465 | ast::UintLiteral* Literal(u32 val) { |
| 466 | return create<ast::UintLiteral>(ty.u32(), val); |
| 467 | } |
| 468 | |
| 469 | /// @param val the integer value |
| 470 | /// @return the ast::SintLiteral with the given value |
| 471 | ast::SintLiteral* Literal(i32 val) { |
| 472 | return create<ast::SintLiteral>(ty.i32(), val); |
| 473 | } |
| 474 | |
| 475 | /// @param args the arguments for the type constructor |
| 476 | /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values |
| 477 | /// of `args` converted to `ast::Expression`s using `Expr()` |
| 478 | template <typename T, typename... ARGS> |
| 479 | ast::TypeConstructorExpression* Construct(ARGS&&... args) { |
| 480 | return create<ast::TypeConstructorExpression>( |
| 481 | ty.Of<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 482 | } |
| 483 | |
| 484 | /// @param type the type to construct |
| 485 | /// @param args the arguments for the constructor |
| 486 | /// @return an `ast::TypeConstructorExpression` of `type` constructed with the |
| 487 | /// values `args`. |
| 488 | template <typename... ARGS> |
| 489 | ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) { |
| 490 | return create<ast::TypeConstructorExpression>( |
| 491 | type, ExprList(std::forward<ARGS>(args)...)); |
| 492 | } |
| 493 | |
| 494 | /// @param args the arguments for the vector constructor |
| 495 | /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type |
| 496 | /// `T`, constructed with the values `args`. |
| 497 | template <typename T, typename... ARGS> |
| 498 | ast::TypeConstructorExpression* vec2(ARGS&&... args) { |
| 499 | return create<ast::TypeConstructorExpression>( |
| 500 | ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 501 | } |
| 502 | |
| 503 | /// @param args the arguments for the vector constructor |
| 504 | /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type |
| 505 | /// `T`, constructed with the values `args`. |
| 506 | template <typename T, typename... ARGS> |
| 507 | ast::TypeConstructorExpression* vec3(ARGS&&... args) { |
| 508 | return create<ast::TypeConstructorExpression>( |
| 509 | ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 510 | } |
| 511 | |
| 512 | /// @param args the arguments for the vector constructor |
| 513 | /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type |
| 514 | /// `T`, constructed with the values `args`. |
| 515 | template <typename T, typename... ARGS> |
| 516 | ast::TypeConstructorExpression* vec4(ARGS&&... args) { |
| 517 | return create<ast::TypeConstructorExpression>( |
| 518 | ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 519 | } |
| 520 | |
| 521 | /// @param args the arguments for the matrix constructor |
| 522 | /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type |
| 523 | /// `T`, constructed with the values `args`. |
| 524 | template <typename T, typename... ARGS> |
| 525 | ast::TypeConstructorExpression* mat2x2(ARGS&&... args) { |
| 526 | return create<ast::TypeConstructorExpression>( |
| 527 | ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 528 | } |
| 529 | |
| 530 | /// @param args the arguments for the matrix constructor |
| 531 | /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type |
| 532 | /// `T`, constructed with the values `args`. |
| 533 | template <typename T, typename... ARGS> |
| 534 | ast::TypeConstructorExpression* mat2x3(ARGS&&... args) { |
| 535 | return create<ast::TypeConstructorExpression>( |
| 536 | ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 537 | } |
| 538 | |
| 539 | /// @param args the arguments for the matrix constructor |
| 540 | /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type |
| 541 | /// `T`, constructed with the values `args`. |
| 542 | template <typename T, typename... ARGS> |
| 543 | ast::TypeConstructorExpression* mat2x4(ARGS&&... args) { |
| 544 | return create<ast::TypeConstructorExpression>( |
| 545 | ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 546 | } |
| 547 | |
| 548 | /// @param args the arguments for the matrix constructor |
| 549 | /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type |
| 550 | /// `T`, constructed with the values `args`. |
| 551 | template <typename T, typename... ARGS> |
| 552 | ast::TypeConstructorExpression* mat3x2(ARGS&&... args) { |
| 553 | return create<ast::TypeConstructorExpression>( |
| 554 | ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 555 | } |
| 556 | |
| 557 | /// @param args the arguments for the matrix constructor |
| 558 | /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type |
| 559 | /// `T`, constructed with the values `args`. |
| 560 | template <typename T, typename... ARGS> |
| 561 | ast::TypeConstructorExpression* mat3x3(ARGS&&... args) { |
| 562 | return create<ast::TypeConstructorExpression>( |
| 563 | ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 564 | } |
| 565 | |
| 566 | /// @param args the arguments for the matrix constructor |
| 567 | /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type |
| 568 | /// `T`, constructed with the values `args`. |
| 569 | template <typename T, typename... ARGS> |
| 570 | ast::TypeConstructorExpression* mat3x4(ARGS&&... args) { |
| 571 | return create<ast::TypeConstructorExpression>( |
| 572 | ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 573 | } |
| 574 | |
| 575 | /// @param args the arguments for the matrix constructor |
| 576 | /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type |
| 577 | /// `T`, constructed with the values `args`. |
| 578 | template <typename T, typename... ARGS> |
| 579 | ast::TypeConstructorExpression* mat4x2(ARGS&&... args) { |
| 580 | return create<ast::TypeConstructorExpression>( |
| 581 | ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 582 | } |
| 583 | |
| 584 | /// @param args the arguments for the matrix constructor |
| 585 | /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type |
| 586 | /// `T`, constructed with the values `args`. |
| 587 | template <typename T, typename... ARGS> |
| 588 | ast::TypeConstructorExpression* mat4x3(ARGS&&... args) { |
| 589 | return create<ast::TypeConstructorExpression>( |
| 590 | ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 591 | } |
| 592 | |
| 593 | /// @param args the arguments for the matrix constructor |
| 594 | /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type |
| 595 | /// `T`, constructed with the values `args`. |
| 596 | template <typename T, typename... ARGS> |
| 597 | ast::TypeConstructorExpression* mat4x4(ARGS&&... args) { |
| 598 | return create<ast::TypeConstructorExpression>( |
| 599 | ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 600 | } |
| 601 | |
| 602 | /// @param args the arguments for the array constructor |
| 603 | /// @return an `ast::TypeConstructorExpression` of an array with element type |
| 604 | /// `T`, constructed with the values `args`. |
| 605 | template <typename T, int N = 0, typename... ARGS> |
| 606 | ast::TypeConstructorExpression* array(ARGS&&... args) { |
| 607 | return create<ast::TypeConstructorExpression>( |
| 608 | ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...)); |
| 609 | } |
| 610 | |
| 611 | /// @param subtype the array element type |
| 612 | /// @param n the array size. 0 represents a runtime-array. |
| 613 | /// @param args the arguments for the array constructor |
| 614 | /// @return an `ast::TypeConstructorExpression` of an array with element type |
| 615 | /// `subtype`, constructed with the values `args`. |
| 616 | template <typename... ARGS> |
| 617 | ast::TypeConstructorExpression* array(type::Type* subtype, |
| 618 | uint32_t n, |
| 619 | ARGS&&... args) { |
| 620 | return create<ast::TypeConstructorExpression>( |
| 621 | ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...)); |
| 622 | } |
| 623 | |
| 624 | /// @param name the variable name |
| 625 | /// @param storage the variable storage class |
| 626 | /// @param type the variable type |
| 627 | /// @returns a `ast::Variable` with the given name, storage and type. The |
| 628 | /// variable will be built with a nullptr constructor and no decorations. |
| 629 | ast::Variable* Var(const std::string& name, |
| 630 | ast::StorageClass storage, |
| 631 | type::Type* type); |
| 632 | |
| 633 | /// @param name the variable name |
| 634 | /// @param storage the variable storage class |
| 635 | /// @param type the variable type |
| 636 | /// @param constructor constructor expression |
| 637 | /// @param decorations variable decorations |
| 638 | /// @returns a `ast::Variable` with the given name, storage and type |
| 639 | ast::Variable* Var(const std::string& name, |
| 640 | ast::StorageClass storage, |
| 641 | type::Type* type, |
| 642 | ast::Expression* constructor, |
| 643 | ast::VariableDecorationList decorations); |
| 644 | |
| 645 | /// @param source the variable source |
| 646 | /// @param name the variable name |
| 647 | /// @param storage the variable storage class |
| 648 | /// @param type the variable type |
| 649 | /// @param constructor constructor expression |
| 650 | /// @param decorations variable decorations |
| 651 | /// @returns a `ast::Variable` with the given name, storage and type |
| 652 | ast::Variable* Var(const Source& source, |
| 653 | const std::string& name, |
| 654 | ast::StorageClass storage, |
| 655 | type::Type* type, |
| 656 | ast::Expression* constructor, |
| 657 | ast::VariableDecorationList decorations); |
| 658 | |
| 659 | /// @param name the variable name |
| 660 | /// @param storage the variable storage class |
| 661 | /// @param type the variable type |
| 662 | /// @returns a constant `ast::Variable` with the given name, storage and type. |
| 663 | /// The variable will be built with a nullptr constructor and no decorations. |
| 664 | ast::Variable* Const(const std::string& name, |
| 665 | ast::StorageClass storage, |
| 666 | type::Type* type); |
| 667 | |
| 668 | /// @param name the variable name |
| 669 | /// @param storage the variable storage class |
| 670 | /// @param type the variable type |
| 671 | /// @param constructor optional constructor expression |
| 672 | /// @param decorations optional variable decorations |
| 673 | /// @returns a constant `ast::Variable` with the given name, storage and type |
| 674 | ast::Variable* Const(const std::string& name, |
| 675 | ast::StorageClass storage, |
| 676 | type::Type* type, |
| 677 | ast::Expression* constructor, |
| 678 | ast::VariableDecorationList decorations); |
| 679 | |
| 680 | /// @param source the variable source |
| 681 | /// @param name the variable name |
| 682 | /// @param storage the variable storage class |
| 683 | /// @param type the variable type |
| 684 | /// @param constructor optional constructor expression |
| 685 | /// @param decorations optional variable decorations |
| 686 | /// @returns a constant `ast::Variable` with the given name, storage and type |
| 687 | ast::Variable* Const(const Source& source, |
| 688 | const std::string& name, |
| 689 | ast::StorageClass storage, |
| 690 | type::Type* type, |
| 691 | ast::Expression* constructor, |
| 692 | ast::VariableDecorationList decorations); |
| 693 | |
| 694 | /// @param func the function name |
| 695 | /// @param args the function call arguments |
| 696 | /// @returns a `ast::CallExpression` to the function `func`, with the |
| 697 | /// arguments of `args` converted to `ast::Expression`s using `Expr()`. |
| 698 | template <typename NAME, typename... ARGS> |
| 699 | ast::CallExpression* Call(NAME&& func, ARGS&&... args) { |
| 700 | return create<ast::CallExpression>(Expr(func), |
| 701 | ExprList(std::forward<ARGS>(args)...)); |
| 702 | } |
| 703 | |
| 704 | /// @param lhs the left hand argument to the addition operation |
| 705 | /// @param rhs the right hand argument to the addition operation |
| 706 | /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs` |
| 707 | template <typename LHS, typename RHS> |
| 708 | ast::Expression* Add(LHS&& lhs, RHS&& rhs) { |
| 709 | return create<ast::BinaryExpression>(ast::BinaryOp::kAdd, |
| 710 | Expr(std::forward<LHS>(lhs)), |
| 711 | Expr(std::forward<RHS>(rhs))); |
| 712 | } |
| 713 | |
| 714 | /// @param lhs the left hand argument to the subtraction operation |
| 715 | /// @param rhs the right hand argument to the subtraction operation |
| 716 | /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs` |
| 717 | template <typename LHS, typename RHS> |
| 718 | ast::Expression* Sub(LHS&& lhs, RHS&& rhs) { |
| 719 | return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract, |
| 720 | Expr(std::forward<LHS>(lhs)), |
| 721 | Expr(std::forward<RHS>(rhs))); |
| 722 | } |
| 723 | |
| 724 | /// @param lhs the left hand argument to the multiplication operation |
| 725 | /// @param rhs the right hand argument to the multiplication operation |
| 726 | /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs` |
| 727 | template <typename LHS, typename RHS> |
| 728 | ast::Expression* Mul(LHS&& lhs, RHS&& rhs) { |
| 729 | return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply, |
| 730 | Expr(std::forward<LHS>(lhs)), |
| 731 | Expr(std::forward<RHS>(rhs))); |
| 732 | } |
| 733 | |
| 734 | /// @param arr the array argument for the array accessor expression |
| 735 | /// @param idx the index argument for the array accessor expression |
| 736 | /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx` |
| 737 | template <typename ARR, typename IDX> |
| 738 | ast::Expression* IndexAccessor(ARR&& arr, IDX&& idx) { |
| 739 | return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)), |
| 740 | Expr(std::forward<IDX>(idx))); |
| 741 | } |
| 742 | |
| 743 | /// @param obj the object for the member accessor expression |
| 744 | /// @param idx the index argument for the array accessor expression |
| 745 | /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx` |
| 746 | template <typename OBJ, typename IDX> |
| 747 | ast::Expression* MemberAccessor(OBJ&& obj, IDX&& idx) { |
| 748 | return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)), |
| 749 | Expr(std::forward<IDX>(idx))); |
| 750 | } |
| 751 | |
| 752 | /// creates a ast::StructMemberOffsetDecoration |
| 753 | /// @param val the offset value |
| 754 | /// @returns the offset decoration pointer |
| 755 | ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) { |
| 756 | return create<ast::StructMemberOffsetDecoration>(source_, val); |
| 757 | } |
| 758 | |
| 759 | /// creates a ast::Function |
| 760 | /// @param source the source information |
| 761 | /// @param name the function name |
| 762 | /// @param params the function parameters |
| 763 | /// @param type the function return type |
| 764 | /// @param body the function body |
| 765 | /// @param decorations the function decorations |
| 766 | /// @returns the function pointer |
| 767 | ast::Function* Func(Source source, |
| 768 | std::string name, |
| 769 | ast::VariableList params, |
| 770 | type::Type* type, |
| 771 | ast::StatementList body, |
| 772 | ast::FunctionDecorationList decorations) { |
| 773 | return create<ast::Function>(source, Symbols().Register(name), params, type, |
| 774 | create<ast::BlockStatement>(body), |
| 775 | decorations); |
| 776 | } |
| 777 | |
| 778 | /// creates a ast::Function |
| 779 | /// @param name the function name |
| 780 | /// @param params the function parameters |
| 781 | /// @param type the function return type |
| 782 | /// @param body the function body |
| 783 | /// @param decorations the function decorations |
| 784 | /// @returns the function pointer |
| 785 | ast::Function* Func(std::string name, |
| 786 | ast::VariableList params, |
| 787 | type::Type* type, |
| 788 | ast::StatementList body, |
| 789 | ast::FunctionDecorationList decorations) { |
| 790 | return create<ast::Function>(Symbols().Register(name), params, type, |
| 791 | create<ast::BlockStatement>(body), |
| 792 | decorations); |
| 793 | } |
| 794 | |
| 795 | /// creates a ast::StructMember |
| 796 | /// @param source the source information |
| 797 | /// @param name the struct member name |
| 798 | /// @param type the struct member type |
| 799 | /// @returns the struct member pointer |
| 800 | ast::StructMember* Member(const Source& source, |
| 801 | const std::string& name, |
| 802 | type::Type* type) { |
| 803 | return create<ast::StructMember>(source, Symbols().Register(name), type, |
| 804 | ast::StructMemberDecorationList{}); |
| 805 | } |
| 806 | |
| 807 | /// creates a ast::StructMember |
| 808 | /// @param name the struct member name |
| 809 | /// @param type the struct member type |
| 810 | /// @returns the struct member pointer |
| 811 | ast::StructMember* Member(const std::string& name, type::Type* type) { |
| 812 | return create<ast::StructMember>(source_, Symbols().Register(name), type, |
| 813 | ast::StructMemberDecorationList{}); |
| 814 | } |
| 815 | |
| 816 | /// creates a ast::StructMember |
| 817 | /// @param name the struct member name |
| 818 | /// @param type the struct member type |
| 819 | /// @param decorations the struct member decorations |
| 820 | /// @returns the struct member pointer |
| 821 | ast::StructMember* Member(const std::string& name, |
| 822 | type::Type* type, |
| 823 | ast::StructMemberDecorationList decorations) { |
| 824 | return create<ast::StructMember>(source_, Symbols().Register(name), type, |
| 825 | decorations); |
| 826 | } |
| 827 | |
| 828 | /// Sets the current builder source to `src` |
| 829 | /// @param src the Source used for future create() calls |
| 830 | void SetSource(const Source& src) { |
| 831 | AssertNotMoved(); |
| 832 | source_ = src; |
| 833 | } |
| 834 | |
| 835 | /// Sets the current builder source to `loc` |
| 836 | /// @param loc the Source used for future create() calls |
| 837 | void SetSource(const Source::Location& loc) { |
| 838 | AssertNotMoved(); |
| 839 | source_ = Source(loc); |
| 840 | } |
| 841 | |
| 842 | /// The builder types |
| 843 | TypesBuilder ty; |
| 844 | |
| 845 | protected: |
| 846 | /// Asserts that the builder has not been moved. |
| 847 | void AssertNotMoved() const; |
| 848 | |
| 849 | /// Called whenever a new variable is built with `Var()`. |
| 850 | virtual void OnVariableBuilt(ast::Variable*) {} |
| 851 | |
| 852 | private: |
| 853 | type::Manager types_; |
| 854 | ASTNodes nodes_; |
| 855 | ast::Module* ast_; |
| 856 | SymbolTable symbols_; |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 857 | diag::List diagnostics_; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 858 | |
| 859 | /// The source to use when creating AST nodes without providing a Source as |
| 860 | /// the first argument. |
| 861 | Source source_; |
| 862 | |
Ben Clayton | dd69ac3 | 2021-01-27 19:23:55 +0000 | [diff] [blame^] | 863 | /// Set by SetResolveOnBuild(). If set, the TypeDeterminer will be run on the |
| 864 | /// program when built. |
| 865 | bool resolve_on_build_ = true; |
| 866 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 867 | /// Set by MarkAsMoved(). Once set, no methods may be called on this builder. |
| 868 | bool moved_ = false; |
| 869 | }; |
| 870 | |
| 871 | //! @cond Doxygen_Suppress |
| 872 | // Various template specializations for ProgramBuilder::TypesBuilder::CToAST. |
| 873 | template <> |
| 874 | struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> { |
| 875 | static type::Type* get(const ProgramBuilder::TypesBuilder* t) { |
| 876 | return t->i32(); |
| 877 | } |
| 878 | }; |
| 879 | template <> |
| 880 | struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> { |
| 881 | static type::Type* get(const ProgramBuilder::TypesBuilder* t) { |
| 882 | return t->u32(); |
| 883 | } |
| 884 | }; |
| 885 | template <> |
| 886 | struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> { |
| 887 | static type::Type* get(const ProgramBuilder::TypesBuilder* t) { |
| 888 | return t->f32(); |
| 889 | } |
| 890 | }; |
| 891 | template <> |
| 892 | struct ProgramBuilder::TypesBuilder::CToAST<bool> { |
| 893 | static type::Type* get(const ProgramBuilder::TypesBuilder* t) { |
| 894 | return t->bool_(); |
| 895 | } |
| 896 | }; |
| 897 | template <> |
| 898 | struct ProgramBuilder::TypesBuilder::CToAST<void> { |
| 899 | static type::Type* get(const ProgramBuilder::TypesBuilder* t) { |
| 900 | return t->void_(); |
| 901 | } |
| 902 | }; |
| 903 | //! @endcond |
| 904 | |
| 905 | } // namespace tint |
| 906 | |
| 907 | #endif // SRC_PROGRAM_BUILDER_H_ |