blob: 984e392ad8ecdd13eb9d2ce3601db73a73bcaec6 [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"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000022#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/binary_expression.h"
24#include "src/ast/bool_literal.h"
25#include "src/ast/call_expression.h"
26#include "src/ast/expression.h"
27#include "src/ast/float_literal.h"
28#include "src/ast/identifier_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000029#include "src/ast/if_statement.h"
30#include "src/ast/loop_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000031#include "src/ast/member_accessor_expression.h"
32#include "src/ast/module.h"
33#include "src/ast/scalar_constructor_expression.h"
34#include "src/ast/sint_literal.h"
Ben Claytonbab31972021-02-08 21:16:21 +000035#include "src/ast/stride_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000036#include "src/ast/struct.h"
37#include "src/ast/struct_member.h"
38#include "src/ast/struct_member_offset_decoration.h"
39#include "src/ast/type_constructor_expression.h"
40#include "src/ast/uint_literal.h"
41#include "src/ast/variable.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000042#include "src/ast/variable_decl_statement.h"
Ben Clayton844217f2021-01-27 18:49:05 +000043#include "src/diagnostic/diagnostic.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/program.h"
Ben Claytondd1b6fc2021-01-29 10:55:40 +000045#include "src/semantic/info.h"
Ben Clayton7fdfff12021-01-29 15:17:30 +000046#include "src/semantic/node.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000047#include "src/symbol_table.h"
48#include "src/type/alias_type.h"
49#include "src/type/array_type.h"
50#include "src/type/bool_type.h"
51#include "src/type/f32_type.h"
52#include "src/type/i32_type.h"
53#include "src/type/matrix_type.h"
54#include "src/type/pointer_type.h"
55#include "src/type/struct_type.h"
56#include "src/type/type_manager.h"
57#include "src/type/u32_type.h"
58#include "src/type/vector_type.h"
59#include "src/type/void_type.h"
60
61namespace tint {
62
Ben Clayton401b96b2021-02-03 17:19:59 +000063// Forward declarations
64namespace ast {
65class VariableDeclStatement;
66} // namespace ast
67
Ben Claytona6b9a8e2021-01-26 16:57:10 +000068class CloneContext;
69
70/// ProgramBuilder is a mutable builder for a Program.
71/// To construct a Program, populate the builder and then `std::move` it to a
72/// Program.
73class ProgramBuilder {
74 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000075 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
76 using ASTNodeAllocator = BlockAllocator<ast::Node>;
77
78 /// SemNodeAllocator is an alias to BlockAllocator<semantic::Node>
79 using SemNodeAllocator = BlockAllocator<semantic::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000080
81 /// `i32` is a type alias to `int`.
82 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
83 /// WGSL syntax.
84 /// Note: this is intentionally not aliased to uint32_t as we want integer
85 /// literals passed to the builder to match WGSL's integer literal types.
86 using i32 = decltype(1);
87 /// `u32` is a type alias to `unsigned int`.
88 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
89 /// WGSL syntax.
90 /// Note: this is intentionally not aliased to uint32_t as we want integer
91 /// literals passed to the builder to match WGSL's integer literal types.
92 using u32 = decltype(1u);
93 /// `f32` is a type alias to `float`
94 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
95 /// WGSL syntax.
96 using f32 = float;
97
98 /// Constructor
99 ProgramBuilder();
100
101 /// Move constructor
102 /// @param rhs the builder to move
103 ProgramBuilder(ProgramBuilder&& rhs);
104
105 /// Destructor
106 virtual ~ProgramBuilder();
107
108 /// Move assignment operator
109 /// @param rhs the builder to move
110 /// @return this builder
111 ProgramBuilder& operator=(ProgramBuilder&& rhs);
112
Ben Claytone43c8302021-01-29 11:59:32 +0000113 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
114 /// making a deep clone of the Program contents.
115 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
116 /// existing immutable program.
117 /// As the returned ProgramBuilder wraps `program`, `program` must not be
118 /// destructed or assigned while using the returned ProgramBuilder.
119 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
120 /// function. See crbug.com/tint/460.
121 /// @param program the immutable Program to wrap
122 /// @return the ProgramBuilder that wraps `program`
123 static ProgramBuilder Wrap(const Program* program);
124
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000125 /// @returns a reference to the program's types
126 type::Manager& Types() {
127 AssertNotMoved();
128 return types_;
129 }
130
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000131 /// @returns a reference to the program's types
132 const type::Manager& Types() const {
133 AssertNotMoved();
134 return types_;
135 }
136
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000137 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000138 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000139 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000140 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000141 }
142
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000143 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000144 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000145 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000146 return ast_nodes_;
147 }
148
149 /// @returns a reference to the program's semantic nodes storage
150 SemNodeAllocator& SemNodes() {
151 AssertNotMoved();
152 return sem_nodes_;
153 }
154
155 /// @returns a reference to the program's semantic nodes storage
156 const SemNodeAllocator& SemNodes() const {
157 AssertNotMoved();
158 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000159 }
160
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000161 /// @returns a reference to the program's AST root Module
162 ast::Module& AST() {
163 AssertNotMoved();
164 return *ast_;
165 }
166
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000167 /// @returns a reference to the program's AST root Module
168 const ast::Module& AST() const {
169 AssertNotMoved();
170 return *ast_;
171 }
172
173 /// @returns a reference to the program's semantic info
174 semantic::Info& Sem() {
175 AssertNotMoved();
176 return sem_;
177 }
178
179 /// @returns a reference to the program's semantic info
180 const semantic::Info& Sem() const {
181 AssertNotMoved();
182 return sem_;
183 }
184
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000185 /// @returns a reference to the program's SymbolTable
186 SymbolTable& Symbols() {
187 AssertNotMoved();
188 return symbols_;
189 }
190
Ben Clayton708dc2d2021-01-29 11:22:40 +0000191 /// @returns a reference to the program's SymbolTable
192 const SymbolTable& Symbols() const {
193 AssertNotMoved();
194 return symbols_;
195 }
196
Ben Clayton844217f2021-01-27 18:49:05 +0000197 /// @returns a reference to the program's diagnostics
198 diag::List& Diagnostics() {
199 AssertNotMoved();
200 return diagnostics_;
201 }
202
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000203 /// @returns a reference to the program's diagnostics
204 const diag::List& Diagnostics() const {
205 AssertNotMoved();
206 return diagnostics_;
207 }
208
Ben Clayton5f0ea112021-03-09 10:54:37 +0000209 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000210 /// @param enable the new flag value (defaults to true)
211 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
212
Ben Clayton5f0ea112021-03-09 10:54:37 +0000213 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000214 /// built.
215 bool ResolveOnBuild() const { return resolve_on_build_; }
216
Ben Clayton844217f2021-01-27 18:49:05 +0000217 /// @returns true if the program has no error diagnostics and is not missing
218 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000219 bool IsValid() const;
220
Ben Clayton708dc2d2021-01-29 11:22:40 +0000221 /// Writes a representation of the node to the output stream
222 /// @note unlike str(), to_str() does not automatically demangle the string.
223 /// @param node the AST node
224 /// @param out the stream to write to
225 /// @param indent number of spaces to indent the node when writing
226 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
227 node->to_str(Sem(), out, indent);
228 }
229
230 /// Returns a demangled, string representation of `node`.
231 /// @param node the AST node
232 /// @returns a string representation of the node
233 std::string str(const ast::Node* node) const;
234
Ben Clayton7fdfff12021-01-29 15:17:30 +0000235 /// Creates a new ast::Node owned by the ProgramBuilder. When the
236 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000237 /// @param source the Source of the node
238 /// @param args the arguments to pass to the type constructor
239 /// @returns the node pointer
240 template <typename T, typename... ARGS>
241 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
242 ARGS&&... args) {
243 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000244 return ast_nodes_.Create<T>(source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000245 }
246
Ben Clayton7fdfff12021-01-29 15:17:30 +0000247 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
248 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000249 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000250 /// When the ProgramBuilder is destructed, the ast::Node will also be
251 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000252 /// @returns the node pointer
253 template <typename T>
254 traits::EnableIfIsType<T, ast::Node>* create() {
255 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000256 return ast_nodes_.Create<T>(source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000257 }
258
Ben Clayton7fdfff12021-01-29 15:17:30 +0000259 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
260 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000261 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000262 /// When the ProgramBuilder is destructed, the ast::Node will also be
263 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000264 /// @param arg0 the first arguments to pass to the type constructor
265 /// @param args the remaining arguments to pass to the type constructor
266 /// @returns the node pointer
267 template <typename T, typename ARG0, typename... ARGS>
268 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
269 traits::IsTypeOrDerived<T, ast::Node>::value &&
270 !traits::IsTypeOrDerived<ARG0, Source>::value,
271 T>*
272 create(ARG0&& arg0, ARGS&&... args) {
273 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000274 return ast_nodes_.Create<T>(source_, std::forward<ARG0>(arg0),
275 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000276 }
277
Ben Clayton7fdfff12021-01-29 15:17:30 +0000278 /// Creates a new semantic::Node owned by the ProgramBuilder.
279 /// When the ProgramBuilder is destructed, the semantic::Node will also be
280 /// destructed.
281 /// @param args the arguments to pass to the type constructor
282 /// @returns the node pointer
283 template <typename T, typename... ARGS>
284 traits::EnableIfIsType<T, semantic::Node>* create(ARGS&&... args) {
285 AssertNotMoved();
286 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
287 }
288
289 /// Creates a new type::Type owned by the ProgramBuilder.
290 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
291 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000292 /// Types are unique (de-aliased), and so calling create() for the same `T`
293 /// and arguments will return the same pointer.
294 /// @warning Use this method to acquire a type only if all of its type
295 /// information is provided in the constructor arguments `args`.<br>
296 /// If the type requires additional configuration after construction that
297 /// affect its fundamental type, build the type with `std::make_unique`, make
298 /// any necessary alterations and then call unique_type() instead.
299 /// @param args the arguments to pass to the type constructor
300 /// @returns the de-aliased type pointer
301 template <typename T, typename... ARGS>
302 traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) {
303 static_assert(std::is_base_of<type::Type, T>::value,
304 "T does not derive from type::Type");
305 AssertNotMoved();
306 return types_.Get<T>(std::forward<ARGS>(args)...);
307 }
308
309 /// Marks this builder as moved, preventing any further use of the builder.
310 void MarkAsMoved();
311
312 //////////////////////////////////////////////////////////////////////////////
313 // TypesBuilder
314 //////////////////////////////////////////////////////////////////////////////
315
316 /// TypesBuilder holds basic `tint` types and methods for constructing
317 /// complex types.
318 class TypesBuilder {
319 public:
320 /// Constructor
321 /// @param builder the program builder
322 explicit TypesBuilder(ProgramBuilder* builder);
323
324 /// @return the tint AST type for the C type `T`.
325 template <typename T>
326 type::Type* Of() const {
327 return CToAST<T>::get(this);
328 }
329
330 /// @returns a boolean type
331 type::Bool* bool_() const { return builder->create<type::Bool>(); }
332
333 /// @returns a f32 type
334 type::F32* f32() const { return builder->create<type::F32>(); }
335
336 /// @returns a i32 type
337 type::I32* i32() const { return builder->create<type::I32>(); }
338
339 /// @returns a u32 type
340 type::U32* u32() const { return builder->create<type::U32>(); }
341
342 /// @returns a void type
343 type::Void* void_() const { return builder->create<type::Void>(); }
344
345 /// @return the tint AST type for a 2-element vector of the C type `T`.
346 template <typename T>
347 type::Vector* vec2() const {
348 return builder->create<type::Vector>(Of<T>(), 2);
349 }
350
351 /// @return the tint AST type for a 3-element vector of the C type `T`.
352 template <typename T>
353 type::Vector* vec3() const {
354 return builder->create<type::Vector>(Of<T>(), 3);
355 }
356
357 /// @return the tint AST type for a 4-element vector of the C type `T`.
358 template <typename T>
359 type::Type* vec4() const {
360 return builder->create<type::Vector>(Of<T>(), 4);
361 }
362
363 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
364 template <typename T>
365 type::Matrix* mat2x2() const {
366 return builder->create<type::Matrix>(Of<T>(), 2, 2);
367 }
368
369 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
370 template <typename T>
371 type::Matrix* mat2x3() const {
372 return builder->create<type::Matrix>(Of<T>(), 3, 2);
373 }
374
375 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
376 template <typename T>
377 type::Matrix* mat2x4() const {
378 return builder->create<type::Matrix>(Of<T>(), 4, 2);
379 }
380
381 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
382 template <typename T>
383 type::Matrix* mat3x2() const {
384 return builder->create<type::Matrix>(Of<T>(), 2, 3);
385 }
386
387 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
388 template <typename T>
389 type::Matrix* mat3x3() const {
390 return builder->create<type::Matrix>(Of<T>(), 3, 3);
391 }
392
393 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
394 template <typename T>
395 type::Matrix* mat3x4() const {
396 return builder->create<type::Matrix>(Of<T>(), 4, 3);
397 }
398
399 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
400 template <typename T>
401 type::Matrix* mat4x2() const {
402 return builder->create<type::Matrix>(Of<T>(), 2, 4);
403 }
404
405 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
406 template <typename T>
407 type::Matrix* mat4x3() const {
408 return builder->create<type::Matrix>(Of<T>(), 3, 4);
409 }
410
411 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
412 template <typename T>
413 type::Matrix* mat4x4() const {
414 return builder->create<type::Matrix>(Of<T>(), 4, 4);
415 }
416
417 /// @param subtype the array element type
418 /// @param n the array size. 0 represents a runtime-array.
419 /// @return the tint AST type for a array of size `n` of type `T`
420 type::Array* array(type::Type* subtype, uint32_t n) const {
421 return builder->create<type::Array>(subtype, n,
422 ast::ArrayDecorationList{});
423 }
424
Ben Claytonbab31972021-02-08 21:16:21 +0000425 /// @param subtype the array element type
426 /// @param n the array size. 0 represents a runtime-array.
427 /// @param stride the array stride.
428 /// @return the tint AST type for a array of size `n` of type `T`
429 type::Array* array(type::Type* subtype, uint32_t n, uint32_t stride) const {
430 return builder->create<type::Array>(
431 subtype, n,
432 ast::ArrayDecorationList{
433 builder->create<ast::StrideDecoration>(stride),
434 });
435 }
436
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000437 /// @return the tint AST type for an array of size `N` of type `T`
438 template <typename T, int N = 0>
439 type::Array* array() const {
440 return array(Of<T>(), N);
441 }
442
Ben Claytonbab31972021-02-08 21:16:21 +0000443 /// @param stride the array stride
444 /// @return the tint AST type for an array of size `N` of type `T`
445 template <typename T, int N = 0>
446 type::Array* array(uint32_t stride) const {
447 return array(Of<T>(), N, stride);
448 }
Ben Clayton42d1e092021-02-02 14:29:15 +0000449 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000450 /// @param name the alias name
451 /// @param type the alias type
452 /// @returns the alias pointer
453 type::Alias* alias(const std::string& name, type::Type* type) const {
454 return builder->create<type::Alias>(builder->Symbols().Register(name),
455 type);
456 }
457
458 /// @return the tint AST pointer to type `T` with the given
459 /// ast::StorageClass.
460 /// @param storage_class the storage class of the pointer
461 template <typename T>
462 type::Pointer* pointer(ast::StorageClass storage_class) const {
463 return builder->create<type::Pointer>(Of<T>(), storage_class);
464 }
465
466 /// @param name the struct name
467 /// @param impl the struct implementation
468 /// @returns a struct pointer
469 type::Struct* struct_(const std::string& name, ast::Struct* impl) const {
470 return builder->create<type::Struct>(builder->Symbols().Register(name),
471 impl);
472 }
473
474 private:
475 /// CToAST<T> is specialized for various `T` types and each specialization
476 /// contains a single static `get()` method for obtaining the corresponding
477 /// AST type for the C type `T`.
478 /// `get()` has the signature:
479 /// `static type::Type* get(Types* t)`
480 template <typename T>
481 struct CToAST {};
482
Ben Claytonc7ca7662021-02-17 16:23:52 +0000483 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000484 };
485
486 //////////////////////////////////////////////////////////////////////////////
487 // AST helper methods
488 //////////////////////////////////////////////////////////////////////////////
489
490 /// @param expr the expression
491 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000492 template <typename T>
493 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
494 return expr;
495 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000496
497 /// @param name the identifier name
498 /// @return an ast::IdentifierExpression with the given name
499 ast::IdentifierExpression* Expr(const std::string& name) {
500 return create<ast::IdentifierExpression>(Symbols().Register(name));
501 }
502
Ben Clayton46d78d72021-02-10 21:34:26 +0000503 /// @param symbol the identifier symbol
504 /// @return an ast::IdentifierExpression with the given symbol
505 ast::IdentifierExpression* Expr(Symbol symbol) {
506 return create<ast::IdentifierExpression>(symbol);
507 }
508
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000509 /// @param source the source information
510 /// @param name the identifier name
511 /// @return an ast::IdentifierExpression with the given name
512 ast::IdentifierExpression* Expr(const Source& source,
513 const std::string& name) {
514 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
515 }
516
517 /// @param name the identifier name
518 /// @return an ast::IdentifierExpression with the given name
519 ast::IdentifierExpression* Expr(const char* name) {
520 return create<ast::IdentifierExpression>(Symbols().Register(name));
521 }
522
523 /// @param value the boolean value
524 /// @return a Scalar constructor for the given value
525 ast::ScalarConstructorExpression* Expr(bool value) {
526 return create<ast::ScalarConstructorExpression>(Literal(value));
527 }
528
529 /// @param value the float value
530 /// @return a Scalar constructor for the given value
531 ast::ScalarConstructorExpression* Expr(f32 value) {
532 return create<ast::ScalarConstructorExpression>(Literal(value));
533 }
534
535 /// @param value the integer value
536 /// @return a Scalar constructor for the given value
537 ast::ScalarConstructorExpression* Expr(i32 value) {
538 return create<ast::ScalarConstructorExpression>(Literal(value));
539 }
540
541 /// @param value the unsigned int value
542 /// @return a Scalar constructor for the given value
543 ast::ScalarConstructorExpression* Expr(u32 value) {
544 return create<ast::ScalarConstructorExpression>(Literal(value));
545 }
546
547 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
548 /// `list`.
549 /// @param list the list to append too
550 /// @param arg the arg to create
551 template <typename ARG>
552 void Append(ast::ExpressionList& list, ARG&& arg) {
553 list.emplace_back(Expr(std::forward<ARG>(arg)));
554 }
555
556 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
557 /// then appends them to `list`.
558 /// @param list the list to append too
559 /// @param arg0 the first argument
560 /// @param args the rest of the arguments
561 template <typename ARG0, typename... ARGS>
562 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
563 Append(list, std::forward<ARG0>(arg0));
564 Append(list, std::forward<ARGS>(args)...);
565 }
566
567 /// @return an empty list of expressions
568 ast::ExpressionList ExprList() { return {}; }
569
570 /// @param args the list of expressions
571 /// @return the list of expressions converted to `ast::Expression`s using
572 /// `Expr()`,
573 template <typename... ARGS>
574 ast::ExpressionList ExprList(ARGS&&... args) {
575 ast::ExpressionList list;
576 list.reserve(sizeof...(args));
577 Append(list, std::forward<ARGS>(args)...);
578 return list;
579 }
580
581 /// @param list the list of expressions
582 /// @return `list`
583 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
584
585 /// @param val the boolan value
586 /// @return a boolean literal with the given value
587 ast::BoolLiteral* Literal(bool val) {
588 return create<ast::BoolLiteral>(ty.bool_(), val);
589 }
590
591 /// @param val the float value
592 /// @return a float literal with the given value
593 ast::FloatLiteral* Literal(f32 val) {
594 return create<ast::FloatLiteral>(ty.f32(), val);
595 }
596
597 /// @param val the unsigned int value
598 /// @return a ast::UintLiteral with the given value
599 ast::UintLiteral* Literal(u32 val) {
600 return create<ast::UintLiteral>(ty.u32(), val);
601 }
602
603 /// @param val the integer value
604 /// @return the ast::SintLiteral with the given value
605 ast::SintLiteral* Literal(i32 val) {
606 return create<ast::SintLiteral>(ty.i32(), val);
607 }
608
609 /// @param args the arguments for the type constructor
610 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
611 /// of `args` converted to `ast::Expression`s using `Expr()`
612 template <typename T, typename... ARGS>
613 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
614 return create<ast::TypeConstructorExpression>(
615 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
616 }
617
618 /// @param type the type to construct
619 /// @param args the arguments for the constructor
620 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
621 /// values `args`.
622 template <typename... ARGS>
623 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
624 return create<ast::TypeConstructorExpression>(
625 type, ExprList(std::forward<ARGS>(args)...));
626 }
627
628 /// @param args the arguments for the vector constructor
629 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
630 /// `T`, constructed with the values `args`.
631 template <typename T, typename... ARGS>
632 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
633 return create<ast::TypeConstructorExpression>(
634 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
635 }
636
637 /// @param args the arguments for the vector constructor
638 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
639 /// `T`, constructed with the values `args`.
640 template <typename T, typename... ARGS>
641 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
642 return create<ast::TypeConstructorExpression>(
643 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
644 }
645
646 /// @param args the arguments for the vector constructor
647 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
648 /// `T`, constructed with the values `args`.
649 template <typename T, typename... ARGS>
650 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
651 return create<ast::TypeConstructorExpression>(
652 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
653 }
654
655 /// @param args the arguments for the matrix constructor
656 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
657 /// `T`, constructed with the values `args`.
658 template <typename T, typename... ARGS>
659 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
660 return create<ast::TypeConstructorExpression>(
661 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
662 }
663
664 /// @param args the arguments for the matrix constructor
665 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
666 /// `T`, constructed with the values `args`.
667 template <typename T, typename... ARGS>
668 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
669 return create<ast::TypeConstructorExpression>(
670 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
671 }
672
673 /// @param args the arguments for the matrix constructor
674 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
675 /// `T`, constructed with the values `args`.
676 template <typename T, typename... ARGS>
677 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
678 return create<ast::TypeConstructorExpression>(
679 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
680 }
681
682 /// @param args the arguments for the matrix constructor
683 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
684 /// `T`, constructed with the values `args`.
685 template <typename T, typename... ARGS>
686 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
687 return create<ast::TypeConstructorExpression>(
688 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
689 }
690
691 /// @param args the arguments for the matrix constructor
692 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
693 /// `T`, constructed with the values `args`.
694 template <typename T, typename... ARGS>
695 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
696 return create<ast::TypeConstructorExpression>(
697 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
698 }
699
700 /// @param args the arguments for the matrix constructor
701 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
702 /// `T`, constructed with the values `args`.
703 template <typename T, typename... ARGS>
704 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
705 return create<ast::TypeConstructorExpression>(
706 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
707 }
708
709 /// @param args the arguments for the matrix constructor
710 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
711 /// `T`, constructed with the values `args`.
712 template <typename T, typename... ARGS>
713 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
714 return create<ast::TypeConstructorExpression>(
715 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
716 }
717
718 /// @param args the arguments for the matrix constructor
719 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
720 /// `T`, constructed with the values `args`.
721 template <typename T, typename... ARGS>
722 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
723 return create<ast::TypeConstructorExpression>(
724 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
725 }
726
727 /// @param args the arguments for the matrix constructor
728 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
729 /// `T`, constructed with the values `args`.
730 template <typename T, typename... ARGS>
731 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
732 return create<ast::TypeConstructorExpression>(
733 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
734 }
735
736 /// @param args the arguments for the array constructor
737 /// @return an `ast::TypeConstructorExpression` of an array with element type
738 /// `T`, constructed with the values `args`.
739 template <typename T, int N = 0, typename... ARGS>
740 ast::TypeConstructorExpression* array(ARGS&&... args) {
741 return create<ast::TypeConstructorExpression>(
742 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
743 }
744
745 /// @param subtype the array element type
746 /// @param n the array size. 0 represents a runtime-array.
747 /// @param args the arguments for the array constructor
748 /// @return an `ast::TypeConstructorExpression` of an array with element type
749 /// `subtype`, constructed with the values `args`.
750 template <typename... ARGS>
751 ast::TypeConstructorExpression* array(type::Type* subtype,
752 uint32_t n,
753 ARGS&&... args) {
754 return create<ast::TypeConstructorExpression>(
755 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
756 }
757
758 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000759 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000760 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000761 /// @param constructor constructor expression
762 /// @param decorations variable decorations
763 /// @returns a `ast::Variable` with the given name, storage and type
764 ast::Variable* Var(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000765 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000766 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000767 ast::Expression* constructor = nullptr,
768 ast::VariableDecorationList decorations = {}) {
769 return create<ast::Variable>(Symbols().Register(name), storage, type, false,
770 constructor, decorations);
771 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000772
773 /// @param source the variable source
774 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000775 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000776 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000777 /// @param constructor constructor expression
778 /// @param decorations variable decorations
779 /// @returns a `ast::Variable` with the given name, storage and type
780 ast::Variable* Var(const Source& source,
781 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000782 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000783 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000784 ast::Expression* constructor = nullptr,
785 ast::VariableDecorationList decorations = {}) {
786 return create<ast::Variable>(source, Symbols().Register(name), storage,
787 type, false, constructor, decorations);
788 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000789
Ben Clayton46d78d72021-02-10 21:34:26 +0000790 /// @param symbol the variable symbol
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000791 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000792 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000793 /// @param constructor constructor expression
794 /// @param decorations variable decorations
795 /// @returns a `ast::Variable` with the given symbol, storage and type
796 ast::Variable* Var(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000797 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000798 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000799 ast::Expression* constructor = nullptr,
800 ast::VariableDecorationList decorations = {}) {
801 return create<ast::Variable>(symbol, storage, type, false, constructor,
802 decorations);
803 }
804
805 /// @param source the variable source
806 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000807 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000808 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000809 /// @param constructor constructor expression
810 /// @param decorations variable decorations
811 /// @returns a `ast::Variable` with the given symbol, storage and type
812 ast::Variable* Var(const Source& source,
813 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000814 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000815 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000816 ast::Expression* constructor = nullptr,
817 ast::VariableDecorationList decorations = {}) {
818 return create<ast::Variable>(source, symbol, storage, type, false,
819 constructor, decorations);
820 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000821
822 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000823 /// @param type the variable type
824 /// @param constructor optional constructor expression
825 /// @param decorations optional variable decorations
826 /// @returns a constant `ast::Variable` with the given name, storage and type
827 ast::Variable* Const(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000828 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000829 ast::Expression* constructor = nullptr,
830 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000831 return create<ast::Variable>(Symbols().Register(name),
832 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000833 constructor, decorations);
834 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000835
836 /// @param source the variable source
837 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000838 /// @param type the variable type
839 /// @param constructor optional constructor expression
840 /// @param decorations optional variable decorations
841 /// @returns a constant `ast::Variable` with the given name, storage and type
842 ast::Variable* Const(const Source& source,
843 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000844 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000845 ast::Expression* constructor = nullptr,
846 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000847 return create<ast::Variable>(source, Symbols().Register(name),
848 ast::StorageClass::kNone, type, true,
849 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000850 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000851
Ben Clayton46d78d72021-02-10 21:34:26 +0000852 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000853 /// @param type the variable type
854 /// @param constructor optional constructor expression
855 /// @param decorations optional variable decorations
856 /// @returns a constant `ast::Variable` with the given symbol, storage and
857 /// type
858 ast::Variable* Const(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000859 type::Type* type,
860 ast::Expression* constructor = nullptr,
861 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000862 return create<ast::Variable>(symbol, ast::StorageClass::kNone, type, true,
863 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000864 }
865
866 /// @param source the variable source
867 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000868 /// @param type the variable type
869 /// @param constructor optional constructor expression
870 /// @param decorations optional variable decorations
871 /// @returns a constant `ast::Variable` with the given symbol, storage and
872 /// type
873 ast::Variable* Const(const Source& source,
874 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000875 type::Type* type,
876 ast::Expression* constructor = nullptr,
877 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000878 return create<ast::Variable>(source, symbol, ast::StorageClass::kNone, type,
879 true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000880 }
Ben Clayton37571bc2021-02-16 23:57:01 +0000881
Ben Clayton401b96b2021-02-03 17:19:59 +0000882 /// @param args the arguments to pass to Var()
883 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
884 /// of `args`, which is automatically registered as a global variable with the
885 /// ast::Module.
886 template <typename... ARGS>
887 ast::Variable* Global(ARGS&&... args) {
888 auto* var = Var(std::forward<ARGS>(args)...);
889 AST().AddGlobalVariable(var);
890 return var;
891 }
892
893 /// @param args the arguments to pass to Const()
894 /// @returns a const `ast::Variable` constructed by calling Var() with the
895 /// arguments of `args`, which is automatically registered as a global
896 /// variable with the ast::Module.
897 template <typename... ARGS>
898 ast::Variable* GlobalConst(ARGS&&... args) {
899 auto* var = Const(std::forward<ARGS>(args)...);
900 AST().AddGlobalVariable(var);
901 return var;
902 }
903
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000904 /// @param func the function name
905 /// @param args the function call arguments
906 /// @returns a `ast::CallExpression` to the function `func`, with the
907 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
908 template <typename NAME, typename... ARGS>
909 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
910 return create<ast::CallExpression>(Expr(func),
911 ExprList(std::forward<ARGS>(args)...));
912 }
913
914 /// @param lhs the left hand argument to the addition operation
915 /// @param rhs the right hand argument to the addition operation
916 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
917 template <typename LHS, typename RHS>
918 ast::Expression* Add(LHS&& lhs, RHS&& rhs) {
919 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
920 Expr(std::forward<LHS>(lhs)),
921 Expr(std::forward<RHS>(rhs)));
922 }
923
924 /// @param lhs the left hand argument to the subtraction operation
925 /// @param rhs the right hand argument to the subtraction operation
926 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
927 template <typename LHS, typename RHS>
928 ast::Expression* Sub(LHS&& lhs, RHS&& rhs) {
929 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
930 Expr(std::forward<LHS>(lhs)),
931 Expr(std::forward<RHS>(rhs)));
932 }
933
934 /// @param lhs the left hand argument to the multiplication operation
935 /// @param rhs the right hand argument to the multiplication operation
936 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
937 template <typename LHS, typename RHS>
938 ast::Expression* Mul(LHS&& lhs, RHS&& rhs) {
939 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
940 Expr(std::forward<LHS>(lhs)),
941 Expr(std::forward<RHS>(rhs)));
942 }
943
944 /// @param arr the array argument for the array accessor expression
945 /// @param idx the index argument for the array accessor expression
946 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
947 template <typename ARR, typename IDX>
948 ast::Expression* IndexAccessor(ARR&& arr, IDX&& idx) {
949 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
950 Expr(std::forward<IDX>(idx)));
951 }
952
953 /// @param obj the object for the member accessor expression
954 /// @param idx the index argument for the array accessor expression
955 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
956 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +0000957 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000958 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
959 Expr(std::forward<IDX>(idx)));
960 }
961
Ben Clayton42d1e092021-02-02 14:29:15 +0000962 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000963 /// @param val the offset value
964 /// @returns the offset decoration pointer
965 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
966 return create<ast::StructMemberOffsetDecoration>(source_, val);
967 }
968
Ben Clayton42d1e092021-02-02 14:29:15 +0000969 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000970 /// @param source the source information
971 /// @param name the function name
972 /// @param params the function parameters
973 /// @param type the function return type
974 /// @param body the function body
975 /// @param decorations the function decorations
976 /// @returns the function pointer
977 ast::Function* Func(Source source,
978 std::string name,
979 ast::VariableList params,
980 type::Type* type,
981 ast::StatementList body,
982 ast::FunctionDecorationList decorations) {
Ben Clayton42d1e092021-02-02 14:29:15 +0000983 auto* func =
984 create<ast::Function>(source, Symbols().Register(name), params, type,
985 create<ast::BlockStatement>(body), decorations);
James Price3eaa4502021-02-09 21:26:21 +0000986 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +0000987 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000988 }
989
Ben Clayton42d1e092021-02-02 14:29:15 +0000990 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000991 /// @param name the function name
992 /// @param params the function parameters
993 /// @param type the function return type
994 /// @param body the function body
995 /// @param decorations the function decorations
996 /// @returns the function pointer
997 ast::Function* Func(std::string name,
998 ast::VariableList params,
999 type::Type* type,
1000 ast::StatementList body,
1001 ast::FunctionDecorationList decorations) {
Ben Clayton42d1e092021-02-02 14:29:15 +00001002 auto* func =
1003 create<ast::Function>(Symbols().Register(name), params, type,
1004 create<ast::BlockStatement>(body), decorations);
James Price3eaa4502021-02-09 21:26:21 +00001005 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001006 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001007 }
1008
Ben Clayton42d1e092021-02-02 14:29:15 +00001009 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001010 /// @param source the source information
1011 /// @param name the struct member name
1012 /// @param type the struct member type
1013 /// @returns the struct member pointer
1014 ast::StructMember* Member(const Source& source,
1015 const std::string& name,
1016 type::Type* type) {
1017 return create<ast::StructMember>(source, Symbols().Register(name), type,
1018 ast::StructMemberDecorationList{});
1019 }
1020
Ben Clayton42d1e092021-02-02 14:29:15 +00001021 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001022 /// @param name the struct member name
1023 /// @param type the struct member type
1024 /// @returns the struct member pointer
1025 ast::StructMember* Member(const std::string& name, type::Type* type) {
1026 return create<ast::StructMember>(source_, Symbols().Register(name), type,
1027 ast::StructMemberDecorationList{});
1028 }
1029
Ben Clayton42d1e092021-02-02 14:29:15 +00001030 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001031 /// @param name the struct member name
1032 /// @param type the struct member type
1033 /// @param decorations the struct member decorations
1034 /// @returns the struct member pointer
1035 ast::StructMember* Member(const std::string& name,
1036 type::Type* type,
1037 ast::StructMemberDecorationList decorations) {
1038 return create<ast::StructMember>(source_, Symbols().Register(name), type,
1039 decorations);
1040 }
1041
Ben Claytonbab31972021-02-08 21:16:21 +00001042 /// Creates a ast::StructMember with the given byte offset
1043 /// @param offset the offset to use in the StructMemberOffsetDecoration
1044 /// @param name the struct member name
1045 /// @param type the struct member type
1046 /// @returns the struct member pointer
1047 ast::StructMember* Member(uint32_t offset,
1048 const std::string& name,
1049 type::Type* type) {
1050 return create<ast::StructMember>(
1051 source_, Symbols().Register(name), type,
1052 ast::StructMemberDecorationList{
1053 create<ast::StructMemberOffsetDecoration>(offset),
1054 });
1055 }
1056
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001057 /// Creates a ast::BlockStatement with input statements
1058 /// @param statements statements of block
1059 /// @returns the block statement pointer
1060 template <typename... Statements>
1061 ast::BlockStatement* Block(Statements&&... statements) {
1062 return create<ast::BlockStatement>(
1063 ast::StatementList{std::forward<Statements>(statements)...});
1064 }
1065
1066 /// Creates a ast::ElseStatement with input condition and body
1067 /// @param condition the else condition expression
1068 /// @param body the else body
1069 /// @returns the else statement pointer
1070 ast::ElseStatement* Else(ast::Expression* condition,
1071 ast::BlockStatement* body) {
1072 return create<ast::ElseStatement>(condition, body);
1073 }
1074
1075 /// Creates a ast::IfStatement with input condition, body, and optional
1076 /// variadic else statements
1077 /// @param condition the if statement condition expression
1078 /// @param body the if statement body
1079 /// @param elseStatements optional variadic else statements
1080 /// @returns the if statement pointer
1081 template <typename... ElseStatements>
1082 ast::IfStatement* If(ast::Expression* condition,
1083 ast::BlockStatement* body,
1084 ElseStatements&&... elseStatements) {
1085 return create<ast::IfStatement>(
1086 condition, body,
1087 ast::ElseStatementList{
1088 std::forward<ElseStatements>(elseStatements)...});
1089 }
1090
1091 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
1092 /// @param lhs the left hand side expression
1093 /// @param rhs the right hand side expression
1094 /// @returns the assignment statement pointer
1095 ast::AssignmentStatement* Assign(ast::Expression* lhs, ast::Expression* rhs) {
1096 return create<ast::AssignmentStatement>(lhs, rhs);
1097 }
1098
1099 /// Creates a ast::LoopStatement with input body and optional continuing
1100 /// @param body the loop body
1101 /// @param continuing the optional continuing block
1102 /// @returns the loop statement pointer
1103 ast::LoopStatement* Loop(ast::BlockStatement* body,
1104 ast::BlockStatement* continuing = nullptr) {
1105 return create<ast::LoopStatement>(body, continuing);
1106 }
1107
1108 /// Creates a ast::VariableDeclStatement for the input variable
1109 /// @param var the variable to wrap in a decl statement
1110 /// @returns the variable decl statement pointer
1111 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1112 return create<ast::VariableDeclStatement>(var);
1113 }
1114
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001115 /// Sets the current builder source to `src`
1116 /// @param src the Source used for future create() calls
1117 void SetSource(const Source& src) {
1118 AssertNotMoved();
1119 source_ = src;
1120 }
1121
1122 /// Sets the current builder source to `loc`
1123 /// @param loc the Source used for future create() calls
1124 void SetSource(const Source::Location& loc) {
1125 AssertNotMoved();
1126 source_ = Source(loc);
1127 }
1128
Ben Clayton33352542021-01-29 16:43:41 +00001129 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001130 /// @note As the Resolver is run when the Program is built, this will only be
1131 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001132 /// @param expr the AST expression
1133 /// @return the resolved semantic type for the expression, or nullptr if the
1134 /// expression has no resolved type.
1135 type::Type* TypeOf(ast::Expression* expr) const;
1136
Ben Clayton401b96b2021-02-03 17:19:59 +00001137 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001138 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001139 /// nodes.
1140 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1141 /// @return the ast::Statement that wraps the ast::Expression
1142 ast::Statement* WrapInStatement(ast::Expression* expr);
1143 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001144 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001145 /// these nodes.
1146 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1147 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1148 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1149 /// Returns the statement argument. Used as a passthrough-overload by
1150 /// WrapInFunction().
1151 /// @param stmt the ast::Statement
1152 /// @return `stmt`
1153 ast::Statement* WrapInStatement(ast::Statement* stmt);
1154 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001155 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001156 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
1157 template <typename... ARGS>
1158 void WrapInFunction(ARGS&&... args) {
1159 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
1160 WrapInFunction(stmts);
1161 }
1162 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001163 /// so that each statement is reachable by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001164 void WrapInFunction(ast::StatementList stmts);
1165
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001166 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001167 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001168
1169 protected:
1170 /// Asserts that the builder has not been moved.
1171 void AssertNotMoved() const;
1172
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001173 private:
1174 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001175 ASTNodeAllocator ast_nodes_;
1176 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001177 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001178 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001179 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001180 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001181
1182 /// The source to use when creating AST nodes without providing a Source as
1183 /// the first argument.
1184 Source source_;
1185
Ben Clayton5f0ea112021-03-09 10:54:37 +00001186 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001187 /// program when built.
1188 bool resolve_on_build_ = true;
1189
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001190 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1191 bool moved_ = false;
1192};
1193
1194//! @cond Doxygen_Suppress
1195// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1196template <>
1197struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1198 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1199 return t->i32();
1200 }
1201};
1202template <>
1203struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1204 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1205 return t->u32();
1206 }
1207};
1208template <>
1209struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1210 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1211 return t->f32();
1212 }
1213};
1214template <>
1215struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1216 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1217 return t->bool_();
1218 }
1219};
1220template <>
1221struct ProgramBuilder::TypesBuilder::CToAST<void> {
1222 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1223 return t->void_();
1224 }
1225};
1226//! @endcond
1227
1228} // namespace tint
1229
1230#endif // SRC_PROGRAM_BUILDER_H_