blob: 6c728cc4b51d8eb0497a36f86ca4b3b0d0b61ab9 [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"
James Price68f558f2021-04-06 15:51:47 +000035#include "src/ast/stage_decoration.h"
Ben Claytonbab31972021-02-08 21:16:21 +000036#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000037#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000039#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000040#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000041#include "src/ast/type_constructor_expression.h"
42#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000043#include "src/ast/variable_decl_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/program.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/type/alias_type.h"
46#include "src/type/array_type.h"
47#include "src/type/bool_type.h"
48#include "src/type/f32_type.h"
49#include "src/type/i32_type.h"
50#include "src/type/matrix_type.h"
51#include "src/type/pointer_type.h"
52#include "src/type/struct_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000053#include "src/type/u32_type.h"
54#include "src/type/vector_type.h"
55#include "src/type/void_type.h"
56
57namespace tint {
58
Ben Clayton401b96b2021-02-03 17:19:59 +000059// Forward declarations
60namespace ast {
61class VariableDeclStatement;
62} // namespace ast
63
Ben Claytona6b9a8e2021-01-26 16:57:10 +000064class CloneContext;
65
66/// ProgramBuilder is a mutable builder for a Program.
67/// To construct a Program, populate the builder and then `std::move` it to a
68/// Program.
69class ProgramBuilder {
70 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000071 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
72 using ASTNodeAllocator = BlockAllocator<ast::Node>;
73
74 /// SemNodeAllocator is an alias to BlockAllocator<semantic::Node>
75 using SemNodeAllocator = BlockAllocator<semantic::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000076
77 /// `i32` is a type alias to `int`.
78 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
79 /// WGSL syntax.
80 /// Note: this is intentionally not aliased to uint32_t as we want integer
81 /// literals passed to the builder to match WGSL's integer literal types.
82 using i32 = decltype(1);
83 /// `u32` is a type alias to `unsigned int`.
84 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
85 /// WGSL syntax.
86 /// Note: this is intentionally not aliased to uint32_t as we want integer
87 /// literals passed to the builder to match WGSL's integer literal types.
88 using u32 = decltype(1u);
89 /// `f32` is a type alias to `float`
90 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
91 /// WGSL syntax.
92 using f32 = float;
93
94 /// Constructor
95 ProgramBuilder();
96
97 /// Move constructor
98 /// @param rhs the builder to move
99 ProgramBuilder(ProgramBuilder&& rhs);
100
101 /// Destructor
102 virtual ~ProgramBuilder();
103
104 /// Move assignment operator
105 /// @param rhs the builder to move
106 /// @return this builder
107 ProgramBuilder& operator=(ProgramBuilder&& rhs);
108
Ben Claytone43c8302021-01-29 11:59:32 +0000109 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
110 /// making a deep clone of the Program contents.
111 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
112 /// existing immutable program.
113 /// As the returned ProgramBuilder wraps `program`, `program` must not be
114 /// destructed or assigned while using the returned ProgramBuilder.
115 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
116 /// function. See crbug.com/tint/460.
117 /// @param program the immutable Program to wrap
118 /// @return the ProgramBuilder that wraps `program`
119 static ProgramBuilder Wrap(const Program* program);
120
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000121 /// @returns a reference to the program's types
122 type::Manager& Types() {
123 AssertNotMoved();
124 return types_;
125 }
126
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000127 /// @returns a reference to the program's types
128 const type::Manager& Types() const {
129 AssertNotMoved();
130 return types_;
131 }
132
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000133 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000134 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000135 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000136 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000137 }
138
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000139 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000140 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000141 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000142 return ast_nodes_;
143 }
144
145 /// @returns a reference to the program's semantic nodes storage
146 SemNodeAllocator& SemNodes() {
147 AssertNotMoved();
148 return sem_nodes_;
149 }
150
151 /// @returns a reference to the program's semantic nodes storage
152 const SemNodeAllocator& SemNodes() const {
153 AssertNotMoved();
154 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000155 }
156
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000157 /// @returns a reference to the program's AST root Module
158 ast::Module& AST() {
159 AssertNotMoved();
160 return *ast_;
161 }
162
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000163 /// @returns a reference to the program's AST root Module
164 const ast::Module& AST() const {
165 AssertNotMoved();
166 return *ast_;
167 }
168
169 /// @returns a reference to the program's semantic info
170 semantic::Info& Sem() {
171 AssertNotMoved();
172 return sem_;
173 }
174
175 /// @returns a reference to the program's semantic info
176 const semantic::Info& Sem() const {
177 AssertNotMoved();
178 return sem_;
179 }
180
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000181 /// @returns a reference to the program's SymbolTable
182 SymbolTable& Symbols() {
183 AssertNotMoved();
184 return symbols_;
185 }
186
Ben Clayton708dc2d2021-01-29 11:22:40 +0000187 /// @returns a reference to the program's SymbolTable
188 const SymbolTable& Symbols() const {
189 AssertNotMoved();
190 return symbols_;
191 }
192
Ben Clayton844217f2021-01-27 18:49:05 +0000193 /// @returns a reference to the program's diagnostics
194 diag::List& Diagnostics() {
195 AssertNotMoved();
196 return diagnostics_;
197 }
198
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000199 /// @returns a reference to the program's diagnostics
200 const diag::List& Diagnostics() const {
201 AssertNotMoved();
202 return diagnostics_;
203 }
204
Ben Clayton5f0ea112021-03-09 10:54:37 +0000205 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000206 /// @param enable the new flag value (defaults to true)
207 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
208
Ben Clayton5f0ea112021-03-09 10:54:37 +0000209 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000210 /// built.
211 bool ResolveOnBuild() const { return resolve_on_build_; }
212
Ben Clayton844217f2021-01-27 18:49:05 +0000213 /// @returns true if the program has no error diagnostics and is not missing
214 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000215 bool IsValid() const;
216
Ben Clayton708dc2d2021-01-29 11:22:40 +0000217 /// Writes a representation of the node to the output stream
218 /// @note unlike str(), to_str() does not automatically demangle the string.
219 /// @param node the AST node
220 /// @param out the stream to write to
221 /// @param indent number of spaces to indent the node when writing
222 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
223 node->to_str(Sem(), out, indent);
224 }
225
226 /// Returns a demangled, string representation of `node`.
227 /// @param node the AST node
228 /// @returns a string representation of the node
229 std::string str(const ast::Node* node) const;
230
Ben Clayton7fdfff12021-01-29 15:17:30 +0000231 /// Creates a new ast::Node owned by the ProgramBuilder. When the
232 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000233 /// @param source the Source of the node
234 /// @param args the arguments to pass to the type constructor
235 /// @returns the node pointer
236 template <typename T, typename... ARGS>
237 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
238 ARGS&&... args) {
239 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000240 return ast_nodes_.Create<T>(source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000241 }
242
Ben Clayton7fdfff12021-01-29 15:17:30 +0000243 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
244 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000245 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000246 /// When the ProgramBuilder is destructed, the ast::Node will also be
247 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000248 /// @returns the node pointer
249 template <typename T>
250 traits::EnableIfIsType<T, ast::Node>* create() {
251 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000252 return ast_nodes_.Create<T>(source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000253 }
254
Ben Clayton7fdfff12021-01-29 15:17:30 +0000255 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
256 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000257 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000258 /// When the ProgramBuilder is destructed, the ast::Node will also be
259 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000260 /// @param arg0 the first arguments to pass to the type constructor
261 /// @param args the remaining arguments to pass to the type constructor
262 /// @returns the node pointer
263 template <typename T, typename ARG0, typename... ARGS>
264 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
265 traits::IsTypeOrDerived<T, ast::Node>::value &&
266 !traits::IsTypeOrDerived<ARG0, Source>::value,
267 T>*
268 create(ARG0&& arg0, ARGS&&... args) {
269 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000270 return ast_nodes_.Create<T>(source_, std::forward<ARG0>(arg0),
271 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000272 }
273
Ben Clayton7fdfff12021-01-29 15:17:30 +0000274 /// Creates a new semantic::Node owned by the ProgramBuilder.
275 /// When the ProgramBuilder is destructed, the semantic::Node will also be
276 /// destructed.
277 /// @param args the arguments to pass to the type constructor
278 /// @returns the node pointer
279 template <typename T, typename... ARGS>
280 traits::EnableIfIsType<T, semantic::Node>* create(ARGS&&... args) {
281 AssertNotMoved();
282 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
283 }
284
285 /// Creates a new type::Type owned by the ProgramBuilder.
286 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
287 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000288 /// Types are unique (de-aliased), and so calling create() for the same `T`
289 /// and arguments will return the same pointer.
290 /// @warning Use this method to acquire a type only if all of its type
291 /// information is provided in the constructor arguments `args`.<br>
292 /// If the type requires additional configuration after construction that
293 /// affect its fundamental type, build the type with `std::make_unique`, make
294 /// any necessary alterations and then call unique_type() instead.
295 /// @param args the arguments to pass to the type constructor
296 /// @returns the de-aliased type pointer
297 template <typename T, typename... ARGS>
298 traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) {
299 static_assert(std::is_base_of<type::Type, T>::value,
300 "T does not derive from type::Type");
301 AssertNotMoved();
302 return types_.Get<T>(std::forward<ARGS>(args)...);
303 }
304
305 /// Marks this builder as moved, preventing any further use of the builder.
306 void MarkAsMoved();
307
308 //////////////////////////////////////////////////////////////////////////////
309 // TypesBuilder
310 //////////////////////////////////////////////////////////////////////////////
311
312 /// TypesBuilder holds basic `tint` types and methods for constructing
313 /// complex types.
314 class TypesBuilder {
315 public:
316 /// Constructor
317 /// @param builder the program builder
318 explicit TypesBuilder(ProgramBuilder* builder);
319
320 /// @return the tint AST type for the C type `T`.
321 template <typename T>
322 type::Type* Of() const {
323 return CToAST<T>::get(this);
324 }
325
326 /// @returns a boolean type
327 type::Bool* bool_() const { return builder->create<type::Bool>(); }
328
329 /// @returns a f32 type
330 type::F32* f32() const { return builder->create<type::F32>(); }
331
332 /// @returns a i32 type
333 type::I32* i32() const { return builder->create<type::I32>(); }
334
335 /// @returns a u32 type
336 type::U32* u32() const { return builder->create<type::U32>(); }
337
338 /// @returns a void type
339 type::Void* void_() const { return builder->create<type::Void>(); }
340
341 /// @return the tint AST type for a 2-element vector of the C type `T`.
342 template <typename T>
343 type::Vector* vec2() const {
344 return builder->create<type::Vector>(Of<T>(), 2);
345 }
346
347 /// @return the tint AST type for a 3-element vector of the C type `T`.
348 template <typename T>
349 type::Vector* vec3() const {
350 return builder->create<type::Vector>(Of<T>(), 3);
351 }
352
353 /// @return the tint AST type for a 4-element vector of the C type `T`.
354 template <typename T>
355 type::Type* vec4() const {
356 return builder->create<type::Vector>(Of<T>(), 4);
357 }
358
359 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
360 template <typename T>
361 type::Matrix* mat2x2() const {
362 return builder->create<type::Matrix>(Of<T>(), 2, 2);
363 }
364
365 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
366 template <typename T>
367 type::Matrix* mat2x3() const {
368 return builder->create<type::Matrix>(Of<T>(), 3, 2);
369 }
370
371 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
372 template <typename T>
373 type::Matrix* mat2x4() const {
374 return builder->create<type::Matrix>(Of<T>(), 4, 2);
375 }
376
377 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
378 template <typename T>
379 type::Matrix* mat3x2() const {
380 return builder->create<type::Matrix>(Of<T>(), 2, 3);
381 }
382
383 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
384 template <typename T>
385 type::Matrix* mat3x3() const {
386 return builder->create<type::Matrix>(Of<T>(), 3, 3);
387 }
388
389 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
390 template <typename T>
391 type::Matrix* mat3x4() const {
392 return builder->create<type::Matrix>(Of<T>(), 4, 3);
393 }
394
395 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
396 template <typename T>
397 type::Matrix* mat4x2() const {
398 return builder->create<type::Matrix>(Of<T>(), 2, 4);
399 }
400
401 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
402 template <typename T>
403 type::Matrix* mat4x3() const {
404 return builder->create<type::Matrix>(Of<T>(), 3, 4);
405 }
406
407 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
408 template <typename T>
409 type::Matrix* mat4x4() const {
410 return builder->create<type::Matrix>(Of<T>(), 4, 4);
411 }
412
413 /// @param subtype the array element type
414 /// @param n the array size. 0 represents a runtime-array.
415 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000416 type::Array* array(type::Type* subtype, uint32_t n = 0) const {
James Price95d40772021-03-11 17:39:32 +0000417 return builder->create<type::Array>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000418 }
419
Ben Claytonbab31972021-02-08 21:16:21 +0000420 /// @param subtype the array element type
421 /// @param n the array size. 0 represents a runtime-array.
422 /// @param stride the array stride.
423 /// @return the tint AST type for a array of size `n` of type `T`
424 type::Array* array(type::Type* subtype, uint32_t n, uint32_t stride) const {
425 return builder->create<type::Array>(
426 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000427 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000428 builder->create<ast::StrideDecoration>(stride),
429 });
430 }
431
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000432 /// @return the tint AST type for an array of size `N` of type `T`
433 template <typename T, int N = 0>
434 type::Array* array() const {
435 return array(Of<T>(), N);
436 }
437
Ben Claytonbab31972021-02-08 21:16:21 +0000438 /// @param stride the array stride
439 /// @return the tint AST type for an array of size `N` of type `T`
440 template <typename T, int N = 0>
441 type::Array* array(uint32_t stride) const {
442 return array(Of<T>(), N, stride);
443 }
Ben Clayton42d1e092021-02-02 14:29:15 +0000444 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000445 /// @param name the alias name
446 /// @param type the alias type
447 /// @returns the alias pointer
448 type::Alias* alias(const std::string& name, type::Type* type) const {
449 return builder->create<type::Alias>(builder->Symbols().Register(name),
450 type);
451 }
452
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000453 /// @return the tint AST pointer to `type` with the given ast::StorageClass
454 /// @param type the type of the pointer
455 /// @param storage_class the storage class of the pointer
456 type::Pointer* pointer(type::Type* type,
457 ast::StorageClass storage_class) const {
458 return builder->create<type::Pointer>(type, storage_class);
459 }
460
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000461 /// @return the tint AST pointer to type `T` with the given
462 /// ast::StorageClass.
463 /// @param storage_class the storage class of the pointer
464 template <typename T>
465 type::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000466 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000467 }
468
469 /// @param name the struct name
470 /// @param impl the struct implementation
471 /// @returns a struct pointer
472 type::Struct* struct_(const std::string& name, ast::Struct* impl) const {
473 return builder->create<type::Struct>(builder->Symbols().Register(name),
474 impl);
475 }
476
477 private:
478 /// CToAST<T> is specialized for various `T` types and each specialization
479 /// contains a single static `get()` method for obtaining the corresponding
480 /// AST type for the C type `T`.
481 /// `get()` has the signature:
482 /// `static type::Type* get(Types* t)`
483 template <typename T>
484 struct CToAST {};
485
Ben Claytonc7ca7662021-02-17 16:23:52 +0000486 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000487 };
488
489 //////////////////////////////////////////////////////////////////////////////
490 // AST helper methods
491 //////////////////////////////////////////////////////////////////////////////
492
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000493 /// @param name the symbol string
494 /// @return a Symbol with the given name
495 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
496
497 /// @param sym the symbol
498 /// @return `sym`
499 Symbol Sym(Symbol sym) { return sym; }
500
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000501 /// @param expr the expression
502 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000503 template <typename T>
504 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
505 return expr;
506 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000507
508 /// @param name the identifier name
509 /// @return an ast::IdentifierExpression with the given name
510 ast::IdentifierExpression* Expr(const std::string& name) {
511 return create<ast::IdentifierExpression>(Symbols().Register(name));
512 }
513
Ben Clayton46d78d72021-02-10 21:34:26 +0000514 /// @param symbol the identifier symbol
515 /// @return an ast::IdentifierExpression with the given symbol
516 ast::IdentifierExpression* Expr(Symbol symbol) {
517 return create<ast::IdentifierExpression>(symbol);
518 }
519
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000520 /// @param source the source information
521 /// @param name the identifier name
522 /// @return an ast::IdentifierExpression with the given name
523 ast::IdentifierExpression* Expr(const Source& source,
524 const std::string& name) {
525 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
526 }
527
528 /// @param name the identifier name
529 /// @return an ast::IdentifierExpression with the given name
530 ast::IdentifierExpression* Expr(const char* name) {
531 return create<ast::IdentifierExpression>(Symbols().Register(name));
532 }
533
534 /// @param value the boolean value
535 /// @return a Scalar constructor for the given value
536 ast::ScalarConstructorExpression* Expr(bool value) {
537 return create<ast::ScalarConstructorExpression>(Literal(value));
538 }
539
540 /// @param value the float value
541 /// @return a Scalar constructor for the given value
542 ast::ScalarConstructorExpression* Expr(f32 value) {
543 return create<ast::ScalarConstructorExpression>(Literal(value));
544 }
545
546 /// @param value the integer value
547 /// @return a Scalar constructor for the given value
548 ast::ScalarConstructorExpression* Expr(i32 value) {
549 return create<ast::ScalarConstructorExpression>(Literal(value));
550 }
551
552 /// @param value the unsigned int value
553 /// @return a Scalar constructor for the given value
554 ast::ScalarConstructorExpression* Expr(u32 value) {
555 return create<ast::ScalarConstructorExpression>(Literal(value));
556 }
557
558 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
559 /// `list`.
560 /// @param list the list to append too
561 /// @param arg the arg to create
562 template <typename ARG>
563 void Append(ast::ExpressionList& list, ARG&& arg) {
564 list.emplace_back(Expr(std::forward<ARG>(arg)));
565 }
566
567 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
568 /// then appends them to `list`.
569 /// @param list the list to append too
570 /// @param arg0 the first argument
571 /// @param args the rest of the arguments
572 template <typename ARG0, typename... ARGS>
573 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
574 Append(list, std::forward<ARG0>(arg0));
575 Append(list, std::forward<ARGS>(args)...);
576 }
577
578 /// @return an empty list of expressions
579 ast::ExpressionList ExprList() { return {}; }
580
581 /// @param args the list of expressions
582 /// @return the list of expressions converted to `ast::Expression`s using
583 /// `Expr()`,
584 template <typename... ARGS>
585 ast::ExpressionList ExprList(ARGS&&... args) {
586 ast::ExpressionList list;
587 list.reserve(sizeof...(args));
588 Append(list, std::forward<ARGS>(args)...);
589 return list;
590 }
591
592 /// @param list the list of expressions
593 /// @return `list`
594 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
595
596 /// @param val the boolan value
597 /// @return a boolean literal with the given value
598 ast::BoolLiteral* Literal(bool val) {
599 return create<ast::BoolLiteral>(ty.bool_(), val);
600 }
601
602 /// @param val the float value
603 /// @return a float literal with the given value
604 ast::FloatLiteral* Literal(f32 val) {
605 return create<ast::FloatLiteral>(ty.f32(), val);
606 }
607
608 /// @param val the unsigned int value
609 /// @return a ast::UintLiteral with the given value
610 ast::UintLiteral* Literal(u32 val) {
611 return create<ast::UintLiteral>(ty.u32(), val);
612 }
613
614 /// @param val the integer value
615 /// @return the ast::SintLiteral with the given value
616 ast::SintLiteral* Literal(i32 val) {
617 return create<ast::SintLiteral>(ty.i32(), val);
618 }
619
620 /// @param args the arguments for the type constructor
621 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
622 /// of `args` converted to `ast::Expression`s using `Expr()`
623 template <typename T, typename... ARGS>
624 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
625 return create<ast::TypeConstructorExpression>(
626 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
627 }
628
629 /// @param type the type to construct
630 /// @param args the arguments for the constructor
631 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
632 /// values `args`.
633 template <typename... ARGS>
634 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
635 return create<ast::TypeConstructorExpression>(
636 type, ExprList(std::forward<ARGS>(args)...));
637 }
638
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000639 /// Creates a constructor expression that constructs an object of
640 /// `type` filled with `elem_value`. For example,
641 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
642 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
643 /// @param type the type to construct
644 /// @param elem_value the initial or element value (for vec and mat) to
645 /// construct with
646 /// @return the constructor expression
647 ast::ConstructorExpression* ConstructValueFilledWith(type::Type* type,
648 int elem_value = 0);
649
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000650 /// @param args the arguments for the vector constructor
651 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
652 /// `T`, constructed with the values `args`.
653 template <typename T, typename... ARGS>
654 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
655 return create<ast::TypeConstructorExpression>(
656 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
657 }
658
659 /// @param args the arguments for the vector constructor
660 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
661 /// `T`, constructed with the values `args`.
662 template <typename T, typename... ARGS>
663 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
664 return create<ast::TypeConstructorExpression>(
665 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
666 }
667
668 /// @param args the arguments for the vector constructor
669 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
670 /// `T`, constructed with the values `args`.
671 template <typename T, typename... ARGS>
672 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
673 return create<ast::TypeConstructorExpression>(
674 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
675 }
676
677 /// @param args the arguments for the matrix constructor
678 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
679 /// `T`, constructed with the values `args`.
680 template <typename T, typename... ARGS>
681 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
682 return create<ast::TypeConstructorExpression>(
683 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
684 }
685
686 /// @param args the arguments for the matrix constructor
687 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
688 /// `T`, constructed with the values `args`.
689 template <typename T, typename... ARGS>
690 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
691 return create<ast::TypeConstructorExpression>(
692 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
693 }
694
695 /// @param args the arguments for the matrix constructor
696 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
697 /// `T`, constructed with the values `args`.
698 template <typename T, typename... ARGS>
699 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
700 return create<ast::TypeConstructorExpression>(
701 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
702 }
703
704 /// @param args the arguments for the matrix constructor
705 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
706 /// `T`, constructed with the values `args`.
707 template <typename T, typename... ARGS>
708 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
709 return create<ast::TypeConstructorExpression>(
710 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
711 }
712
713 /// @param args the arguments for the matrix constructor
714 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
715 /// `T`, constructed with the values `args`.
716 template <typename T, typename... ARGS>
717 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
718 return create<ast::TypeConstructorExpression>(
719 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
720 }
721
722 /// @param args the arguments for the matrix constructor
723 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
724 /// `T`, constructed with the values `args`.
725 template <typename T, typename... ARGS>
726 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
727 return create<ast::TypeConstructorExpression>(
728 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
729 }
730
731 /// @param args the arguments for the matrix constructor
732 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
733 /// `T`, constructed with the values `args`.
734 template <typename T, typename... ARGS>
735 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
736 return create<ast::TypeConstructorExpression>(
737 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
738 }
739
740 /// @param args the arguments for the matrix constructor
741 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
742 /// `T`, constructed with the values `args`.
743 template <typename T, typename... ARGS>
744 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
745 return create<ast::TypeConstructorExpression>(
746 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
747 }
748
749 /// @param args the arguments for the matrix constructor
750 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
751 /// `T`, constructed with the values `args`.
752 template <typename T, typename... ARGS>
753 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
754 return create<ast::TypeConstructorExpression>(
755 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
756 }
757
758 /// @param args the arguments for the array constructor
759 /// @return an `ast::TypeConstructorExpression` of an array with element type
760 /// `T`, constructed with the values `args`.
761 template <typename T, int N = 0, typename... ARGS>
762 ast::TypeConstructorExpression* array(ARGS&&... args) {
763 return create<ast::TypeConstructorExpression>(
764 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
765 }
766
767 /// @param subtype the array element type
768 /// @param n the array size. 0 represents a runtime-array.
769 /// @param args the arguments for the array constructor
770 /// @return an `ast::TypeConstructorExpression` of an array with element type
771 /// `subtype`, constructed with the values `args`.
772 template <typename... ARGS>
773 ast::TypeConstructorExpression* array(type::Type* subtype,
774 uint32_t n,
775 ARGS&&... args) {
776 return create<ast::TypeConstructorExpression>(
777 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
778 }
779
780 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000781 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000782 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000783 /// @param constructor constructor expression
784 /// @param decorations variable decorations
785 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000786 template <typename NAME>
787 ast::Variable* Var(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000788 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000789 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000790 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000791 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000792 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
793 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000794 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000795
796 /// @param source the variable source
797 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000798 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000799 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000800 /// @param constructor constructor expression
801 /// @param decorations variable decorations
802 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000803 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000804 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000805 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +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 Clayton1b8d9f22021-04-07 11:16:01 +0000810 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000811 type, false, constructor, decorations);
812 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000813
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000814 /// @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
James Pricedaf1f1c2021-04-08 22:15:48 +0000818 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000819 template <typename NAME>
820 ast::Variable* Const(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000821 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000822 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000823 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000824 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000825 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000826 constructor, decorations);
827 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000828
829 /// @param source the variable source
830 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000831 /// @param type the variable type
832 /// @param constructor optional constructor expression
833 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000834 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000835 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000836 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000837 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000838 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000839 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000840 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000841 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000842 ast::StorageClass::kNone, type, true,
843 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000844 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000845
James Pricedaf1f1c2021-04-08 22:15:48 +0000846 /// @param name the parameter name
847 /// @param type the parameter type
848 /// @param decorations optional parameter decorations
849 /// @returns a constant `ast::Variable` with the given name and type
850 template <typename NAME>
851 ast::Variable* Param(NAME&& name,
852 type::Type* type,
853 ast::DecorationList decorations = {}) {
854 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
855 ast::StorageClass::kNone, type, true, nullptr,
856 decorations);
857 }
858
859 /// @param source the parameter source
860 /// @param name the parameter name
861 /// @param type the parameter type
862 /// @param decorations optional parameter decorations
863 /// @returns a constant `ast::Variable` with the given name and type
864 template <typename NAME>
865 ast::Variable* Param(const Source& source,
866 NAME&& name,
867 type::Type* type,
868 ast::DecorationList decorations = {}) {
869 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
870 ast::StorageClass::kNone, type, true, nullptr,
871 decorations);
872 }
873
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>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000910 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000911 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>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000920 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000921 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>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000930 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000931 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
932 Expr(std::forward<LHS>(lhs)),
933 Expr(std::forward<RHS>(rhs)));
934 }
935
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000936 /// @param source the source information
937 /// @param lhs the left hand argument to the multiplication operation
938 /// @param rhs the right hand argument to the multiplication operation
939 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
940 template <typename LHS, typename RHS>
941 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
942 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
943 Expr(std::forward<LHS>(lhs)),
944 Expr(std::forward<RHS>(rhs)));
945 }
946
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000947 /// @param lhs the left hand argument to the division operation
948 /// @param rhs the right hand argument to the division operation
949 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
950 template <typename LHS, typename RHS>
951 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
952 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
953 Expr(std::forward<LHS>(lhs)),
954 Expr(std::forward<RHS>(rhs)));
955 }
956
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000957 /// @param arr the array argument for the array accessor expression
958 /// @param idx the index argument for the array accessor expression
959 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
960 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000961 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000962 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
963 Expr(std::forward<IDX>(idx)));
964 }
965
966 /// @param obj the object for the member accessor expression
967 /// @param idx the index argument for the array accessor expression
968 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
969 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +0000970 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000971 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
972 Expr(std::forward<IDX>(idx)));
973 }
974
Ben Clayton42d1e092021-02-02 14:29:15 +0000975 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000976 /// @param val the offset value
977 /// @returns the offset decoration pointer
978 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
979 return create<ast::StructMemberOffsetDecoration>(source_, val);
980 }
981
Ben Claytond614dd52021-03-15 10:43:11 +0000982 /// Creates a ast::StructMemberSizeDecoration
983 /// @param source the source information
984 /// @param val the size value
985 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +0000986 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
987 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +0000988 return create<ast::StructMemberSizeDecoration>(source, val);
989 }
990
991 /// Creates a ast::StructMemberSizeDecoration
992 /// @param val the size value
993 /// @returns the size decoration pointer
994 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
995 return create<ast::StructMemberSizeDecoration>(source_, val);
996 }
997
998 /// Creates a ast::StructMemberAlignDecoration
999 /// @param source the source information
1000 /// @param val the align value
1001 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001002 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1003 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001004 return create<ast::StructMemberAlignDecoration>(source, val);
1005 }
1006
1007 /// Creates a ast::StructMemberAlignDecoration
1008 /// @param val the align value
1009 /// @returns the align decoration pointer
1010 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1011 return create<ast::StructMemberAlignDecoration>(source_, val);
1012 }
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 source the source information
1016 /// @param name the function name
1017 /// @param params the function parameters
1018 /// @param type the function return type
1019 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001020 /// @param decorations the optional function decorations
1021 /// @param return_type_decorations the optional function return type
1022 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001023 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001024 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001025 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001026 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001027 ast::VariableList params,
1028 type::Type* type,
1029 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001030 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001031 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001032 auto* func =
1033 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1034 type, create<ast::BlockStatement>(body),
1035 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001036 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001037 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001038 }
1039
Ben Clayton42d1e092021-02-02 14:29:15 +00001040 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001041 /// @param name the function name
1042 /// @param params the function parameters
1043 /// @param type the function return type
1044 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001045 /// @param decorations the optional function decorations
1046 /// @param return_type_decorations the optional function return type
1047 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001048 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001049 template <typename NAME>
1050 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001051 ast::VariableList params,
1052 type::Type* type,
1053 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001054 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001055 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001056 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1057 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001058 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001059 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001060 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001061 }
1062
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001063 /// Creates an ast::ReturnStatement with the input args
1064 /// @param args arguments to construct a return statement with
1065 /// @returns the return statement pointer
1066 template <typename... Args>
1067 ast::ReturnStatement* Return(Args&&... args) {
1068 return create<ast::ReturnStatement>(std::forward<Args>(args)...);
1069 }
1070
Ben Claytond614dd52021-03-15 10:43:11 +00001071 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1072 /// the AST().ConstructedTypes().
1073 /// @param source the source information
1074 /// @param name the struct name
1075 /// @param members the struct members
1076 /// @param decorations the optional struct decorations
1077 /// @returns the struct type
1078 type::Struct* Structure(const Source& source,
1079 const std::string& name,
1080 ast::StructMemberList members,
1081 ast::DecorationList decorations = {}) {
1082 auto* impl =
1083 create<ast::Struct>(source, std::move(members), std::move(decorations));
1084 auto* type = ty.struct_(name, impl);
1085 AST().AddConstructedType(type);
1086 return type;
1087 }
1088
1089 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1090 /// the AST().ConstructedTypes().
1091 /// @param name the struct name
1092 /// @param members the struct members
1093 /// @param decorations the optional struct decorations
1094 /// @returns the struct type
1095 type::Struct* Structure(const std::string& name,
1096 ast::StructMemberList members,
1097 ast::DecorationList decorations = {}) {
1098 auto* impl =
1099 create<ast::Struct>(std::move(members), std::move(decorations));
1100 auto* type = ty.struct_(name, impl);
1101 AST().AddConstructedType(type);
1102 return type;
1103 }
1104
Ben Clayton42d1e092021-02-02 14:29:15 +00001105 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001106 /// @param source the source information
1107 /// @param name the struct member name
1108 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001109 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001110 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001111 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001112 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001113 NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001114 type::Type* type,
1115 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001116 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1117 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001118 }
1119
Ben Clayton42d1e092021-02-02 14:29:15 +00001120 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001121 /// @param name the struct member name
1122 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001123 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001124 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001125 template <typename NAME>
1126 ast::StructMember* Member(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001127 type::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001128 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001129 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1130 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001131 }
1132
Ben Claytonbab31972021-02-08 21:16:21 +00001133 /// Creates a ast::StructMember with the given byte offset
1134 /// @param offset the offset to use in the StructMemberOffsetDecoration
1135 /// @param name the struct member name
1136 /// @param type the struct member type
1137 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001138 template <typename NAME>
1139 ast::StructMember* Member(uint32_t offset, NAME&& name, type::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001140 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001141 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001142 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001143 create<ast::StructMemberOffsetDecoration>(offset),
1144 });
1145 }
1146
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001147 /// Creates a ast::BlockStatement with input statements
1148 /// @param statements statements of block
1149 /// @returns the block statement pointer
1150 template <typename... Statements>
1151 ast::BlockStatement* Block(Statements&&... statements) {
1152 return create<ast::BlockStatement>(
1153 ast::StatementList{std::forward<Statements>(statements)...});
1154 }
1155
1156 /// Creates a ast::ElseStatement with input condition and body
1157 /// @param condition the else condition expression
1158 /// @param body the else body
1159 /// @returns the else statement pointer
1160 ast::ElseStatement* Else(ast::Expression* condition,
1161 ast::BlockStatement* body) {
1162 return create<ast::ElseStatement>(condition, body);
1163 }
1164
1165 /// Creates a ast::IfStatement with input condition, body, and optional
1166 /// variadic else statements
1167 /// @param condition the if statement condition expression
1168 /// @param body the if statement body
1169 /// @param elseStatements optional variadic else statements
1170 /// @returns the if statement pointer
1171 template <typename... ElseStatements>
1172 ast::IfStatement* If(ast::Expression* condition,
1173 ast::BlockStatement* body,
1174 ElseStatements&&... elseStatements) {
1175 return create<ast::IfStatement>(
1176 condition, body,
1177 ast::ElseStatementList{
1178 std::forward<ElseStatements>(elseStatements)...});
1179 }
1180
1181 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001182 /// @param lhs the left hand side expression initializer
1183 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001184 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001185 template <typename LhsExpressionInit, typename RhsExpressionInit>
1186 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1187 RhsExpressionInit&& rhs) {
1188 return create<ast::AssignmentStatement>(
1189 Expr(std::forward<LhsExpressionInit>(lhs)),
1190 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001191 }
1192
1193 /// Creates a ast::LoopStatement with input body and optional continuing
1194 /// @param body the loop body
1195 /// @param continuing the optional continuing block
1196 /// @returns the loop statement pointer
1197 ast::LoopStatement* Loop(ast::BlockStatement* body,
1198 ast::BlockStatement* continuing = nullptr) {
1199 return create<ast::LoopStatement>(body, continuing);
1200 }
1201
1202 /// Creates a ast::VariableDeclStatement for the input variable
1203 /// @param var the variable to wrap in a decl statement
1204 /// @returns the variable decl statement pointer
1205 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1206 return create<ast::VariableDeclStatement>(var);
1207 }
1208
Antonio Maioranocea744d2021-03-25 12:55:27 +00001209 /// Creates a ast::SwitchStatement with input expression and cases
1210 /// @param condition the condition expression initializer
1211 /// @param cases case statements
1212 /// @returns the switch statement pointer
1213 template <typename ExpressionInit, typename... Cases>
1214 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1215 return create<ast::SwitchStatement>(
1216 Expr(std::forward<ExpressionInit>(condition)),
1217 ast::CaseStatementList{std::forward<Cases>(cases)...});
1218 }
1219
1220 /// Creates a ast::CaseStatement with input list of selectors, and body
1221 /// @param selectors list of selectors
1222 /// @param body the case body
1223 /// @returns the case statement pointer
1224 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1225 ast::BlockStatement* body = nullptr) {
1226 return create<ast::CaseStatement>(std::move(selectors),
1227 body ? body : Block());
1228 }
1229
1230 /// Convenient overload that takes a single selector
1231 /// @param selector a single case selector
1232 /// @param body the case body
1233 /// @returns the case statement pointer
1234 ast::CaseStatement* Case(ast::IntLiteral* selector,
1235 ast::BlockStatement* body = nullptr) {
1236 return Case(ast::CaseSelectorList{selector}, body);
1237 }
1238
1239 /// Convenience function that creates a 'default' ast::CaseStatement
1240 /// @param body the case body
1241 /// @returns the case statement pointer
1242 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1243 return Case(ast::CaseSelectorList{}, body);
1244 }
1245
James Price68f558f2021-04-06 15:51:47 +00001246 /// Creates an ast::BuiltinDecoration
1247 /// @param source the source information
1248 /// @param builtin the builtin value
1249 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001250 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001251 return create<ast::BuiltinDecoration>(source, builtin);
1252 }
1253
1254 /// Creates an ast::BuiltinDecoration
1255 /// @param builtin the builtin value
1256 /// @returns the builtin decoration pointer
1257 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1258 return create<ast::BuiltinDecoration>(source_, builtin);
1259 }
1260
1261 /// Creates an ast::LocationDecoration
1262 /// @param source the source information
1263 /// @param location the location value
1264 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001265 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001266 return create<ast::LocationDecoration>(source, location);
1267 }
1268
1269 /// Creates an ast::LocationDecoration
1270 /// @param location the location value
1271 /// @returns the location decoration pointer
1272 ast::LocationDecoration* Location(uint32_t location) {
1273 return create<ast::LocationDecoration>(source_, location);
1274 }
1275
1276 /// Creates an ast::StageDecoration
1277 /// @param source the source information
1278 /// @param stage the pipeline stage
1279 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001280 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001281 return create<ast::StageDecoration>(source, stage);
1282 }
1283
1284 /// Creates an ast::StageDecoration
1285 /// @param stage the pipeline stage
1286 /// @returns the stage decoration pointer
1287 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1288 return create<ast::StageDecoration>(source_, stage);
1289 }
1290
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001291 /// Sets the current builder source to `src`
1292 /// @param src the Source used for future create() calls
1293 void SetSource(const Source& src) {
1294 AssertNotMoved();
1295 source_ = src;
1296 }
1297
1298 /// Sets the current builder source to `loc`
1299 /// @param loc the Source used for future create() calls
1300 void SetSource(const Source::Location& loc) {
1301 AssertNotMoved();
1302 source_ = Source(loc);
1303 }
1304
Ben Clayton33352542021-01-29 16:43:41 +00001305 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001306 /// @note As the Resolver is run when the Program is built, this will only be
1307 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001308 /// @param expr the AST expression
1309 /// @return the resolved semantic type for the expression, or nullptr if the
1310 /// expression has no resolved type.
1311 type::Type* TypeOf(ast::Expression* expr) const;
1312
Ben Clayton401b96b2021-02-03 17:19:59 +00001313 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001314 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001315 /// nodes.
1316 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1317 /// @return the ast::Statement that wraps the ast::Expression
1318 ast::Statement* WrapInStatement(ast::Expression* expr);
1319 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001320 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001321 /// these nodes.
1322 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1323 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1324 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1325 /// Returns the statement argument. Used as a passthrough-overload by
1326 /// WrapInFunction().
1327 /// @param stmt the ast::Statement
1328 /// @return `stmt`
1329 ast::Statement* WrapInStatement(ast::Statement* stmt);
1330 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001331 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001332 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001333 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001334 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001335 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001336 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001337 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001338 }
1339 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001340 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001341 /// @returns the function
1342 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001343
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001344 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001345 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001346
1347 protected:
1348 /// Asserts that the builder has not been moved.
1349 void AssertNotMoved() const;
1350
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001351 private:
1352 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001353 ASTNodeAllocator ast_nodes_;
1354 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001355 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001356 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001357 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001358 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001359
1360 /// The source to use when creating AST nodes without providing a Source as
1361 /// the first argument.
1362 Source source_;
1363
Ben Clayton5f0ea112021-03-09 10:54:37 +00001364 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001365 /// program when built.
1366 bool resolve_on_build_ = true;
1367
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001368 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1369 bool moved_ = false;
1370};
1371
1372//! @cond Doxygen_Suppress
1373// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1374template <>
1375struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1376 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1377 return t->i32();
1378 }
1379};
1380template <>
1381struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1382 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1383 return t->u32();
1384 }
1385};
1386template <>
1387struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1388 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1389 return t->f32();
1390 }
1391};
1392template <>
1393struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1394 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1395 return t->bool_();
1396 }
1397};
1398template <>
1399struct ProgramBuilder::TypesBuilder::CToAST<void> {
1400 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1401 return t->void_();
1402 }
1403};
1404//! @endcond
1405
1406} // namespace tint
1407
1408#endif // SRC_PROGRAM_BUILDER_H_