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