blob: ffe747623a65bcbc2fd66906b7034edeef5e6012 [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"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000031#include "src/ast/return_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000032#include "src/ast/scalar_constructor_expression.h"
33#include "src/ast/sint_literal.h"
Ben Claytonbab31972021-02-08 21:16:21 +000034#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000035#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000036#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000037#include "src/ast/struct_member_size_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/type_constructor_expression.h"
39#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000040#include "src/ast/variable_decl_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000041#include "src/program.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000042#include "src/type/alias_type.h"
43#include "src/type/array_type.h"
44#include "src/type/bool_type.h"
45#include "src/type/f32_type.h"
46#include "src/type/i32_type.h"
47#include "src/type/matrix_type.h"
48#include "src/type/pointer_type.h"
49#include "src/type/struct_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000050#include "src/type/u32_type.h"
51#include "src/type/vector_type.h"
52#include "src/type/void_type.h"
53
54namespace tint {
55
Ben Clayton401b96b2021-02-03 17:19:59 +000056// Forward declarations
57namespace ast {
58class VariableDeclStatement;
59} // namespace ast
60
Ben Claytona6b9a8e2021-01-26 16:57:10 +000061class CloneContext;
62
63/// ProgramBuilder is a mutable builder for a Program.
64/// To construct a Program, populate the builder and then `std::move` it to a
65/// Program.
66class ProgramBuilder {
67 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000068 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
69 using ASTNodeAllocator = BlockAllocator<ast::Node>;
70
71 /// SemNodeAllocator is an alias to BlockAllocator<semantic::Node>
72 using SemNodeAllocator = BlockAllocator<semantic::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000073
74 /// `i32` is a type alias to `int`.
75 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
76 /// WGSL syntax.
77 /// Note: this is intentionally not aliased to uint32_t as we want integer
78 /// literals passed to the builder to match WGSL's integer literal types.
79 using i32 = decltype(1);
80 /// `u32` is a type alias to `unsigned int`.
81 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
82 /// WGSL syntax.
83 /// Note: this is intentionally not aliased to uint32_t as we want integer
84 /// literals passed to the builder to match WGSL's integer literal types.
85 using u32 = decltype(1u);
86 /// `f32` is a type alias to `float`
87 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
88 /// WGSL syntax.
89 using f32 = float;
90
91 /// Constructor
92 ProgramBuilder();
93
94 /// Move constructor
95 /// @param rhs the builder to move
96 ProgramBuilder(ProgramBuilder&& rhs);
97
98 /// Destructor
99 virtual ~ProgramBuilder();
100
101 /// Move assignment operator
102 /// @param rhs the builder to move
103 /// @return this builder
104 ProgramBuilder& operator=(ProgramBuilder&& rhs);
105
Ben Claytone43c8302021-01-29 11:59:32 +0000106 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
107 /// making a deep clone of the Program contents.
108 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
109 /// existing immutable program.
110 /// As the returned ProgramBuilder wraps `program`, `program` must not be
111 /// destructed or assigned while using the returned ProgramBuilder.
112 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
113 /// function. See crbug.com/tint/460.
114 /// @param program the immutable Program to wrap
115 /// @return the ProgramBuilder that wraps `program`
116 static ProgramBuilder Wrap(const Program* program);
117
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000118 /// @returns a reference to the program's types
119 type::Manager& Types() {
120 AssertNotMoved();
121 return types_;
122 }
123
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000124 /// @returns a reference to the program's types
125 const type::Manager& Types() const {
126 AssertNotMoved();
127 return types_;
128 }
129
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000130 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000131 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000132 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000133 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000134 }
135
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000136 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000137 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000138 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000139 return ast_nodes_;
140 }
141
142 /// @returns a reference to the program's semantic nodes storage
143 SemNodeAllocator& SemNodes() {
144 AssertNotMoved();
145 return sem_nodes_;
146 }
147
148 /// @returns a reference to the program's semantic nodes storage
149 const SemNodeAllocator& SemNodes() const {
150 AssertNotMoved();
151 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000152 }
153
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000154 /// @returns a reference to the program's AST root Module
155 ast::Module& AST() {
156 AssertNotMoved();
157 return *ast_;
158 }
159
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000160 /// @returns a reference to the program's AST root Module
161 const ast::Module& AST() const {
162 AssertNotMoved();
163 return *ast_;
164 }
165
166 /// @returns a reference to the program's semantic info
167 semantic::Info& Sem() {
168 AssertNotMoved();
169 return sem_;
170 }
171
172 /// @returns a reference to the program's semantic info
173 const semantic::Info& Sem() const {
174 AssertNotMoved();
175 return sem_;
176 }
177
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000178 /// @returns a reference to the program's SymbolTable
179 SymbolTable& Symbols() {
180 AssertNotMoved();
181 return symbols_;
182 }
183
Ben Clayton708dc2d2021-01-29 11:22:40 +0000184 /// @returns a reference to the program's SymbolTable
185 const SymbolTable& Symbols() const {
186 AssertNotMoved();
187 return symbols_;
188 }
189
Ben Clayton844217f2021-01-27 18:49:05 +0000190 /// @returns a reference to the program's diagnostics
191 diag::List& Diagnostics() {
192 AssertNotMoved();
193 return diagnostics_;
194 }
195
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000196 /// @returns a reference to the program's diagnostics
197 const diag::List& Diagnostics() const {
198 AssertNotMoved();
199 return diagnostics_;
200 }
201
Ben Clayton5f0ea112021-03-09 10:54:37 +0000202 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000203 /// @param enable the new flag value (defaults to true)
204 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
205
Ben Clayton5f0ea112021-03-09 10:54:37 +0000206 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000207 /// built.
208 bool ResolveOnBuild() const { return resolve_on_build_; }
209
Ben Clayton844217f2021-01-27 18:49:05 +0000210 /// @returns true if the program has no error diagnostics and is not missing
211 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000212 bool IsValid() const;
213
Ben Clayton708dc2d2021-01-29 11:22:40 +0000214 /// Writes a representation of the node to the output stream
215 /// @note unlike str(), to_str() does not automatically demangle the string.
216 /// @param node the AST node
217 /// @param out the stream to write to
218 /// @param indent number of spaces to indent the node when writing
219 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
220 node->to_str(Sem(), out, indent);
221 }
222
223 /// Returns a demangled, string representation of `node`.
224 /// @param node the AST node
225 /// @returns a string representation of the node
226 std::string str(const ast::Node* node) const;
227
Ben Clayton7fdfff12021-01-29 15:17:30 +0000228 /// Creates a new ast::Node owned by the ProgramBuilder. When the
229 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000230 /// @param source the Source of the node
231 /// @param args the arguments to pass to the type constructor
232 /// @returns the node pointer
233 template <typename T, typename... ARGS>
234 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
235 ARGS&&... args) {
236 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000237 return ast_nodes_.Create<T>(source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000238 }
239
Ben Clayton7fdfff12021-01-29 15:17:30 +0000240 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
241 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000242 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000243 /// When the ProgramBuilder is destructed, the ast::Node will also be
244 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000245 /// @returns the node pointer
246 template <typename T>
247 traits::EnableIfIsType<T, ast::Node>* create() {
248 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000249 return ast_nodes_.Create<T>(source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000250 }
251
Ben Clayton7fdfff12021-01-29 15:17:30 +0000252 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
253 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000254 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000255 /// When the ProgramBuilder is destructed, the ast::Node will also be
256 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000257 /// @param arg0 the first arguments to pass to the type constructor
258 /// @param args the remaining arguments to pass to the type constructor
259 /// @returns the node pointer
260 template <typename T, typename ARG0, typename... ARGS>
261 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
262 traits::IsTypeOrDerived<T, ast::Node>::value &&
263 !traits::IsTypeOrDerived<ARG0, Source>::value,
264 T>*
265 create(ARG0&& arg0, ARGS&&... args) {
266 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000267 return ast_nodes_.Create<T>(source_, std::forward<ARG0>(arg0),
268 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000269 }
270
Ben Clayton7fdfff12021-01-29 15:17:30 +0000271 /// Creates a new semantic::Node owned by the ProgramBuilder.
272 /// When the ProgramBuilder is destructed, the semantic::Node will also be
273 /// destructed.
274 /// @param args the arguments to pass to the type constructor
275 /// @returns the node pointer
276 template <typename T, typename... ARGS>
277 traits::EnableIfIsType<T, semantic::Node>* create(ARGS&&... args) {
278 AssertNotMoved();
279 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
280 }
281
282 /// Creates a new type::Type owned by the ProgramBuilder.
283 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
284 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000285 /// Types are unique (de-aliased), and so calling create() for the same `T`
286 /// and arguments will return the same pointer.
287 /// @warning Use this method to acquire a type only if all of its type
288 /// information is provided in the constructor arguments `args`.<br>
289 /// If the type requires additional configuration after construction that
290 /// affect its fundamental type, build the type with `std::make_unique`, make
291 /// any necessary alterations and then call unique_type() instead.
292 /// @param args the arguments to pass to the type constructor
293 /// @returns the de-aliased type pointer
294 template <typename T, typename... ARGS>
295 traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) {
296 static_assert(std::is_base_of<type::Type, T>::value,
297 "T does not derive from type::Type");
298 AssertNotMoved();
299 return types_.Get<T>(std::forward<ARGS>(args)...);
300 }
301
302 /// Marks this builder as moved, preventing any further use of the builder.
303 void MarkAsMoved();
304
305 //////////////////////////////////////////////////////////////////////////////
306 // TypesBuilder
307 //////////////////////////////////////////////////////////////////////////////
308
309 /// TypesBuilder holds basic `tint` types and methods for constructing
310 /// complex types.
311 class TypesBuilder {
312 public:
313 /// Constructor
314 /// @param builder the program builder
315 explicit TypesBuilder(ProgramBuilder* builder);
316
317 /// @return the tint AST type for the C type `T`.
318 template <typename T>
319 type::Type* Of() const {
320 return CToAST<T>::get(this);
321 }
322
323 /// @returns a boolean type
324 type::Bool* bool_() const { return builder->create<type::Bool>(); }
325
326 /// @returns a f32 type
327 type::F32* f32() const { return builder->create<type::F32>(); }
328
329 /// @returns a i32 type
330 type::I32* i32() const { return builder->create<type::I32>(); }
331
332 /// @returns a u32 type
333 type::U32* u32() const { return builder->create<type::U32>(); }
334
335 /// @returns a void type
336 type::Void* void_() const { return builder->create<type::Void>(); }
337
338 /// @return the tint AST type for a 2-element vector of the C type `T`.
339 template <typename T>
340 type::Vector* vec2() const {
341 return builder->create<type::Vector>(Of<T>(), 2);
342 }
343
344 /// @return the tint AST type for a 3-element vector of the C type `T`.
345 template <typename T>
346 type::Vector* vec3() const {
347 return builder->create<type::Vector>(Of<T>(), 3);
348 }
349
350 /// @return the tint AST type for a 4-element vector of the C type `T`.
351 template <typename T>
352 type::Type* vec4() const {
353 return builder->create<type::Vector>(Of<T>(), 4);
354 }
355
356 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
357 template <typename T>
358 type::Matrix* mat2x2() const {
359 return builder->create<type::Matrix>(Of<T>(), 2, 2);
360 }
361
362 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
363 template <typename T>
364 type::Matrix* mat2x3() const {
365 return builder->create<type::Matrix>(Of<T>(), 3, 2);
366 }
367
368 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
369 template <typename T>
370 type::Matrix* mat2x4() const {
371 return builder->create<type::Matrix>(Of<T>(), 4, 2);
372 }
373
374 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
375 template <typename T>
376 type::Matrix* mat3x2() const {
377 return builder->create<type::Matrix>(Of<T>(), 2, 3);
378 }
379
380 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
381 template <typename T>
382 type::Matrix* mat3x3() const {
383 return builder->create<type::Matrix>(Of<T>(), 3, 3);
384 }
385
386 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
387 template <typename T>
388 type::Matrix* mat3x4() const {
389 return builder->create<type::Matrix>(Of<T>(), 4, 3);
390 }
391
392 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
393 template <typename T>
394 type::Matrix* mat4x2() const {
395 return builder->create<type::Matrix>(Of<T>(), 2, 4);
396 }
397
398 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
399 template <typename T>
400 type::Matrix* mat4x3() const {
401 return builder->create<type::Matrix>(Of<T>(), 3, 4);
402 }
403
404 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
405 template <typename T>
406 type::Matrix* mat4x4() const {
407 return builder->create<type::Matrix>(Of<T>(), 4, 4);
408 }
409
410 /// @param subtype the array element type
411 /// @param n the array size. 0 represents a runtime-array.
412 /// @return the tint AST type for a array of size `n` of type `T`
413 type::Array* array(type::Type* subtype, uint32_t n) const {
James Price95d40772021-03-11 17:39:32 +0000414 return builder->create<type::Array>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000415 }
416
Ben Claytonbab31972021-02-08 21:16:21 +0000417 /// @param subtype the array element type
418 /// @param n the array size. 0 represents a runtime-array.
419 /// @param stride the array stride.
420 /// @return the tint AST type for a array of size `n` of type `T`
421 type::Array* array(type::Type* subtype, uint32_t n, uint32_t stride) const {
422 return builder->create<type::Array>(
423 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000424 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000425 builder->create<ast::StrideDecoration>(stride),
426 });
427 }
428
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000429 /// @return the tint AST type for an array of size `N` of type `T`
430 template <typename T, int N = 0>
431 type::Array* array() const {
432 return array(Of<T>(), N);
433 }
434
Ben Claytonbab31972021-02-08 21:16:21 +0000435 /// @param stride the array stride
436 /// @return the tint AST type for an array of size `N` of type `T`
437 template <typename T, int N = 0>
438 type::Array* array(uint32_t stride) const {
439 return array(Of<T>(), N, stride);
440 }
Ben Clayton42d1e092021-02-02 14:29:15 +0000441 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000442 /// @param name the alias name
443 /// @param type the alias type
444 /// @returns the alias pointer
445 type::Alias* alias(const std::string& name, type::Type* type) const {
446 return builder->create<type::Alias>(builder->Symbols().Register(name),
447 type);
448 }
449
450 /// @return the tint AST pointer to type `T` with the given
451 /// ast::StorageClass.
452 /// @param storage_class the storage class of the pointer
453 template <typename T>
454 type::Pointer* pointer(ast::StorageClass storage_class) const {
455 return builder->create<type::Pointer>(Of<T>(), storage_class);
456 }
457
458 /// @param name the struct name
459 /// @param impl the struct implementation
460 /// @returns a struct pointer
461 type::Struct* struct_(const std::string& name, ast::Struct* impl) const {
462 return builder->create<type::Struct>(builder->Symbols().Register(name),
463 impl);
464 }
465
466 private:
467 /// CToAST<T> is specialized for various `T` types and each specialization
468 /// contains a single static `get()` method for obtaining the corresponding
469 /// AST type for the C type `T`.
470 /// `get()` has the signature:
471 /// `static type::Type* get(Types* t)`
472 template <typename T>
473 struct CToAST {};
474
Ben Claytonc7ca7662021-02-17 16:23:52 +0000475 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000476 };
477
478 //////////////////////////////////////////////////////////////////////////////
479 // AST helper methods
480 //////////////////////////////////////////////////////////////////////////////
481
482 /// @param expr the expression
483 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000484 template <typename T>
485 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
486 return expr;
487 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000488
489 /// @param name the identifier name
490 /// @return an ast::IdentifierExpression with the given name
491 ast::IdentifierExpression* Expr(const std::string& name) {
492 return create<ast::IdentifierExpression>(Symbols().Register(name));
493 }
494
Ben Clayton46d78d72021-02-10 21:34:26 +0000495 /// @param symbol the identifier symbol
496 /// @return an ast::IdentifierExpression with the given symbol
497 ast::IdentifierExpression* Expr(Symbol symbol) {
498 return create<ast::IdentifierExpression>(symbol);
499 }
500
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000501 /// @param source the source information
502 /// @param name the identifier name
503 /// @return an ast::IdentifierExpression with the given name
504 ast::IdentifierExpression* Expr(const Source& source,
505 const std::string& name) {
506 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
507 }
508
509 /// @param name the identifier name
510 /// @return an ast::IdentifierExpression with the given name
511 ast::IdentifierExpression* Expr(const char* name) {
512 return create<ast::IdentifierExpression>(Symbols().Register(name));
513 }
514
515 /// @param value the boolean value
516 /// @return a Scalar constructor for the given value
517 ast::ScalarConstructorExpression* Expr(bool value) {
518 return create<ast::ScalarConstructorExpression>(Literal(value));
519 }
520
521 /// @param value the float value
522 /// @return a Scalar constructor for the given value
523 ast::ScalarConstructorExpression* Expr(f32 value) {
524 return create<ast::ScalarConstructorExpression>(Literal(value));
525 }
526
527 /// @param value the integer value
528 /// @return a Scalar constructor for the given value
529 ast::ScalarConstructorExpression* Expr(i32 value) {
530 return create<ast::ScalarConstructorExpression>(Literal(value));
531 }
532
533 /// @param value the unsigned int value
534 /// @return a Scalar constructor for the given value
535 ast::ScalarConstructorExpression* Expr(u32 value) {
536 return create<ast::ScalarConstructorExpression>(Literal(value));
537 }
538
539 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
540 /// `list`.
541 /// @param list the list to append too
542 /// @param arg the arg to create
543 template <typename ARG>
544 void Append(ast::ExpressionList& list, ARG&& arg) {
545 list.emplace_back(Expr(std::forward<ARG>(arg)));
546 }
547
548 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
549 /// then appends them to `list`.
550 /// @param list the list to append too
551 /// @param arg0 the first argument
552 /// @param args the rest of the arguments
553 template <typename ARG0, typename... ARGS>
554 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
555 Append(list, std::forward<ARG0>(arg0));
556 Append(list, std::forward<ARGS>(args)...);
557 }
558
559 /// @return an empty list of expressions
560 ast::ExpressionList ExprList() { return {}; }
561
562 /// @param args the list of expressions
563 /// @return the list of expressions converted to `ast::Expression`s using
564 /// `Expr()`,
565 template <typename... ARGS>
566 ast::ExpressionList ExprList(ARGS&&... args) {
567 ast::ExpressionList list;
568 list.reserve(sizeof...(args));
569 Append(list, std::forward<ARGS>(args)...);
570 return list;
571 }
572
573 /// @param list the list of expressions
574 /// @return `list`
575 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
576
577 /// @param val the boolan value
578 /// @return a boolean literal with the given value
579 ast::BoolLiteral* Literal(bool val) {
580 return create<ast::BoolLiteral>(ty.bool_(), val);
581 }
582
583 /// @param val the float value
584 /// @return a float literal with the given value
585 ast::FloatLiteral* Literal(f32 val) {
586 return create<ast::FloatLiteral>(ty.f32(), val);
587 }
588
589 /// @param val the unsigned int value
590 /// @return a ast::UintLiteral with the given value
591 ast::UintLiteral* Literal(u32 val) {
592 return create<ast::UintLiteral>(ty.u32(), val);
593 }
594
595 /// @param val the integer value
596 /// @return the ast::SintLiteral with the given value
597 ast::SintLiteral* Literal(i32 val) {
598 return create<ast::SintLiteral>(ty.i32(), val);
599 }
600
601 /// @param args the arguments for the type constructor
602 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
603 /// of `args` converted to `ast::Expression`s using `Expr()`
604 template <typename T, typename... ARGS>
605 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
606 return create<ast::TypeConstructorExpression>(
607 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
608 }
609
610 /// @param type the type to construct
611 /// @param args the arguments for the constructor
612 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
613 /// values `args`.
614 template <typename... ARGS>
615 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
616 return create<ast::TypeConstructorExpression>(
617 type, ExprList(std::forward<ARGS>(args)...));
618 }
619
620 /// @param args the arguments for the vector constructor
621 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
622 /// `T`, constructed with the values `args`.
623 template <typename T, typename... ARGS>
624 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
625 return create<ast::TypeConstructorExpression>(
626 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
627 }
628
629 /// @param args the arguments for the vector constructor
630 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
631 /// `T`, constructed with the values `args`.
632 template <typename T, typename... ARGS>
633 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
634 return create<ast::TypeConstructorExpression>(
635 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
636 }
637
638 /// @param args the arguments for the vector constructor
639 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
640 /// `T`, constructed with the values `args`.
641 template <typename T, typename... ARGS>
642 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
643 return create<ast::TypeConstructorExpression>(
644 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
645 }
646
647 /// @param args the arguments for the matrix constructor
648 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
649 /// `T`, constructed with the values `args`.
650 template <typename T, typename... ARGS>
651 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
652 return create<ast::TypeConstructorExpression>(
653 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
654 }
655
656 /// @param args the arguments for the matrix constructor
657 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
658 /// `T`, constructed with the values `args`.
659 template <typename T, typename... ARGS>
660 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
661 return create<ast::TypeConstructorExpression>(
662 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
663 }
664
665 /// @param args the arguments for the matrix constructor
666 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
667 /// `T`, constructed with the values `args`.
668 template <typename T, typename... ARGS>
669 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
670 return create<ast::TypeConstructorExpression>(
671 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
672 }
673
674 /// @param args the arguments for the matrix constructor
675 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
676 /// `T`, constructed with the values `args`.
677 template <typename T, typename... ARGS>
678 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
679 return create<ast::TypeConstructorExpression>(
680 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
681 }
682
683 /// @param args the arguments for the matrix constructor
684 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
685 /// `T`, constructed with the values `args`.
686 template <typename T, typename... ARGS>
687 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
688 return create<ast::TypeConstructorExpression>(
689 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
690 }
691
692 /// @param args the arguments for the matrix constructor
693 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
694 /// `T`, constructed with the values `args`.
695 template <typename T, typename... ARGS>
696 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
697 return create<ast::TypeConstructorExpression>(
698 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
699 }
700
701 /// @param args the arguments for the matrix constructor
702 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
703 /// `T`, constructed with the values `args`.
704 template <typename T, typename... ARGS>
705 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
706 return create<ast::TypeConstructorExpression>(
707 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
708 }
709
710 /// @param args the arguments for the matrix constructor
711 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
712 /// `T`, constructed with the values `args`.
713 template <typename T, typename... ARGS>
714 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
715 return create<ast::TypeConstructorExpression>(
716 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
717 }
718
719 /// @param args the arguments for the matrix constructor
720 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
721 /// `T`, constructed with the values `args`.
722 template <typename T, typename... ARGS>
723 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
724 return create<ast::TypeConstructorExpression>(
725 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
726 }
727
728 /// @param args the arguments for the array constructor
729 /// @return an `ast::TypeConstructorExpression` of an array with element type
730 /// `T`, constructed with the values `args`.
731 template <typename T, int N = 0, typename... ARGS>
732 ast::TypeConstructorExpression* array(ARGS&&... args) {
733 return create<ast::TypeConstructorExpression>(
734 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
735 }
736
737 /// @param subtype the array element type
738 /// @param n the array size. 0 represents a runtime-array.
739 /// @param args the arguments for the array constructor
740 /// @return an `ast::TypeConstructorExpression` of an array with element type
741 /// `subtype`, constructed with the values `args`.
742 template <typename... ARGS>
743 ast::TypeConstructorExpression* array(type::Type* subtype,
744 uint32_t n,
745 ARGS&&... args) {
746 return create<ast::TypeConstructorExpression>(
747 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
748 }
749
750 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000751 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000752 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000753 /// @param constructor constructor expression
754 /// @param decorations variable decorations
755 /// @returns a `ast::Variable` with the given name, storage and type
756 ast::Variable* Var(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000757 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000758 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000759 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000760 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000761 return create<ast::Variable>(Symbols().Register(name), storage, type, false,
762 constructor, decorations);
763 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000764
765 /// @param source the variable source
766 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000767 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000768 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000769 /// @param constructor constructor expression
770 /// @param decorations variable decorations
771 /// @returns a `ast::Variable` with the given name, storage and type
772 ast::Variable* Var(const Source& source,
773 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000774 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000775 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000776 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000777 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000778 return create<ast::Variable>(source, Symbols().Register(name), storage,
779 type, false, constructor, decorations);
780 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000781
Ben Clayton46d78d72021-02-10 21:34:26 +0000782 /// @param symbol the variable symbol
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000783 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000784 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000785 /// @param constructor constructor expression
786 /// @param decorations variable decorations
787 /// @returns a `ast::Variable` with the given symbol, storage and type
788 ast::Variable* Var(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000789 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000790 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000791 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000792 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000793 return create<ast::Variable>(symbol, storage, type, false, constructor,
794 decorations);
795 }
796
797 /// @param source the variable source
798 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000799 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000800 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000801 /// @param constructor constructor expression
802 /// @param decorations variable decorations
803 /// @returns a `ast::Variable` with the given symbol, storage and type
804 ast::Variable* Var(const Source& source,
805 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000806 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000807 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000808 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000809 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000810 return create<ast::Variable>(source, symbol, storage, type, false,
811 constructor, decorations);
812 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000813
814 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000815 /// @param type the variable type
816 /// @param constructor optional constructor expression
817 /// @param decorations optional variable decorations
818 /// @returns a constant `ast::Variable` with the given name, storage and type
819 ast::Variable* Const(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000820 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000821 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000822 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000823 return create<ast::Variable>(Symbols().Register(name),
824 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000825 constructor, decorations);
826 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000827
828 /// @param source the variable source
829 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000830 /// @param type the variable type
831 /// @param constructor optional constructor expression
832 /// @param decorations optional variable decorations
833 /// @returns a constant `ast::Variable` with the given name, storage and type
834 ast::Variable* Const(const Source& source,
835 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000836 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000837 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000838 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000839 return create<ast::Variable>(source, Symbols().Register(name),
840 ast::StorageClass::kNone, type, true,
841 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000842 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000843
Ben Clayton46d78d72021-02-10 21:34:26 +0000844 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000845 /// @param type the variable type
846 /// @param constructor optional constructor expression
847 /// @param decorations optional variable decorations
848 /// @returns a constant `ast::Variable` with the given symbol, storage and
849 /// type
850 ast::Variable* Const(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000851 type::Type* type,
852 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000853 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000854 return create<ast::Variable>(symbol, ast::StorageClass::kNone, type, true,
855 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000856 }
857
858 /// @param source the variable source
859 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000860 /// @param type the variable type
861 /// @param constructor optional constructor expression
862 /// @param decorations optional variable decorations
863 /// @returns a constant `ast::Variable` with the given symbol, storage and
864 /// type
865 ast::Variable* Const(const Source& source,
866 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000867 type::Type* type,
868 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000869 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000870 return create<ast::Variable>(source, symbol, ast::StorageClass::kNone, type,
871 true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000872 }
Ben Clayton37571bc2021-02-16 23:57:01 +0000873
Ben Clayton401b96b2021-02-03 17:19:59 +0000874 /// @param args the arguments to pass to Var()
875 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
876 /// of `args`, which is automatically registered as a global variable with the
877 /// ast::Module.
878 template <typename... ARGS>
879 ast::Variable* Global(ARGS&&... args) {
880 auto* var = Var(std::forward<ARGS>(args)...);
881 AST().AddGlobalVariable(var);
882 return var;
883 }
884
885 /// @param args the arguments to pass to Const()
886 /// @returns a const `ast::Variable` constructed by calling Var() with the
887 /// arguments of `args`, which is automatically registered as a global
888 /// variable with the ast::Module.
889 template <typename... ARGS>
890 ast::Variable* GlobalConst(ARGS&&... args) {
891 auto* var = Const(std::forward<ARGS>(args)...);
892 AST().AddGlobalVariable(var);
893 return var;
894 }
895
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000896 /// @param func the function name
897 /// @param args the function call arguments
898 /// @returns a `ast::CallExpression` to the function `func`, with the
899 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
900 template <typename NAME, typename... ARGS>
901 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
902 return create<ast::CallExpression>(Expr(func),
903 ExprList(std::forward<ARGS>(args)...));
904 }
905
906 /// @param lhs the left hand argument to the addition operation
907 /// @param rhs the right hand argument to the addition operation
908 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
909 template <typename LHS, typename RHS>
910 ast::Expression* Add(LHS&& lhs, RHS&& rhs) {
911 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
912 Expr(std::forward<LHS>(lhs)),
913 Expr(std::forward<RHS>(rhs)));
914 }
915
916 /// @param lhs the left hand argument to the subtraction operation
917 /// @param rhs the right hand argument to the subtraction operation
918 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
919 template <typename LHS, typename RHS>
920 ast::Expression* Sub(LHS&& lhs, RHS&& rhs) {
921 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
922 Expr(std::forward<LHS>(lhs)),
923 Expr(std::forward<RHS>(rhs)));
924 }
925
926 /// @param lhs the left hand argument to the multiplication operation
927 /// @param rhs the right hand argument to the multiplication operation
928 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
929 template <typename LHS, typename RHS>
930 ast::Expression* Mul(LHS&& lhs, RHS&& rhs) {
931 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
932 Expr(std::forward<LHS>(lhs)),
933 Expr(std::forward<RHS>(rhs)));
934 }
935
936 /// @param arr the array argument for the array accessor expression
937 /// @param idx the index argument for the array accessor expression
938 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
939 template <typename ARR, typename IDX>
940 ast::Expression* IndexAccessor(ARR&& arr, IDX&& idx) {
941 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
942 Expr(std::forward<IDX>(idx)));
943 }
944
945 /// @param obj the object for the member accessor expression
946 /// @param idx the index argument for the array accessor expression
947 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
948 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +0000949 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000950 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
951 Expr(std::forward<IDX>(idx)));
952 }
953
Ben Clayton42d1e092021-02-02 14:29:15 +0000954 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000955 /// @param val the offset value
956 /// @returns the offset decoration pointer
957 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
958 return create<ast::StructMemberOffsetDecoration>(source_, val);
959 }
960
Ben Claytond614dd52021-03-15 10:43:11 +0000961 /// Creates a ast::StructMemberSizeDecoration
962 /// @param source the source information
963 /// @param val the size value
964 /// @returns the size decoration pointer
965 ast::StructMemberSizeDecoration* MemberSize(Source source, uint32_t val) {
966 return create<ast::StructMemberSizeDecoration>(source, val);
967 }
968
969 /// Creates a ast::StructMemberSizeDecoration
970 /// @param val the size value
971 /// @returns the size decoration pointer
972 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
973 return create<ast::StructMemberSizeDecoration>(source_, val);
974 }
975
976 /// Creates a ast::StructMemberAlignDecoration
977 /// @param source the source information
978 /// @param val the align value
979 /// @returns the align decoration pointer
980 ast::StructMemberAlignDecoration* MemberAlign(Source source, uint32_t val) {
981 return create<ast::StructMemberAlignDecoration>(source, val);
982 }
983
984 /// Creates a ast::StructMemberAlignDecoration
985 /// @param val the align value
986 /// @returns the align decoration pointer
987 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
988 return create<ast::StructMemberAlignDecoration>(source_, val);
989 }
990
Ben Clayton42d1e092021-02-02 14:29:15 +0000991 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000992 /// @param source the source information
993 /// @param name the function name
994 /// @param params the function parameters
995 /// @param type the function return type
996 /// @param body the function body
997 /// @param decorations the function decorations
James Pricefeecbe02021-03-15 17:01:34 +0000998 /// @param return_type_decorations the function return type decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000999 /// @returns the function pointer
1000 ast::Function* Func(Source source,
1001 std::string name,
1002 ast::VariableList params,
1003 type::Type* type,
1004 ast::StatementList body,
James Pricefeecbe02021-03-15 17:01:34 +00001005 ast::DecorationList decorations,
1006 ast::DecorationList return_type_decorations = {}) {
1007 auto* func = create<ast::Function>(source, Symbols().Register(name), params,
1008 type, create<ast::BlockStatement>(body),
1009 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001010 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001011 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001012 }
1013
Ben Clayton42d1e092021-02-02 14:29:15 +00001014 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001015 /// @param name the function name
1016 /// @param params the function parameters
1017 /// @param type the function return type
1018 /// @param body the function body
1019 /// @param decorations the function decorations
James Pricefeecbe02021-03-15 17:01:34 +00001020 /// @param return_type_decorations the function return type decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001021 /// @returns the function pointer
1022 ast::Function* Func(std::string name,
1023 ast::VariableList params,
1024 type::Type* type,
1025 ast::StatementList body,
James Pricefeecbe02021-03-15 17:01:34 +00001026 ast::DecorationList decorations,
1027 ast::DecorationList return_type_decorations = {}) {
1028 auto* func = create<ast::Function>(Symbols().Register(name), params, type,
1029 create<ast::BlockStatement>(body),
1030 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001031 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001032 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001033 }
1034
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001035 /// Creates an ast::ReturnStatement with the input args
1036 /// @param args arguments to construct a return statement with
1037 /// @returns the return statement pointer
1038 template <typename... Args>
1039 ast::ReturnStatement* Return(Args&&... args) {
1040 return create<ast::ReturnStatement>(std::forward<Args>(args)...);
1041 }
1042
Ben Claytond614dd52021-03-15 10:43:11 +00001043 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1044 /// the AST().ConstructedTypes().
1045 /// @param source the source information
1046 /// @param name the struct name
1047 /// @param members the struct members
1048 /// @param decorations the optional struct decorations
1049 /// @returns the struct type
1050 type::Struct* Structure(const Source& source,
1051 const std::string& name,
1052 ast::StructMemberList members,
1053 ast::DecorationList decorations = {}) {
1054 auto* impl =
1055 create<ast::Struct>(source, std::move(members), std::move(decorations));
1056 auto* type = ty.struct_(name, impl);
1057 AST().AddConstructedType(type);
1058 return type;
1059 }
1060
1061 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1062 /// the AST().ConstructedTypes().
1063 /// @param name the struct name
1064 /// @param members the struct members
1065 /// @param decorations the optional struct decorations
1066 /// @returns the struct type
1067 type::Struct* Structure(const std::string& name,
1068 ast::StructMemberList members,
1069 ast::DecorationList decorations = {}) {
1070 auto* impl =
1071 create<ast::Struct>(std::move(members), std::move(decorations));
1072 auto* type = ty.struct_(name, impl);
1073 AST().AddConstructedType(type);
1074 return type;
1075 }
1076
Ben Clayton42d1e092021-02-02 14:29:15 +00001077 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078 /// @param source the source information
1079 /// @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 Source& source,
1084 const std::string& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001085 type::Type* type,
1086 ast::DecorationList decorations = {}) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001087 return create<ast::StructMember>(source, Symbols().Register(name), type,
Ben Claytond614dd52021-03-15 10:43:11 +00001088 std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001089 }
1090
Ben Clayton42d1e092021-02-02 14:29:15 +00001091 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001092 /// @param name the struct member name
1093 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001094 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001095 /// @returns the struct member pointer
1096 ast::StructMember* Member(const std::string& name,
1097 type::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001098 ast::DecorationList decorations = {}) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001099 return create<ast::StructMember>(source_, Symbols().Register(name), type,
Ben Claytond614dd52021-03-15 10:43:11 +00001100 std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001101 }
1102
Ben Claytonbab31972021-02-08 21:16:21 +00001103 /// Creates a ast::StructMember with the given byte offset
1104 /// @param offset the offset to use in the StructMemberOffsetDecoration
1105 /// @param name the struct member name
1106 /// @param type the struct member type
1107 /// @returns the struct member pointer
1108 ast::StructMember* Member(uint32_t offset,
1109 const std::string& name,
1110 type::Type* type) {
1111 return create<ast::StructMember>(
1112 source_, Symbols().Register(name), type,
James Price95d40772021-03-11 17:39:32 +00001113 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001114 create<ast::StructMemberOffsetDecoration>(offset),
1115 });
1116 }
1117
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001118 /// Creates a ast::BlockStatement with input statements
1119 /// @param statements statements of block
1120 /// @returns the block statement pointer
1121 template <typename... Statements>
1122 ast::BlockStatement* Block(Statements&&... statements) {
1123 return create<ast::BlockStatement>(
1124 ast::StatementList{std::forward<Statements>(statements)...});
1125 }
1126
1127 /// Creates a ast::ElseStatement with input condition and body
1128 /// @param condition the else condition expression
1129 /// @param body the else body
1130 /// @returns the else statement pointer
1131 ast::ElseStatement* Else(ast::Expression* condition,
1132 ast::BlockStatement* body) {
1133 return create<ast::ElseStatement>(condition, body);
1134 }
1135
1136 /// Creates a ast::IfStatement with input condition, body, and optional
1137 /// variadic else statements
1138 /// @param condition the if statement condition expression
1139 /// @param body the if statement body
1140 /// @param elseStatements optional variadic else statements
1141 /// @returns the if statement pointer
1142 template <typename... ElseStatements>
1143 ast::IfStatement* If(ast::Expression* condition,
1144 ast::BlockStatement* body,
1145 ElseStatements&&... elseStatements) {
1146 return create<ast::IfStatement>(
1147 condition, body,
1148 ast::ElseStatementList{
1149 std::forward<ElseStatements>(elseStatements)...});
1150 }
1151
1152 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
1153 /// @param lhs the left hand side expression
1154 /// @param rhs the right hand side expression
1155 /// @returns the assignment statement pointer
1156 ast::AssignmentStatement* Assign(ast::Expression* lhs, ast::Expression* rhs) {
1157 return create<ast::AssignmentStatement>(lhs, rhs);
1158 }
1159
1160 /// Creates a ast::LoopStatement with input body and optional continuing
1161 /// @param body the loop body
1162 /// @param continuing the optional continuing block
1163 /// @returns the loop statement pointer
1164 ast::LoopStatement* Loop(ast::BlockStatement* body,
1165 ast::BlockStatement* continuing = nullptr) {
1166 return create<ast::LoopStatement>(body, continuing);
1167 }
1168
1169 /// Creates a ast::VariableDeclStatement for the input variable
1170 /// @param var the variable to wrap in a decl statement
1171 /// @returns the variable decl statement pointer
1172 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1173 return create<ast::VariableDeclStatement>(var);
1174 }
1175
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001176 /// Sets the current builder source to `src`
1177 /// @param src the Source used for future create() calls
1178 void SetSource(const Source& src) {
1179 AssertNotMoved();
1180 source_ = src;
1181 }
1182
1183 /// Sets the current builder source to `loc`
1184 /// @param loc the Source used for future create() calls
1185 void SetSource(const Source::Location& loc) {
1186 AssertNotMoved();
1187 source_ = Source(loc);
1188 }
1189
Ben Clayton33352542021-01-29 16:43:41 +00001190 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001191 /// @note As the Resolver is run when the Program is built, this will only be
1192 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001193 /// @param expr the AST expression
1194 /// @return the resolved semantic type for the expression, or nullptr if the
1195 /// expression has no resolved type.
1196 type::Type* TypeOf(ast::Expression* expr) const;
1197
Ben Clayton401b96b2021-02-03 17:19:59 +00001198 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001199 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001200 /// nodes.
1201 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1202 /// @return the ast::Statement that wraps the ast::Expression
1203 ast::Statement* WrapInStatement(ast::Expression* expr);
1204 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001205 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001206 /// these nodes.
1207 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1208 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1209 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1210 /// Returns the statement argument. Used as a passthrough-overload by
1211 /// WrapInFunction().
1212 /// @param stmt the ast::Statement
1213 /// @return `stmt`
1214 ast::Statement* WrapInStatement(ast::Statement* stmt);
1215 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001216 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001217 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001218 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001219 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001220 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001221 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001222 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001223 }
1224 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001225 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001226 /// @returns the function
1227 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001228
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001229 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001230 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231
1232 protected:
1233 /// Asserts that the builder has not been moved.
1234 void AssertNotMoved() const;
1235
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001236 private:
1237 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001238 ASTNodeAllocator ast_nodes_;
1239 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001240 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001241 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001242 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001243 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001244
1245 /// The source to use when creating AST nodes without providing a Source as
1246 /// the first argument.
1247 Source source_;
1248
Ben Clayton5f0ea112021-03-09 10:54:37 +00001249 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001250 /// program when built.
1251 bool resolve_on_build_ = true;
1252
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001253 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1254 bool moved_ = false;
1255};
1256
1257//! @cond Doxygen_Suppress
1258// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1259template <>
1260struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1261 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1262 return t->i32();
1263 }
1264};
1265template <>
1266struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1267 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1268 return t->u32();
1269 }
1270};
1271template <>
1272struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1273 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1274 return t->f32();
1275 }
1276};
1277template <>
1278struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1279 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1280 return t->bool_();
1281 }
1282};
1283template <>
1284struct ProgramBuilder::TypesBuilder::CToAST<void> {
1285 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1286 return t->void_();
1287 }
1288};
1289//! @endcond
1290
1291} // namespace tint
1292
1293#endif // SRC_PROGRAM_BUILDER_H_