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