blob: 613fb62baa704cdb69ec24d55f73bc59ee8ab57e [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"
Antonio Maioranocea744d2021-03-25 12:55:27 +000026#include "src/ast/case_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/float_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000028#include "src/ast/if_statement.h"
29#include "src/ast/loop_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000030#include "src/ast/member_accessor_expression.h"
31#include "src/ast/module.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000032#include "src/ast/return_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/scalar_constructor_expression.h"
34#include "src/ast/sint_literal.h"
Ben Claytonbab31972021-02-08 21:16:21 +000035#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000036#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000037#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000038#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000039#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000040#include "src/ast/type_constructor_expression.h"
41#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000042#include "src/ast/variable_decl_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000043#include "src/program.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/type/alias_type.h"
45#include "src/type/array_type.h"
46#include "src/type/bool_type.h"
47#include "src/type/f32_type.h"
48#include "src/type/i32_type.h"
49#include "src/type/matrix_type.h"
50#include "src/type/pointer_type.h"
51#include "src/type/struct_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000052#include "src/type/u32_type.h"
53#include "src/type/vector_type.h"
54#include "src/type/void_type.h"
55
56namespace tint {
57
Ben Clayton401b96b2021-02-03 17:19:59 +000058// Forward declarations
59namespace ast {
60class VariableDeclStatement;
61} // namespace ast
62
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063class CloneContext;
64
65/// ProgramBuilder is a mutable builder for a Program.
66/// To construct a Program, populate the builder and then `std::move` it to a
67/// Program.
68class ProgramBuilder {
69 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000070 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
71 using ASTNodeAllocator = BlockAllocator<ast::Node>;
72
73 /// SemNodeAllocator is an alias to BlockAllocator<semantic::Node>
74 using SemNodeAllocator = BlockAllocator<semantic::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000075
76 /// `i32` is a type alias to `int`.
77 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
78 /// WGSL syntax.
79 /// Note: this is intentionally not aliased to uint32_t as we want integer
80 /// literals passed to the builder to match WGSL's integer literal types.
81 using i32 = decltype(1);
82 /// `u32` is a type alias to `unsigned int`.
83 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
84 /// WGSL syntax.
85 /// Note: this is intentionally not aliased to uint32_t as we want integer
86 /// literals passed to the builder to match WGSL's integer literal types.
87 using u32 = decltype(1u);
88 /// `f32` is a type alias to `float`
89 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
90 /// WGSL syntax.
91 using f32 = float;
92
93 /// Constructor
94 ProgramBuilder();
95
96 /// Move constructor
97 /// @param rhs the builder to move
98 ProgramBuilder(ProgramBuilder&& rhs);
99
100 /// Destructor
101 virtual ~ProgramBuilder();
102
103 /// Move assignment operator
104 /// @param rhs the builder to move
105 /// @return this builder
106 ProgramBuilder& operator=(ProgramBuilder&& rhs);
107
Ben Claytone43c8302021-01-29 11:59:32 +0000108 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
109 /// making a deep clone of the Program contents.
110 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
111 /// existing immutable program.
112 /// As the returned ProgramBuilder wraps `program`, `program` must not be
113 /// destructed or assigned while using the returned ProgramBuilder.
114 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
115 /// function. See crbug.com/tint/460.
116 /// @param program the immutable Program to wrap
117 /// @return the ProgramBuilder that wraps `program`
118 static ProgramBuilder Wrap(const Program* program);
119
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000120 /// @returns a reference to the program's types
121 type::Manager& Types() {
122 AssertNotMoved();
123 return types_;
124 }
125
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000126 /// @returns a reference to the program's types
127 const type::Manager& Types() const {
128 AssertNotMoved();
129 return types_;
130 }
131
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000132 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000133 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000134 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000135 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000136 }
137
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000138 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000139 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000140 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000141 return ast_nodes_;
142 }
143
144 /// @returns a reference to the program's semantic nodes storage
145 SemNodeAllocator& SemNodes() {
146 AssertNotMoved();
147 return sem_nodes_;
148 }
149
150 /// @returns a reference to the program's semantic nodes storage
151 const SemNodeAllocator& SemNodes() const {
152 AssertNotMoved();
153 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000154 }
155
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000156 /// @returns a reference to the program's AST root Module
157 ast::Module& AST() {
158 AssertNotMoved();
159 return *ast_;
160 }
161
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000162 /// @returns a reference to the program's AST root Module
163 const ast::Module& AST() const {
164 AssertNotMoved();
165 return *ast_;
166 }
167
168 /// @returns a reference to the program's semantic info
169 semantic::Info& Sem() {
170 AssertNotMoved();
171 return sem_;
172 }
173
174 /// @returns a reference to the program's semantic info
175 const semantic::Info& Sem() const {
176 AssertNotMoved();
177 return sem_;
178 }
179
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000180 /// @returns a reference to the program's SymbolTable
181 SymbolTable& Symbols() {
182 AssertNotMoved();
183 return symbols_;
184 }
185
Ben Clayton708dc2d2021-01-29 11:22:40 +0000186 /// @returns a reference to the program's SymbolTable
187 const SymbolTable& Symbols() const {
188 AssertNotMoved();
189 return symbols_;
190 }
191
Ben Clayton844217f2021-01-27 18:49:05 +0000192 /// @returns a reference to the program's diagnostics
193 diag::List& Diagnostics() {
194 AssertNotMoved();
195 return diagnostics_;
196 }
197
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000198 /// @returns a reference to the program's diagnostics
199 const diag::List& Diagnostics() const {
200 AssertNotMoved();
201 return diagnostics_;
202 }
203
Ben Clayton5f0ea112021-03-09 10:54:37 +0000204 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000205 /// @param enable the new flag value (defaults to true)
206 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
207
Ben Clayton5f0ea112021-03-09 10:54:37 +0000208 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000209 /// built.
210 bool ResolveOnBuild() const { return resolve_on_build_; }
211
Ben Clayton844217f2021-01-27 18:49:05 +0000212 /// @returns true if the program has no error diagnostics and is not missing
213 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000214 bool IsValid() const;
215
Ben Clayton708dc2d2021-01-29 11:22:40 +0000216 /// Writes a representation of the node to the output stream
217 /// @note unlike str(), to_str() does not automatically demangle the string.
218 /// @param node the AST node
219 /// @param out the stream to write to
220 /// @param indent number of spaces to indent the node when writing
221 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
222 node->to_str(Sem(), out, indent);
223 }
224
225 /// Returns a demangled, string representation of `node`.
226 /// @param node the AST node
227 /// @returns a string representation of the node
228 std::string str(const ast::Node* node) const;
229
Ben Clayton7fdfff12021-01-29 15:17:30 +0000230 /// Creates a new ast::Node owned by the ProgramBuilder. When the
231 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000232 /// @param source the Source of the node
233 /// @param args the arguments to pass to the type constructor
234 /// @returns the node pointer
235 template <typename T, typename... ARGS>
236 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
237 ARGS&&... args) {
238 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000239 return ast_nodes_.Create<T>(source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000240 }
241
Ben Clayton7fdfff12021-01-29 15:17:30 +0000242 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
243 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000244 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000245 /// When the ProgramBuilder is destructed, the ast::Node will also be
246 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000247 /// @returns the node pointer
248 template <typename T>
249 traits::EnableIfIsType<T, ast::Node>* create() {
250 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000251 return ast_nodes_.Create<T>(source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000252 }
253
Ben Clayton7fdfff12021-01-29 15:17:30 +0000254 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
255 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000256 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000257 /// When the ProgramBuilder is destructed, the ast::Node will also be
258 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000259 /// @param arg0 the first arguments to pass to the type constructor
260 /// @param args the remaining arguments to pass to the type constructor
261 /// @returns the node pointer
262 template <typename T, typename ARG0, typename... ARGS>
263 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
264 traits::IsTypeOrDerived<T, ast::Node>::value &&
265 !traits::IsTypeOrDerived<ARG0, Source>::value,
266 T>*
267 create(ARG0&& arg0, ARGS&&... args) {
268 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000269 return ast_nodes_.Create<T>(source_, std::forward<ARG0>(arg0),
270 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000271 }
272
Ben Clayton7fdfff12021-01-29 15:17:30 +0000273 /// Creates a new semantic::Node owned by the ProgramBuilder.
274 /// When the ProgramBuilder is destructed, the semantic::Node will also be
275 /// destructed.
276 /// @param args the arguments to pass to the type constructor
277 /// @returns the node pointer
278 template <typename T, typename... ARGS>
279 traits::EnableIfIsType<T, semantic::Node>* create(ARGS&&... args) {
280 AssertNotMoved();
281 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
282 }
283
284 /// Creates a new type::Type owned by the ProgramBuilder.
285 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
286 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000287 /// Types are unique (de-aliased), and so calling create() for the same `T`
288 /// and arguments will return the same pointer.
289 /// @warning Use this method to acquire a type only if all of its type
290 /// information is provided in the constructor arguments `args`.<br>
291 /// If the type requires additional configuration after construction that
292 /// affect its fundamental type, build the type with `std::make_unique`, make
293 /// any necessary alterations and then call unique_type() instead.
294 /// @param args the arguments to pass to the type constructor
295 /// @returns the de-aliased type pointer
296 template <typename T, typename... ARGS>
297 traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) {
298 static_assert(std::is_base_of<type::Type, T>::value,
299 "T does not derive from type::Type");
300 AssertNotMoved();
301 return types_.Get<T>(std::forward<ARGS>(args)...);
302 }
303
304 /// Marks this builder as moved, preventing any further use of the builder.
305 void MarkAsMoved();
306
307 //////////////////////////////////////////////////////////////////////////////
308 // TypesBuilder
309 //////////////////////////////////////////////////////////////////////////////
310
311 /// TypesBuilder holds basic `tint` types and methods for constructing
312 /// complex types.
313 class TypesBuilder {
314 public:
315 /// Constructor
316 /// @param builder the program builder
317 explicit TypesBuilder(ProgramBuilder* builder);
318
319 /// @return the tint AST type for the C type `T`.
320 template <typename T>
321 type::Type* Of() const {
322 return CToAST<T>::get(this);
323 }
324
325 /// @returns a boolean type
326 type::Bool* bool_() const { return builder->create<type::Bool>(); }
327
328 /// @returns a f32 type
329 type::F32* f32() const { return builder->create<type::F32>(); }
330
331 /// @returns a i32 type
332 type::I32* i32() const { return builder->create<type::I32>(); }
333
334 /// @returns a u32 type
335 type::U32* u32() const { return builder->create<type::U32>(); }
336
337 /// @returns a void type
338 type::Void* void_() const { return builder->create<type::Void>(); }
339
340 /// @return the tint AST type for a 2-element vector of the C type `T`.
341 template <typename T>
342 type::Vector* vec2() const {
343 return builder->create<type::Vector>(Of<T>(), 2);
344 }
345
346 /// @return the tint AST type for a 3-element vector of the C type `T`.
347 template <typename T>
348 type::Vector* vec3() const {
349 return builder->create<type::Vector>(Of<T>(), 3);
350 }
351
352 /// @return the tint AST type for a 4-element vector of the C type `T`.
353 template <typename T>
354 type::Type* vec4() const {
355 return builder->create<type::Vector>(Of<T>(), 4);
356 }
357
358 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
359 template <typename T>
360 type::Matrix* mat2x2() const {
361 return builder->create<type::Matrix>(Of<T>(), 2, 2);
362 }
363
364 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
365 template <typename T>
366 type::Matrix* mat2x3() const {
367 return builder->create<type::Matrix>(Of<T>(), 3, 2);
368 }
369
370 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
371 template <typename T>
372 type::Matrix* mat2x4() const {
373 return builder->create<type::Matrix>(Of<T>(), 4, 2);
374 }
375
376 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
377 template <typename T>
378 type::Matrix* mat3x2() const {
379 return builder->create<type::Matrix>(Of<T>(), 2, 3);
380 }
381
382 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
383 template <typename T>
384 type::Matrix* mat3x3() const {
385 return builder->create<type::Matrix>(Of<T>(), 3, 3);
386 }
387
388 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
389 template <typename T>
390 type::Matrix* mat3x4() const {
391 return builder->create<type::Matrix>(Of<T>(), 4, 3);
392 }
393
394 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
395 template <typename T>
396 type::Matrix* mat4x2() const {
397 return builder->create<type::Matrix>(Of<T>(), 2, 4);
398 }
399
400 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
401 template <typename T>
402 type::Matrix* mat4x3() const {
403 return builder->create<type::Matrix>(Of<T>(), 3, 4);
404 }
405
406 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
407 template <typename T>
408 type::Matrix* mat4x4() const {
409 return builder->create<type::Matrix>(Of<T>(), 4, 4);
410 }
411
412 /// @param subtype the array element type
413 /// @param n the array size. 0 represents a runtime-array.
414 /// @return the tint AST type for a array of size `n` of type `T`
415 type::Array* array(type::Type* subtype, uint32_t n) const {
James Price95d40772021-03-11 17:39:32 +0000416 return builder->create<type::Array>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000417 }
418
Ben Claytonbab31972021-02-08 21:16:21 +0000419 /// @param subtype the array element type
420 /// @param n the array size. 0 represents a runtime-array.
421 /// @param stride the array stride.
422 /// @return the tint AST type for a array of size `n` of type `T`
423 type::Array* array(type::Type* subtype, uint32_t n, uint32_t stride) const {
424 return builder->create<type::Array>(
425 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000426 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000427 builder->create<ast::StrideDecoration>(stride),
428 });
429 }
430
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000431 /// @return the tint AST type for an array of size `N` of type `T`
432 template <typename T, int N = 0>
433 type::Array* array() const {
434 return array(Of<T>(), N);
435 }
436
Ben Claytonbab31972021-02-08 21:16:21 +0000437 /// @param stride the array stride
438 /// @return the tint AST type for an array of size `N` of type `T`
439 template <typename T, int N = 0>
440 type::Array* array(uint32_t stride) const {
441 return array(Of<T>(), N, stride);
442 }
Ben Clayton42d1e092021-02-02 14:29:15 +0000443 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000444 /// @param name the alias name
445 /// @param type the alias type
446 /// @returns the alias pointer
447 type::Alias* alias(const std::string& name, type::Type* type) const {
448 return builder->create<type::Alias>(builder->Symbols().Register(name),
449 type);
450 }
451
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000452 /// @return the tint AST pointer to `type` with the given ast::StorageClass
453 /// @param type the type of the pointer
454 /// @param storage_class the storage class of the pointer
455 type::Pointer* pointer(type::Type* type,
456 ast::StorageClass storage_class) const {
457 return builder->create<type::Pointer>(type, storage_class);
458 }
459
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000460 /// @return the tint AST pointer to type `T` with the given
461 /// ast::StorageClass.
462 /// @param storage_class the storage class of the pointer
463 template <typename T>
464 type::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000465 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000466 }
467
468 /// @param name the struct name
469 /// @param impl the struct implementation
470 /// @returns a struct pointer
471 type::Struct* struct_(const std::string& name, ast::Struct* impl) const {
472 return builder->create<type::Struct>(builder->Symbols().Register(name),
473 impl);
474 }
475
476 private:
477 /// CToAST<T> is specialized for various `T` types and each specialization
478 /// contains a single static `get()` method for obtaining the corresponding
479 /// AST type for the C type `T`.
480 /// `get()` has the signature:
481 /// `static type::Type* get(Types* t)`
482 template <typename T>
483 struct CToAST {};
484
Ben Claytonc7ca7662021-02-17 16:23:52 +0000485 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000486 };
487
488 //////////////////////////////////////////////////////////////////////////////
489 // AST helper methods
490 //////////////////////////////////////////////////////////////////////////////
491
492 /// @param expr the expression
493 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000494 template <typename T>
495 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
496 return expr;
497 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000498
499 /// @param name the identifier name
500 /// @return an ast::IdentifierExpression with the given name
501 ast::IdentifierExpression* Expr(const std::string& name) {
502 return create<ast::IdentifierExpression>(Symbols().Register(name));
503 }
504
Ben Clayton46d78d72021-02-10 21:34:26 +0000505 /// @param symbol the identifier symbol
506 /// @return an ast::IdentifierExpression with the given symbol
507 ast::IdentifierExpression* Expr(Symbol symbol) {
508 return create<ast::IdentifierExpression>(symbol);
509 }
510
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000511 /// @param source the source information
512 /// @param name the identifier name
513 /// @return an ast::IdentifierExpression with the given name
514 ast::IdentifierExpression* Expr(const Source& source,
515 const std::string& name) {
516 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
517 }
518
519 /// @param name the identifier name
520 /// @return an ast::IdentifierExpression with the given name
521 ast::IdentifierExpression* Expr(const char* name) {
522 return create<ast::IdentifierExpression>(Symbols().Register(name));
523 }
524
525 /// @param value the boolean value
526 /// @return a Scalar constructor for the given value
527 ast::ScalarConstructorExpression* Expr(bool value) {
528 return create<ast::ScalarConstructorExpression>(Literal(value));
529 }
530
531 /// @param value the float value
532 /// @return a Scalar constructor for the given value
533 ast::ScalarConstructorExpression* Expr(f32 value) {
534 return create<ast::ScalarConstructorExpression>(Literal(value));
535 }
536
537 /// @param value the integer value
538 /// @return a Scalar constructor for the given value
539 ast::ScalarConstructorExpression* Expr(i32 value) {
540 return create<ast::ScalarConstructorExpression>(Literal(value));
541 }
542
543 /// @param value the unsigned int value
544 /// @return a Scalar constructor for the given value
545 ast::ScalarConstructorExpression* Expr(u32 value) {
546 return create<ast::ScalarConstructorExpression>(Literal(value));
547 }
548
549 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
550 /// `list`.
551 /// @param list the list to append too
552 /// @param arg the arg to create
553 template <typename ARG>
554 void Append(ast::ExpressionList& list, ARG&& arg) {
555 list.emplace_back(Expr(std::forward<ARG>(arg)));
556 }
557
558 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
559 /// then appends them to `list`.
560 /// @param list the list to append too
561 /// @param arg0 the first argument
562 /// @param args the rest of the arguments
563 template <typename ARG0, typename... ARGS>
564 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
565 Append(list, std::forward<ARG0>(arg0));
566 Append(list, std::forward<ARGS>(args)...);
567 }
568
569 /// @return an empty list of expressions
570 ast::ExpressionList ExprList() { return {}; }
571
572 /// @param args the list of expressions
573 /// @return the list of expressions converted to `ast::Expression`s using
574 /// `Expr()`,
575 template <typename... ARGS>
576 ast::ExpressionList ExprList(ARGS&&... args) {
577 ast::ExpressionList list;
578 list.reserve(sizeof...(args));
579 Append(list, std::forward<ARGS>(args)...);
580 return list;
581 }
582
583 /// @param list the list of expressions
584 /// @return `list`
585 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
586
587 /// @param val the boolan value
588 /// @return a boolean literal with the given value
589 ast::BoolLiteral* Literal(bool val) {
590 return create<ast::BoolLiteral>(ty.bool_(), val);
591 }
592
593 /// @param val the float value
594 /// @return a float literal with the given value
595 ast::FloatLiteral* Literal(f32 val) {
596 return create<ast::FloatLiteral>(ty.f32(), val);
597 }
598
599 /// @param val the unsigned int value
600 /// @return a ast::UintLiteral with the given value
601 ast::UintLiteral* Literal(u32 val) {
602 return create<ast::UintLiteral>(ty.u32(), val);
603 }
604
605 /// @param val the integer value
606 /// @return the ast::SintLiteral with the given value
607 ast::SintLiteral* Literal(i32 val) {
608 return create<ast::SintLiteral>(ty.i32(), val);
609 }
610
611 /// @param args the arguments for the type constructor
612 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
613 /// of `args` converted to `ast::Expression`s using `Expr()`
614 template <typename T, typename... ARGS>
615 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
616 return create<ast::TypeConstructorExpression>(
617 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
618 }
619
620 /// @param type the type to construct
621 /// @param args the arguments for the constructor
622 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
623 /// values `args`.
624 template <typename... ARGS>
625 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
626 return create<ast::TypeConstructorExpression>(
627 type, ExprList(std::forward<ARGS>(args)...));
628 }
629
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000630 /// Creates a constructor expression that constructs an object of
631 /// `type` filled with `elem_value`. For example,
632 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
633 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
634 /// @param type the type to construct
635 /// @param elem_value the initial or element value (for vec and mat) to
636 /// construct with
637 /// @return the constructor expression
638 ast::ConstructorExpression* ConstructValueFilledWith(type::Type* type,
639 int elem_value = 0);
640
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000641 /// @param args the arguments for the vector constructor
642 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
643 /// `T`, constructed with the values `args`.
644 template <typename T, typename... ARGS>
645 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
646 return create<ast::TypeConstructorExpression>(
647 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
648 }
649
650 /// @param args the arguments for the vector constructor
651 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
652 /// `T`, constructed with the values `args`.
653 template <typename T, typename... ARGS>
654 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
655 return create<ast::TypeConstructorExpression>(
656 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
657 }
658
659 /// @param args the arguments for the vector constructor
660 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
661 /// `T`, constructed with the values `args`.
662 template <typename T, typename... ARGS>
663 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
664 return create<ast::TypeConstructorExpression>(
665 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
666 }
667
668 /// @param args the arguments for the matrix constructor
669 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
670 /// `T`, constructed with the values `args`.
671 template <typename T, typename... ARGS>
672 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
673 return create<ast::TypeConstructorExpression>(
674 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
675 }
676
677 /// @param args the arguments for the matrix constructor
678 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
679 /// `T`, constructed with the values `args`.
680 template <typename T, typename... ARGS>
681 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
682 return create<ast::TypeConstructorExpression>(
683 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
684 }
685
686 /// @param args the arguments for the matrix constructor
687 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
688 /// `T`, constructed with the values `args`.
689 template <typename T, typename... ARGS>
690 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
691 return create<ast::TypeConstructorExpression>(
692 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
693 }
694
695 /// @param args the arguments for the matrix constructor
696 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
697 /// `T`, constructed with the values `args`.
698 template <typename T, typename... ARGS>
699 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
700 return create<ast::TypeConstructorExpression>(
701 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
702 }
703
704 /// @param args the arguments for the matrix constructor
705 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
706 /// `T`, constructed with the values `args`.
707 template <typename T, typename... ARGS>
708 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
709 return create<ast::TypeConstructorExpression>(
710 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
711 }
712
713 /// @param args the arguments for the matrix constructor
714 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
715 /// `T`, constructed with the values `args`.
716 template <typename T, typename... ARGS>
717 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
718 return create<ast::TypeConstructorExpression>(
719 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
720 }
721
722 /// @param args the arguments for the matrix constructor
723 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
724 /// `T`, constructed with the values `args`.
725 template <typename T, typename... ARGS>
726 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
727 return create<ast::TypeConstructorExpression>(
728 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
729 }
730
731 /// @param args the arguments for the matrix constructor
732 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
733 /// `T`, constructed with the values `args`.
734 template <typename T, typename... ARGS>
735 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
736 return create<ast::TypeConstructorExpression>(
737 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
738 }
739
740 /// @param args the arguments for the matrix constructor
741 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
742 /// `T`, constructed with the values `args`.
743 template <typename T, typename... ARGS>
744 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
745 return create<ast::TypeConstructorExpression>(
746 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
747 }
748
749 /// @param args the arguments for the array constructor
750 /// @return an `ast::TypeConstructorExpression` of an array with element type
751 /// `T`, constructed with the values `args`.
752 template <typename T, int N = 0, typename... ARGS>
753 ast::TypeConstructorExpression* array(ARGS&&... args) {
754 return create<ast::TypeConstructorExpression>(
755 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
756 }
757
758 /// @param subtype the array element type
759 /// @param n the array size. 0 represents a runtime-array.
760 /// @param args the arguments for the array constructor
761 /// @return an `ast::TypeConstructorExpression` of an array with element type
762 /// `subtype`, constructed with the values `args`.
763 template <typename... ARGS>
764 ast::TypeConstructorExpression* array(type::Type* subtype,
765 uint32_t n,
766 ARGS&&... args) {
767 return create<ast::TypeConstructorExpression>(
768 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
769 }
770
771 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000772 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000773 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000774 /// @param constructor constructor expression
775 /// @param decorations variable decorations
776 /// @returns a `ast::Variable` with the given name, storage and type
777 ast::Variable* Var(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000778 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000779 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000780 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000781 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000782 return create<ast::Variable>(Symbols().Register(name), storage, type, false,
783 constructor, decorations);
784 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000785
786 /// @param source the variable source
787 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000788 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000789 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000790 /// @param constructor constructor expression
791 /// @param decorations variable decorations
792 /// @returns a `ast::Variable` with the given name, storage and type
793 ast::Variable* Var(const Source& source,
794 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000795 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000796 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000797 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000798 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000799 return create<ast::Variable>(source, Symbols().Register(name), storage,
800 type, false, constructor, decorations);
801 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000802
Ben Clayton46d78d72021-02-10 21:34:26 +0000803 /// @param symbol the variable symbol
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000804 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000805 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000806 /// @param constructor constructor expression
807 /// @param decorations variable decorations
808 /// @returns a `ast::Variable` with the given symbol, storage and type
809 ast::Variable* Var(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000810 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000811 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000812 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000813 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000814 return create<ast::Variable>(symbol, storage, type, false, constructor,
815 decorations);
816 }
817
818 /// @param source the variable source
819 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000820 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000821 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000822 /// @param constructor constructor expression
823 /// @param decorations variable decorations
824 /// @returns a `ast::Variable` with the given symbol, storage and type
825 ast::Variable* Var(const Source& source,
826 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000827 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000828 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000829 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000830 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000831 return create<ast::Variable>(source, symbol, storage, type, false,
832 constructor, decorations);
833 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000834
835 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000836 /// @param type the variable type
837 /// @param constructor optional constructor expression
838 /// @param decorations optional variable decorations
839 /// @returns a constant `ast::Variable` with the given name, storage and type
840 ast::Variable* Const(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000841 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000842 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000843 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000844 return create<ast::Variable>(Symbols().Register(name),
845 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000846 constructor, decorations);
847 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000848
849 /// @param source the variable source
850 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000851 /// @param type the variable type
852 /// @param constructor optional constructor expression
853 /// @param decorations optional variable decorations
854 /// @returns a constant `ast::Variable` with the given name, storage and type
855 ast::Variable* Const(const Source& source,
856 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000857 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000858 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000859 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000860 return create<ast::Variable>(source, Symbols().Register(name),
861 ast::StorageClass::kNone, type, true,
862 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000863 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000864
Ben Clayton46d78d72021-02-10 21:34:26 +0000865 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000866 /// @param type the variable type
867 /// @param constructor optional constructor expression
868 /// @param decorations optional variable decorations
869 /// @returns a constant `ast::Variable` with the given symbol, storage and
870 /// type
871 ast::Variable* Const(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000872 type::Type* type,
873 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000874 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000875 return create<ast::Variable>(symbol, ast::StorageClass::kNone, type, true,
876 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000877 }
878
879 /// @param source the variable source
880 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000881 /// @param type the variable type
882 /// @param constructor optional constructor expression
883 /// @param decorations optional variable decorations
884 /// @returns a constant `ast::Variable` with the given symbol, storage and
885 /// type
886 ast::Variable* Const(const Source& source,
887 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000888 type::Type* type,
889 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000890 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000891 return create<ast::Variable>(source, symbol, ast::StorageClass::kNone, type,
892 true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000893 }
Ben Clayton37571bc2021-02-16 23:57:01 +0000894
Ben Clayton401b96b2021-02-03 17:19:59 +0000895 /// @param args the arguments to pass to Var()
896 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
897 /// of `args`, which is automatically registered as a global variable with the
898 /// ast::Module.
899 template <typename... ARGS>
900 ast::Variable* Global(ARGS&&... args) {
901 auto* var = Var(std::forward<ARGS>(args)...);
902 AST().AddGlobalVariable(var);
903 return var;
904 }
905
906 /// @param args the arguments to pass to Const()
907 /// @returns a const `ast::Variable` constructed by calling Var() with the
908 /// arguments of `args`, which is automatically registered as a global
909 /// variable with the ast::Module.
910 template <typename... ARGS>
911 ast::Variable* GlobalConst(ARGS&&... args) {
912 auto* var = Const(std::forward<ARGS>(args)...);
913 AST().AddGlobalVariable(var);
914 return var;
915 }
916
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000917 /// @param func the function name
918 /// @param args the function call arguments
919 /// @returns a `ast::CallExpression` to the function `func`, with the
920 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
921 template <typename NAME, typename... ARGS>
922 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
923 return create<ast::CallExpression>(Expr(func),
924 ExprList(std::forward<ARGS>(args)...));
925 }
926
927 /// @param lhs the left hand argument to the addition operation
928 /// @param rhs the right hand argument to the addition operation
929 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
930 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000931 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000932 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
933 Expr(std::forward<LHS>(lhs)),
934 Expr(std::forward<RHS>(rhs)));
935 }
936
937 /// @param lhs the left hand argument to the subtraction operation
938 /// @param rhs the right hand argument to the subtraction operation
939 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
940 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000941 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000942 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
943 Expr(std::forward<LHS>(lhs)),
944 Expr(std::forward<RHS>(rhs)));
945 }
946
947 /// @param lhs the left hand argument to the multiplication operation
948 /// @param rhs the right hand argument to the multiplication operation
949 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
950 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000951 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000952 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
953 Expr(std::forward<LHS>(lhs)),
954 Expr(std::forward<RHS>(rhs)));
955 }
956
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000957 /// @param source the source information
958 /// @param lhs the left hand argument to the multiplication operation
959 /// @param rhs the right hand argument to the multiplication operation
960 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
961 template <typename LHS, typename RHS>
962 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
963 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
964 Expr(std::forward<LHS>(lhs)),
965 Expr(std::forward<RHS>(rhs)));
966 }
967
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000968 /// @param arr the array argument for the array accessor expression
969 /// @param idx the index argument for the array accessor expression
970 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
971 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000972 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000973 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
974 Expr(std::forward<IDX>(idx)));
975 }
976
977 /// @param obj the object for the member accessor expression
978 /// @param idx the index argument for the array accessor expression
979 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
980 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +0000981 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000982 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
983 Expr(std::forward<IDX>(idx)));
984 }
985
Ben Clayton42d1e092021-02-02 14:29:15 +0000986 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000987 /// @param val the offset value
988 /// @returns the offset decoration pointer
989 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
990 return create<ast::StructMemberOffsetDecoration>(source_, val);
991 }
992
Ben Claytond614dd52021-03-15 10:43:11 +0000993 /// Creates a ast::StructMemberSizeDecoration
994 /// @param source the source information
995 /// @param val the size value
996 /// @returns the size decoration pointer
997 ast::StructMemberSizeDecoration* MemberSize(Source source, uint32_t val) {
998 return create<ast::StructMemberSizeDecoration>(source, val);
999 }
1000
1001 /// Creates a ast::StructMemberSizeDecoration
1002 /// @param val the size value
1003 /// @returns the size decoration pointer
1004 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1005 return create<ast::StructMemberSizeDecoration>(source_, val);
1006 }
1007
1008 /// Creates a ast::StructMemberAlignDecoration
1009 /// @param source the source information
1010 /// @param val the align value
1011 /// @returns the align decoration pointer
1012 ast::StructMemberAlignDecoration* MemberAlign(Source source, uint32_t val) {
1013 return create<ast::StructMemberAlignDecoration>(source, val);
1014 }
1015
1016 /// Creates a ast::StructMemberAlignDecoration
1017 /// @param val the align value
1018 /// @returns the align decoration pointer
1019 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1020 return create<ast::StructMemberAlignDecoration>(source_, val);
1021 }
1022
Ben Clayton42d1e092021-02-02 14:29:15 +00001023 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001024 /// @param source the source information
1025 /// @param name the function name
1026 /// @param params the function parameters
1027 /// @param type the function return type
1028 /// @param body the function body
1029 /// @param decorations the function decorations
James Pricefeecbe02021-03-15 17:01:34 +00001030 /// @param return_type_decorations the function return type decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001031 /// @returns the function pointer
1032 ast::Function* Func(Source source,
1033 std::string name,
1034 ast::VariableList params,
1035 type::Type* type,
1036 ast::StatementList body,
James Pricefeecbe02021-03-15 17:01:34 +00001037 ast::DecorationList decorations,
1038 ast::DecorationList return_type_decorations = {}) {
1039 auto* func = create<ast::Function>(source, Symbols().Register(name), params,
1040 type, create<ast::BlockStatement>(body),
1041 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001042 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001043 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001044 }
1045
Ben Clayton42d1e092021-02-02 14:29:15 +00001046 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001047 /// @param name the function name
1048 /// @param params the function parameters
1049 /// @param type the function return type
1050 /// @param body the function body
1051 /// @param decorations the function decorations
James Pricefeecbe02021-03-15 17:01:34 +00001052 /// @param return_type_decorations the function return type decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001053 /// @returns the function pointer
1054 ast::Function* Func(std::string name,
1055 ast::VariableList params,
1056 type::Type* type,
1057 ast::StatementList body,
James Pricefeecbe02021-03-15 17:01:34 +00001058 ast::DecorationList decorations,
1059 ast::DecorationList return_type_decorations = {}) {
1060 auto* func = create<ast::Function>(Symbols().Register(name), params, type,
1061 create<ast::BlockStatement>(body),
1062 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001063 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001064 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001065 }
1066
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001067 /// Creates an ast::ReturnStatement with the input args
1068 /// @param args arguments to construct a return statement with
1069 /// @returns the return statement pointer
1070 template <typename... Args>
1071 ast::ReturnStatement* Return(Args&&... args) {
1072 return create<ast::ReturnStatement>(std::forward<Args>(args)...);
1073 }
1074
Ben Claytond614dd52021-03-15 10:43:11 +00001075 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1076 /// the AST().ConstructedTypes().
1077 /// @param source the source information
1078 /// @param name the struct name
1079 /// @param members the struct members
1080 /// @param decorations the optional struct decorations
1081 /// @returns the struct type
1082 type::Struct* Structure(const Source& source,
1083 const std::string& name,
1084 ast::StructMemberList members,
1085 ast::DecorationList decorations = {}) {
1086 auto* impl =
1087 create<ast::Struct>(source, std::move(members), std::move(decorations));
1088 auto* type = ty.struct_(name, impl);
1089 AST().AddConstructedType(type);
1090 return type;
1091 }
1092
1093 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1094 /// the AST().ConstructedTypes().
1095 /// @param name the struct name
1096 /// @param members the struct members
1097 /// @param decorations the optional struct decorations
1098 /// @returns the struct type
1099 type::Struct* Structure(const std::string& name,
1100 ast::StructMemberList members,
1101 ast::DecorationList decorations = {}) {
1102 auto* impl =
1103 create<ast::Struct>(std::move(members), std::move(decorations));
1104 auto* type = ty.struct_(name, impl);
1105 AST().AddConstructedType(type);
1106 return type;
1107 }
1108
Ben Clayton42d1e092021-02-02 14:29:15 +00001109 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001110 /// @param source the source information
1111 /// @param name the struct member name
1112 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001113 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001114 /// @returns the struct member pointer
1115 ast::StructMember* Member(const Source& source,
1116 const std::string& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001117 type::Type* type,
1118 ast::DecorationList decorations = {}) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001119 return create<ast::StructMember>(source, Symbols().Register(name), type,
Ben Claytond614dd52021-03-15 10:43:11 +00001120 std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001121 }
1122
Ben Clayton42d1e092021-02-02 14:29:15 +00001123 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001124 /// @param name the struct member name
1125 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001126 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001127 /// @returns the struct member pointer
1128 ast::StructMember* Member(const std::string& name,
1129 type::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001130 ast::DecorationList decorations = {}) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001131 return create<ast::StructMember>(source_, Symbols().Register(name), type,
Ben Claytond614dd52021-03-15 10:43:11 +00001132 std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001133 }
1134
Ben Claytonbab31972021-02-08 21:16:21 +00001135 /// Creates a ast::StructMember with the given byte offset
1136 /// @param offset the offset to use in the StructMemberOffsetDecoration
1137 /// @param name the struct member name
1138 /// @param type the struct member type
1139 /// @returns the struct member pointer
1140 ast::StructMember* Member(uint32_t offset,
1141 const std::string& name,
1142 type::Type* type) {
1143 return create<ast::StructMember>(
1144 source_, Symbols().Register(name), type,
James Price95d40772021-03-11 17:39:32 +00001145 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001146 create<ast::StructMemberOffsetDecoration>(offset),
1147 });
1148 }
1149
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001150 /// Creates a ast::BlockStatement with input statements
1151 /// @param statements statements of block
1152 /// @returns the block statement pointer
1153 template <typename... Statements>
1154 ast::BlockStatement* Block(Statements&&... statements) {
1155 return create<ast::BlockStatement>(
1156 ast::StatementList{std::forward<Statements>(statements)...});
1157 }
1158
1159 /// Creates a ast::ElseStatement with input condition and body
1160 /// @param condition the else condition expression
1161 /// @param body the else body
1162 /// @returns the else statement pointer
1163 ast::ElseStatement* Else(ast::Expression* condition,
1164 ast::BlockStatement* body) {
1165 return create<ast::ElseStatement>(condition, body);
1166 }
1167
1168 /// Creates a ast::IfStatement with input condition, body, and optional
1169 /// variadic else statements
1170 /// @param condition the if statement condition expression
1171 /// @param body the if statement body
1172 /// @param elseStatements optional variadic else statements
1173 /// @returns the if statement pointer
1174 template <typename... ElseStatements>
1175 ast::IfStatement* If(ast::Expression* condition,
1176 ast::BlockStatement* body,
1177 ElseStatements&&... elseStatements) {
1178 return create<ast::IfStatement>(
1179 condition, body,
1180 ast::ElseStatementList{
1181 std::forward<ElseStatements>(elseStatements)...});
1182 }
1183
1184 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001185 /// @param lhs the left hand side expression initializer
1186 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001187 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001188 template <typename LhsExpressionInit, typename RhsExpressionInit>
1189 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1190 RhsExpressionInit&& rhs) {
1191 return create<ast::AssignmentStatement>(
1192 Expr(std::forward<LhsExpressionInit>(lhs)),
1193 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001194 }
1195
1196 /// Creates a ast::LoopStatement with input body and optional continuing
1197 /// @param body the loop body
1198 /// @param continuing the optional continuing block
1199 /// @returns the loop statement pointer
1200 ast::LoopStatement* Loop(ast::BlockStatement* body,
1201 ast::BlockStatement* continuing = nullptr) {
1202 return create<ast::LoopStatement>(body, continuing);
1203 }
1204
1205 /// Creates a ast::VariableDeclStatement for the input variable
1206 /// @param var the variable to wrap in a decl statement
1207 /// @returns the variable decl statement pointer
1208 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1209 return create<ast::VariableDeclStatement>(var);
1210 }
1211
Antonio Maioranocea744d2021-03-25 12:55:27 +00001212 /// Creates a ast::SwitchStatement with input expression and cases
1213 /// @param condition the condition expression initializer
1214 /// @param cases case statements
1215 /// @returns the switch statement pointer
1216 template <typename ExpressionInit, typename... Cases>
1217 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1218 return create<ast::SwitchStatement>(
1219 Expr(std::forward<ExpressionInit>(condition)),
1220 ast::CaseStatementList{std::forward<Cases>(cases)...});
1221 }
1222
1223 /// Creates a ast::CaseStatement with input list of selectors, and body
1224 /// @param selectors list of selectors
1225 /// @param body the case body
1226 /// @returns the case statement pointer
1227 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1228 ast::BlockStatement* body = nullptr) {
1229 return create<ast::CaseStatement>(std::move(selectors),
1230 body ? body : Block());
1231 }
1232
1233 /// Convenient overload that takes a single selector
1234 /// @param selector a single case selector
1235 /// @param body the case body
1236 /// @returns the case statement pointer
1237 ast::CaseStatement* Case(ast::IntLiteral* selector,
1238 ast::BlockStatement* body = nullptr) {
1239 return Case(ast::CaseSelectorList{selector}, body);
1240 }
1241
1242 /// Convenience function that creates a 'default' ast::CaseStatement
1243 /// @param body the case body
1244 /// @returns the case statement pointer
1245 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1246 return Case(ast::CaseSelectorList{}, body);
1247 }
1248
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001249 /// Sets the current builder source to `src`
1250 /// @param src the Source used for future create() calls
1251 void SetSource(const Source& src) {
1252 AssertNotMoved();
1253 source_ = src;
1254 }
1255
1256 /// Sets the current builder source to `loc`
1257 /// @param loc the Source used for future create() calls
1258 void SetSource(const Source::Location& loc) {
1259 AssertNotMoved();
1260 source_ = Source(loc);
1261 }
1262
Ben Clayton33352542021-01-29 16:43:41 +00001263 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001264 /// @note As the Resolver is run when the Program is built, this will only be
1265 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001266 /// @param expr the AST expression
1267 /// @return the resolved semantic type for the expression, or nullptr if the
1268 /// expression has no resolved type.
1269 type::Type* TypeOf(ast::Expression* expr) const;
1270
Ben Clayton401b96b2021-02-03 17:19:59 +00001271 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001272 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001273 /// nodes.
1274 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1275 /// @return the ast::Statement that wraps the ast::Expression
1276 ast::Statement* WrapInStatement(ast::Expression* expr);
1277 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001278 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001279 /// these nodes.
1280 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1281 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1282 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1283 /// Returns the statement argument. Used as a passthrough-overload by
1284 /// WrapInFunction().
1285 /// @param stmt the ast::Statement
1286 /// @return `stmt`
1287 ast::Statement* WrapInStatement(ast::Statement* stmt);
1288 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001289 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001290 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001291 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001292 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001293 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001294 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001295 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001296 }
1297 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001298 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001299 /// @returns the function
1300 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001301
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001302 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001303 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001304
1305 protected:
1306 /// Asserts that the builder has not been moved.
1307 void AssertNotMoved() const;
1308
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001309 private:
1310 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001311 ASTNodeAllocator ast_nodes_;
1312 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001313 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001314 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001315 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001316 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001317
1318 /// The source to use when creating AST nodes without providing a Source as
1319 /// the first argument.
1320 Source source_;
1321
Ben Clayton5f0ea112021-03-09 10:54:37 +00001322 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001323 /// program when built.
1324 bool resolve_on_build_ = true;
1325
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001326 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1327 bool moved_ = false;
1328};
1329
1330//! @cond Doxygen_Suppress
1331// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1332template <>
1333struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1334 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1335 return t->i32();
1336 }
1337};
1338template <>
1339struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1340 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1341 return t->u32();
1342 }
1343};
1344template <>
1345struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1346 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1347 return t->f32();
1348 }
1349};
1350template <>
1351struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1352 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1353 return t->bool_();
1354 }
1355};
1356template <>
1357struct ProgramBuilder::TypesBuilder::CToAST<void> {
1358 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1359 return t->void_();
1360 }
1361};
1362//! @endcond
1363
1364} // namespace tint
1365
1366#endif // SRC_PROGRAM_BUILDER_H_