blob: f0ecfb34716e6186a7ddeabb8e97aabf0f89217d [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"
22#include "src/ast/binary_expression.h"
23#include "src/ast/bool_literal.h"
24#include "src/ast/call_expression.h"
25#include "src/ast/expression.h"
26#include "src/ast/float_literal.h"
27#include "src/ast/identifier_expression.h"
28#include "src/ast/member_accessor_expression.h"
29#include "src/ast/module.h"
30#include "src/ast/scalar_constructor_expression.h"
31#include "src/ast/sint_literal.h"
Ben Claytonbab31972021-02-08 21:16:21 +000032#include "src/ast/stride_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/struct.h"
34#include "src/ast/struct_member.h"
35#include "src/ast/struct_member_offset_decoration.h"
36#include "src/ast/type_constructor_expression.h"
37#include "src/ast/uint_literal.h"
38#include "src/ast/variable.h"
Ben Clayton844217f2021-01-27 18:49:05 +000039#include "src/diagnostic/diagnostic.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000040#include "src/program.h"
Ben Claytondd1b6fc2021-01-29 10:55:40 +000041#include "src/semantic/info.h"
Ben Clayton7fdfff12021-01-29 15:17:30 +000042#include "src/semantic/node.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000043#include "src/symbol_table.h"
44#include "src/type/alias_type.h"
45#include "src/type/array_type.h"
46#include "src/type/bool_type.h"
47#include "src/type/f32_type.h"
48#include "src/type/i32_type.h"
49#include "src/type/matrix_type.h"
50#include "src/type/pointer_type.h"
51#include "src/type/struct_type.h"
52#include "src/type/type_manager.h"
53#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 Claytondd69ac32021-01-27 19:23:55 +0000205 /// Controls whether the TypeDeterminer will be run on the program when it is
206 /// built.
207 /// @param enable the new flag value (defaults to true)
208 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
209
210 /// @return true if the TypeDeterminer will be run on the program when it is
211 /// built.
212 bool ResolveOnBuild() const { return resolve_on_build_; }
213
Ben Clayton844217f2021-01-27 18:49:05 +0000214 /// @returns true if the program has no error diagnostics and is not missing
215 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000216 bool IsValid() const;
217
Ben Clayton708dc2d2021-01-29 11:22:40 +0000218 /// Writes a representation of the node to the output stream
219 /// @note unlike str(), to_str() does not automatically demangle the string.
220 /// @param node the AST node
221 /// @param out the stream to write to
222 /// @param indent number of spaces to indent the node when writing
223 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
224 node->to_str(Sem(), out, indent);
225 }
226
227 /// Returns a demangled, string representation of `node`.
228 /// @param node the AST node
229 /// @returns a string representation of the node
230 std::string str(const ast::Node* node) const;
231
Ben Clayton7fdfff12021-01-29 15:17:30 +0000232 /// Creates a new ast::Node owned by the ProgramBuilder. When the
233 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000234 /// @param source the Source of the node
235 /// @param args the arguments to pass to the type constructor
236 /// @returns the node pointer
237 template <typename T, typename... ARGS>
238 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
239 ARGS&&... args) {
240 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000241 return ast_nodes_.Create<T>(source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000242 }
243
Ben Clayton7fdfff12021-01-29 15:17:30 +0000244 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
245 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000246 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000247 /// When the ProgramBuilder is destructed, the ast::Node will also be
248 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000249 /// @returns the node pointer
250 template <typename T>
251 traits::EnableIfIsType<T, ast::Node>* create() {
252 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000253 return ast_nodes_.Create<T>(source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000254 }
255
Ben Clayton7fdfff12021-01-29 15:17:30 +0000256 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
257 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000258 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000259 /// When the ProgramBuilder is destructed, the ast::Node will also be
260 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000261 /// @param arg0 the first arguments to pass to the type constructor
262 /// @param args the remaining arguments to pass to the type constructor
263 /// @returns the node pointer
264 template <typename T, typename ARG0, typename... ARGS>
265 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
266 traits::IsTypeOrDerived<T, ast::Node>::value &&
267 !traits::IsTypeOrDerived<ARG0, Source>::value,
268 T>*
269 create(ARG0&& arg0, ARGS&&... args) {
270 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000271 return ast_nodes_.Create<T>(source_, std::forward<ARG0>(arg0),
272 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000273 }
274
Ben Clayton7fdfff12021-01-29 15:17:30 +0000275 /// Creates a new semantic::Node owned by the ProgramBuilder.
276 /// When the ProgramBuilder is destructed, the semantic::Node will also be
277 /// destructed.
278 /// @param args the arguments to pass to the type constructor
279 /// @returns the node pointer
280 template <typename T, typename... ARGS>
281 traits::EnableIfIsType<T, semantic::Node>* create(ARGS&&... args) {
282 AssertNotMoved();
283 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
284 }
285
286 /// Creates a new type::Type owned by the ProgramBuilder.
287 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
288 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000289 /// Types are unique (de-aliased), and so calling create() for the same `T`
290 /// and arguments will return the same pointer.
291 /// @warning Use this method to acquire a type only if all of its type
292 /// information is provided in the constructor arguments `args`.<br>
293 /// If the type requires additional configuration after construction that
294 /// affect its fundamental type, build the type with `std::make_unique`, make
295 /// any necessary alterations and then call unique_type() instead.
296 /// @param args the arguments to pass to the type constructor
297 /// @returns the de-aliased type pointer
298 template <typename T, typename... ARGS>
299 traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) {
300 static_assert(std::is_base_of<type::Type, T>::value,
301 "T does not derive from type::Type");
302 AssertNotMoved();
303 return types_.Get<T>(std::forward<ARGS>(args)...);
304 }
305
306 /// Marks this builder as moved, preventing any further use of the builder.
307 void MarkAsMoved();
308
309 //////////////////////////////////////////////////////////////////////////////
310 // TypesBuilder
311 //////////////////////////////////////////////////////////////////////////////
312
313 /// TypesBuilder holds basic `tint` types and methods for constructing
314 /// complex types.
315 class TypesBuilder {
316 public:
317 /// Constructor
318 /// @param builder the program builder
319 explicit TypesBuilder(ProgramBuilder* builder);
320
321 /// @return the tint AST type for the C type `T`.
322 template <typename T>
323 type::Type* Of() const {
324 return CToAST<T>::get(this);
325 }
326
327 /// @returns a boolean type
328 type::Bool* bool_() const { return builder->create<type::Bool>(); }
329
330 /// @returns a f32 type
331 type::F32* f32() const { return builder->create<type::F32>(); }
332
333 /// @returns a i32 type
334 type::I32* i32() const { return builder->create<type::I32>(); }
335
336 /// @returns a u32 type
337 type::U32* u32() const { return builder->create<type::U32>(); }
338
339 /// @returns a void type
340 type::Void* void_() const { return builder->create<type::Void>(); }
341
342 /// @return the tint AST type for a 2-element vector of the C type `T`.
343 template <typename T>
344 type::Vector* vec2() const {
345 return builder->create<type::Vector>(Of<T>(), 2);
346 }
347
348 /// @return the tint AST type for a 3-element vector of the C type `T`.
349 template <typename T>
350 type::Vector* vec3() const {
351 return builder->create<type::Vector>(Of<T>(), 3);
352 }
353
354 /// @return the tint AST type for a 4-element vector of the C type `T`.
355 template <typename T>
356 type::Type* vec4() const {
357 return builder->create<type::Vector>(Of<T>(), 4);
358 }
359
360 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
361 template <typename T>
362 type::Matrix* mat2x2() const {
363 return builder->create<type::Matrix>(Of<T>(), 2, 2);
364 }
365
366 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
367 template <typename T>
368 type::Matrix* mat2x3() const {
369 return builder->create<type::Matrix>(Of<T>(), 3, 2);
370 }
371
372 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
373 template <typename T>
374 type::Matrix* mat2x4() const {
375 return builder->create<type::Matrix>(Of<T>(), 4, 2);
376 }
377
378 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
379 template <typename T>
380 type::Matrix* mat3x2() const {
381 return builder->create<type::Matrix>(Of<T>(), 2, 3);
382 }
383
384 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
385 template <typename T>
386 type::Matrix* mat3x3() const {
387 return builder->create<type::Matrix>(Of<T>(), 3, 3);
388 }
389
390 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
391 template <typename T>
392 type::Matrix* mat3x4() const {
393 return builder->create<type::Matrix>(Of<T>(), 4, 3);
394 }
395
396 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
397 template <typename T>
398 type::Matrix* mat4x2() const {
399 return builder->create<type::Matrix>(Of<T>(), 2, 4);
400 }
401
402 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
403 template <typename T>
404 type::Matrix* mat4x3() const {
405 return builder->create<type::Matrix>(Of<T>(), 3, 4);
406 }
407
408 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
409 template <typename T>
410 type::Matrix* mat4x4() const {
411 return builder->create<type::Matrix>(Of<T>(), 4, 4);
412 }
413
414 /// @param subtype the array element type
415 /// @param n the array size. 0 represents a runtime-array.
416 /// @return the tint AST type for a array of size `n` of type `T`
417 type::Array* array(type::Type* subtype, uint32_t n) const {
418 return builder->create<type::Array>(subtype, n,
419 ast::ArrayDecorationList{});
420 }
421
Ben Claytonbab31972021-02-08 21:16:21 +0000422 /// @param subtype the array element type
423 /// @param n the array size. 0 represents a runtime-array.
424 /// @param stride the array stride.
425 /// @return the tint AST type for a array of size `n` of type `T`
426 type::Array* array(type::Type* subtype, uint32_t n, uint32_t stride) const {
427 return builder->create<type::Array>(
428 subtype, n,
429 ast::ArrayDecorationList{
430 builder->create<ast::StrideDecoration>(stride),
431 });
432 }
433
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000434 /// @return the tint AST type for an array of size `N` of type `T`
435 template <typename T, int N = 0>
436 type::Array* array() const {
437 return array(Of<T>(), N);
438 }
439
Ben Claytonbab31972021-02-08 21:16:21 +0000440 /// @param stride the array stride
441 /// @return the tint AST type for an array of size `N` of type `T`
442 template <typename T, int N = 0>
443 type::Array* array(uint32_t stride) const {
444 return array(Of<T>(), N, stride);
445 }
Ben Clayton42d1e092021-02-02 14:29:15 +0000446 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000447 /// @param name the alias name
448 /// @param type the alias type
449 /// @returns the alias pointer
450 type::Alias* alias(const std::string& name, type::Type* type) const {
451 return builder->create<type::Alias>(builder->Symbols().Register(name),
452 type);
453 }
454
455 /// @return the tint AST pointer to type `T` with the given
456 /// ast::StorageClass.
457 /// @param storage_class the storage class of the pointer
458 template <typename T>
459 type::Pointer* pointer(ast::StorageClass storage_class) const {
460 return builder->create<type::Pointer>(Of<T>(), storage_class);
461 }
462
463 /// @param name the struct name
464 /// @param impl the struct implementation
465 /// @returns a struct pointer
466 type::Struct* struct_(const std::string& name, ast::Struct* impl) const {
467 return builder->create<type::Struct>(builder->Symbols().Register(name),
468 impl);
469 }
470
471 private:
472 /// CToAST<T> is specialized for various `T` types and each specialization
473 /// contains a single static `get()` method for obtaining the corresponding
474 /// AST type for the C type `T`.
475 /// `get()` has the signature:
476 /// `static type::Type* get(Types* t)`
477 template <typename T>
478 struct CToAST {};
479
480 ProgramBuilder* builder;
481 };
482
483 //////////////////////////////////////////////////////////////////////////////
484 // AST helper methods
485 //////////////////////////////////////////////////////////////////////////////
486
487 /// @param expr the expression
488 /// @return expr
489 ast::Expression* Expr(ast::Expression* expr) { return expr; }
490
491 /// @param name the identifier name
492 /// @return an ast::IdentifierExpression with the given name
493 ast::IdentifierExpression* Expr(const std::string& name) {
494 return create<ast::IdentifierExpression>(Symbols().Register(name));
495 }
496
Ben Clayton46d78d72021-02-10 21:34:26 +0000497 /// @param symbol the identifier symbol
498 /// @return an ast::IdentifierExpression with the given symbol
499 ast::IdentifierExpression* Expr(Symbol symbol) {
500 return create<ast::IdentifierExpression>(symbol);
501 }
502
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000503 /// @param source the source information
504 /// @param name the identifier name
505 /// @return an ast::IdentifierExpression with the given name
506 ast::IdentifierExpression* Expr(const Source& source,
507 const std::string& name) {
508 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
509 }
510
511 /// @param name the identifier name
512 /// @return an ast::IdentifierExpression with the given name
513 ast::IdentifierExpression* Expr(const char* name) {
514 return create<ast::IdentifierExpression>(Symbols().Register(name));
515 }
516
517 /// @param value the boolean value
518 /// @return a Scalar constructor for the given value
519 ast::ScalarConstructorExpression* Expr(bool value) {
520 return create<ast::ScalarConstructorExpression>(Literal(value));
521 }
522
523 /// @param value the float value
524 /// @return a Scalar constructor for the given value
525 ast::ScalarConstructorExpression* Expr(f32 value) {
526 return create<ast::ScalarConstructorExpression>(Literal(value));
527 }
528
529 /// @param value the integer value
530 /// @return a Scalar constructor for the given value
531 ast::ScalarConstructorExpression* Expr(i32 value) {
532 return create<ast::ScalarConstructorExpression>(Literal(value));
533 }
534
535 /// @param value the unsigned int value
536 /// @return a Scalar constructor for the given value
537 ast::ScalarConstructorExpression* Expr(u32 value) {
538 return create<ast::ScalarConstructorExpression>(Literal(value));
539 }
540
541 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
542 /// `list`.
543 /// @param list the list to append too
544 /// @param arg the arg to create
545 template <typename ARG>
546 void Append(ast::ExpressionList& list, ARG&& arg) {
547 list.emplace_back(Expr(std::forward<ARG>(arg)));
548 }
549
550 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
551 /// then appends them to `list`.
552 /// @param list the list to append too
553 /// @param arg0 the first argument
554 /// @param args the rest of the arguments
555 template <typename ARG0, typename... ARGS>
556 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
557 Append(list, std::forward<ARG0>(arg0));
558 Append(list, std::forward<ARGS>(args)...);
559 }
560
561 /// @return an empty list of expressions
562 ast::ExpressionList ExprList() { return {}; }
563
564 /// @param args the list of expressions
565 /// @return the list of expressions converted to `ast::Expression`s using
566 /// `Expr()`,
567 template <typename... ARGS>
568 ast::ExpressionList ExprList(ARGS&&... args) {
569 ast::ExpressionList list;
570 list.reserve(sizeof...(args));
571 Append(list, std::forward<ARGS>(args)...);
572 return list;
573 }
574
575 /// @param list the list of expressions
576 /// @return `list`
577 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
578
579 /// @param val the boolan value
580 /// @return a boolean literal with the given value
581 ast::BoolLiteral* Literal(bool val) {
582 return create<ast::BoolLiteral>(ty.bool_(), val);
583 }
584
585 /// @param val the float value
586 /// @return a float literal with the given value
587 ast::FloatLiteral* Literal(f32 val) {
588 return create<ast::FloatLiteral>(ty.f32(), val);
589 }
590
591 /// @param val the unsigned int value
592 /// @return a ast::UintLiteral with the given value
593 ast::UintLiteral* Literal(u32 val) {
594 return create<ast::UintLiteral>(ty.u32(), val);
595 }
596
597 /// @param val the integer value
598 /// @return the ast::SintLiteral with the given value
599 ast::SintLiteral* Literal(i32 val) {
600 return create<ast::SintLiteral>(ty.i32(), val);
601 }
602
603 /// @param args the arguments for the type constructor
604 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
605 /// of `args` converted to `ast::Expression`s using `Expr()`
606 template <typename T, typename... ARGS>
607 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
608 return create<ast::TypeConstructorExpression>(
609 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
610 }
611
612 /// @param type the type to construct
613 /// @param args the arguments for the constructor
614 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
615 /// values `args`.
616 template <typename... ARGS>
617 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
618 return create<ast::TypeConstructorExpression>(
619 type, ExprList(std::forward<ARGS>(args)...));
620 }
621
622 /// @param args the arguments for the vector constructor
623 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
624 /// `T`, constructed with the values `args`.
625 template <typename T, typename... ARGS>
626 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
627 return create<ast::TypeConstructorExpression>(
628 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
629 }
630
631 /// @param args the arguments for the vector constructor
632 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
633 /// `T`, constructed with the values `args`.
634 template <typename T, typename... ARGS>
635 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
636 return create<ast::TypeConstructorExpression>(
637 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
638 }
639
640 /// @param args the arguments for the vector constructor
641 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
642 /// `T`, constructed with the values `args`.
643 template <typename T, typename... ARGS>
644 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
645 return create<ast::TypeConstructorExpression>(
646 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
647 }
648
649 /// @param args the arguments for the matrix constructor
650 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
651 /// `T`, constructed with the values `args`.
652 template <typename T, typename... ARGS>
653 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
654 return create<ast::TypeConstructorExpression>(
655 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
656 }
657
658 /// @param args the arguments for the matrix constructor
659 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
660 /// `T`, constructed with the values `args`.
661 template <typename T, typename... ARGS>
662 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
663 return create<ast::TypeConstructorExpression>(
664 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
665 }
666
667 /// @param args the arguments for the matrix constructor
668 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
669 /// `T`, constructed with the values `args`.
670 template <typename T, typename... ARGS>
671 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
672 return create<ast::TypeConstructorExpression>(
673 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
674 }
675
676 /// @param args the arguments for the matrix constructor
677 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
678 /// `T`, constructed with the values `args`.
679 template <typename T, typename... ARGS>
680 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
681 return create<ast::TypeConstructorExpression>(
682 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
683 }
684
685 /// @param args the arguments for the matrix constructor
686 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
687 /// `T`, constructed with the values `args`.
688 template <typename T, typename... ARGS>
689 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
690 return create<ast::TypeConstructorExpression>(
691 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
692 }
693
694 /// @param args the arguments for the matrix constructor
695 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
696 /// `T`, constructed with the values `args`.
697 template <typename T, typename... ARGS>
698 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
699 return create<ast::TypeConstructorExpression>(
700 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
701 }
702
703 /// @param args the arguments for the matrix constructor
704 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
705 /// `T`, constructed with the values `args`.
706 template <typename T, typename... ARGS>
707 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
708 return create<ast::TypeConstructorExpression>(
709 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
710 }
711
712 /// @param args the arguments for the matrix constructor
713 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
714 /// `T`, constructed with the values `args`.
715 template <typename T, typename... ARGS>
716 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
717 return create<ast::TypeConstructorExpression>(
718 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
719 }
720
721 /// @param args the arguments for the matrix constructor
722 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
723 /// `T`, constructed with the values `args`.
724 template <typename T, typename... ARGS>
725 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
726 return create<ast::TypeConstructorExpression>(
727 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
728 }
729
730 /// @param args the arguments for the array constructor
731 /// @return an `ast::TypeConstructorExpression` of an array with element type
732 /// `T`, constructed with the values `args`.
733 template <typename T, int N = 0, typename... ARGS>
734 ast::TypeConstructorExpression* array(ARGS&&... args) {
735 return create<ast::TypeConstructorExpression>(
736 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
737 }
738
739 /// @param subtype the array element type
740 /// @param n the array size. 0 represents a runtime-array.
741 /// @param args the arguments for the array constructor
742 /// @return an `ast::TypeConstructorExpression` of an array with element type
743 /// `subtype`, constructed with the values `args`.
744 template <typename... ARGS>
745 ast::TypeConstructorExpression* array(type::Type* subtype,
746 uint32_t n,
747 ARGS&&... args) {
748 return create<ast::TypeConstructorExpression>(
749 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
750 }
751
752 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000753 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000754 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000755 /// @param constructor constructor expression
756 /// @param decorations variable decorations
757 /// @returns a `ast::Variable` with the given name, storage and type
758 ast::Variable* Var(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000759 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000760 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000761 ast::Expression* constructor = nullptr,
762 ast::VariableDecorationList decorations = {}) {
763 return create<ast::Variable>(Symbols().Register(name), storage, type, false,
764 constructor, decorations);
765 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000766
767 /// @param source the variable source
768 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000769 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000770 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000771 /// @param constructor constructor expression
772 /// @param decorations variable decorations
773 /// @returns a `ast::Variable` with the given name, storage and type
774 ast::Variable* Var(const Source& source,
775 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000776 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000777 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000778 ast::Expression* constructor = nullptr,
779 ast::VariableDecorationList decorations = {}) {
780 return create<ast::Variable>(source, Symbols().Register(name), storage,
781 type, false, constructor, decorations);
782 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000783
Ben Clayton46d78d72021-02-10 21:34:26 +0000784 /// @param symbol the variable symbol
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000785 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000786 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000787 /// @param constructor constructor expression
788 /// @param decorations variable decorations
789 /// @returns a `ast::Variable` with the given symbol, storage and type
790 ast::Variable* Var(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000791 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000792 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000793 ast::Expression* constructor = nullptr,
794 ast::VariableDecorationList decorations = {}) {
795 return create<ast::Variable>(symbol, storage, type, false, constructor,
796 decorations);
797 }
798
799 /// @param source the variable source
800 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000801 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000802 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000803 /// @param constructor constructor expression
804 /// @param decorations variable decorations
805 /// @returns a `ast::Variable` with the given symbol, storage and type
806 ast::Variable* Var(const Source& source,
807 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000808 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000809 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000810 ast::Expression* constructor = nullptr,
811 ast::VariableDecorationList decorations = {}) {
812 return create<ast::Variable>(source, symbol, storage, type, false,
813 constructor, decorations);
814 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000815
816 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000817 /// @param type the variable type
818 /// @param constructor optional constructor expression
819 /// @param decorations optional variable decorations
820 /// @returns a constant `ast::Variable` with the given name, storage and type
821 ast::Variable* Const(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000822 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000823 ast::Expression* constructor = nullptr,
824 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000825 return create<ast::Variable>(Symbols().Register(name),
826 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000827 constructor, decorations);
828 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000829
830 /// @param source the variable source
831 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000832 /// @param type the variable type
833 /// @param constructor optional constructor expression
834 /// @param decorations optional variable decorations
835 /// @returns a constant `ast::Variable` with the given name, storage and type
836 ast::Variable* Const(const Source& source,
837 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000838 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000839 ast::Expression* constructor = nullptr,
840 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000841 return create<ast::Variable>(source, Symbols().Register(name),
842 ast::StorageClass::kNone, type, true,
843 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000844 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000845
Ben Clayton46d78d72021-02-10 21:34:26 +0000846 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000847 /// @param type the variable type
848 /// @param constructor optional constructor expression
849 /// @param decorations optional variable decorations
850 /// @returns a constant `ast::Variable` with the given symbol, storage and
851 /// type
852 ast::Variable* Const(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000853 type::Type* type,
854 ast::Expression* constructor = nullptr,
855 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000856 return create<ast::Variable>(symbol, ast::StorageClass::kNone, type, true,
857 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000858 }
859
860 /// @param source the variable source
861 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000862 /// @param type the variable type
863 /// @param constructor optional constructor expression
864 /// @param decorations optional variable decorations
865 /// @returns a constant `ast::Variable` with the given symbol, storage and
866 /// type
867 ast::Variable* Const(const Source& source,
868 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000869 type::Type* type,
870 ast::Expression* constructor = nullptr,
871 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000872 return create<ast::Variable>(source, symbol, ast::StorageClass::kNone, type,
873 true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000874 }
Ben Clayton37571bc2021-02-16 23:57:01 +0000875
Ben Clayton401b96b2021-02-03 17:19:59 +0000876 /// @param args the arguments to pass to Var()
877 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
878 /// of `args`, which is automatically registered as a global variable with the
879 /// ast::Module.
880 template <typename... ARGS>
881 ast::Variable* Global(ARGS&&... args) {
882 auto* var = Var(std::forward<ARGS>(args)...);
883 AST().AddGlobalVariable(var);
884 return var;
885 }
886
887 /// @param args the arguments to pass to Const()
888 /// @returns a const `ast::Variable` constructed by calling Var() with the
889 /// arguments of `args`, which is automatically registered as a global
890 /// variable with the ast::Module.
891 template <typename... ARGS>
892 ast::Variable* GlobalConst(ARGS&&... args) {
893 auto* var = Const(std::forward<ARGS>(args)...);
894 AST().AddGlobalVariable(var);
895 return var;
896 }
897
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000898 /// @param func the function name
899 /// @param args the function call arguments
900 /// @returns a `ast::CallExpression` to the function `func`, with the
901 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
902 template <typename NAME, typename... ARGS>
903 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
904 return create<ast::CallExpression>(Expr(func),
905 ExprList(std::forward<ARGS>(args)...));
906 }
907
908 /// @param lhs the left hand argument to the addition operation
909 /// @param rhs the right hand argument to the addition operation
910 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
911 template <typename LHS, typename RHS>
912 ast::Expression* Add(LHS&& lhs, RHS&& rhs) {
913 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
914 Expr(std::forward<LHS>(lhs)),
915 Expr(std::forward<RHS>(rhs)));
916 }
917
918 /// @param lhs the left hand argument to the subtraction operation
919 /// @param rhs the right hand argument to the subtraction operation
920 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
921 template <typename LHS, typename RHS>
922 ast::Expression* Sub(LHS&& lhs, RHS&& rhs) {
923 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
924 Expr(std::forward<LHS>(lhs)),
925 Expr(std::forward<RHS>(rhs)));
926 }
927
928 /// @param lhs the left hand argument to the multiplication operation
929 /// @param rhs the right hand argument to the multiplication operation
930 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
931 template <typename LHS, typename RHS>
932 ast::Expression* Mul(LHS&& lhs, RHS&& rhs) {
933 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
934 Expr(std::forward<LHS>(lhs)),
935 Expr(std::forward<RHS>(rhs)));
936 }
937
938 /// @param arr the array argument for the array accessor expression
939 /// @param idx the index argument for the array accessor expression
940 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
941 template <typename ARR, typename IDX>
942 ast::Expression* IndexAccessor(ARR&& arr, IDX&& idx) {
943 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
944 Expr(std::forward<IDX>(idx)));
945 }
946
947 /// @param obj the object for the member accessor expression
948 /// @param idx the index argument for the array accessor expression
949 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
950 template <typename OBJ, typename IDX>
951 ast::Expression* MemberAccessor(OBJ&& obj, IDX&& idx) {
952 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
953 Expr(std::forward<IDX>(idx)));
954 }
955
Ben Clayton42d1e092021-02-02 14:29:15 +0000956 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000957 /// @param val the offset value
958 /// @returns the offset decoration pointer
959 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
960 return create<ast::StructMemberOffsetDecoration>(source_, val);
961 }
962
Ben Clayton42d1e092021-02-02 14:29:15 +0000963 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000964 /// @param source the source information
965 /// @param name the function name
966 /// @param params the function parameters
967 /// @param type the function return type
968 /// @param body the function body
969 /// @param decorations the function decorations
970 /// @returns the function pointer
971 ast::Function* Func(Source source,
972 std::string name,
973 ast::VariableList params,
974 type::Type* type,
975 ast::StatementList body,
976 ast::FunctionDecorationList decorations) {
Ben Clayton42d1e092021-02-02 14:29:15 +0000977 auto* func =
978 create<ast::Function>(source, Symbols().Register(name), params, type,
979 create<ast::BlockStatement>(body), decorations);
James Price3eaa4502021-02-09 21:26:21 +0000980 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +0000981 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000982 }
983
Ben Clayton42d1e092021-02-02 14:29:15 +0000984 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000985 /// @param name the function name
986 /// @param params the function parameters
987 /// @param type the function return type
988 /// @param body the function body
989 /// @param decorations the function decorations
990 /// @returns the function pointer
991 ast::Function* Func(std::string name,
992 ast::VariableList params,
993 type::Type* type,
994 ast::StatementList body,
995 ast::FunctionDecorationList decorations) {
Ben Clayton42d1e092021-02-02 14:29:15 +0000996 auto* func =
997 create<ast::Function>(Symbols().Register(name), params, type,
998 create<ast::BlockStatement>(body), decorations);
James Price3eaa4502021-02-09 21:26:21 +0000999 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001000 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001001 }
1002
Ben Clayton42d1e092021-02-02 14:29:15 +00001003 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001004 /// @param source the source information
1005 /// @param name the struct member name
1006 /// @param type the struct member type
1007 /// @returns the struct member pointer
1008 ast::StructMember* Member(const Source& source,
1009 const std::string& name,
1010 type::Type* type) {
1011 return create<ast::StructMember>(source, Symbols().Register(name), type,
1012 ast::StructMemberDecorationList{});
1013 }
1014
Ben Clayton42d1e092021-02-02 14:29:15 +00001015 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001016 /// @param name the struct member name
1017 /// @param type the struct member type
1018 /// @returns the struct member pointer
1019 ast::StructMember* Member(const std::string& name, type::Type* type) {
1020 return create<ast::StructMember>(source_, Symbols().Register(name), type,
1021 ast::StructMemberDecorationList{});
1022 }
1023
Ben Clayton42d1e092021-02-02 14:29:15 +00001024 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001025 /// @param name the struct member name
1026 /// @param type the struct member type
1027 /// @param decorations the struct member decorations
1028 /// @returns the struct member pointer
1029 ast::StructMember* Member(const std::string& name,
1030 type::Type* type,
1031 ast::StructMemberDecorationList decorations) {
1032 return create<ast::StructMember>(source_, Symbols().Register(name), type,
1033 decorations);
1034 }
1035
Ben Claytonbab31972021-02-08 21:16:21 +00001036 /// Creates a ast::StructMember with the given byte offset
1037 /// @param offset the offset to use in the StructMemberOffsetDecoration
1038 /// @param name the struct member name
1039 /// @param type the struct member type
1040 /// @returns the struct member pointer
1041 ast::StructMember* Member(uint32_t offset,
1042 const std::string& name,
1043 type::Type* type) {
1044 return create<ast::StructMember>(
1045 source_, Symbols().Register(name), type,
1046 ast::StructMemberDecorationList{
1047 create<ast::StructMemberOffsetDecoration>(offset),
1048 });
1049 }
1050
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001051 /// Sets the current builder source to `src`
1052 /// @param src the Source used for future create() calls
1053 void SetSource(const Source& src) {
1054 AssertNotMoved();
1055 source_ = src;
1056 }
1057
1058 /// Sets the current builder source to `loc`
1059 /// @param loc the Source used for future create() calls
1060 void SetSource(const Source::Location& loc) {
1061 AssertNotMoved();
1062 source_ = Source(loc);
1063 }
1064
Ben Clayton33352542021-01-29 16:43:41 +00001065 /// Helper for returning the resolved semantic type of the expression `expr`.
1066 /// @note As the TypeDeterminator is run when the Program is built, this will
1067 /// only be useful for the TypeDeterminer itself and tests that use their own
1068 /// TypeDeterminer.
1069 /// @param expr the AST expression
1070 /// @return the resolved semantic type for the expression, or nullptr if the
1071 /// expression has no resolved type.
1072 type::Type* TypeOf(ast::Expression* expr) const;
1073
Ben Clayton401b96b2021-02-03 17:19:59 +00001074 /// Wraps the ast::Expression in a statement. This is used by tests that
1075 /// construct a partial AST and require the TypeDeterminer to reach these
1076 /// nodes.
1077 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1078 /// @return the ast::Statement that wraps the ast::Expression
1079 ast::Statement* WrapInStatement(ast::Expression* expr);
1080 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
1081 /// tests that construct a partial AST and require the TypeDeterminer to reach
1082 /// these nodes.
1083 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1084 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1085 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1086 /// Returns the statement argument. Used as a passthrough-overload by
1087 /// WrapInFunction().
1088 /// @param stmt the ast::Statement
1089 /// @return `stmt`
1090 ast::Statement* WrapInStatement(ast::Statement* stmt);
1091 /// Wraps the list of arguments in a simple function so that each is reachable
1092 /// by the TypeDeterminer.
1093 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
1094 template <typename... ARGS>
1095 void WrapInFunction(ARGS&&... args) {
1096 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
1097 WrapInFunction(stmts);
1098 }
1099 /// @param stmts a list of ast::Statement that will be wrapped by a function,
1100 /// so that each statement is reachable by the TypeDeterminer.
1101 void WrapInFunction(ast::StatementList stmts);
1102
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001103 /// The builder types
1104 TypesBuilder ty;
1105
1106 protected:
1107 /// Asserts that the builder has not been moved.
1108 void AssertNotMoved() const;
1109
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001110 private:
1111 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001112 ASTNodeAllocator ast_nodes_;
1113 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001114 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001115 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001116 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001117 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001118
1119 /// The source to use when creating AST nodes without providing a Source as
1120 /// the first argument.
1121 Source source_;
1122
Ben Claytondd69ac32021-01-27 19:23:55 +00001123 /// Set by SetResolveOnBuild(). If set, the TypeDeterminer will be run on the
1124 /// program when built.
1125 bool resolve_on_build_ = true;
1126
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001127 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1128 bool moved_ = false;
1129};
1130
1131//! @cond Doxygen_Suppress
1132// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1133template <>
1134struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1135 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1136 return t->i32();
1137 }
1138};
1139template <>
1140struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1141 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1142 return t->u32();
1143 }
1144};
1145template <>
1146struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1147 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1148 return t->f32();
1149 }
1150};
1151template <>
1152struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1153 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1154 return t->bool_();
1155 }
1156};
1157template <>
1158struct ProgramBuilder::TypesBuilder::CToAST<void> {
1159 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1160 return t->void_();
1161 }
1162};
1163//! @endcond
1164
1165} // namespace tint
1166
1167#endif // SRC_PROGRAM_BUILDER_H_