blob: d4b8ff6a71d4da87e205722e912d42e9095cfcad [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
Ben Claytonc7ca7662021-02-17 16:23:52 +0000480 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000481 };
482
483 //////////////////////////////////////////////////////////////////////////////
484 // AST helper methods
485 //////////////////////////////////////////////////////////////////////////////
486
487 /// @param expr the expression
488 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000489 template <typename T>
490 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
491 return expr;
492 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000493
494 /// @param name the identifier name
495 /// @return an ast::IdentifierExpression with the given name
496 ast::IdentifierExpression* Expr(const std::string& name) {
497 return create<ast::IdentifierExpression>(Symbols().Register(name));
498 }
499
Ben Clayton46d78d72021-02-10 21:34:26 +0000500 /// @param symbol the identifier symbol
501 /// @return an ast::IdentifierExpression with the given symbol
502 ast::IdentifierExpression* Expr(Symbol symbol) {
503 return create<ast::IdentifierExpression>(symbol);
504 }
505
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000506 /// @param source the source information
507 /// @param name the identifier name
508 /// @return an ast::IdentifierExpression with the given name
509 ast::IdentifierExpression* Expr(const Source& source,
510 const std::string& name) {
511 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
512 }
513
514 /// @param name the identifier name
515 /// @return an ast::IdentifierExpression with the given name
516 ast::IdentifierExpression* Expr(const char* name) {
517 return create<ast::IdentifierExpression>(Symbols().Register(name));
518 }
519
520 /// @param value the boolean value
521 /// @return a Scalar constructor for the given value
522 ast::ScalarConstructorExpression* Expr(bool value) {
523 return create<ast::ScalarConstructorExpression>(Literal(value));
524 }
525
526 /// @param value the float value
527 /// @return a Scalar constructor for the given value
528 ast::ScalarConstructorExpression* Expr(f32 value) {
529 return create<ast::ScalarConstructorExpression>(Literal(value));
530 }
531
532 /// @param value the integer value
533 /// @return a Scalar constructor for the given value
534 ast::ScalarConstructorExpression* Expr(i32 value) {
535 return create<ast::ScalarConstructorExpression>(Literal(value));
536 }
537
538 /// @param value the unsigned int value
539 /// @return a Scalar constructor for the given value
540 ast::ScalarConstructorExpression* Expr(u32 value) {
541 return create<ast::ScalarConstructorExpression>(Literal(value));
542 }
543
544 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
545 /// `list`.
546 /// @param list the list to append too
547 /// @param arg the arg to create
548 template <typename ARG>
549 void Append(ast::ExpressionList& list, ARG&& arg) {
550 list.emplace_back(Expr(std::forward<ARG>(arg)));
551 }
552
553 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
554 /// then appends them to `list`.
555 /// @param list the list to append too
556 /// @param arg0 the first argument
557 /// @param args the rest of the arguments
558 template <typename ARG0, typename... ARGS>
559 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
560 Append(list, std::forward<ARG0>(arg0));
561 Append(list, std::forward<ARGS>(args)...);
562 }
563
564 /// @return an empty list of expressions
565 ast::ExpressionList ExprList() { return {}; }
566
567 /// @param args the list of expressions
568 /// @return the list of expressions converted to `ast::Expression`s using
569 /// `Expr()`,
570 template <typename... ARGS>
571 ast::ExpressionList ExprList(ARGS&&... args) {
572 ast::ExpressionList list;
573 list.reserve(sizeof...(args));
574 Append(list, std::forward<ARGS>(args)...);
575 return list;
576 }
577
578 /// @param list the list of expressions
579 /// @return `list`
580 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
581
582 /// @param val the boolan value
583 /// @return a boolean literal with the given value
584 ast::BoolLiteral* Literal(bool val) {
585 return create<ast::BoolLiteral>(ty.bool_(), val);
586 }
587
588 /// @param val the float value
589 /// @return a float literal with the given value
590 ast::FloatLiteral* Literal(f32 val) {
591 return create<ast::FloatLiteral>(ty.f32(), val);
592 }
593
594 /// @param val the unsigned int value
595 /// @return a ast::UintLiteral with the given value
596 ast::UintLiteral* Literal(u32 val) {
597 return create<ast::UintLiteral>(ty.u32(), val);
598 }
599
600 /// @param val the integer value
601 /// @return the ast::SintLiteral with the given value
602 ast::SintLiteral* Literal(i32 val) {
603 return create<ast::SintLiteral>(ty.i32(), val);
604 }
605
606 /// @param args the arguments for the type constructor
607 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
608 /// of `args` converted to `ast::Expression`s using `Expr()`
609 template <typename T, typename... ARGS>
610 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
611 return create<ast::TypeConstructorExpression>(
612 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
613 }
614
615 /// @param type the type to construct
616 /// @param args the arguments for the constructor
617 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
618 /// values `args`.
619 template <typename... ARGS>
620 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
621 return create<ast::TypeConstructorExpression>(
622 type, ExprList(std::forward<ARGS>(args)...));
623 }
624
625 /// @param args the arguments for the vector constructor
626 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
627 /// `T`, constructed with the values `args`.
628 template <typename T, typename... ARGS>
629 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
630 return create<ast::TypeConstructorExpression>(
631 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
632 }
633
634 /// @param args the arguments for the vector constructor
635 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
636 /// `T`, constructed with the values `args`.
637 template <typename T, typename... ARGS>
638 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
639 return create<ast::TypeConstructorExpression>(
640 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
641 }
642
643 /// @param args the arguments for the vector constructor
644 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
645 /// `T`, constructed with the values `args`.
646 template <typename T, typename... ARGS>
647 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
648 return create<ast::TypeConstructorExpression>(
649 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
650 }
651
652 /// @param args the arguments for the matrix constructor
653 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
654 /// `T`, constructed with the values `args`.
655 template <typename T, typename... ARGS>
656 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
657 return create<ast::TypeConstructorExpression>(
658 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
659 }
660
661 /// @param args the arguments for the matrix constructor
662 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
663 /// `T`, constructed with the values `args`.
664 template <typename T, typename... ARGS>
665 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
666 return create<ast::TypeConstructorExpression>(
667 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
668 }
669
670 /// @param args the arguments for the matrix constructor
671 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
672 /// `T`, constructed with the values `args`.
673 template <typename T, typename... ARGS>
674 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
675 return create<ast::TypeConstructorExpression>(
676 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
677 }
678
679 /// @param args the arguments for the matrix constructor
680 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
681 /// `T`, constructed with the values `args`.
682 template <typename T, typename... ARGS>
683 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
684 return create<ast::TypeConstructorExpression>(
685 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
686 }
687
688 /// @param args the arguments for the matrix constructor
689 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
690 /// `T`, constructed with the values `args`.
691 template <typename T, typename... ARGS>
692 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
693 return create<ast::TypeConstructorExpression>(
694 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
695 }
696
697 /// @param args the arguments for the matrix constructor
698 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
699 /// `T`, constructed with the values `args`.
700 template <typename T, typename... ARGS>
701 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
702 return create<ast::TypeConstructorExpression>(
703 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
704 }
705
706 /// @param args the arguments for the matrix constructor
707 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
708 /// `T`, constructed with the values `args`.
709 template <typename T, typename... ARGS>
710 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
711 return create<ast::TypeConstructorExpression>(
712 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
713 }
714
715 /// @param args the arguments for the matrix constructor
716 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
717 /// `T`, constructed with the values `args`.
718 template <typename T, typename... ARGS>
719 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
720 return create<ast::TypeConstructorExpression>(
721 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
722 }
723
724 /// @param args the arguments for the matrix constructor
725 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
726 /// `T`, constructed with the values `args`.
727 template <typename T, typename... ARGS>
728 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
729 return create<ast::TypeConstructorExpression>(
730 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
731 }
732
733 /// @param args the arguments for the array constructor
734 /// @return an `ast::TypeConstructorExpression` of an array with element type
735 /// `T`, constructed with the values `args`.
736 template <typename T, int N = 0, typename... ARGS>
737 ast::TypeConstructorExpression* array(ARGS&&... args) {
738 return create<ast::TypeConstructorExpression>(
739 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
740 }
741
742 /// @param subtype the array element type
743 /// @param n the array size. 0 represents a runtime-array.
744 /// @param args the arguments for the array constructor
745 /// @return an `ast::TypeConstructorExpression` of an array with element type
746 /// `subtype`, constructed with the values `args`.
747 template <typename... ARGS>
748 ast::TypeConstructorExpression* array(type::Type* subtype,
749 uint32_t n,
750 ARGS&&... args) {
751 return create<ast::TypeConstructorExpression>(
752 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
753 }
754
755 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000756 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000757 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000758 /// @param constructor constructor expression
759 /// @param decorations variable decorations
760 /// @returns a `ast::Variable` with the given name, storage and type
761 ast::Variable* Var(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000762 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000763 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000764 ast::Expression* constructor = nullptr,
765 ast::VariableDecorationList decorations = {}) {
766 return create<ast::Variable>(Symbols().Register(name), storage, type, false,
767 constructor, decorations);
768 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000769
770 /// @param source the variable source
771 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000772 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000773 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000774 /// @param constructor constructor expression
775 /// @param decorations variable decorations
776 /// @returns a `ast::Variable` with the given name, storage and type
777 ast::Variable* Var(const Source& source,
778 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000779 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000780 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000781 ast::Expression* constructor = nullptr,
782 ast::VariableDecorationList decorations = {}) {
783 return create<ast::Variable>(source, Symbols().Register(name), storage,
784 type, false, constructor, decorations);
785 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000786
Ben Clayton46d78d72021-02-10 21:34:26 +0000787 /// @param symbol the variable symbol
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000788 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000789 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000790 /// @param constructor constructor expression
791 /// @param decorations variable decorations
792 /// @returns a `ast::Variable` with the given symbol, storage and type
793 ast::Variable* Var(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000794 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000795 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000796 ast::Expression* constructor = nullptr,
797 ast::VariableDecorationList decorations = {}) {
798 return create<ast::Variable>(symbol, storage, type, false, constructor,
799 decorations);
800 }
801
802 /// @param source the variable source
803 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000804 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000805 /// @param storage the variable storage class
Ben Clayton46d78d72021-02-10 21:34:26 +0000806 /// @param constructor constructor expression
807 /// @param decorations variable decorations
808 /// @returns a `ast::Variable` with the given symbol, storage and type
809 ast::Variable* Var(const Source& source,
810 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000811 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000812 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000813 ast::Expression* constructor = nullptr,
814 ast::VariableDecorationList decorations = {}) {
815 return create<ast::Variable>(source, symbol, storage, type, false,
816 constructor, decorations);
817 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000818
819 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000820 /// @param type the variable type
821 /// @param constructor optional constructor expression
822 /// @param decorations optional variable decorations
823 /// @returns a constant `ast::Variable` with the given name, storage and type
824 ast::Variable* Const(const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000825 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000826 ast::Expression* constructor = nullptr,
827 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000828 return create<ast::Variable>(Symbols().Register(name),
829 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000830 constructor, decorations);
831 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000832
833 /// @param source the variable source
834 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000835 /// @param type the variable type
836 /// @param constructor optional constructor expression
837 /// @param decorations optional variable decorations
838 /// @returns a constant `ast::Variable` with the given name, storage and type
839 ast::Variable* Const(const Source& source,
840 const std::string& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000841 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000842 ast::Expression* constructor = nullptr,
843 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000844 return create<ast::Variable>(source, Symbols().Register(name),
845 ast::StorageClass::kNone, type, true,
846 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000847 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000848
Ben Clayton46d78d72021-02-10 21:34:26 +0000849 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000850 /// @param type the variable type
851 /// @param constructor optional constructor expression
852 /// @param decorations optional variable decorations
853 /// @returns a constant `ast::Variable` with the given symbol, storage and
854 /// type
855 ast::Variable* Const(Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000856 type::Type* type,
857 ast::Expression* constructor = nullptr,
858 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000859 return create<ast::Variable>(symbol, ast::StorageClass::kNone, type, true,
860 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000861 }
862
863 /// @param source the variable source
864 /// @param symbol the variable symbol
Ben Clayton46d78d72021-02-10 21:34:26 +0000865 /// @param type the variable type
866 /// @param constructor optional constructor expression
867 /// @param decorations optional variable decorations
868 /// @returns a constant `ast::Variable` with the given symbol, storage and
869 /// type
870 ast::Variable* Const(const Source& source,
871 Symbol symbol,
Ben Clayton46d78d72021-02-10 21:34:26 +0000872 type::Type* type,
873 ast::Expression* constructor = nullptr,
874 ast::VariableDecorationList decorations = {}) {
Ben Clayton81a29fe2021-02-17 00:26:52 +0000875 return create<ast::Variable>(source, symbol, ast::StorageClass::kNone, type,
876 true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000877 }
Ben Clayton37571bc2021-02-16 23:57:01 +0000878
Ben Clayton401b96b2021-02-03 17:19:59 +0000879 /// @param args the arguments to pass to Var()
880 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
881 /// of `args`, which is automatically registered as a global variable with the
882 /// ast::Module.
883 template <typename... ARGS>
884 ast::Variable* Global(ARGS&&... args) {
885 auto* var = Var(std::forward<ARGS>(args)...);
886 AST().AddGlobalVariable(var);
887 return var;
888 }
889
890 /// @param args the arguments to pass to Const()
891 /// @returns a const `ast::Variable` constructed by calling Var() with the
892 /// arguments of `args`, which is automatically registered as a global
893 /// variable with the ast::Module.
894 template <typename... ARGS>
895 ast::Variable* GlobalConst(ARGS&&... args) {
896 auto* var = Const(std::forward<ARGS>(args)...);
897 AST().AddGlobalVariable(var);
898 return var;
899 }
900
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000901 /// @param func the function name
902 /// @param args the function call arguments
903 /// @returns a `ast::CallExpression` to the function `func`, with the
904 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
905 template <typename NAME, typename... ARGS>
906 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
907 return create<ast::CallExpression>(Expr(func),
908 ExprList(std::forward<ARGS>(args)...));
909 }
910
911 /// @param lhs the left hand argument to the addition operation
912 /// @param rhs the right hand argument to the addition operation
913 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
914 template <typename LHS, typename RHS>
915 ast::Expression* Add(LHS&& lhs, RHS&& rhs) {
916 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
917 Expr(std::forward<LHS>(lhs)),
918 Expr(std::forward<RHS>(rhs)));
919 }
920
921 /// @param lhs the left hand argument to the subtraction operation
922 /// @param rhs the right hand argument to the subtraction operation
923 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
924 template <typename LHS, typename RHS>
925 ast::Expression* Sub(LHS&& lhs, RHS&& rhs) {
926 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
927 Expr(std::forward<LHS>(lhs)),
928 Expr(std::forward<RHS>(rhs)));
929 }
930
931 /// @param lhs the left hand argument to the multiplication operation
932 /// @param rhs the right hand argument to the multiplication operation
933 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
934 template <typename LHS, typename RHS>
935 ast::Expression* Mul(LHS&& lhs, RHS&& rhs) {
936 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
937 Expr(std::forward<LHS>(lhs)),
938 Expr(std::forward<RHS>(rhs)));
939 }
940
941 /// @param arr the array argument for the array accessor expression
942 /// @param idx the index argument for the array accessor expression
943 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
944 template <typename ARR, typename IDX>
945 ast::Expression* IndexAccessor(ARR&& arr, IDX&& idx) {
946 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
947 Expr(std::forward<IDX>(idx)));
948 }
949
950 /// @param obj the object for the member accessor expression
951 /// @param idx the index argument for the array accessor expression
952 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
953 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +0000954 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000955 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
956 Expr(std::forward<IDX>(idx)));
957 }
958
Ben Clayton42d1e092021-02-02 14:29:15 +0000959 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000960 /// @param val the offset value
961 /// @returns the offset decoration pointer
962 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
963 return create<ast::StructMemberOffsetDecoration>(source_, val);
964 }
965
Ben Clayton42d1e092021-02-02 14:29:15 +0000966 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000967 /// @param source the source information
968 /// @param name the function name
969 /// @param params the function parameters
970 /// @param type the function return type
971 /// @param body the function body
972 /// @param decorations the function decorations
973 /// @returns the function pointer
974 ast::Function* Func(Source source,
975 std::string name,
976 ast::VariableList params,
977 type::Type* type,
978 ast::StatementList body,
979 ast::FunctionDecorationList decorations) {
Ben Clayton42d1e092021-02-02 14:29:15 +0000980 auto* func =
981 create<ast::Function>(source, Symbols().Register(name), params, type,
982 create<ast::BlockStatement>(body), decorations);
James Price3eaa4502021-02-09 21:26:21 +0000983 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +0000984 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000985 }
986
Ben Clayton42d1e092021-02-02 14:29:15 +0000987 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000988 /// @param name the function name
989 /// @param params the function parameters
990 /// @param type the function return type
991 /// @param body the function body
992 /// @param decorations the function decorations
993 /// @returns the function pointer
994 ast::Function* Func(std::string name,
995 ast::VariableList params,
996 type::Type* type,
997 ast::StatementList body,
998 ast::FunctionDecorationList decorations) {
Ben Clayton42d1e092021-02-02 14:29:15 +0000999 auto* func =
1000 create<ast::Function>(Symbols().Register(name), params, type,
1001 create<ast::BlockStatement>(body), decorations);
James Price3eaa4502021-02-09 21:26:21 +00001002 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001003 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001004 }
1005
Ben Clayton42d1e092021-02-02 14:29:15 +00001006 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001007 /// @param source the source information
1008 /// @param name the struct member name
1009 /// @param type the struct member type
1010 /// @returns the struct member pointer
1011 ast::StructMember* Member(const Source& source,
1012 const std::string& name,
1013 type::Type* type) {
1014 return create<ast::StructMember>(source, Symbols().Register(name), type,
1015 ast::StructMemberDecorationList{});
1016 }
1017
Ben Clayton42d1e092021-02-02 14:29:15 +00001018 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001019 /// @param name the struct member name
1020 /// @param type the struct member type
1021 /// @returns the struct member pointer
1022 ast::StructMember* Member(const std::string& name, type::Type* type) {
1023 return create<ast::StructMember>(source_, Symbols().Register(name), type,
1024 ast::StructMemberDecorationList{});
1025 }
1026
Ben Clayton42d1e092021-02-02 14:29:15 +00001027 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001028 /// @param name the struct member name
1029 /// @param type the struct member type
1030 /// @param decorations the struct member decorations
1031 /// @returns the struct member pointer
1032 ast::StructMember* Member(const std::string& name,
1033 type::Type* type,
1034 ast::StructMemberDecorationList decorations) {
1035 return create<ast::StructMember>(source_, Symbols().Register(name), type,
1036 decorations);
1037 }
1038
Ben Claytonbab31972021-02-08 21:16:21 +00001039 /// Creates a ast::StructMember with the given byte offset
1040 /// @param offset the offset to use in the StructMemberOffsetDecoration
1041 /// @param name the struct member name
1042 /// @param type the struct member type
1043 /// @returns the struct member pointer
1044 ast::StructMember* Member(uint32_t offset,
1045 const std::string& name,
1046 type::Type* type) {
1047 return create<ast::StructMember>(
1048 source_, Symbols().Register(name), type,
1049 ast::StructMemberDecorationList{
1050 create<ast::StructMemberOffsetDecoration>(offset),
1051 });
1052 }
1053
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001054 /// Sets the current builder source to `src`
1055 /// @param src the Source used for future create() calls
1056 void SetSource(const Source& src) {
1057 AssertNotMoved();
1058 source_ = src;
1059 }
1060
1061 /// Sets the current builder source to `loc`
1062 /// @param loc the Source used for future create() calls
1063 void SetSource(const Source::Location& loc) {
1064 AssertNotMoved();
1065 source_ = Source(loc);
1066 }
1067
Ben Clayton33352542021-01-29 16:43:41 +00001068 /// Helper for returning the resolved semantic type of the expression `expr`.
1069 /// @note As the TypeDeterminator is run when the Program is built, this will
1070 /// only be useful for the TypeDeterminer itself and tests that use their own
1071 /// TypeDeterminer.
1072 /// @param expr the AST expression
1073 /// @return the resolved semantic type for the expression, or nullptr if the
1074 /// expression has no resolved type.
1075 type::Type* TypeOf(ast::Expression* expr) const;
1076
Ben Clayton401b96b2021-02-03 17:19:59 +00001077 /// Wraps the ast::Expression in a statement. This is used by tests that
1078 /// construct a partial AST and require the TypeDeterminer to reach these
1079 /// nodes.
1080 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1081 /// @return the ast::Statement that wraps the ast::Expression
1082 ast::Statement* WrapInStatement(ast::Expression* expr);
1083 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
1084 /// tests that construct a partial AST and require the TypeDeterminer to reach
1085 /// these nodes.
1086 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1087 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1088 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1089 /// Returns the statement argument. Used as a passthrough-overload by
1090 /// WrapInFunction().
1091 /// @param stmt the ast::Statement
1092 /// @return `stmt`
1093 ast::Statement* WrapInStatement(ast::Statement* stmt);
1094 /// Wraps the list of arguments in a simple function so that each is reachable
1095 /// by the TypeDeterminer.
1096 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
1097 template <typename... ARGS>
1098 void WrapInFunction(ARGS&&... args) {
1099 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
1100 WrapInFunction(stmts);
1101 }
1102 /// @param stmts a list of ast::Statement that will be wrapped by a function,
1103 /// so that each statement is reachable by the TypeDeterminer.
1104 void WrapInFunction(ast::StatementList stmts);
1105
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001106 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001107 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001108
1109 protected:
1110 /// Asserts that the builder has not been moved.
1111 void AssertNotMoved() const;
1112
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001113 private:
1114 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001115 ASTNodeAllocator ast_nodes_;
1116 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001117 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001118 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001119 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001120 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001121
1122 /// The source to use when creating AST nodes without providing a Source as
1123 /// the first argument.
1124 Source source_;
1125
Ben Claytondd69ac32021-01-27 19:23:55 +00001126 /// Set by SetResolveOnBuild(). If set, the TypeDeterminer will be run on the
1127 /// program when built.
1128 bool resolve_on_build_ = true;
1129
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001130 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1131 bool moved_ = false;
1132};
1133
1134//! @cond Doxygen_Suppress
1135// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1136template <>
1137struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1138 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1139 return t->i32();
1140 }
1141};
1142template <>
1143struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1144 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1145 return t->u32();
1146 }
1147};
1148template <>
1149struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1150 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1151 return t->f32();
1152 }
1153};
1154template <>
1155struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1156 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1157 return t->bool_();
1158 }
1159};
1160template <>
1161struct ProgramBuilder::TypesBuilder::CToAST<void> {
1162 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1163 return t->void_();
1164 }
1165};
1166//! @endcond
1167
1168} // namespace tint
1169
1170#endif // SRC_PROGRAM_BUILDER_H_