blob: b063779277300904799c97fad4b985fe21e23921 [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`
416 type::Array* array(type::Type* subtype, uint32_t n) 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
493 /// @param expr the expression
494 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000495 template <typename T>
496 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
497 return expr;
498 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000499
500 /// @param name the identifier name
501 /// @return an ast::IdentifierExpression with the given name
502 ast::IdentifierExpression* Expr(const std::string& name) {
503 return create<ast::IdentifierExpression>(Symbols().Register(name));
504 }
505
Ben Clayton46d78d72021-02-10 21:34:26 +0000506 /// @param symbol the identifier symbol
507 /// @return an ast::IdentifierExpression with the given symbol
508 ast::IdentifierExpression* Expr(Symbol symbol) {
509 return create<ast::IdentifierExpression>(symbol);
510 }
511
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000512 /// @param source the source information
513 /// @param name the identifier name
514 /// @return an ast::IdentifierExpression with the given name
515 ast::IdentifierExpression* Expr(const Source& source,
516 const std::string& name) {
517 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
518 }
519
520 /// @param name the identifier name
521 /// @return an ast::IdentifierExpression with the given name
522 ast::IdentifierExpression* Expr(const char* name) {
523 return create<ast::IdentifierExpression>(Symbols().Register(name));
524 }
525
526 /// @param value the boolean value
527 /// @return a Scalar constructor for the given value
528 ast::ScalarConstructorExpression* Expr(bool value) {
529 return create<ast::ScalarConstructorExpression>(Literal(value));
530 }
531
532 /// @param value the float value
533 /// @return a Scalar constructor for the given value
534 ast::ScalarConstructorExpression* Expr(f32 value) {
535 return create<ast::ScalarConstructorExpression>(Literal(value));
536 }
537
538 /// @param value the integer value
539 /// @return a Scalar constructor for the given value
540 ast::ScalarConstructorExpression* Expr(i32 value) {
541 return create<ast::ScalarConstructorExpression>(Literal(value));
542 }
543
544 /// @param value the unsigned int value
545 /// @return a Scalar constructor for the given value
546 ast::ScalarConstructorExpression* Expr(u32 value) {
547 return create<ast::ScalarConstructorExpression>(Literal(value));
548 }
549
550 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
551 /// `list`.
552 /// @param list the list to append too
553 /// @param arg the arg to create
554 template <typename ARG>
555 void Append(ast::ExpressionList& list, ARG&& arg) {
556 list.emplace_back(Expr(std::forward<ARG>(arg)));
557 }
558
559 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
560 /// then appends them to `list`.
561 /// @param list the list to append too
562 /// @param arg0 the first argument
563 /// @param args the rest of the arguments
564 template <typename ARG0, typename... ARGS>
565 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
566 Append(list, std::forward<ARG0>(arg0));
567 Append(list, std::forward<ARGS>(args)...);
568 }
569
570 /// @return an empty list of expressions
571 ast::ExpressionList ExprList() { return {}; }
572
573 /// @param args the list of expressions
574 /// @return the list of expressions converted to `ast::Expression`s using
575 /// `Expr()`,
576 template <typename... ARGS>
577 ast::ExpressionList ExprList(ARGS&&... args) {
578 ast::ExpressionList list;
579 list.reserve(sizeof...(args));
580 Append(list, std::forward<ARGS>(args)...);
581 return list;
582 }
583
584 /// @param list the list of expressions
585 /// @return `list`
586 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
587
588 /// @param val the boolan value
589 /// @return a boolean literal with the given value
590 ast::BoolLiteral* Literal(bool val) {
591 return create<ast::BoolLiteral>(ty.bool_(), val);
592 }
593
594 /// @param val the float value
595 /// @return a float literal with the given value
596 ast::FloatLiteral* Literal(f32 val) {
597 return create<ast::FloatLiteral>(ty.f32(), val);
598 }
599
600 /// @param val the unsigned int value
601 /// @return a ast::UintLiteral with the given value
602 ast::UintLiteral* Literal(u32 val) {
603 return create<ast::UintLiteral>(ty.u32(), val);
604 }
605
606 /// @param val the integer value
607 /// @return the ast::SintLiteral with the given value
608 ast::SintLiteral* Literal(i32 val) {
609 return create<ast::SintLiteral>(ty.i32(), val);
610 }
611
612 /// @param args the arguments for the type constructor
613 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
614 /// of `args` converted to `ast::Expression`s using `Expr()`
615 template <typename T, typename... ARGS>
616 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
617 return create<ast::TypeConstructorExpression>(
618 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
619 }
620
621 /// @param type the type to construct
622 /// @param args the arguments for the constructor
623 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
624 /// values `args`.
625 template <typename... ARGS>
626 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
627 return create<ast::TypeConstructorExpression>(
628 type, ExprList(std::forward<ARGS>(args)...));
629 }
630
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000631 /// Creates a constructor expression that constructs an object of
632 /// `type` filled with `elem_value`. For example,
633 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
634 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
635 /// @param type the type to construct
636 /// @param elem_value the initial or element value (for vec and mat) to
637 /// construct with
638 /// @return the constructor expression
639 ast::ConstructorExpression* ConstructValueFilledWith(type::Type* type,
640 int elem_value = 0);
641
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000642 /// @param args the arguments for the vector constructor
643 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
644 /// `T`, constructed with the values `args`.
645 template <typename T, typename... ARGS>
646 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
647 return create<ast::TypeConstructorExpression>(
648 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
649 }
650
651 /// @param args the arguments for the vector constructor
652 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
653 /// `T`, constructed with the values `args`.
654 template <typename T, typename... ARGS>
655 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
656 return create<ast::TypeConstructorExpression>(
657 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
658 }
659
660 /// @param args the arguments for the vector constructor
661 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
662 /// `T`, constructed with the values `args`.
663 template <typename T, typename... ARGS>
664 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
665 return create<ast::TypeConstructorExpression>(
666 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
667 }
668
669 /// @param args the arguments for the matrix constructor
670 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
671 /// `T`, constructed with the values `args`.
672 template <typename T, typename... ARGS>
673 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
674 return create<ast::TypeConstructorExpression>(
675 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
676 }
677
678 /// @param args the arguments for the matrix constructor
679 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
680 /// `T`, constructed with the values `args`.
681 template <typename T, typename... ARGS>
682 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
683 return create<ast::TypeConstructorExpression>(
684 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
685 }
686
687 /// @param args the arguments for the matrix constructor
688 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
689 /// `T`, constructed with the values `args`.
690 template <typename T, typename... ARGS>
691 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
692 return create<ast::TypeConstructorExpression>(
693 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
694 }
695
696 /// @param args the arguments for the matrix constructor
697 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
698 /// `T`, constructed with the values `args`.
699 template <typename T, typename... ARGS>
700 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
701 return create<ast::TypeConstructorExpression>(
702 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
703 }
704
705 /// @param args the arguments for the matrix constructor
706 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
707 /// `T`, constructed with the values `args`.
708 template <typename T, typename... ARGS>
709 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
710 return create<ast::TypeConstructorExpression>(
711 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
712 }
713
714 /// @param args the arguments for the matrix constructor
715 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
716 /// `T`, constructed with the values `args`.
717 template <typename T, typename... ARGS>
718 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
719 return create<ast::TypeConstructorExpression>(
720 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
721 }
722
723 /// @param args the arguments for the matrix constructor
724 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
725 /// `T`, constructed with the values `args`.
726 template <typename T, typename... ARGS>
727 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
728 return create<ast::TypeConstructorExpression>(
729 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
730 }
731
732 /// @param args the arguments for the matrix constructor
733 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
734 /// `T`, constructed with the values `args`.
735 template <typename T, typename... ARGS>
736 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
737 return create<ast::TypeConstructorExpression>(
738 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
739 }
740
741 /// @param args the arguments for the matrix constructor
742 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
743 /// `T`, constructed with the values `args`.
744 template <typename T, typename... ARGS>
745 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
746 return create<ast::TypeConstructorExpression>(
747 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
748 }
749
750 /// @param args the arguments for the array constructor
751 /// @return an `ast::TypeConstructorExpression` of an array with element type
752 /// `T`, constructed with the values `args`.
753 template <typename T, int N = 0, typename... ARGS>
754 ast::TypeConstructorExpression* array(ARGS&&... args) {
755 return create<ast::TypeConstructorExpression>(
756 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
757 }
758
759 /// @param subtype the array element type
760 /// @param n the array size. 0 represents a runtime-array.
761 /// @param args the arguments for the array constructor
762 /// @return an `ast::TypeConstructorExpression` of an array with element type
763 /// `subtype`, constructed with the values `args`.
764 template <typename... ARGS>
765 ast::TypeConstructorExpression* array(type::Type* subtype,
766 uint32_t n,
767 ARGS&&... args) {
768 return create<ast::TypeConstructorExpression>(
769 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
770 }
771
772 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000773 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000774 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000775 /// @param constructor constructor expression
776 /// @param decorations variable decorations
777 /// @returns a `ast::Variable` with the given name, storage and type
778 ast::Variable* Var(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000779 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000780 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000781 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000782 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000783 return create<ast::Variable>(Symbols().Register(name), storage, type, false,
784 constructor, decorations);
785 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000786
787 /// @param source the variable source
788 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000789 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000790 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000791 /// @param constructor constructor expression
792 /// @param decorations variable decorations
793 /// @returns a `ast::Variable` with the given name, storage and type
794 ast::Variable* Var(const Source& source,
795 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000796 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000797 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000798 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000799 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000800 return create<ast::Variable>(source, Symbols().Register(name), storage,
801 type, false, constructor, decorations);
802 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000803
Ben Clayton46d78d72021-02-10 21:34:26 +0000804 /// @param symbol the variable symbol
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000805 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000806 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000807 /// @param constructor constructor expression
808 /// @param decorations variable decorations
809 /// @returns a `ast::Variable` with the given symbol, storage and type
810 ast::Variable* Var(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000811 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000812 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000813 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000814 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000815 return create<ast::Variable>(symbol, storage, type, false, constructor,
816 decorations);
817 }
818
819 /// @param source the variable source
820 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000821 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000822 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000823 /// @param constructor constructor expression
824 /// @param decorations variable decorations
825 /// @returns a `ast::Variable` with the given symbol, storage and type
826 ast::Variable* Var(const Source& source,
827 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000828 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000829 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000830 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000831 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000832 return create<ast::Variable>(source, symbol, storage, type, false,
833 constructor, decorations);
834 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000835
836 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000837 /// @param type the variable type
838 /// @param constructor optional constructor expression
839 /// @param decorations optional variable decorations
840 /// @returns a constant `ast::Variable` with the given name, storage and type
841 ast::Variable* Const(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000842 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000843 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000844 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000845 return create<ast::Variable>(Symbols().Register(name),
846 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000847 constructor, decorations);
848 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000849
850 /// @param source the variable source
851 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000852 /// @param type the variable type
853 /// @param constructor optional constructor expression
854 /// @param decorations optional variable decorations
855 /// @returns a constant `ast::Variable` with the given name, storage and type
856 ast::Variable* Const(const Source& source,
857 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000858 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000859 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000860 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000861 return create<ast::Variable>(source, Symbols().Register(name),
862 ast::StorageClass::kNone, type, true,
863 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000864 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000865
Ben Clayton46d78d72021-02-10 21:34:26 +0000866 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000867 /// @param type the variable type
868 /// @param constructor optional constructor expression
869 /// @param decorations optional variable decorations
870 /// @returns a constant `ast::Variable` with the given symbol, storage and
871 /// type
872 ast::Variable* Const(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000873 type::Type* type,
874 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000875 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000876 return create<ast::Variable>(symbol, ast::StorageClass::kNone, type, true,
877 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000878 }
879
880 /// @param source the variable source
881 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000882 /// @param type the variable type
883 /// @param constructor optional constructor expression
884 /// @param decorations optional variable decorations
885 /// @returns a constant `ast::Variable` with the given symbol, storage and
886 /// type
887 ast::Variable* Const(const Source& source,
888 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000889 type::Type* type,
890 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000891 ast::DecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000892 return create<ast::Variable>(source, symbol, ast::StorageClass::kNone, type,
893 true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000894 }
Ben Clayton37571bc2021-02-16 23:57:01 +0000895
Ben Clayton401b96b2021-02-03 17:19:59 +0000896 /// @param args the arguments to pass to Var()
897 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
898 /// of `args`, which is automatically registered as a global variable with the
899 /// ast::Module.
900 template <typename... ARGS>
901 ast::Variable* Global(ARGS&&... args) {
902 auto* var = Var(std::forward<ARGS>(args)...);
903 AST().AddGlobalVariable(var);
904 return var;
905 }
906
907 /// @param args the arguments to pass to Const()
908 /// @returns a const `ast::Variable` constructed by calling Var() with the
909 /// arguments of `args`, which is automatically registered as a global
910 /// variable with the ast::Module.
911 template <typename... ARGS>
912 ast::Variable* GlobalConst(ARGS&&... args) {
913 auto* var = Const(std::forward<ARGS>(args)...);
914 AST().AddGlobalVariable(var);
915 return var;
916 }
917
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000918 /// @param func the function name
919 /// @param args the function call arguments
920 /// @returns a `ast::CallExpression` to the function `func`, with the
921 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
922 template <typename NAME, typename... ARGS>
923 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
924 return create<ast::CallExpression>(Expr(func),
925 ExprList(std::forward<ARGS>(args)...));
926 }
927
928 /// @param lhs the left hand argument to the addition operation
929 /// @param rhs the right hand argument to the addition operation
930 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
931 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000932 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000933 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
934 Expr(std::forward<LHS>(lhs)),
935 Expr(std::forward<RHS>(rhs)));
936 }
937
938 /// @param lhs the left hand argument to the subtraction operation
939 /// @param rhs the right hand argument to the subtraction operation
940 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
941 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000942 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000943 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
944 Expr(std::forward<LHS>(lhs)),
945 Expr(std::forward<RHS>(rhs)));
946 }
947
948 /// @param lhs the left hand argument to the multiplication operation
949 /// @param rhs the right hand argument to the multiplication operation
950 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
951 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000952 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000953 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
954 Expr(std::forward<LHS>(lhs)),
955 Expr(std::forward<RHS>(rhs)));
956 }
957
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000958 /// @param source the source information
959 /// @param lhs the left hand argument to the multiplication operation
960 /// @param rhs the right hand argument to the multiplication operation
961 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
962 template <typename LHS, typename RHS>
963 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
964 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
965 Expr(std::forward<LHS>(lhs)),
966 Expr(std::forward<RHS>(rhs)));
967 }
968
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000969 /// @param arr the array argument for the array accessor expression
970 /// @param idx the index argument for the array accessor expression
971 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
972 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000973 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000974 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
975 Expr(std::forward<IDX>(idx)));
976 }
977
978 /// @param obj the object for the member accessor expression
979 /// @param idx the index argument for the array accessor expression
980 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
981 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +0000982 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000983 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
984 Expr(std::forward<IDX>(idx)));
985 }
986
Ben Clayton42d1e092021-02-02 14:29:15 +0000987 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000988 /// @param val the offset value
989 /// @returns the offset decoration pointer
990 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
991 return create<ast::StructMemberOffsetDecoration>(source_, val);
992 }
993
Ben Claytond614dd52021-03-15 10:43:11 +0000994 /// Creates a ast::StructMemberSizeDecoration
995 /// @param source the source information
996 /// @param val the size value
997 /// @returns the size decoration pointer
998 ast::StructMemberSizeDecoration* MemberSize(Source source, uint32_t val) {
999 return create<ast::StructMemberSizeDecoration>(source, val);
1000 }
1001
1002 /// Creates a ast::StructMemberSizeDecoration
1003 /// @param val the size value
1004 /// @returns the size decoration pointer
1005 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1006 return create<ast::StructMemberSizeDecoration>(source_, val);
1007 }
1008
1009 /// Creates a ast::StructMemberAlignDecoration
1010 /// @param source the source information
1011 /// @param val the align value
1012 /// @returns the align decoration pointer
1013 ast::StructMemberAlignDecoration* MemberAlign(Source source, uint32_t val) {
1014 return create<ast::StructMemberAlignDecoration>(source, val);
1015 }
1016
1017 /// Creates a ast::StructMemberAlignDecoration
1018 /// @param val the align value
1019 /// @returns the align decoration pointer
1020 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1021 return create<ast::StructMemberAlignDecoration>(source_, val);
1022 }
1023
Ben Clayton42d1e092021-02-02 14:29:15 +00001024 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001025 /// @param source the source information
1026 /// @param name the function name
1027 /// @param params the function parameters
1028 /// @param type the function return type
1029 /// @param body the function body
1030 /// @param decorations the function decorations
James Pricefeecbe02021-03-15 17:01:34 +00001031 /// @param return_type_decorations the function return type decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001032 /// @returns the function pointer
1033 ast::Function* Func(Source source,
1034 std::string name,
1035 ast::VariableList params,
1036 type::Type* type,
1037 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001038 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001039 ast::DecorationList return_type_decorations = {}) {
1040 auto* func = create<ast::Function>(source, Symbols().Register(name), params,
1041 type, create<ast::BlockStatement>(body),
1042 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001043 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001044 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001045 }
1046
Ben Clayton42d1e092021-02-02 14:29:15 +00001047 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001048 /// @param name the function name
1049 /// @param params the function parameters
1050 /// @param type the function return type
1051 /// @param body the function body
1052 /// @param decorations the function decorations
James Pricefeecbe02021-03-15 17:01:34 +00001053 /// @param return_type_decorations the function return type decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001054 /// @returns the function pointer
1055 ast::Function* Func(std::string name,
1056 ast::VariableList params,
1057 type::Type* type,
1058 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001059 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001060 ast::DecorationList return_type_decorations = {}) {
1061 auto* func = create<ast::Function>(Symbols().Register(name), params, type,
1062 create<ast::BlockStatement>(body),
1063 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001064 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001065 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001066 }
1067
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001068 /// Creates an ast::ReturnStatement with the input args
1069 /// @param args arguments to construct a return statement with
1070 /// @returns the return statement pointer
1071 template <typename... Args>
1072 ast::ReturnStatement* Return(Args&&... args) {
1073 return create<ast::ReturnStatement>(std::forward<Args>(args)...);
1074 }
1075
Ben Claytond614dd52021-03-15 10:43:11 +00001076 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1077 /// the AST().ConstructedTypes().
1078 /// @param source the source information
1079 /// @param name the struct name
1080 /// @param members the struct members
1081 /// @param decorations the optional struct decorations
1082 /// @returns the struct type
1083 type::Struct* Structure(const Source& source,
1084 const std::string& name,
1085 ast::StructMemberList members,
1086 ast::DecorationList decorations = {}) {
1087 auto* impl =
1088 create<ast::Struct>(source, std::move(members), std::move(decorations));
1089 auto* type = ty.struct_(name, impl);
1090 AST().AddConstructedType(type);
1091 return type;
1092 }
1093
1094 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1095 /// the AST().ConstructedTypes().
1096 /// @param name the struct name
1097 /// @param members the struct members
1098 /// @param decorations the optional struct decorations
1099 /// @returns the struct type
1100 type::Struct* Structure(const std::string& name,
1101 ast::StructMemberList members,
1102 ast::DecorationList decorations = {}) {
1103 auto* impl =
1104 create<ast::Struct>(std::move(members), std::move(decorations));
1105 auto* type = ty.struct_(name, impl);
1106 AST().AddConstructedType(type);
1107 return type;
1108 }
1109
Ben Clayton42d1e092021-02-02 14:29:15 +00001110 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001111 /// @param source the source information
1112 /// @param name the struct member name
1113 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001114 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001115 /// @returns the struct member pointer
1116 ast::StructMember* Member(const Source& source,
1117 const std::string& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001118 type::Type* type,
1119 ast::DecorationList decorations = {}) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001120 return create<ast::StructMember>(source, Symbols().Register(name), type,
Ben Claytond614dd52021-03-15 10:43:11 +00001121 std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001122 }
1123
Ben Clayton42d1e092021-02-02 14:29:15 +00001124 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001125 /// @param name the struct member name
1126 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001127 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001128 /// @returns the struct member pointer
1129 ast::StructMember* Member(const std::string& name,
1130 type::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001131 ast::DecorationList decorations = {}) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001132 return create<ast::StructMember>(source_, Symbols().Register(name), type,
Ben Claytond614dd52021-03-15 10:43:11 +00001133 std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001134 }
1135
Ben Claytonbab31972021-02-08 21:16:21 +00001136 /// Creates a ast::StructMember with the given byte offset
1137 /// @param offset the offset to use in the StructMemberOffsetDecoration
1138 /// @param name the struct member name
1139 /// @param type the struct member type
1140 /// @returns the struct member pointer
1141 ast::StructMember* Member(uint32_t offset,
1142 const std::string& name,
1143 type::Type* type) {
1144 return create<ast::StructMember>(
1145 source_, Symbols().Register(name), type,
James Price95d40772021-03-11 17:39:32 +00001146 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001147 create<ast::StructMemberOffsetDecoration>(offset),
1148 });
1149 }
1150
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001151 /// Creates a ast::BlockStatement with input statements
1152 /// @param statements statements of block
1153 /// @returns the block statement pointer
1154 template <typename... Statements>
1155 ast::BlockStatement* Block(Statements&&... statements) {
1156 return create<ast::BlockStatement>(
1157 ast::StatementList{std::forward<Statements>(statements)...});
1158 }
1159
1160 /// Creates a ast::ElseStatement with input condition and body
1161 /// @param condition the else condition expression
1162 /// @param body the else body
1163 /// @returns the else statement pointer
1164 ast::ElseStatement* Else(ast::Expression* condition,
1165 ast::BlockStatement* body) {
1166 return create<ast::ElseStatement>(condition, body);
1167 }
1168
1169 /// Creates a ast::IfStatement with input condition, body, and optional
1170 /// variadic else statements
1171 /// @param condition the if statement condition expression
1172 /// @param body the if statement body
1173 /// @param elseStatements optional variadic else statements
1174 /// @returns the if statement pointer
1175 template <typename... ElseStatements>
1176 ast::IfStatement* If(ast::Expression* condition,
1177 ast::BlockStatement* body,
1178 ElseStatements&&... elseStatements) {
1179 return create<ast::IfStatement>(
1180 condition, body,
1181 ast::ElseStatementList{
1182 std::forward<ElseStatements>(elseStatements)...});
1183 }
1184
1185 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001186 /// @param lhs the left hand side expression initializer
1187 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001188 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001189 template <typename LhsExpressionInit, typename RhsExpressionInit>
1190 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1191 RhsExpressionInit&& rhs) {
1192 return create<ast::AssignmentStatement>(
1193 Expr(std::forward<LhsExpressionInit>(lhs)),
1194 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001195 }
1196
1197 /// Creates a ast::LoopStatement with input body and optional continuing
1198 /// @param body the loop body
1199 /// @param continuing the optional continuing block
1200 /// @returns the loop statement pointer
1201 ast::LoopStatement* Loop(ast::BlockStatement* body,
1202 ast::BlockStatement* continuing = nullptr) {
1203 return create<ast::LoopStatement>(body, continuing);
1204 }
1205
1206 /// Creates a ast::VariableDeclStatement for the input variable
1207 /// @param var the variable to wrap in a decl statement
1208 /// @returns the variable decl statement pointer
1209 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1210 return create<ast::VariableDeclStatement>(var);
1211 }
1212
Antonio Maioranocea744d2021-03-25 12:55:27 +00001213 /// Creates a ast::SwitchStatement with input expression and cases
1214 /// @param condition the condition expression initializer
1215 /// @param cases case statements
1216 /// @returns the switch statement pointer
1217 template <typename ExpressionInit, typename... Cases>
1218 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1219 return create<ast::SwitchStatement>(
1220 Expr(std::forward<ExpressionInit>(condition)),
1221 ast::CaseStatementList{std::forward<Cases>(cases)...});
1222 }
1223
1224 /// Creates a ast::CaseStatement with input list of selectors, and body
1225 /// @param selectors list of selectors
1226 /// @param body the case body
1227 /// @returns the case statement pointer
1228 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1229 ast::BlockStatement* body = nullptr) {
1230 return create<ast::CaseStatement>(std::move(selectors),
1231 body ? body : Block());
1232 }
1233
1234 /// Convenient overload that takes a single selector
1235 /// @param selector a single case selector
1236 /// @param body the case body
1237 /// @returns the case statement pointer
1238 ast::CaseStatement* Case(ast::IntLiteral* selector,
1239 ast::BlockStatement* body = nullptr) {
1240 return Case(ast::CaseSelectorList{selector}, body);
1241 }
1242
1243 /// Convenience function that creates a 'default' ast::CaseStatement
1244 /// @param body the case body
1245 /// @returns the case statement pointer
1246 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1247 return Case(ast::CaseSelectorList{}, body);
1248 }
1249
James Price68f558f2021-04-06 15:51:47 +00001250 /// Creates an ast::BuiltinDecoration
1251 /// @param source the source information
1252 /// @param builtin the builtin value
1253 /// @returns the builtin decoration pointer
1254 ast::BuiltinDecoration* Builtin(Source source, ast::Builtin builtin) {
1255 return create<ast::BuiltinDecoration>(source, builtin);
1256 }
1257
1258 /// Creates an ast::BuiltinDecoration
1259 /// @param builtin the builtin value
1260 /// @returns the builtin decoration pointer
1261 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1262 return create<ast::BuiltinDecoration>(source_, builtin);
1263 }
1264
1265 /// Creates an ast::LocationDecoration
1266 /// @param source the source information
1267 /// @param location the location value
1268 /// @returns the location decoration pointer
1269 ast::LocationDecoration* Location(Source source, uint32_t location) {
1270 return create<ast::LocationDecoration>(source, location);
1271 }
1272
1273 /// Creates an ast::LocationDecoration
1274 /// @param location the location value
1275 /// @returns the location decoration pointer
1276 ast::LocationDecoration* Location(uint32_t location) {
1277 return create<ast::LocationDecoration>(source_, location);
1278 }
1279
1280 /// Creates an ast::StageDecoration
1281 /// @param source the source information
1282 /// @param stage the pipeline stage
1283 /// @returns the stage decoration pointer
1284 ast::StageDecoration* Stage(Source source, ast::PipelineStage stage) {
1285 return create<ast::StageDecoration>(source, stage);
1286 }
1287
1288 /// Creates an ast::StageDecoration
1289 /// @param stage the pipeline stage
1290 /// @returns the stage decoration pointer
1291 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1292 return create<ast::StageDecoration>(source_, stage);
1293 }
1294
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001295 /// Sets the current builder source to `src`
1296 /// @param src the Source used for future create() calls
1297 void SetSource(const Source& src) {
1298 AssertNotMoved();
1299 source_ = src;
1300 }
1301
1302 /// Sets the current builder source to `loc`
1303 /// @param loc the Source used for future create() calls
1304 void SetSource(const Source::Location& loc) {
1305 AssertNotMoved();
1306 source_ = Source(loc);
1307 }
1308
Ben Clayton33352542021-01-29 16:43:41 +00001309 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001310 /// @note As the Resolver is run when the Program is built, this will only be
1311 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001312 /// @param expr the AST expression
1313 /// @return the resolved semantic type for the expression, or nullptr if the
1314 /// expression has no resolved type.
1315 type::Type* TypeOf(ast::Expression* expr) const;
1316
Ben Clayton401b96b2021-02-03 17:19:59 +00001317 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001318 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001319 /// nodes.
1320 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1321 /// @return the ast::Statement that wraps the ast::Expression
1322 ast::Statement* WrapInStatement(ast::Expression* expr);
1323 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001324 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001325 /// these nodes.
1326 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1327 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1328 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1329 /// Returns the statement argument. Used as a passthrough-overload by
1330 /// WrapInFunction().
1331 /// @param stmt the ast::Statement
1332 /// @return `stmt`
1333 ast::Statement* WrapInStatement(ast::Statement* stmt);
1334 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001335 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001336 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001337 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001338 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001339 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001340 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001341 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001342 }
1343 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001344 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001345 /// @returns the function
1346 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001347
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001348 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001349 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001350
1351 protected:
1352 /// Asserts that the builder has not been moved.
1353 void AssertNotMoved() const;
1354
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001355 private:
1356 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001357 ASTNodeAllocator ast_nodes_;
1358 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001359 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001360 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001361 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001362 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001363
1364 /// The source to use when creating AST nodes without providing a Source as
1365 /// the first argument.
1366 Source source_;
1367
Ben Clayton5f0ea112021-03-09 10:54:37 +00001368 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001369 /// program when built.
1370 bool resolve_on_build_ = true;
1371
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001372 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1373 bool moved_ = false;
1374};
1375
1376//! @cond Doxygen_Suppress
1377// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1378template <>
1379struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1380 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1381 return t->i32();
1382 }
1383};
1384template <>
1385struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1386 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1387 return t->u32();
1388 }
1389};
1390template <>
1391struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1392 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1393 return t->f32();
1394 }
1395};
1396template <>
1397struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1398 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1399 return t->bool_();
1400 }
1401};
1402template <>
1403struct ProgramBuilder::TypesBuilder::CToAST<void> {
1404 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1405 return t->void_();
1406 }
1407};
1408//! @endcond
1409
1410} // namespace tint
1411
1412#endif // SRC_PROGRAM_BUILDER_H_