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