blob: 39832a213c6e111a108aa559f298b57bfaf62157 [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"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000026#include "src/ast/float_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000027#include "src/ast/if_statement.h"
28#include "src/ast/loop_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000029#include "src/ast/member_accessor_expression.h"
30#include "src/ast/module.h"
31#include "src/ast/scalar_constructor_expression.h"
32#include "src/ast/sint_literal.h"
Ben Claytonbab31972021-02-08 21:16:21 +000033#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000034#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000035#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000036#include "src/ast/struct_member_size_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000037#include "src/ast/type_constructor_expression.h"
38#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000039#include "src/ast/variable_decl_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000040#include "src/program.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000041#include "src/type/alias_type.h"
42#include "src/type/array_type.h"
43#include "src/type/bool_type.h"
44#include "src/type/f32_type.h"
45#include "src/type/i32_type.h"
46#include "src/type/matrix_type.h"
47#include "src/type/pointer_type.h"
48#include "src/type/struct_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000049#include "src/type/u32_type.h"
50#include "src/type/vector_type.h"
51#include "src/type/void_type.h"
52
53namespace tint {
54
Ben Clayton401b96b2021-02-03 17:19:59 +000055// Forward declarations
56namespace ast {
57class VariableDeclStatement;
58} // namespace ast
59
Ben Claytona6b9a8e2021-01-26 16:57:10 +000060class CloneContext;
61
62/// ProgramBuilder is a mutable builder for a Program.
63/// To construct a Program, populate the builder and then `std::move` it to a
64/// Program.
65class ProgramBuilder {
66 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000067 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
68 using ASTNodeAllocator = BlockAllocator<ast::Node>;
69
70 /// SemNodeAllocator is an alias to BlockAllocator<semantic::Node>
71 using SemNodeAllocator = BlockAllocator<semantic::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000072
73 /// `i32` is a type alias to `int`.
74 /// Useful for passing to template methods such as `vec2<i32>()` 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 i32 = decltype(1);
79 /// `u32` is a type alias to `unsigned int`.
80 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
81 /// WGSL syntax.
82 /// Note: this is intentionally not aliased to uint32_t as we want integer
83 /// literals passed to the builder to match WGSL's integer literal types.
84 using u32 = decltype(1u);
85 /// `f32` is a type alias to `float`
86 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
87 /// WGSL syntax.
88 using f32 = float;
89
90 /// Constructor
91 ProgramBuilder();
92
93 /// Move constructor
94 /// @param rhs the builder to move
95 ProgramBuilder(ProgramBuilder&& rhs);
96
97 /// Destructor
98 virtual ~ProgramBuilder();
99
100 /// Move assignment operator
101 /// @param rhs the builder to move
102 /// @return this builder
103 ProgramBuilder& operator=(ProgramBuilder&& rhs);
104
Ben Claytone43c8302021-01-29 11:59:32 +0000105 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
106 /// making a deep clone of the Program contents.
107 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
108 /// existing immutable program.
109 /// As the returned ProgramBuilder wraps `program`, `program` must not be
110 /// destructed or assigned while using the returned ProgramBuilder.
111 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
112 /// function. See crbug.com/tint/460.
113 /// @param program the immutable Program to wrap
114 /// @return the ProgramBuilder that wraps `program`
115 static ProgramBuilder Wrap(const Program* program);
116
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000117 /// @returns a reference to the program's types
118 type::Manager& Types() {
119 AssertNotMoved();
120 return types_;
121 }
122
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000123 /// @returns a reference to the program's types
124 const type::Manager& Types() const {
125 AssertNotMoved();
126 return types_;
127 }
128
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000129 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000130 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000131 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000132 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000133 }
134
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000135 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000136 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000137 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000138 return ast_nodes_;
139 }
140
141 /// @returns a reference to the program's semantic nodes storage
142 SemNodeAllocator& SemNodes() {
143 AssertNotMoved();
144 return sem_nodes_;
145 }
146
147 /// @returns a reference to the program's semantic nodes storage
148 const SemNodeAllocator& SemNodes() const {
149 AssertNotMoved();
150 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000151 }
152
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000153 /// @returns a reference to the program's AST root Module
154 ast::Module& AST() {
155 AssertNotMoved();
156 return *ast_;
157 }
158
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000159 /// @returns a reference to the program's AST root Module
160 const ast::Module& AST() const {
161 AssertNotMoved();
162 return *ast_;
163 }
164
165 /// @returns a reference to the program's semantic info
166 semantic::Info& Sem() {
167 AssertNotMoved();
168 return sem_;
169 }
170
171 /// @returns a reference to the program's semantic info
172 const semantic::Info& Sem() const {
173 AssertNotMoved();
174 return sem_;
175 }
176
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000177 /// @returns a reference to the program's SymbolTable
178 SymbolTable& Symbols() {
179 AssertNotMoved();
180 return symbols_;
181 }
182
Ben Clayton708dc2d2021-01-29 11:22:40 +0000183 /// @returns a reference to the program's SymbolTable
184 const SymbolTable& Symbols() const {
185 AssertNotMoved();
186 return symbols_;
187 }
188
Ben Clayton844217f2021-01-27 18:49:05 +0000189 /// @returns a reference to the program's diagnostics
190 diag::List& Diagnostics() {
191 AssertNotMoved();
192 return diagnostics_;
193 }
194
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000195 /// @returns a reference to the program's diagnostics
196 const diag::List& Diagnostics() const {
197 AssertNotMoved();
198 return diagnostics_;
199 }
200
Ben Clayton5f0ea112021-03-09 10:54:37 +0000201 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000202 /// @param enable the new flag value (defaults to true)
203 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
204
Ben Clayton5f0ea112021-03-09 10:54:37 +0000205 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000206 /// built.
207 bool ResolveOnBuild() const { return resolve_on_build_; }
208
Ben Clayton844217f2021-01-27 18:49:05 +0000209 /// @returns true if the program has no error diagnostics and is not missing
210 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000211 bool IsValid() const;
212
Ben Clayton708dc2d2021-01-29 11:22:40 +0000213 /// Writes a representation of the node to the output stream
214 /// @note unlike str(), to_str() does not automatically demangle the string.
215 /// @param node the AST node
216 /// @param out the stream to write to
217 /// @param indent number of spaces to indent the node when writing
218 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
219 node->to_str(Sem(), out, indent);
220 }
221
222 /// Returns a demangled, string representation of `node`.
223 /// @param node the AST node
224 /// @returns a string representation of the node
225 std::string str(const ast::Node* node) const;
226
Ben Clayton7fdfff12021-01-29 15:17:30 +0000227 /// Creates a new ast::Node owned by the ProgramBuilder. When the
228 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000229 /// @param source the Source of the node
230 /// @param args the arguments to pass to the type constructor
231 /// @returns the node pointer
232 template <typename T, typename... ARGS>
233 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
234 ARGS&&... args) {
235 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000236 return ast_nodes_.Create<T>(source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000237 }
238
Ben Clayton7fdfff12021-01-29 15:17:30 +0000239 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
240 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000241 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000242 /// When the ProgramBuilder is destructed, the ast::Node will also be
243 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000244 /// @returns the node pointer
245 template <typename T>
246 traits::EnableIfIsType<T, ast::Node>* create() {
247 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000248 return ast_nodes_.Create<T>(source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000249 }
250
Ben Clayton7fdfff12021-01-29 15:17:30 +0000251 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
252 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000253 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000254 /// When the ProgramBuilder is destructed, the ast::Node will also be
255 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000256 /// @param arg0 the first arguments to pass to the type constructor
257 /// @param args the remaining arguments to pass to the type constructor
258 /// @returns the node pointer
259 template <typename T, typename ARG0, typename... ARGS>
260 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
261 traits::IsTypeOrDerived<T, ast::Node>::value &&
262 !traits::IsTypeOrDerived<ARG0, Source>::value,
263 T>*
264 create(ARG0&& arg0, ARGS&&... args) {
265 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000266 return ast_nodes_.Create<T>(source_, std::forward<ARG0>(arg0),
267 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000268 }
269
Ben Clayton7fdfff12021-01-29 15:17:30 +0000270 /// Creates a new semantic::Node owned by the ProgramBuilder.
271 /// When the ProgramBuilder is destructed, the semantic::Node will also be
272 /// destructed.
273 /// @param args the arguments to pass to the type constructor
274 /// @returns the node pointer
275 template <typename T, typename... ARGS>
276 traits::EnableIfIsType<T, semantic::Node>* create(ARGS&&... args) {
277 AssertNotMoved();
278 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
279 }
280
281 /// Creates a new type::Type owned by the ProgramBuilder.
282 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
283 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000284 /// Types are unique (de-aliased), and so calling create() for the same `T`
285 /// and arguments will return the same pointer.
286 /// @warning Use this method to acquire a type only if all of its type
287 /// information is provided in the constructor arguments `args`.<br>
288 /// If the type requires additional configuration after construction that
289 /// affect its fundamental type, build the type with `std::make_unique`, make
290 /// any necessary alterations and then call unique_type() instead.
291 /// @param args the arguments to pass to the type constructor
292 /// @returns the de-aliased type pointer
293 template <typename T, typename... ARGS>
294 traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) {
295 static_assert(std::is_base_of<type::Type, T>::value,
296 "T does not derive from type::Type");
297 AssertNotMoved();
298 return types_.Get<T>(std::forward<ARGS>(args)...);
299 }
300
301 /// Marks this builder as moved, preventing any further use of the builder.
302 void MarkAsMoved();
303
304 //////////////////////////////////////////////////////////////////////////////
305 // TypesBuilder
306 //////////////////////////////////////////////////////////////////////////////
307
308 /// TypesBuilder holds basic `tint` types and methods for constructing
309 /// complex types.
310 class TypesBuilder {
311 public:
312 /// Constructor
313 /// @param builder the program builder
314 explicit TypesBuilder(ProgramBuilder* builder);
315
316 /// @return the tint AST type for the C type `T`.
317 template <typename T>
318 type::Type* Of() const {
319 return CToAST<T>::get(this);
320 }
321
322 /// @returns a boolean type
323 type::Bool* bool_() const { return builder->create<type::Bool>(); }
324
325 /// @returns a f32 type
326 type::F32* f32() const { return builder->create<type::F32>(); }
327
328 /// @returns a i32 type
329 type::I32* i32() const { return builder->create<type::I32>(); }
330
331 /// @returns a u32 type
332 type::U32* u32() const { return builder->create<type::U32>(); }
333
334 /// @returns a void type
335 type::Void* void_() const { return builder->create<type::Void>(); }
336
337 /// @return the tint AST type for a 2-element vector of the C type `T`.
338 template <typename T>
339 type::Vector* vec2() const {
340 return builder->create<type::Vector>(Of<T>(), 2);
341 }
342
343 /// @return the tint AST type for a 3-element vector of the C type `T`.
344 template <typename T>
345 type::Vector* vec3() const {
346 return builder->create<type::Vector>(Of<T>(), 3);
347 }
348
349 /// @return the tint AST type for a 4-element vector of the C type `T`.
350 template <typename T>
351 type::Type* vec4() const {
352 return builder->create<type::Vector>(Of<T>(), 4);
353 }
354
355 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
356 template <typename T>
357 type::Matrix* mat2x2() const {
358 return builder->create<type::Matrix>(Of<T>(), 2, 2);
359 }
360
361 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
362 template <typename T>
363 type::Matrix* mat2x3() const {
364 return builder->create<type::Matrix>(Of<T>(), 3, 2);
365 }
366
367 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
368 template <typename T>
369 type::Matrix* mat2x4() const {
370 return builder->create<type::Matrix>(Of<T>(), 4, 2);
371 }
372
373 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
374 template <typename T>
375 type::Matrix* mat3x2() const {
376 return builder->create<type::Matrix>(Of<T>(), 2, 3);
377 }
378
379 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
380 template <typename T>
381 type::Matrix* mat3x3() const {
382 return builder->create<type::Matrix>(Of<T>(), 3, 3);
383 }
384
385 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
386 template <typename T>
387 type::Matrix* mat3x4() const {
388 return builder->create<type::Matrix>(Of<T>(), 4, 3);
389 }
390
391 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
392 template <typename T>
393 type::Matrix* mat4x2() const {
394 return builder->create<type::Matrix>(Of<T>(), 2, 4);
395 }
396
397 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
398 template <typename T>
399 type::Matrix* mat4x3() const {
400 return builder->create<type::Matrix>(Of<T>(), 3, 4);
401 }
402
403 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
404 template <typename T>
405 type::Matrix* mat4x4() const {
406 return builder->create<type::Matrix>(Of<T>(), 4, 4);
407 }
408
409 /// @param subtype the array element type
410 /// @param n the array size. 0 represents a runtime-array.
411 /// @return the tint AST type for a array of size `n` of type `T`
412 type::Array* array(type::Type* subtype, uint32_t n) const {
James Price95d40772021-03-11 17:39:32 +0000413 return builder->create<type::Array>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000414 }
415
Ben Claytonbab31972021-02-08 21:16:21 +0000416 /// @param subtype the array element type
417 /// @param n the array size. 0 represents a runtime-array.
418 /// @param stride the array stride.
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, uint32_t stride) const {
421 return builder->create<type::Array>(
422 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000423 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000424 builder->create<ast::StrideDecoration>(stride),
425 });
426 }
427
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000428 /// @return the tint AST type for an array of size `N` of type `T`
429 template <typename T, int N = 0>
430 type::Array* array() const {
431 return array(Of<T>(), N);
432 }
433
Ben Claytonbab31972021-02-08 21:16:21 +0000434 /// @param stride the array stride
435 /// @return the tint AST type for an array of size `N` of type `T`
436 template <typename T, int N = 0>
437 type::Array* array(uint32_t stride) const {
438 return array(Of<T>(), N, stride);
439 }
Ben Clayton42d1e092021-02-02 14:29:15 +0000440 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000441 /// @param name the alias name
442 /// @param type the alias type
443 /// @returns the alias pointer
444 type::Alias* alias(const std::string& name, type::Type* type) const {
445 return builder->create<type::Alias>(builder->Symbols().Register(name),
446 type);
447 }
448
449 /// @return the tint AST pointer to type `T` with the given
450 /// ast::StorageClass.
451 /// @param storage_class the storage class of the pointer
452 template <typename T>
453 type::Pointer* pointer(ast::StorageClass storage_class) const {
454 return builder->create<type::Pointer>(Of<T>(), storage_class);
455 }
456
457 /// @param name the struct name
458 /// @param impl the struct implementation
459 /// @returns a struct pointer
460 type::Struct* struct_(const std::string& name, ast::Struct* impl) const {
461 return builder->create<type::Struct>(builder->Symbols().Register(name),
462 impl);
463 }
464
465 private:
466 /// CToAST<T> is specialized for various `T` types and each specialization
467 /// contains a single static `get()` method for obtaining the corresponding
468 /// AST type for the C type `T`.
469 /// `get()` has the signature:
470 /// `static type::Type* get(Types* t)`
471 template <typename T>
472 struct CToAST {};
473
Ben Claytonc7ca7662021-02-17 16:23:52 +0000474 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000475 };
476
477 //////////////////////////////////////////////////////////////////////////////
478 // AST helper methods
479 //////////////////////////////////////////////////////////////////////////////
480
481 /// @param expr the expression
482 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000483 template <typename T>
484 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
485 return expr;
486 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000487
488 /// @param name the identifier name
489 /// @return an ast::IdentifierExpression with the given name
490 ast::IdentifierExpression* Expr(const std::string& name) {
491 return create<ast::IdentifierExpression>(Symbols().Register(name));
492 }
493
Ben Clayton46d78d72021-02-10 21:34:26 +0000494 /// @param symbol the identifier symbol
495 /// @return an ast::IdentifierExpression with the given symbol
496 ast::IdentifierExpression* Expr(Symbol symbol) {
497 return create<ast::IdentifierExpression>(symbol);
498 }
499
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000500 /// @param source the source information
501 /// @param name the identifier name
502 /// @return an ast::IdentifierExpression with the given name
503 ast::IdentifierExpression* Expr(const Source& source,
504 const std::string& name) {
505 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
506 }
507
508 /// @param name the identifier name
509 /// @return an ast::IdentifierExpression with the given name
510 ast::IdentifierExpression* Expr(const char* name) {
511 return create<ast::IdentifierExpression>(Symbols().Register(name));
512 }
513
514 /// @param value the boolean value
515 /// @return a Scalar constructor for the given value
516 ast::ScalarConstructorExpression* Expr(bool value) {
517 return create<ast::ScalarConstructorExpression>(Literal(value));
518 }
519
520 /// @param value the float value
521 /// @return a Scalar constructor for the given value
522 ast::ScalarConstructorExpression* Expr(f32 value) {
523 return create<ast::ScalarConstructorExpression>(Literal(value));
524 }
525
526 /// @param value the integer value
527 /// @return a Scalar constructor for the given value
528 ast::ScalarConstructorExpression* Expr(i32 value) {
529 return create<ast::ScalarConstructorExpression>(Literal(value));
530 }
531
532 /// @param value the unsigned int value
533 /// @return a Scalar constructor for the given value
534 ast::ScalarConstructorExpression* Expr(u32 value) {
535 return create<ast::ScalarConstructorExpression>(Literal(value));
536 }
537
538 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
539 /// `list`.
540 /// @param list the list to append too
541 /// @param arg the arg to create
542 template <typename ARG>
543 void Append(ast::ExpressionList& list, ARG&& arg) {
544 list.emplace_back(Expr(std::forward<ARG>(arg)));
545 }
546
547 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
548 /// then appends them to `list`.
549 /// @param list the list to append too
550 /// @param arg0 the first argument
551 /// @param args the rest of the arguments
552 template <typename ARG0, typename... ARGS>
553 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
554 Append(list, std::forward<ARG0>(arg0));
555 Append(list, std::forward<ARGS>(args)...);
556 }
557
558 /// @return an empty list of expressions
559 ast::ExpressionList ExprList() { return {}; }
560
561 /// @param args the list of expressions
562 /// @return the list of expressions converted to `ast::Expression`s using
563 /// `Expr()`,
564 template <typename... ARGS>
565 ast::ExpressionList ExprList(ARGS&&... args) {
566 ast::ExpressionList list;
567 list.reserve(sizeof...(args));
568 Append(list, std::forward<ARGS>(args)...);
569 return list;
570 }
571
572 /// @param list the list of expressions
573 /// @return `list`
574 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
575
576 /// @param val the boolan value
577 /// @return a boolean literal with the given value
578 ast::BoolLiteral* Literal(bool val) {
579 return create<ast::BoolLiteral>(ty.bool_(), val);
580 }
581
582 /// @param val the float value
583 /// @return a float literal with the given value
584 ast::FloatLiteral* Literal(f32 val) {
585 return create<ast::FloatLiteral>(ty.f32(), val);
586 }
587
588 /// @param val the unsigned int value
589 /// @return a ast::UintLiteral with the given value
590 ast::UintLiteral* Literal(u32 val) {
591 return create<ast::UintLiteral>(ty.u32(), val);
592 }
593
594 /// @param val the integer value
595 /// @return the ast::SintLiteral with the given value
596 ast::SintLiteral* Literal(i32 val) {
597 return create<ast::SintLiteral>(ty.i32(), val);
598 }
599
600 /// @param args the arguments for the type constructor
601 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
602 /// of `args` converted to `ast::Expression`s using `Expr()`
603 template <typename T, typename... ARGS>
604 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
605 return create<ast::TypeConstructorExpression>(
606 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
607 }
608
609 /// @param type the type to construct
610 /// @param args the arguments for the constructor
611 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
612 /// values `args`.
613 template <typename... ARGS>
614 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
615 return create<ast::TypeConstructorExpression>(
616 type, ExprList(std::forward<ARGS>(args)...));
617 }
618
619 /// @param args the arguments for the vector constructor
620 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
621 /// `T`, constructed with the values `args`.
622 template <typename T, typename... ARGS>
623 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
624 return create<ast::TypeConstructorExpression>(
625 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
626 }
627
628 /// @param args the arguments for the vector constructor
629 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
630 /// `T`, constructed with the values `args`.
631 template <typename T, typename... ARGS>
632 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
633 return create<ast::TypeConstructorExpression>(
634 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
635 }
636
637 /// @param args the arguments for the vector constructor
638 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
639 /// `T`, constructed with the values `args`.
640 template <typename T, typename... ARGS>
641 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
642 return create<ast::TypeConstructorExpression>(
643 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
644 }
645
646 /// @param args the arguments for the matrix constructor
647 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
648 /// `T`, constructed with the values `args`.
649 template <typename T, typename... ARGS>
650 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
651 return create<ast::TypeConstructorExpression>(
652 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
653 }
654
655 /// @param args the arguments for the matrix constructor
656 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
657 /// `T`, constructed with the values `args`.
658 template <typename T, typename... ARGS>
659 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
660 return create<ast::TypeConstructorExpression>(
661 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
662 }
663
664 /// @param args the arguments for the matrix constructor
665 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
666 /// `T`, constructed with the values `args`.
667 template <typename T, typename... ARGS>
668 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
669 return create<ast::TypeConstructorExpression>(
670 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
671 }
672
673 /// @param args the arguments for the matrix constructor
674 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
675 /// `T`, constructed with the values `args`.
676 template <typename T, typename... ARGS>
677 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
678 return create<ast::TypeConstructorExpression>(
679 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
680 }
681
682 /// @param args the arguments for the matrix constructor
683 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
684 /// `T`, constructed with the values `args`.
685 template <typename T, typename... ARGS>
686 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
687 return create<ast::TypeConstructorExpression>(
688 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
689 }
690
691 /// @param args the arguments for the matrix constructor
692 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
693 /// `T`, constructed with the values `args`.
694 template <typename T, typename... ARGS>
695 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
696 return create<ast::TypeConstructorExpression>(
697 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
698 }
699
700 /// @param args the arguments for the matrix constructor
701 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
702 /// `T`, constructed with the values `args`.
703 template <typename T, typename... ARGS>
704 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
705 return create<ast::TypeConstructorExpression>(
706 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
707 }
708
709 /// @param args the arguments for the matrix constructor
710 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
711 /// `T`, constructed with the values `args`.
712 template <typename T, typename... ARGS>
713 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
714 return create<ast::TypeConstructorExpression>(
715 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
716 }
717
718 /// @param args the arguments for the matrix constructor
719 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
720 /// `T`, constructed with the values `args`.
721 template <typename T, typename... ARGS>
722 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
723 return create<ast::TypeConstructorExpression>(
724 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
725 }
726
727 /// @param args the arguments for the array constructor
728 /// @return an `ast::TypeConstructorExpression` of an array with element type
729 /// `T`, constructed with the values `args`.
730 template <typename T, int N = 0, typename... ARGS>
731 ast::TypeConstructorExpression* array(ARGS&&... args) {
732 return create<ast::TypeConstructorExpression>(
733 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
734 }
735
736 /// @param subtype the array element type
737 /// @param n the array size. 0 represents a runtime-array.
738 /// @param args the arguments for the array constructor
739 /// @return an `ast::TypeConstructorExpression` of an array with element type
740 /// `subtype`, constructed with the values `args`.
741 template <typename... ARGS>
742 ast::TypeConstructorExpression* array(type::Type* subtype,
743 uint32_t n,
744 ARGS&&... args) {
745 return create<ast::TypeConstructorExpression>(
746 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
747 }
748
749 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000750 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000751 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000752 /// @param constructor constructor expression
753 /// @param decorations variable decorations
754 /// @returns a `ast::Variable` with the given name, storage and type
755 ast::Variable* Var(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000756 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000757 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000758 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000759 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000760 return create<ast::Variable>(Symbols().Register(name), storage, type, false,
761 constructor, decorations);
762 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000763
764 /// @param source the variable source
765 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000766 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000767 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000768 /// @param constructor constructor expression
769 /// @param decorations variable decorations
770 /// @returns a `ast::Variable` with the given name, storage and type
771 ast::Variable* Var(const Source& source,
772 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000773 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000774 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000775 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000776 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000777 return create<ast::Variable>(source, Symbols().Register(name), storage,
778 type, false, constructor, decorations);
779 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000780
Ben Clayton46d78d72021-02-10 21:34:26 +0000781 /// @param symbol the variable symbol
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000782 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000783 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000784 /// @param constructor constructor expression
785 /// @param decorations variable decorations
786 /// @returns a `ast::Variable` with the given symbol, storage and type
787 ast::Variable* Var(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000788 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000789 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000790 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000791 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000792 return create<ast::Variable>(symbol, storage, type, false, constructor,
793 decorations);
794 }
795
796 /// @param source the variable source
797 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000798 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000799 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000800 /// @param constructor constructor expression
801 /// @param decorations variable decorations
802 /// @returns a `ast::Variable` with the given symbol, storage and type
803 ast::Variable* Var(const Source& source,
804 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000805 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000806 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000807 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000808 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000809 return create<ast::Variable>(source, symbol, storage, type, false,
810 constructor, decorations);
811 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000812
813 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000814 /// @param type the variable type
815 /// @param constructor optional constructor expression
816 /// @param decorations optional variable decorations
817 /// @returns a constant `ast::Variable` with the given name, storage and type
818 ast::Variable* Const(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000819 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000820 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000821 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000822 return create<ast::Variable>(Symbols().Register(name),
823 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000824 constructor, decorations);
825 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000826
827 /// @param source the variable source
828 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000829 /// @param type the variable type
830 /// @param constructor optional constructor expression
831 /// @param decorations optional variable decorations
832 /// @returns a constant `ast::Variable` with the given name, storage and type
833 ast::Variable* Const(const Source& source,
834 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000835 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000836 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000837 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000838 return create<ast::Variable>(source, Symbols().Register(name),
839 ast::StorageClass::kNone, type, true,
840 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000841 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000842
Ben Clayton46d78d72021-02-10 21:34:26 +0000843 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000844 /// @param type the variable type
845 /// @param constructor optional constructor expression
846 /// @param decorations optional variable decorations
847 /// @returns a constant `ast::Variable` with the given symbol, storage and
848 /// type
849 ast::Variable* Const(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000850 type::Type* type,
851 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000852 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000853 return create<ast::Variable>(symbol, ast::StorageClass::kNone, type, true,
854 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000855 }
856
857 /// @param source the variable source
858 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000859 /// @param type the variable type
860 /// @param constructor optional constructor expression
861 /// @param decorations optional variable decorations
862 /// @returns a constant `ast::Variable` with the given symbol, storage and
863 /// type
864 ast::Variable* Const(const Source& source,
865 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000866 type::Type* type,
867 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000868 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000869 return create<ast::Variable>(source, symbol, ast::StorageClass::kNone, type,
870 true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000871 }
Ben Clayton37571bc2021-02-16 23:57:01 +0000872
Ben Clayton401b96b2021-02-03 17:19:59 +0000873 /// @param args the arguments to pass to Var()
874 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
875 /// of `args`, which is automatically registered as a global variable with the
876 /// ast::Module.
877 template <typename... ARGS>
878 ast::Variable* Global(ARGS&&... args) {
879 auto* var = Var(std::forward<ARGS>(args)...);
880 AST().AddGlobalVariable(var);
881 return var;
882 }
883
884 /// @param args the arguments to pass to Const()
885 /// @returns a const `ast::Variable` constructed by calling Var() with the
886 /// arguments of `args`, which is automatically registered as a global
887 /// variable with the ast::Module.
888 template <typename... ARGS>
889 ast::Variable* GlobalConst(ARGS&&... args) {
890 auto* var = Const(std::forward<ARGS>(args)...);
891 AST().AddGlobalVariable(var);
892 return var;
893 }
894
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000895 /// @param func the function name
896 /// @param args the function call arguments
897 /// @returns a `ast::CallExpression` to the function `func`, with the
898 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
899 template <typename NAME, typename... ARGS>
900 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
901 return create<ast::CallExpression>(Expr(func),
902 ExprList(std::forward<ARGS>(args)...));
903 }
904
905 /// @param lhs the left hand argument to the addition operation
906 /// @param rhs the right hand argument to the addition operation
907 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
908 template <typename LHS, typename RHS>
909 ast::Expression* Add(LHS&& lhs, RHS&& rhs) {
910 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
911 Expr(std::forward<LHS>(lhs)),
912 Expr(std::forward<RHS>(rhs)));
913 }
914
915 /// @param lhs the left hand argument to the subtraction operation
916 /// @param rhs the right hand argument to the subtraction operation
917 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
918 template <typename LHS, typename RHS>
919 ast::Expression* Sub(LHS&& lhs, RHS&& rhs) {
920 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
921 Expr(std::forward<LHS>(lhs)),
922 Expr(std::forward<RHS>(rhs)));
923 }
924
925 /// @param lhs the left hand argument to the multiplication operation
926 /// @param rhs the right hand argument to the multiplication operation
927 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
928 template <typename LHS, typename RHS>
929 ast::Expression* Mul(LHS&& lhs, RHS&& rhs) {
930 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
931 Expr(std::forward<LHS>(lhs)),
932 Expr(std::forward<RHS>(rhs)));
933 }
934
935 /// @param arr the array argument for the array accessor expression
936 /// @param idx the index argument for the array accessor expression
937 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
938 template <typename ARR, typename IDX>
939 ast::Expression* IndexAccessor(ARR&& arr, IDX&& idx) {
940 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
941 Expr(std::forward<IDX>(idx)));
942 }
943
944 /// @param obj the object for the member accessor expression
945 /// @param idx the index argument for the array accessor expression
946 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
947 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +0000948 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000949 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
950 Expr(std::forward<IDX>(idx)));
951 }
952
Ben Clayton42d1e092021-02-02 14:29:15 +0000953 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000954 /// @param val the offset value
955 /// @returns the offset decoration pointer
956 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
957 return create<ast::StructMemberOffsetDecoration>(source_, val);
958 }
959
Ben Claytond614dd52021-03-15 10:43:11 +0000960 /// Creates a ast::StructMemberSizeDecoration
961 /// @param source the source information
962 /// @param val the size value
963 /// @returns the size decoration pointer
964 ast::StructMemberSizeDecoration* MemberSize(Source source, uint32_t val) {
965 return create<ast::StructMemberSizeDecoration>(source, val);
966 }
967
968 /// Creates a ast::StructMemberSizeDecoration
969 /// @param val the size value
970 /// @returns the size decoration pointer
971 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
972 return create<ast::StructMemberSizeDecoration>(source_, val);
973 }
974
975 /// Creates a ast::StructMemberAlignDecoration
976 /// @param source the source information
977 /// @param val the align value
978 /// @returns the align decoration pointer
979 ast::StructMemberAlignDecoration* MemberAlign(Source source, uint32_t val) {
980 return create<ast::StructMemberAlignDecoration>(source, val);
981 }
982
983 /// Creates a ast::StructMemberAlignDecoration
984 /// @param val the align value
985 /// @returns the align decoration pointer
986 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
987 return create<ast::StructMemberAlignDecoration>(source_, val);
988 }
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 source the source information
992 /// @param name the function name
993 /// @param params the function parameters
994 /// @param type the function return type
995 /// @param body the function body
996 /// @param decorations the function decorations
997 /// @returns the function pointer
998 ast::Function* Func(Source source,
999 std::string name,
1000 ast::VariableList params,
1001 type::Type* type,
1002 ast::StatementList body,
James Price95d40772021-03-11 17:39:32 +00001003 ast::DecorationList decorations) {
Ben Clayton42d1e092021-02-02 14:29:15 +00001004 auto* func =
1005 create<ast::Function>(source, Symbols().Register(name), params, type,
1006 create<ast::BlockStatement>(body), decorations);
James Price3eaa4502021-02-09 21:26:21 +00001007 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001008 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001009 }
1010
Ben Clayton42d1e092021-02-02 14:29:15 +00001011 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001012 /// @param name the function name
1013 /// @param params the function parameters
1014 /// @param type the function return type
1015 /// @param body the function body
1016 /// @param decorations the function decorations
1017 /// @returns the function pointer
1018 ast::Function* Func(std::string name,
1019 ast::VariableList params,
1020 type::Type* type,
1021 ast::StatementList body,
James Price95d40772021-03-11 17:39:32 +00001022 ast::DecorationList decorations) {
Ben Clayton42d1e092021-02-02 14:29:15 +00001023 auto* func =
1024 create<ast::Function>(Symbols().Register(name), params, type,
1025 create<ast::BlockStatement>(body), decorations);
James Price3eaa4502021-02-09 21:26:21 +00001026 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001027 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001028 }
1029
Ben Claytond614dd52021-03-15 10:43:11 +00001030 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1031 /// the AST().ConstructedTypes().
1032 /// @param source the source information
1033 /// @param name the struct name
1034 /// @param members the struct members
1035 /// @param decorations the optional struct decorations
1036 /// @returns the struct type
1037 type::Struct* Structure(const Source& source,
1038 const std::string& name,
1039 ast::StructMemberList members,
1040 ast::DecorationList decorations = {}) {
1041 auto* impl =
1042 create<ast::Struct>(source, std::move(members), std::move(decorations));
1043 auto* type = ty.struct_(name, impl);
1044 AST().AddConstructedType(type);
1045 return type;
1046 }
1047
1048 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1049 /// the AST().ConstructedTypes().
1050 /// @param name the struct name
1051 /// @param members the struct members
1052 /// @param decorations the optional struct decorations
1053 /// @returns the struct type
1054 type::Struct* Structure(const std::string& name,
1055 ast::StructMemberList members,
1056 ast::DecorationList decorations = {}) {
1057 auto* impl =
1058 create<ast::Struct>(std::move(members), std::move(decorations));
1059 auto* type = ty.struct_(name, impl);
1060 AST().AddConstructedType(type);
1061 return type;
1062 }
1063
Ben Clayton42d1e092021-02-02 14:29:15 +00001064 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001065 /// @param source the source information
1066 /// @param name the struct member name
1067 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001068 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001069 /// @returns the struct member pointer
1070 ast::StructMember* Member(const Source& source,
1071 const std::string& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001072 type::Type* type,
1073 ast::DecorationList decorations = {}) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001074 return create<ast::StructMember>(source, Symbols().Register(name), type,
Ben Claytond614dd52021-03-15 10:43:11 +00001075 std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001076 }
1077
Ben Clayton42d1e092021-02-02 14:29:15 +00001078 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001079 /// @param name the struct member name
1080 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001081 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001082 /// @returns the struct member pointer
1083 ast::StructMember* Member(const std::string& name,
1084 type::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001085 ast::DecorationList decorations = {}) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001086 return create<ast::StructMember>(source_, Symbols().Register(name), type,
Ben Claytond614dd52021-03-15 10:43:11 +00001087 std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001088 }
1089
Ben Claytonbab31972021-02-08 21:16:21 +00001090 /// Creates a ast::StructMember with the given byte offset
1091 /// @param offset the offset to use in the StructMemberOffsetDecoration
1092 /// @param name the struct member name
1093 /// @param type the struct member type
1094 /// @returns the struct member pointer
1095 ast::StructMember* Member(uint32_t offset,
1096 const std::string& name,
1097 type::Type* type) {
1098 return create<ast::StructMember>(
1099 source_, Symbols().Register(name), type,
James Price95d40772021-03-11 17:39:32 +00001100 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001101 create<ast::StructMemberOffsetDecoration>(offset),
1102 });
1103 }
1104
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001105 /// Creates a ast::BlockStatement with input statements
1106 /// @param statements statements of block
1107 /// @returns the block statement pointer
1108 template <typename... Statements>
1109 ast::BlockStatement* Block(Statements&&... statements) {
1110 return create<ast::BlockStatement>(
1111 ast::StatementList{std::forward<Statements>(statements)...});
1112 }
1113
1114 /// Creates a ast::ElseStatement with input condition and body
1115 /// @param condition the else condition expression
1116 /// @param body the else body
1117 /// @returns the else statement pointer
1118 ast::ElseStatement* Else(ast::Expression* condition,
1119 ast::BlockStatement* body) {
1120 return create<ast::ElseStatement>(condition, body);
1121 }
1122
1123 /// Creates a ast::IfStatement with input condition, body, and optional
1124 /// variadic else statements
1125 /// @param condition the if statement condition expression
1126 /// @param body the if statement body
1127 /// @param elseStatements optional variadic else statements
1128 /// @returns the if statement pointer
1129 template <typename... ElseStatements>
1130 ast::IfStatement* If(ast::Expression* condition,
1131 ast::BlockStatement* body,
1132 ElseStatements&&... elseStatements) {
1133 return create<ast::IfStatement>(
1134 condition, body,
1135 ast::ElseStatementList{
1136 std::forward<ElseStatements>(elseStatements)...});
1137 }
1138
1139 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
1140 /// @param lhs the left hand side expression
1141 /// @param rhs the right hand side expression
1142 /// @returns the assignment statement pointer
1143 ast::AssignmentStatement* Assign(ast::Expression* lhs, ast::Expression* rhs) {
1144 return create<ast::AssignmentStatement>(lhs, rhs);
1145 }
1146
1147 /// Creates a ast::LoopStatement with input body and optional continuing
1148 /// @param body the loop body
1149 /// @param continuing the optional continuing block
1150 /// @returns the loop statement pointer
1151 ast::LoopStatement* Loop(ast::BlockStatement* body,
1152 ast::BlockStatement* continuing = nullptr) {
1153 return create<ast::LoopStatement>(body, continuing);
1154 }
1155
1156 /// Creates a ast::VariableDeclStatement for the input variable
1157 /// @param var the variable to wrap in a decl statement
1158 /// @returns the variable decl statement pointer
1159 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1160 return create<ast::VariableDeclStatement>(var);
1161 }
1162
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001163 /// Sets the current builder source to `src`
1164 /// @param src the Source used for future create() calls
1165 void SetSource(const Source& src) {
1166 AssertNotMoved();
1167 source_ = src;
1168 }
1169
1170 /// Sets the current builder source to `loc`
1171 /// @param loc the Source used for future create() calls
1172 void SetSource(const Source::Location& loc) {
1173 AssertNotMoved();
1174 source_ = Source(loc);
1175 }
1176
Ben Clayton33352542021-01-29 16:43:41 +00001177 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001178 /// @note As the Resolver is run when the Program is built, this will only be
1179 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001180 /// @param expr the AST expression
1181 /// @return the resolved semantic type for the expression, or nullptr if the
1182 /// expression has no resolved type.
1183 type::Type* TypeOf(ast::Expression* expr) const;
1184
Ben Clayton401b96b2021-02-03 17:19:59 +00001185 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001186 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001187 /// nodes.
1188 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1189 /// @return the ast::Statement that wraps the ast::Expression
1190 ast::Statement* WrapInStatement(ast::Expression* expr);
1191 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001192 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001193 /// these nodes.
1194 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1195 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1196 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1197 /// Returns the statement argument. Used as a passthrough-overload by
1198 /// WrapInFunction().
1199 /// @param stmt the ast::Statement
1200 /// @return `stmt`
1201 ast::Statement* WrapInStatement(ast::Statement* stmt);
1202 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001203 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001204 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
1205 template <typename... ARGS>
1206 void WrapInFunction(ARGS&&... args) {
1207 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
1208 WrapInFunction(stmts);
1209 }
1210 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001211 /// so that each statement is reachable by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001212 void WrapInFunction(ast::StatementList stmts);
1213
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001214 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001215 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001216
1217 protected:
1218 /// Asserts that the builder has not been moved.
1219 void AssertNotMoved() const;
1220
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001221 private:
1222 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001223 ASTNodeAllocator ast_nodes_;
1224 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001225 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001226 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001227 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001228 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001229
1230 /// The source to use when creating AST nodes without providing a Source as
1231 /// the first argument.
1232 Source source_;
1233
Ben Clayton5f0ea112021-03-09 10:54:37 +00001234 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001235 /// program when built.
1236 bool resolve_on_build_ = true;
1237
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001238 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1239 bool moved_ = false;
1240};
1241
1242//! @cond Doxygen_Suppress
1243// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1244template <>
1245struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1246 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1247 return t->i32();
1248 }
1249};
1250template <>
1251struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1252 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1253 return t->u32();
1254 }
1255};
1256template <>
1257struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1258 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1259 return t->f32();
1260 }
1261};
1262template <>
1263struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1264 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1265 return t->bool_();
1266 }
1267};
1268template <>
1269struct ProgramBuilder::TypesBuilder::CToAST<void> {
1270 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1271 return t->void_();
1272 }
1273};
1274//! @endcond
1275
1276} // namespace tint
1277
1278#endif // SRC_PROGRAM_BUILDER_H_