blob: d7f0f2ab0ff56dccc87eaa49b3efeccf3412ff7e [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"
26#include "src/ast/expression.h"
27#include "src/ast/float_literal.h"
28#include "src/ast/identifier_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000029#include "src/ast/if_statement.h"
30#include "src/ast/loop_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000031#include "src/ast/member_accessor_expression.h"
32#include "src/ast/module.h"
33#include "src/ast/scalar_constructor_expression.h"
34#include "src/ast/sint_literal.h"
Ben Claytonbab31972021-02-08 21:16:21 +000035#include "src/ast/stride_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000036#include "src/ast/struct.h"
37#include "src/ast/struct_member.h"
38#include "src/ast/struct_member_offset_decoration.h"
39#include "src/ast/type_constructor_expression.h"
40#include "src/ast/uint_literal.h"
41#include "src/ast/variable.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000042#include "src/ast/variable_decl_statement.h"
Ben Clayton844217f2021-01-27 18:49:05 +000043#include "src/diagnostic/diagnostic.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/program.h"
Ben Claytondd1b6fc2021-01-29 10:55:40 +000045#include "src/semantic/info.h"
Ben Clayton7fdfff12021-01-29 15:17:30 +000046#include "src/semantic/node.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000047#include "src/symbol_table.h"
48#include "src/type/alias_type.h"
49#include "src/type/array_type.h"
50#include "src/type/bool_type.h"
51#include "src/type/f32_type.h"
52#include "src/type/i32_type.h"
53#include "src/type/matrix_type.h"
54#include "src/type/pointer_type.h"
55#include "src/type/struct_type.h"
56#include "src/type/type_manager.h"
57#include "src/type/u32_type.h"
58#include "src/type/vector_type.h"
59#include "src/type/void_type.h"
60
61namespace tint {
62
Ben Clayton401b96b2021-02-03 17:19:59 +000063// Forward declarations
64namespace ast {
65class VariableDeclStatement;
66} // namespace ast
67
Ben Claytona6b9a8e2021-01-26 16:57:10 +000068class CloneContext;
69
70/// ProgramBuilder is a mutable builder for a Program.
71/// To construct a Program, populate the builder and then `std::move` it to a
72/// Program.
73class ProgramBuilder {
74 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000075 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
76 using ASTNodeAllocator = BlockAllocator<ast::Node>;
77
78 /// SemNodeAllocator is an alias to BlockAllocator<semantic::Node>
79 using SemNodeAllocator = BlockAllocator<semantic::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000080
81 /// `i32` is a type alias to `int`.
82 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
83 /// WGSL syntax.
84 /// Note: this is intentionally not aliased to uint32_t as we want integer
85 /// literals passed to the builder to match WGSL's integer literal types.
86 using i32 = decltype(1);
87 /// `u32` is a type alias to `unsigned int`.
88 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
89 /// WGSL syntax.
90 /// Note: this is intentionally not aliased to uint32_t as we want integer
91 /// literals passed to the builder to match WGSL's integer literal types.
92 using u32 = decltype(1u);
93 /// `f32` is a type alias to `float`
94 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
95 /// WGSL syntax.
96 using f32 = float;
97
98 /// Constructor
99 ProgramBuilder();
100
101 /// Move constructor
102 /// @param rhs the builder to move
103 ProgramBuilder(ProgramBuilder&& rhs);
104
105 /// Destructor
106 virtual ~ProgramBuilder();
107
108 /// Move assignment operator
109 /// @param rhs the builder to move
110 /// @return this builder
111 ProgramBuilder& operator=(ProgramBuilder&& rhs);
112
Ben Claytone43c8302021-01-29 11:59:32 +0000113 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
114 /// making a deep clone of the Program contents.
115 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
116 /// existing immutable program.
117 /// As the returned ProgramBuilder wraps `program`, `program` must not be
118 /// destructed or assigned while using the returned ProgramBuilder.
119 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
120 /// function. See crbug.com/tint/460.
121 /// @param program the immutable Program to wrap
122 /// @return the ProgramBuilder that wraps `program`
123 static ProgramBuilder Wrap(const Program* program);
124
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000125 /// @returns a reference to the program's types
126 type::Manager& Types() {
127 AssertNotMoved();
128 return types_;
129 }
130
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000131 /// @returns a reference to the program's types
132 const type::Manager& Types() const {
133 AssertNotMoved();
134 return types_;
135 }
136
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000137 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000138 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000139 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000140 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000141 }
142
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000143 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000144 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000145 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000146 return ast_nodes_;
147 }
148
149 /// @returns a reference to the program's semantic nodes storage
150 SemNodeAllocator& SemNodes() {
151 AssertNotMoved();
152 return sem_nodes_;
153 }
154
155 /// @returns a reference to the program's semantic nodes storage
156 const SemNodeAllocator& SemNodes() const {
157 AssertNotMoved();
158 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000159 }
160
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000161 /// @returns a reference to the program's AST root Module
162 ast::Module& AST() {
163 AssertNotMoved();
164 return *ast_;
165 }
166
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000167 /// @returns a reference to the program's AST root Module
168 const ast::Module& AST() const {
169 AssertNotMoved();
170 return *ast_;
171 }
172
173 /// @returns a reference to the program's semantic info
174 semantic::Info& Sem() {
175 AssertNotMoved();
176 return sem_;
177 }
178
179 /// @returns a reference to the program's semantic info
180 const semantic::Info& Sem() const {
181 AssertNotMoved();
182 return sem_;
183 }
184
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000185 /// @returns a reference to the program's SymbolTable
186 SymbolTable& Symbols() {
187 AssertNotMoved();
188 return symbols_;
189 }
190
Ben Clayton708dc2d2021-01-29 11:22:40 +0000191 /// @returns a reference to the program's SymbolTable
192 const SymbolTable& Symbols() const {
193 AssertNotMoved();
194 return symbols_;
195 }
196
Ben Clayton844217f2021-01-27 18:49:05 +0000197 /// @returns a reference to the program's diagnostics
198 diag::List& Diagnostics() {
199 AssertNotMoved();
200 return diagnostics_;
201 }
202
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000203 /// @returns a reference to the program's diagnostics
204 const diag::List& Diagnostics() const {
205 AssertNotMoved();
206 return diagnostics_;
207 }
208
Ben Claytondd69ac32021-01-27 19:23:55 +0000209 /// Controls whether the TypeDeterminer will be run on the program when it is
210 /// built.
211 /// @param enable the new flag value (defaults to true)
212 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
213
214 /// @return true if the TypeDeterminer will be run on the program when it is
215 /// built.
216 bool ResolveOnBuild() const { return resolve_on_build_; }
217
Ben Clayton844217f2021-01-27 18:49:05 +0000218 /// @returns true if the program has no error diagnostics and is not missing
219 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000220 bool IsValid() const;
221
Ben Clayton708dc2d2021-01-29 11:22:40 +0000222 /// Writes a representation of the node to the output stream
223 /// @note unlike str(), to_str() does not automatically demangle the string.
224 /// @param node the AST node
225 /// @param out the stream to write to
226 /// @param indent number of spaces to indent the node when writing
227 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
228 node->to_str(Sem(), out, indent);
229 }
230
231 /// Returns a demangled, string representation of `node`.
232 /// @param node the AST node
233 /// @returns a string representation of the node
234 std::string str(const ast::Node* node) const;
235
Ben Clayton7fdfff12021-01-29 15:17:30 +0000236 /// Creates a new ast::Node owned by the ProgramBuilder. When the
237 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000238 /// @param source the Source of the node
239 /// @param args the arguments to pass to the type constructor
240 /// @returns the node pointer
241 template <typename T, typename... ARGS>
242 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
243 ARGS&&... args) {
244 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000245 return ast_nodes_.Create<T>(source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000246 }
247
Ben Clayton7fdfff12021-01-29 15:17:30 +0000248 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
249 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000250 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000251 /// When the ProgramBuilder is destructed, the ast::Node will also be
252 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000253 /// @returns the node pointer
254 template <typename T>
255 traits::EnableIfIsType<T, ast::Node>* create() {
256 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000257 return ast_nodes_.Create<T>(source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000258 }
259
Ben Clayton7fdfff12021-01-29 15:17:30 +0000260 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
261 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000262 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000263 /// When the ProgramBuilder is destructed, the ast::Node will also be
264 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000265 /// @param arg0 the first arguments to pass to the type constructor
266 /// @param args the remaining arguments to pass to the type constructor
267 /// @returns the node pointer
268 template <typename T, typename ARG0, typename... ARGS>
269 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
270 traits::IsTypeOrDerived<T, ast::Node>::value &&
271 !traits::IsTypeOrDerived<ARG0, Source>::value,
272 T>*
273 create(ARG0&& arg0, ARGS&&... args) {
274 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000275 return ast_nodes_.Create<T>(source_, std::forward<ARG0>(arg0),
276 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000277 }
278
Ben Clayton7fdfff12021-01-29 15:17:30 +0000279 /// Creates a new semantic::Node owned by the ProgramBuilder.
280 /// When the ProgramBuilder is destructed, the semantic::Node will also be
281 /// destructed.
282 /// @param args the arguments to pass to the type constructor
283 /// @returns the node pointer
284 template <typename T, typename... ARGS>
285 traits::EnableIfIsType<T, semantic::Node>* create(ARGS&&... args) {
286 AssertNotMoved();
287 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
288 }
289
290 /// Creates a new type::Type owned by the ProgramBuilder.
291 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
292 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000293 /// Types are unique (de-aliased), and so calling create() for the same `T`
294 /// and arguments will return the same pointer.
295 /// @warning Use this method to acquire a type only if all of its type
296 /// information is provided in the constructor arguments `args`.<br>
297 /// If the type requires additional configuration after construction that
298 /// affect its fundamental type, build the type with `std::make_unique`, make
299 /// any necessary alterations and then call unique_type() instead.
300 /// @param args the arguments to pass to the type constructor
301 /// @returns the de-aliased type pointer
302 template <typename T, typename... ARGS>
303 traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) {
304 static_assert(std::is_base_of<type::Type, T>::value,
305 "T does not derive from type::Type");
306 AssertNotMoved();
307 return types_.Get<T>(std::forward<ARGS>(args)...);
308 }
309
310 /// Marks this builder as moved, preventing any further use of the builder.
311 void MarkAsMoved();
312
313 //////////////////////////////////////////////////////////////////////////////
314 // TypesBuilder
315 //////////////////////////////////////////////////////////////////////////////
316
317 /// TypesBuilder holds basic `tint` types and methods for constructing
318 /// complex types.
319 class TypesBuilder {
320 public:
321 /// Constructor
322 /// @param builder the program builder
323 explicit TypesBuilder(ProgramBuilder* builder);
324
325 /// @return the tint AST type for the C type `T`.
326 template <typename T>
327 type::Type* Of() const {
328 return CToAST<T>::get(this);
329 }
330
331 /// @returns a boolean type
332 type::Bool* bool_() const { return builder->create<type::Bool>(); }
333
334 /// @returns a f32 type
335 type::F32* f32() const { return builder->create<type::F32>(); }
336
337 /// @returns a i32 type
338 type::I32* i32() const { return builder->create<type::I32>(); }
339
340 /// @returns a u32 type
341 type::U32* u32() const { return builder->create<type::U32>(); }
342
343 /// @returns a void type
344 type::Void* void_() const { return builder->create<type::Void>(); }
345
346 /// @return the tint AST type for a 2-element vector of the C type `T`.
347 template <typename T>
348 type::Vector* vec2() const {
349 return builder->create<type::Vector>(Of<T>(), 2);
350 }
351
352 /// @return the tint AST type for a 3-element vector of the C type `T`.
353 template <typename T>
354 type::Vector* vec3() const {
355 return builder->create<type::Vector>(Of<T>(), 3);
356 }
357
358 /// @return the tint AST type for a 4-element vector of the C type `T`.
359 template <typename T>
360 type::Type* vec4() const {
361 return builder->create<type::Vector>(Of<T>(), 4);
362 }
363
364 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
365 template <typename T>
366 type::Matrix* mat2x2() const {
367 return builder->create<type::Matrix>(Of<T>(), 2, 2);
368 }
369
370 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
371 template <typename T>
372 type::Matrix* mat2x3() const {
373 return builder->create<type::Matrix>(Of<T>(), 3, 2);
374 }
375
376 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
377 template <typename T>
378 type::Matrix* mat2x4() const {
379 return builder->create<type::Matrix>(Of<T>(), 4, 2);
380 }
381
382 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
383 template <typename T>
384 type::Matrix* mat3x2() const {
385 return builder->create<type::Matrix>(Of<T>(), 2, 3);
386 }
387
388 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
389 template <typename T>
390 type::Matrix* mat3x3() const {
391 return builder->create<type::Matrix>(Of<T>(), 3, 3);
392 }
393
394 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
395 template <typename T>
396 type::Matrix* mat3x4() const {
397 return builder->create<type::Matrix>(Of<T>(), 4, 3);
398 }
399
400 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
401 template <typename T>
402 type::Matrix* mat4x2() const {
403 return builder->create<type::Matrix>(Of<T>(), 2, 4);
404 }
405
406 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
407 template <typename T>
408 type::Matrix* mat4x3() const {
409 return builder->create<type::Matrix>(Of<T>(), 3, 4);
410 }
411
412 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
413 template <typename T>
414 type::Matrix* mat4x4() const {
415 return builder->create<type::Matrix>(Of<T>(), 4, 4);
416 }
417
418 /// @param subtype the array element type
419 /// @param n the array size. 0 represents a runtime-array.
420 /// @return the tint AST type for a array of size `n` of type `T`
421 type::Array* array(type::Type* subtype, uint32_t n) const {
422 return builder->create<type::Array>(subtype, n,
423 ast::ArrayDecorationList{});
424 }
425
Ben Claytonbab31972021-02-08 21:16:21 +0000426 /// @param subtype the array element type
427 /// @param n the array size. 0 represents a runtime-array.
428 /// @param stride the array stride.
429 /// @return the tint AST type for a array of size `n` of type `T`
430 type::Array* array(type::Type* subtype, uint32_t n, uint32_t stride) const {
431 return builder->create<type::Array>(
432 subtype, n,
433 ast::ArrayDecorationList{
434 builder->create<ast::StrideDecoration>(stride),
435 });
436 }
437
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000438 /// @return the tint AST type for an array of size `N` of type `T`
439 template <typename T, int N = 0>
440 type::Array* array() const {
441 return array(Of<T>(), N);
442 }
443
Ben Claytonbab31972021-02-08 21:16:21 +0000444 /// @param stride the array stride
445 /// @return the tint AST type for an array of size `N` of type `T`
446 template <typename T, int N = 0>
447 type::Array* array(uint32_t stride) const {
448 return array(Of<T>(), N, stride);
449 }
Ben Clayton42d1e092021-02-02 14:29:15 +0000450 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000451 /// @param name the alias name
452 /// @param type the alias type
453 /// @returns the alias pointer
454 type::Alias* alias(const std::string& name, type::Type* type) const {
455 return builder->create<type::Alias>(builder->Symbols().Register(name),
456 type);
457 }
458
459 /// @return the tint AST pointer to type `T` with the given
460 /// ast::StorageClass.
461 /// @param storage_class the storage class of the pointer
462 template <typename T>
463 type::Pointer* pointer(ast::StorageClass storage_class) const {
464 return builder->create<type::Pointer>(Of<T>(), storage_class);
465 }
466
467 /// @param name the struct name
468 /// @param impl the struct implementation
469 /// @returns a struct pointer
470 type::Struct* struct_(const std::string& name, ast::Struct* impl) const {
471 return builder->create<type::Struct>(builder->Symbols().Register(name),
472 impl);
473 }
474
475 private:
476 /// CToAST<T> is specialized for various `T` types and each specialization
477 /// contains a single static `get()` method for obtaining the corresponding
478 /// AST type for the C type `T`.
479 /// `get()` has the signature:
480 /// `static type::Type* get(Types* t)`
481 template <typename T>
482 struct CToAST {};
483
Ben Claytonc7ca7662021-02-17 16:23:52 +0000484 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000485 };
486
487 //////////////////////////////////////////////////////////////////////////////
488 // AST helper methods
489 //////////////////////////////////////////////////////////////////////////////
490
491 /// @param expr the expression
492 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000493 template <typename T>
494 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
495 return expr;
496 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000497
498 /// @param name the identifier name
499 /// @return an ast::IdentifierExpression with the given name
500 ast::IdentifierExpression* Expr(const std::string& name) {
501 return create<ast::IdentifierExpression>(Symbols().Register(name));
502 }
503
Ben Clayton46d78d72021-02-10 21:34:26 +0000504 /// @param symbol the identifier symbol
505 /// @return an ast::IdentifierExpression with the given symbol
506 ast::IdentifierExpression* Expr(Symbol symbol) {
507 return create<ast::IdentifierExpression>(symbol);
508 }
509
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000510 /// @param source the source information
511 /// @param name the identifier name
512 /// @return an ast::IdentifierExpression with the given name
513 ast::IdentifierExpression* Expr(const Source& source,
514 const std::string& name) {
515 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
516 }
517
518 /// @param name the identifier name
519 /// @return an ast::IdentifierExpression with the given name
520 ast::IdentifierExpression* Expr(const char* name) {
521 return create<ast::IdentifierExpression>(Symbols().Register(name));
522 }
523
524 /// @param value the boolean value
525 /// @return a Scalar constructor for the given value
526 ast::ScalarConstructorExpression* Expr(bool value) {
527 return create<ast::ScalarConstructorExpression>(Literal(value));
528 }
529
530 /// @param value the float value
531 /// @return a Scalar constructor for the given value
532 ast::ScalarConstructorExpression* Expr(f32 value) {
533 return create<ast::ScalarConstructorExpression>(Literal(value));
534 }
535
536 /// @param value the integer value
537 /// @return a Scalar constructor for the given value
538 ast::ScalarConstructorExpression* Expr(i32 value) {
539 return create<ast::ScalarConstructorExpression>(Literal(value));
540 }
541
542 /// @param value the unsigned int value
543 /// @return a Scalar constructor for the given value
544 ast::ScalarConstructorExpression* Expr(u32 value) {
545 return create<ast::ScalarConstructorExpression>(Literal(value));
546 }
547
548 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
549 /// `list`.
550 /// @param list the list to append too
551 /// @param arg the arg to create
552 template <typename ARG>
553 void Append(ast::ExpressionList& list, ARG&& arg) {
554 list.emplace_back(Expr(std::forward<ARG>(arg)));
555 }
556
557 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
558 /// then appends them to `list`.
559 /// @param list the list to append too
560 /// @param arg0 the first argument
561 /// @param args the rest of the arguments
562 template <typename ARG0, typename... ARGS>
563 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
564 Append(list, std::forward<ARG0>(arg0));
565 Append(list, std::forward<ARGS>(args)...);
566 }
567
568 /// @return an empty list of expressions
569 ast::ExpressionList ExprList() { return {}; }
570
571 /// @param args the list of expressions
572 /// @return the list of expressions converted to `ast::Expression`s using
573 /// `Expr()`,
574 template <typename... ARGS>
575 ast::ExpressionList ExprList(ARGS&&... args) {
576 ast::ExpressionList list;
577 list.reserve(sizeof...(args));
578 Append(list, std::forward<ARGS>(args)...);
579 return list;
580 }
581
582 /// @param list the list of expressions
583 /// @return `list`
584 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
585
586 /// @param val the boolan value
587 /// @return a boolean literal with the given value
588 ast::BoolLiteral* Literal(bool val) {
589 return create<ast::BoolLiteral>(ty.bool_(), val);
590 }
591
592 /// @param val the float value
593 /// @return a float literal with the given value
594 ast::FloatLiteral* Literal(f32 val) {
595 return create<ast::FloatLiteral>(ty.f32(), val);
596 }
597
598 /// @param val the unsigned int value
599 /// @return a ast::UintLiteral with the given value
600 ast::UintLiteral* Literal(u32 val) {
601 return create<ast::UintLiteral>(ty.u32(), val);
602 }
603
604 /// @param val the integer value
605 /// @return the ast::SintLiteral with the given value
606 ast::SintLiteral* Literal(i32 val) {
607 return create<ast::SintLiteral>(ty.i32(), val);
608 }
609
610 /// @param args the arguments for the type constructor
611 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
612 /// of `args` converted to `ast::Expression`s using `Expr()`
613 template <typename T, typename... ARGS>
614 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
615 return create<ast::TypeConstructorExpression>(
616 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
617 }
618
619 /// @param type the type to construct
620 /// @param args the arguments for the constructor
621 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
622 /// values `args`.
623 template <typename... ARGS>
624 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
625 return create<ast::TypeConstructorExpression>(
626 type, ExprList(std::forward<ARGS>(args)...));
627 }
628
629 /// @param args the arguments for the vector constructor
630 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
631 /// `T`, constructed with the values `args`.
632 template <typename T, typename... ARGS>
633 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
634 return create<ast::TypeConstructorExpression>(
635 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
636 }
637
638 /// @param args the arguments for the vector constructor
639 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
640 /// `T`, constructed with the values `args`.
641 template <typename T, typename... ARGS>
642 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
643 return create<ast::TypeConstructorExpression>(
644 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
645 }
646
647 /// @param args the arguments for the vector constructor
648 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
649 /// `T`, constructed with the values `args`.
650 template <typename T, typename... ARGS>
651 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
652 return create<ast::TypeConstructorExpression>(
653 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
654 }
655
656 /// @param args the arguments for the matrix constructor
657 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
658 /// `T`, constructed with the values `args`.
659 template <typename T, typename... ARGS>
660 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
661 return create<ast::TypeConstructorExpression>(
662 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
663 }
664
665 /// @param args the arguments for the matrix constructor
666 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
667 /// `T`, constructed with the values `args`.
668 template <typename T, typename... ARGS>
669 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
670 return create<ast::TypeConstructorExpression>(
671 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
672 }
673
674 /// @param args the arguments for the matrix constructor
675 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
676 /// `T`, constructed with the values `args`.
677 template <typename T, typename... ARGS>
678 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
679 return create<ast::TypeConstructorExpression>(
680 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
681 }
682
683 /// @param args the arguments for the matrix constructor
684 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
685 /// `T`, constructed with the values `args`.
686 template <typename T, typename... ARGS>
687 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
688 return create<ast::TypeConstructorExpression>(
689 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
690 }
691
692 /// @param args the arguments for the matrix constructor
693 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
694 /// `T`, constructed with the values `args`.
695 template <typename T, typename... ARGS>
696 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
697 return create<ast::TypeConstructorExpression>(
698 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
699 }
700
701 /// @param args the arguments for the matrix constructor
702 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
703 /// `T`, constructed with the values `args`.
704 template <typename T, typename... ARGS>
705 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
706 return create<ast::TypeConstructorExpression>(
707 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
708 }
709
710 /// @param args the arguments for the matrix constructor
711 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
712 /// `T`, constructed with the values `args`.
713 template <typename T, typename... ARGS>
714 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
715 return create<ast::TypeConstructorExpression>(
716 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
717 }
718
719 /// @param args the arguments for the matrix constructor
720 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
721 /// `T`, constructed with the values `args`.
722 template <typename T, typename... ARGS>
723 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
724 return create<ast::TypeConstructorExpression>(
725 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
726 }
727
728 /// @param args the arguments for the matrix constructor
729 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
730 /// `T`, constructed with the values `args`.
731 template <typename T, typename... ARGS>
732 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
733 return create<ast::TypeConstructorExpression>(
734 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
735 }
736
737 /// @param args the arguments for the array constructor
738 /// @return an `ast::TypeConstructorExpression` of an array with element type
739 /// `T`, constructed with the values `args`.
740 template <typename T, int N = 0, typename... ARGS>
741 ast::TypeConstructorExpression* array(ARGS&&... args) {
742 return create<ast::TypeConstructorExpression>(
743 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
744 }
745
746 /// @param subtype the array element type
747 /// @param n the array size. 0 represents a runtime-array.
748 /// @param args the arguments for the array constructor
749 /// @return an `ast::TypeConstructorExpression` of an array with element type
750 /// `subtype`, constructed with the values `args`.
751 template <typename... ARGS>
752 ast::TypeConstructorExpression* array(type::Type* subtype,
753 uint32_t n,
754 ARGS&&... args) {
755 return create<ast::TypeConstructorExpression>(
756 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
757 }
758
759 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000760 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000761 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000762 /// @param constructor constructor expression
763 /// @param decorations variable decorations
764 /// @returns a `ast::Variable` with the given name, storage and type
765 ast::Variable* Var(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000766 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000767 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000768 ast::Expression* constructor = nullptr,
769 ast::VariableDecorationList decorations = {}) {
770 return create<ast::Variable>(Symbols().Register(name), storage, type, false,
771 constructor, decorations);
772 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000773
774 /// @param source the variable source
775 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000776 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000777 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000778 /// @param constructor constructor expression
779 /// @param decorations variable decorations
780 /// @returns a `ast::Variable` with the given name, storage and type
781 ast::Variable* Var(const Source& source,
782 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000783 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000784 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000785 ast::Expression* constructor = nullptr,
786 ast::VariableDecorationList decorations = {}) {
787 return create<ast::Variable>(source, Symbols().Register(name), storage,
788 type, false, constructor, decorations);
789 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000790
Ben Clayton46d78d72021-02-10 21:34:26 +0000791 /// @param symbol the variable symbol
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000792 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000793 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000794 /// @param constructor constructor expression
795 /// @param decorations variable decorations
796 /// @returns a `ast::Variable` with the given symbol, storage and type
797 ast::Variable* Var(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000798 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000799 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000800 ast::Expression* constructor = nullptr,
801 ast::VariableDecorationList decorations = {}) {
802 return create<ast::Variable>(symbol, storage, type, false, constructor,
803 decorations);
804 }
805
806 /// @param source the variable source
807 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000808 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000809 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000810 /// @param constructor constructor expression
811 /// @param decorations variable decorations
812 /// @returns a `ast::Variable` with the given symbol, storage and type
813 ast::Variable* Var(const Source& source,
814 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000815 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000816 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000817 ast::Expression* constructor = nullptr,
818 ast::VariableDecorationList decorations = {}) {
819 return create<ast::Variable>(source, symbol, storage, type, false,
820 constructor, decorations);
821 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000822
823 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000824 /// @param type the variable type
825 /// @param constructor optional constructor expression
826 /// @param decorations optional variable decorations
827 /// @returns a constant `ast::Variable` with the given name, storage and type
828 ast::Variable* Const(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000829 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000830 ast::Expression* constructor = nullptr,
831 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000832 return create<ast::Variable>(Symbols().Register(name),
833 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000834 constructor, decorations);
835 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000836
837 /// @param source the variable source
838 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000839 /// @param type the variable type
840 /// @param constructor optional constructor expression
841 /// @param decorations optional variable decorations
842 /// @returns a constant `ast::Variable` with the given name, storage and type
843 ast::Variable* Const(const Source& source,
844 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000845 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000846 ast::Expression* constructor = nullptr,
847 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000848 return create<ast::Variable>(source, Symbols().Register(name),
849 ast::StorageClass::kNone, type, true,
850 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000851 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000852
Ben Clayton46d78d72021-02-10 21:34:26 +0000853 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000854 /// @param type the variable type
855 /// @param constructor optional constructor expression
856 /// @param decorations optional variable decorations
857 /// @returns a constant `ast::Variable` with the given symbol, storage and
858 /// type
859 ast::Variable* Const(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000860 type::Type* type,
861 ast::Expression* constructor = nullptr,
862 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000863 return create<ast::Variable>(symbol, ast::StorageClass::kNone, type, true,
864 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000865 }
866
867 /// @param source the variable source
868 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000869 /// @param type the variable type
870 /// @param constructor optional constructor expression
871 /// @param decorations optional variable decorations
872 /// @returns a constant `ast::Variable` with the given symbol, storage and
873 /// type
874 ast::Variable* Const(const Source& source,
875 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000876 type::Type* type,
877 ast::Expression* constructor = nullptr,
878 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000879 return create<ast::Variable>(source, symbol, ast::StorageClass::kNone, type,
880 true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000881 }
Ben Clayton37571bc2021-02-16 23:57:01 +0000882
Ben Clayton401b96b2021-02-03 17:19:59 +0000883 /// @param args the arguments to pass to Var()
884 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
885 /// of `args`, which is automatically registered as a global variable with the
886 /// ast::Module.
887 template <typename... ARGS>
888 ast::Variable* Global(ARGS&&... args) {
889 auto* var = Var(std::forward<ARGS>(args)...);
890 AST().AddGlobalVariable(var);
891 return var;
892 }
893
894 /// @param args the arguments to pass to Const()
895 /// @returns a const `ast::Variable` constructed by calling Var() with the
896 /// arguments of `args`, which is automatically registered as a global
897 /// variable with the ast::Module.
898 template <typename... ARGS>
899 ast::Variable* GlobalConst(ARGS&&... args) {
900 auto* var = Const(std::forward<ARGS>(args)...);
901 AST().AddGlobalVariable(var);
902 return var;
903 }
904
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000905 /// @param func the function name
906 /// @param args the function call arguments
907 /// @returns a `ast::CallExpression` to the function `func`, with the
908 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
909 template <typename NAME, typename... ARGS>
910 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
911 return create<ast::CallExpression>(Expr(func),
912 ExprList(std::forward<ARGS>(args)...));
913 }
914
915 /// @param lhs the left hand argument to the addition operation
916 /// @param rhs the right hand argument to the addition operation
917 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
918 template <typename LHS, typename RHS>
919 ast::Expression* Add(LHS&& lhs, RHS&& rhs) {
920 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
921 Expr(std::forward<LHS>(lhs)),
922 Expr(std::forward<RHS>(rhs)));
923 }
924
925 /// @param lhs the left hand argument to the subtraction operation
926 /// @param rhs the right hand argument to the subtraction operation
927 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
928 template <typename LHS, typename RHS>
929 ast::Expression* Sub(LHS&& lhs, RHS&& rhs) {
930 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
931 Expr(std::forward<LHS>(lhs)),
932 Expr(std::forward<RHS>(rhs)));
933 }
934
935 /// @param lhs the left hand argument to the multiplication operation
936 /// @param rhs the right hand argument to the multiplication operation
937 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
938 template <typename LHS, typename RHS>
939 ast::Expression* Mul(LHS&& lhs, RHS&& rhs) {
940 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
941 Expr(std::forward<LHS>(lhs)),
942 Expr(std::forward<RHS>(rhs)));
943 }
944
945 /// @param arr the array argument for the array accessor expression
946 /// @param idx the index argument for the array accessor expression
947 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
948 template <typename ARR, typename IDX>
949 ast::Expression* IndexAccessor(ARR&& arr, IDX&& idx) {
950 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
951 Expr(std::forward<IDX>(idx)));
952 }
953
954 /// @param obj the object for the member accessor expression
955 /// @param idx the index argument for the array accessor expression
956 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
957 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +0000958 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000959 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
960 Expr(std::forward<IDX>(idx)));
961 }
962
Ben Clayton42d1e092021-02-02 14:29:15 +0000963 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000964 /// @param val the offset value
965 /// @returns the offset decoration pointer
966 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
967 return create<ast::StructMemberOffsetDecoration>(source_, val);
968 }
969
Ben Clayton42d1e092021-02-02 14:29:15 +0000970 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000971 /// @param source the source information
972 /// @param name the function name
973 /// @param params the function parameters
974 /// @param type the function return type
975 /// @param body the function body
976 /// @param decorations the function decorations
977 /// @returns the function pointer
978 ast::Function* Func(Source source,
979 std::string name,
980 ast::VariableList params,
981 type::Type* type,
982 ast::StatementList body,
983 ast::FunctionDecorationList decorations) {
Ben Clayton42d1e092021-02-02 14:29:15 +0000984 auto* func =
985 create<ast::Function>(source, Symbols().Register(name), params, type,
986 create<ast::BlockStatement>(body), decorations);
James Price3eaa4502021-02-09 21:26:21 +0000987 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +0000988 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000989 }
990
Ben Clayton42d1e092021-02-02 14:29:15 +0000991 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000992 /// @param name the function name
993 /// @param params the function parameters
994 /// @param type the function return type
995 /// @param body the function body
996 /// @param decorations the function decorations
997 /// @returns the function pointer
998 ast::Function* Func(std::string name,
999 ast::VariableList params,
1000 type::Type* type,
1001 ast::StatementList body,
1002 ast::FunctionDecorationList decorations) {
Ben Clayton42d1e092021-02-02 14:29:15 +00001003 auto* func =
1004 create<ast::Function>(Symbols().Register(name), params, type,
1005 create<ast::BlockStatement>(body), decorations);
James Price3eaa4502021-02-09 21:26:21 +00001006 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001007 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001008 }
1009
Ben Clayton42d1e092021-02-02 14:29:15 +00001010 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001011 /// @param source the source information
1012 /// @param name the struct member name
1013 /// @param type the struct member type
1014 /// @returns the struct member pointer
1015 ast::StructMember* Member(const Source& source,
1016 const std::string& name,
1017 type::Type* type) {
1018 return create<ast::StructMember>(source, Symbols().Register(name), type,
1019 ast::StructMemberDecorationList{});
1020 }
1021
Ben Clayton42d1e092021-02-02 14:29:15 +00001022 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001023 /// @param name the struct member name
1024 /// @param type the struct member type
1025 /// @returns the struct member pointer
1026 ast::StructMember* Member(const std::string& name, type::Type* type) {
1027 return create<ast::StructMember>(source_, Symbols().Register(name), type,
1028 ast::StructMemberDecorationList{});
1029 }
1030
Ben Clayton42d1e092021-02-02 14:29:15 +00001031 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001032 /// @param name the struct member name
1033 /// @param type the struct member type
1034 /// @param decorations the struct member decorations
1035 /// @returns the struct member pointer
1036 ast::StructMember* Member(const std::string& name,
1037 type::Type* type,
1038 ast::StructMemberDecorationList decorations) {
1039 return create<ast::StructMember>(source_, Symbols().Register(name), type,
1040 decorations);
1041 }
1042
Ben Claytonbab31972021-02-08 21:16:21 +00001043 /// Creates a ast::StructMember with the given byte offset
1044 /// @param offset the offset to use in the StructMemberOffsetDecoration
1045 /// @param name the struct member name
1046 /// @param type the struct member type
1047 /// @returns the struct member pointer
1048 ast::StructMember* Member(uint32_t offset,
1049 const std::string& name,
1050 type::Type* type) {
1051 return create<ast::StructMember>(
1052 source_, Symbols().Register(name), type,
1053 ast::StructMemberDecorationList{
1054 create<ast::StructMemberOffsetDecoration>(offset),
1055 });
1056 }
1057
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001058 /// Creates a ast::BlockStatement with input statements
1059 /// @param statements statements of block
1060 /// @returns the block statement pointer
1061 template <typename... Statements>
1062 ast::BlockStatement* Block(Statements&&... statements) {
1063 return create<ast::BlockStatement>(
1064 ast::StatementList{std::forward<Statements>(statements)...});
1065 }
1066
1067 /// Creates a ast::ElseStatement with input condition and body
1068 /// @param condition the else condition expression
1069 /// @param body the else body
1070 /// @returns the else statement pointer
1071 ast::ElseStatement* Else(ast::Expression* condition,
1072 ast::BlockStatement* body) {
1073 return create<ast::ElseStatement>(condition, body);
1074 }
1075
1076 /// Creates a ast::IfStatement with input condition, body, and optional
1077 /// variadic else statements
1078 /// @param condition the if statement condition expression
1079 /// @param body the if statement body
1080 /// @param elseStatements optional variadic else statements
1081 /// @returns the if statement pointer
1082 template <typename... ElseStatements>
1083 ast::IfStatement* If(ast::Expression* condition,
1084 ast::BlockStatement* body,
1085 ElseStatements&&... elseStatements) {
1086 return create<ast::IfStatement>(
1087 condition, body,
1088 ast::ElseStatementList{
1089 std::forward<ElseStatements>(elseStatements)...});
1090 }
1091
1092 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
1093 /// @param lhs the left hand side expression
1094 /// @param rhs the right hand side expression
1095 /// @returns the assignment statement pointer
1096 ast::AssignmentStatement* Assign(ast::Expression* lhs, ast::Expression* rhs) {
1097 return create<ast::AssignmentStatement>(lhs, rhs);
1098 }
1099
1100 /// Creates a ast::LoopStatement with input body and optional continuing
1101 /// @param body the loop body
1102 /// @param continuing the optional continuing block
1103 /// @returns the loop statement pointer
1104 ast::LoopStatement* Loop(ast::BlockStatement* body,
1105 ast::BlockStatement* continuing = nullptr) {
1106 return create<ast::LoopStatement>(body, continuing);
1107 }
1108
1109 /// Creates a ast::VariableDeclStatement for the input variable
1110 /// @param var the variable to wrap in a decl statement
1111 /// @returns the variable decl statement pointer
1112 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1113 return create<ast::VariableDeclStatement>(var);
1114 }
1115
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001116 /// Sets the current builder source to `src`
1117 /// @param src the Source used for future create() calls
1118 void SetSource(const Source& src) {
1119 AssertNotMoved();
1120 source_ = src;
1121 }
1122
1123 /// Sets the current builder source to `loc`
1124 /// @param loc the Source used for future create() calls
1125 void SetSource(const Source::Location& loc) {
1126 AssertNotMoved();
1127 source_ = Source(loc);
1128 }
1129
Ben Clayton33352542021-01-29 16:43:41 +00001130 /// Helper for returning the resolved semantic type of the expression `expr`.
1131 /// @note As the TypeDeterminator is run when the Program is built, this will
1132 /// only be useful for the TypeDeterminer itself and tests that use their own
1133 /// TypeDeterminer.
1134 /// @param expr the AST expression
1135 /// @return the resolved semantic type for the expression, or nullptr if the
1136 /// expression has no resolved type.
1137 type::Type* TypeOf(ast::Expression* expr) const;
1138
Ben Clayton401b96b2021-02-03 17:19:59 +00001139 /// Wraps the ast::Expression in a statement. This is used by tests that
1140 /// construct a partial AST and require the TypeDeterminer to reach these
1141 /// nodes.
1142 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1143 /// @return the ast::Statement that wraps the ast::Expression
1144 ast::Statement* WrapInStatement(ast::Expression* expr);
1145 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
1146 /// tests that construct a partial AST and require the TypeDeterminer to reach
1147 /// these nodes.
1148 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1149 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1150 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1151 /// Returns the statement argument. Used as a passthrough-overload by
1152 /// WrapInFunction().
1153 /// @param stmt the ast::Statement
1154 /// @return `stmt`
1155 ast::Statement* WrapInStatement(ast::Statement* stmt);
1156 /// Wraps the list of arguments in a simple function so that each is reachable
1157 /// by the TypeDeterminer.
1158 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
1159 template <typename... ARGS>
1160 void WrapInFunction(ARGS&&... args) {
1161 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
1162 WrapInFunction(stmts);
1163 }
1164 /// @param stmts a list of ast::Statement that will be wrapped by a function,
1165 /// so that each statement is reachable by the TypeDeterminer.
1166 void WrapInFunction(ast::StatementList stmts);
1167
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001168 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001169 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001170
1171 protected:
1172 /// Asserts that the builder has not been moved.
1173 void AssertNotMoved() const;
1174
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001175 private:
1176 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001177 ASTNodeAllocator ast_nodes_;
1178 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001179 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001180 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001181 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001182 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001183
1184 /// The source to use when creating AST nodes without providing a Source as
1185 /// the first argument.
1186 Source source_;
1187
Ben Claytondd69ac32021-01-27 19:23:55 +00001188 /// Set by SetResolveOnBuild(). If set, the TypeDeterminer will be run on the
1189 /// program when built.
1190 bool resolve_on_build_ = true;
1191
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001192 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1193 bool moved_ = false;
1194};
1195
1196//! @cond Doxygen_Suppress
1197// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1198template <>
1199struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1200 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1201 return t->i32();
1202 }
1203};
1204template <>
1205struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1206 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1207 return t->u32();
1208 }
1209};
1210template <>
1211struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1212 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1213 return t->f32();
1214 }
1215};
1216template <>
1217struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1218 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1219 return t->bool_();
1220 }
1221};
1222template <>
1223struct ProgramBuilder::TypesBuilder::CToAST<void> {
1224 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1225 return t->void_();
1226 }
1227};
1228//! @endcond
1229
1230} // namespace tint
1231
1232#endif // SRC_PROGRAM_BUILDER_H_