blob: 12b9d41af7abadc8229439361d772c194b6804a9 [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
21#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000022#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/binary_expression.h"
24#include "src/ast/bool_literal.h"
25#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000026#include "src/ast/case_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/float_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000028#include "src/ast/if_statement.h"
29#include "src/ast/loop_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000030#include "src/ast/member_accessor_expression.h"
31#include "src/ast/module.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000032#include "src/ast/return_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/scalar_constructor_expression.h"
34#include "src/ast/sint_literal.h"
Ben Claytonbab31972021-02-08 21:16:21 +000035#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000036#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000037#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000038#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000039#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000040#include "src/ast/type_constructor_expression.h"
41#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000042#include "src/ast/variable_decl_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000043#include "src/program.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#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"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000052#include "src/type/u32_type.h"
53#include "src/type/vector_type.h"
54#include "src/type/void_type.h"
55
56namespace tint {
57
Ben Clayton401b96b2021-02-03 17:19:59 +000058// Forward declarations
59namespace ast {
60class VariableDeclStatement;
61} // namespace ast
62
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063class CloneContext;
64
65/// ProgramBuilder is a mutable builder for a Program.
66/// To construct a Program, populate the builder and then `std::move` it to a
67/// Program.
68class ProgramBuilder {
69 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000070 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
71 using ASTNodeAllocator = BlockAllocator<ast::Node>;
72
73 /// SemNodeAllocator is an alias to BlockAllocator<semantic::Node>
74 using SemNodeAllocator = BlockAllocator<semantic::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000075
76 /// `i32` is a type alias to `int`.
77 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
78 /// WGSL syntax.
79 /// Note: this is intentionally not aliased to uint32_t as we want integer
80 /// literals passed to the builder to match WGSL's integer literal types.
81 using i32 = decltype(1);
82 /// `u32` is a type alias to `unsigned int`.
83 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
84 /// WGSL syntax.
85 /// Note: this is intentionally not aliased to uint32_t as we want integer
86 /// literals passed to the builder to match WGSL's integer literal types.
87 using u32 = decltype(1u);
88 /// `f32` is a type alias to `float`
89 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
90 /// WGSL syntax.
91 using f32 = float;
92
93 /// Constructor
94 ProgramBuilder();
95
96 /// Move constructor
97 /// @param rhs the builder to move
98 ProgramBuilder(ProgramBuilder&& rhs);
99
100 /// Destructor
101 virtual ~ProgramBuilder();
102
103 /// Move assignment operator
104 /// @param rhs the builder to move
105 /// @return this builder
106 ProgramBuilder& operator=(ProgramBuilder&& rhs);
107
Ben Claytone43c8302021-01-29 11:59:32 +0000108 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
109 /// making a deep clone of the Program contents.
110 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
111 /// existing immutable program.
112 /// As the returned ProgramBuilder wraps `program`, `program` must not be
113 /// destructed or assigned while using the returned ProgramBuilder.
114 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
115 /// function. See crbug.com/tint/460.
116 /// @param program the immutable Program to wrap
117 /// @return the ProgramBuilder that wraps `program`
118 static ProgramBuilder Wrap(const Program* program);
119
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000120 /// @returns a reference to the program's types
121 type::Manager& Types() {
122 AssertNotMoved();
123 return types_;
124 }
125
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000126 /// @returns a reference to the program's types
127 const type::Manager& Types() const {
128 AssertNotMoved();
129 return types_;
130 }
131
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000132 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000133 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000134 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000135 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000136 }
137
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000138 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000139 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000140 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000141 return ast_nodes_;
142 }
143
144 /// @returns a reference to the program's semantic nodes storage
145 SemNodeAllocator& SemNodes() {
146 AssertNotMoved();
147 return sem_nodes_;
148 }
149
150 /// @returns a reference to the program's semantic nodes storage
151 const SemNodeAllocator& SemNodes() const {
152 AssertNotMoved();
153 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000154 }
155
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000156 /// @returns a reference to the program's AST root Module
157 ast::Module& AST() {
158 AssertNotMoved();
159 return *ast_;
160 }
161
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000162 /// @returns a reference to the program's AST root Module
163 const ast::Module& AST() const {
164 AssertNotMoved();
165 return *ast_;
166 }
167
168 /// @returns a reference to the program's semantic info
169 semantic::Info& Sem() {
170 AssertNotMoved();
171 return sem_;
172 }
173
174 /// @returns a reference to the program's semantic info
175 const semantic::Info& Sem() const {
176 AssertNotMoved();
177 return sem_;
178 }
179
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000180 /// @returns a reference to the program's SymbolTable
181 SymbolTable& Symbols() {
182 AssertNotMoved();
183 return symbols_;
184 }
185
Ben Clayton708dc2d2021-01-29 11:22:40 +0000186 /// @returns a reference to the program's SymbolTable
187 const SymbolTable& Symbols() const {
188 AssertNotMoved();
189 return symbols_;
190 }
191
Ben Clayton844217f2021-01-27 18:49:05 +0000192 /// @returns a reference to the program's diagnostics
193 diag::List& Diagnostics() {
194 AssertNotMoved();
195 return diagnostics_;
196 }
197
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000198 /// @returns a reference to the program's diagnostics
199 const diag::List& Diagnostics() const {
200 AssertNotMoved();
201 return diagnostics_;
202 }
203
Ben Clayton5f0ea112021-03-09 10:54:37 +0000204 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000205 /// @param enable the new flag value (defaults to true)
206 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
207
Ben Clayton5f0ea112021-03-09 10:54:37 +0000208 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000209 /// built.
210 bool ResolveOnBuild() const { return resolve_on_build_; }
211
Ben Clayton844217f2021-01-27 18:49:05 +0000212 /// @returns true if the program has no error diagnostics and is not missing
213 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000214 bool IsValid() const;
215
Ben Clayton708dc2d2021-01-29 11:22:40 +0000216 /// Writes a representation of the node to the output stream
217 /// @note unlike str(), to_str() does not automatically demangle the string.
218 /// @param node the AST node
219 /// @param out the stream to write to
220 /// @param indent number of spaces to indent the node when writing
221 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
222 node->to_str(Sem(), out, indent);
223 }
224
225 /// Returns a demangled, string representation of `node`.
226 /// @param node the AST node
227 /// @returns a string representation of the node
228 std::string str(const ast::Node* node) const;
229
Ben Clayton7fdfff12021-01-29 15:17:30 +0000230 /// Creates a new ast::Node owned by the ProgramBuilder. When the
231 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000232 /// @param source the Source of the node
233 /// @param args the arguments to pass to the type constructor
234 /// @returns the node pointer
235 template <typename T, typename... ARGS>
236 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
237 ARGS&&... args) {
238 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000239 return ast_nodes_.Create<T>(source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000240 }
241
Ben Clayton7fdfff12021-01-29 15:17:30 +0000242 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
243 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000244 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000245 /// When the ProgramBuilder is destructed, the ast::Node will also be
246 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000247 /// @returns the node pointer
248 template <typename T>
249 traits::EnableIfIsType<T, ast::Node>* create() {
250 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000251 return ast_nodes_.Create<T>(source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000252 }
253
Ben Clayton7fdfff12021-01-29 15:17:30 +0000254 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
255 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000256 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000257 /// When the ProgramBuilder is destructed, the ast::Node will also be
258 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000259 /// @param arg0 the first arguments to pass to the type constructor
260 /// @param args the remaining arguments to pass to the type constructor
261 /// @returns the node pointer
262 template <typename T, typename ARG0, typename... ARGS>
263 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
264 traits::IsTypeOrDerived<T, ast::Node>::value &&
265 !traits::IsTypeOrDerived<ARG0, Source>::value,
266 T>*
267 create(ARG0&& arg0, ARGS&&... args) {
268 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000269 return ast_nodes_.Create<T>(source_, std::forward<ARG0>(arg0),
270 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000271 }
272
Ben Clayton7fdfff12021-01-29 15:17:30 +0000273 /// Creates a new semantic::Node owned by the ProgramBuilder.
274 /// When the ProgramBuilder is destructed, the semantic::Node will also be
275 /// destructed.
276 /// @param args the arguments to pass to the type constructor
277 /// @returns the node pointer
278 template <typename T, typename... ARGS>
279 traits::EnableIfIsType<T, semantic::Node>* create(ARGS&&... args) {
280 AssertNotMoved();
281 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
282 }
283
284 /// Creates a new type::Type owned by the ProgramBuilder.
285 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
286 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000287 /// Types are unique (de-aliased), and so calling create() for the same `T`
288 /// and arguments will return the same pointer.
289 /// @warning Use this method to acquire a type only if all of its type
290 /// information is provided in the constructor arguments `args`.<br>
291 /// If the type requires additional configuration after construction that
292 /// affect its fundamental type, build the type with `std::make_unique`, make
293 /// any necessary alterations and then call unique_type() instead.
294 /// @param args the arguments to pass to the type constructor
295 /// @returns the de-aliased type pointer
296 template <typename T, typename... ARGS>
297 traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) {
298 static_assert(std::is_base_of<type::Type, T>::value,
299 "T does not derive from type::Type");
300 AssertNotMoved();
301 return types_.Get<T>(std::forward<ARGS>(args)...);
302 }
303
304 /// Marks this builder as moved, preventing any further use of the builder.
305 void MarkAsMoved();
306
307 //////////////////////////////////////////////////////////////////////////////
308 // TypesBuilder
309 //////////////////////////////////////////////////////////////////////////////
310
311 /// TypesBuilder holds basic `tint` types and methods for constructing
312 /// complex types.
313 class TypesBuilder {
314 public:
315 /// Constructor
316 /// @param builder the program builder
317 explicit TypesBuilder(ProgramBuilder* builder);
318
319 /// @return the tint AST type for the C type `T`.
320 template <typename T>
321 type::Type* Of() const {
322 return CToAST<T>::get(this);
323 }
324
325 /// @returns a boolean type
326 type::Bool* bool_() const { return builder->create<type::Bool>(); }
327
328 /// @returns a f32 type
329 type::F32* f32() const { return builder->create<type::F32>(); }
330
331 /// @returns a i32 type
332 type::I32* i32() const { return builder->create<type::I32>(); }
333
334 /// @returns a u32 type
335 type::U32* u32() const { return builder->create<type::U32>(); }
336
337 /// @returns a void type
338 type::Void* void_() const { return builder->create<type::Void>(); }
339
340 /// @return the tint AST type for a 2-element vector of the C type `T`.
341 template <typename T>
342 type::Vector* vec2() const {
343 return builder->create<type::Vector>(Of<T>(), 2);
344 }
345
346 /// @return the tint AST type for a 3-element vector of the C type `T`.
347 template <typename T>
348 type::Vector* vec3() const {
349 return builder->create<type::Vector>(Of<T>(), 3);
350 }
351
352 /// @return the tint AST type for a 4-element vector of the C type `T`.
353 template <typename T>
354 type::Type* vec4() const {
355 return builder->create<type::Vector>(Of<T>(), 4);
356 }
357
358 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
359 template <typename T>
360 type::Matrix* mat2x2() const {
361 return builder->create<type::Matrix>(Of<T>(), 2, 2);
362 }
363
364 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
365 template <typename T>
366 type::Matrix* mat2x3() const {
367 return builder->create<type::Matrix>(Of<T>(), 3, 2);
368 }
369
370 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
371 template <typename T>
372 type::Matrix* mat2x4() const {
373 return builder->create<type::Matrix>(Of<T>(), 4, 2);
374 }
375
376 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
377 template <typename T>
378 type::Matrix* mat3x2() const {
379 return builder->create<type::Matrix>(Of<T>(), 2, 3);
380 }
381
382 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
383 template <typename T>
384 type::Matrix* mat3x3() const {
385 return builder->create<type::Matrix>(Of<T>(), 3, 3);
386 }
387
388 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
389 template <typename T>
390 type::Matrix* mat3x4() const {
391 return builder->create<type::Matrix>(Of<T>(), 4, 3);
392 }
393
394 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
395 template <typename T>
396 type::Matrix* mat4x2() const {
397 return builder->create<type::Matrix>(Of<T>(), 2, 4);
398 }
399
400 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
401 template <typename T>
402 type::Matrix* mat4x3() const {
403 return builder->create<type::Matrix>(Of<T>(), 3, 4);
404 }
405
406 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
407 template <typename T>
408 type::Matrix* mat4x4() const {
409 return builder->create<type::Matrix>(Of<T>(), 4, 4);
410 }
411
412 /// @param subtype the array element type
413 /// @param n the array size. 0 represents a runtime-array.
414 /// @return the tint AST type for a array of size `n` of type `T`
415 type::Array* array(type::Type* subtype, uint32_t n) const {
James Price95d40772021-03-11 17:39:32 +0000416 return builder->create<type::Array>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000417 }
418
Ben Claytonbab31972021-02-08 21:16:21 +0000419 /// @param subtype the array element type
420 /// @param n the array size. 0 represents a runtime-array.
421 /// @param stride the array stride.
422 /// @return the tint AST type for a array of size `n` of type `T`
423 type::Array* array(type::Type* subtype, uint32_t n, uint32_t stride) const {
424 return builder->create<type::Array>(
425 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000426 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000427 builder->create<ast::StrideDecoration>(stride),
428 });
429 }
430
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000431 /// @return the tint AST type for an array of size `N` of type `T`
432 template <typename T, int N = 0>
433 type::Array* array() const {
434 return array(Of<T>(), N);
435 }
436
Ben Claytonbab31972021-02-08 21:16:21 +0000437 /// @param stride the array stride
438 /// @return the tint AST type for an array of size `N` of type `T`
439 template <typename T, int N = 0>
440 type::Array* array(uint32_t stride) const {
441 return array(Of<T>(), N, stride);
442 }
Ben Clayton42d1e092021-02-02 14:29:15 +0000443 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000444 /// @param name the alias name
445 /// @param type the alias type
446 /// @returns the alias pointer
447 type::Alias* alias(const std::string& name, type::Type* type) const {
448 return builder->create<type::Alias>(builder->Symbols().Register(name),
449 type);
450 }
451
452 /// @return the tint AST pointer to type `T` with the given
453 /// ast::StorageClass.
454 /// @param storage_class the storage class of the pointer
455 template <typename T>
456 type::Pointer* pointer(ast::StorageClass storage_class) const {
457 return builder->create<type::Pointer>(Of<T>(), storage_class);
458 }
459
460 /// @param name the struct name
461 /// @param impl the struct implementation
462 /// @returns a struct pointer
463 type::Struct* struct_(const std::string& name, ast::Struct* impl) const {
464 return builder->create<type::Struct>(builder->Symbols().Register(name),
465 impl);
466 }
467
468 private:
469 /// CToAST<T> is specialized for various `T` types and each specialization
470 /// contains a single static `get()` method for obtaining the corresponding
471 /// AST type for the C type `T`.
472 /// `get()` has the signature:
473 /// `static type::Type* get(Types* t)`
474 template <typename T>
475 struct CToAST {};
476
Ben Claytonc7ca7662021-02-17 16:23:52 +0000477 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000478 };
479
480 //////////////////////////////////////////////////////////////////////////////
481 // AST helper methods
482 //////////////////////////////////////////////////////////////////////////////
483
484 /// @param expr the expression
485 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000486 template <typename T>
487 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
488 return expr;
489 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000490
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,
James Price95d40772021-03-11 17:39:32 +0000762 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000763 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,
James Price95d40772021-03-11 17:39:32 +0000779 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000780 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,
James Price95d40772021-03-11 17:39:32 +0000794 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000795 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,
James Price95d40772021-03-11 17:39:32 +0000811 ast::DecorationList decorations = {}) {
Ben Clayton46d78d72021-02-10 21:34:26 +0000812 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,
James Price95d40772021-03-11 17:39:32 +0000824 ast::DecorationList 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,
James Price95d40772021-03-11 17:39:32 +0000840 ast::DecorationList 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,
James Price95d40772021-03-11 17:39:32 +0000855 ast::DecorationList 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,
James Price95d40772021-03-11 17:39:32 +0000871 ast::DecorationList 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>
Ben Clayton6d612ad2021-02-24 14:15:02 +0000951 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000952 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 Claytond614dd52021-03-15 10:43:11 +0000963 /// Creates a ast::StructMemberSizeDecoration
964 /// @param source the source information
965 /// @param val the size value
966 /// @returns the size decoration pointer
967 ast::StructMemberSizeDecoration* MemberSize(Source source, uint32_t val) {
968 return create<ast::StructMemberSizeDecoration>(source, val);
969 }
970
971 /// Creates a ast::StructMemberSizeDecoration
972 /// @param val the size value
973 /// @returns the size decoration pointer
974 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
975 return create<ast::StructMemberSizeDecoration>(source_, val);
976 }
977
978 /// Creates a ast::StructMemberAlignDecoration
979 /// @param source the source information
980 /// @param val the align value
981 /// @returns the align decoration pointer
982 ast::StructMemberAlignDecoration* MemberAlign(Source source, uint32_t val) {
983 return create<ast::StructMemberAlignDecoration>(source, val);
984 }
985
986 /// Creates a ast::StructMemberAlignDecoration
987 /// @param val the align value
988 /// @returns the align decoration pointer
989 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
990 return create<ast::StructMemberAlignDecoration>(source_, val);
991 }
992
Ben Clayton42d1e092021-02-02 14:29:15 +0000993 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000994 /// @param source the source information
995 /// @param name the function name
996 /// @param params the function parameters
997 /// @param type the function return type
998 /// @param body the function body
999 /// @param decorations the function decorations
James Pricefeecbe02021-03-15 17:01:34 +00001000 /// @param return_type_decorations the function return type decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001001 /// @returns the function pointer
1002 ast::Function* Func(Source source,
1003 std::string name,
1004 ast::VariableList params,
1005 type::Type* type,
1006 ast::StatementList body,
James Pricefeecbe02021-03-15 17:01:34 +00001007 ast::DecorationList decorations,
1008 ast::DecorationList return_type_decorations = {}) {
1009 auto* func = create<ast::Function>(source, Symbols().Register(name), params,
1010 type, create<ast::BlockStatement>(body),
1011 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001012 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001013 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001014 }
1015
Ben Clayton42d1e092021-02-02 14:29:15 +00001016 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001017 /// @param name the function name
1018 /// @param params the function parameters
1019 /// @param type the function return type
1020 /// @param body the function body
1021 /// @param decorations the function decorations
James Pricefeecbe02021-03-15 17:01:34 +00001022 /// @param return_type_decorations the function return type decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001023 /// @returns the function pointer
1024 ast::Function* Func(std::string name,
1025 ast::VariableList params,
1026 type::Type* type,
1027 ast::StatementList body,
James Pricefeecbe02021-03-15 17:01:34 +00001028 ast::DecorationList decorations,
1029 ast::DecorationList return_type_decorations = {}) {
1030 auto* func = create<ast::Function>(Symbols().Register(name), params, type,
1031 create<ast::BlockStatement>(body),
1032 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001033 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001034 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001035 }
1036
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001037 /// Creates an ast::ReturnStatement with the input args
1038 /// @param args arguments to construct a return statement with
1039 /// @returns the return statement pointer
1040 template <typename... Args>
1041 ast::ReturnStatement* Return(Args&&... args) {
1042 return create<ast::ReturnStatement>(std::forward<Args>(args)...);
1043 }
1044
Ben Claytond614dd52021-03-15 10:43:11 +00001045 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1046 /// the AST().ConstructedTypes().
1047 /// @param source the source information
1048 /// @param name the struct name
1049 /// @param members the struct members
1050 /// @param decorations the optional struct decorations
1051 /// @returns the struct type
1052 type::Struct* Structure(const Source& source,
1053 const std::string& name,
1054 ast::StructMemberList members,
1055 ast::DecorationList decorations = {}) {
1056 auto* impl =
1057 create<ast::Struct>(source, std::move(members), std::move(decorations));
1058 auto* type = ty.struct_(name, impl);
1059 AST().AddConstructedType(type);
1060 return type;
1061 }
1062
1063 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1064 /// the AST().ConstructedTypes().
1065 /// @param name the struct name
1066 /// @param members the struct members
1067 /// @param decorations the optional struct decorations
1068 /// @returns the struct type
1069 type::Struct* Structure(const std::string& name,
1070 ast::StructMemberList members,
1071 ast::DecorationList decorations = {}) {
1072 auto* impl =
1073 create<ast::Struct>(std::move(members), std::move(decorations));
1074 auto* type = ty.struct_(name, impl);
1075 AST().AddConstructedType(type);
1076 return type;
1077 }
1078
Ben Clayton42d1e092021-02-02 14:29:15 +00001079 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001080 /// @param source the source information
1081 /// @param name the struct member name
1082 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001083 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001084 /// @returns the struct member pointer
1085 ast::StructMember* Member(const Source& source,
1086 const std::string& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001087 type::Type* type,
1088 ast::DecorationList decorations = {}) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001089 return create<ast::StructMember>(source, Symbols().Register(name), type,
Ben Claytond614dd52021-03-15 10:43:11 +00001090 std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001091 }
1092
Ben Clayton42d1e092021-02-02 14:29:15 +00001093 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001094 /// @param name the struct member name
1095 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001096 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001097 /// @returns the struct member pointer
1098 ast::StructMember* Member(const std::string& name,
1099 type::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001100 ast::DecorationList decorations = {}) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001101 return create<ast::StructMember>(source_, Symbols().Register(name), type,
Ben Claytond614dd52021-03-15 10:43:11 +00001102 std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001103 }
1104
Ben Claytonbab31972021-02-08 21:16:21 +00001105 /// Creates a ast::StructMember with the given byte offset
1106 /// @param offset the offset to use in the StructMemberOffsetDecoration
1107 /// @param name the struct member name
1108 /// @param type the struct member type
1109 /// @returns the struct member pointer
1110 ast::StructMember* Member(uint32_t offset,
1111 const std::string& name,
1112 type::Type* type) {
1113 return create<ast::StructMember>(
1114 source_, Symbols().Register(name), type,
James Price95d40772021-03-11 17:39:32 +00001115 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001116 create<ast::StructMemberOffsetDecoration>(offset),
1117 });
1118 }
1119
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001120 /// Creates a ast::BlockStatement with input statements
1121 /// @param statements statements of block
1122 /// @returns the block statement pointer
1123 template <typename... Statements>
1124 ast::BlockStatement* Block(Statements&&... statements) {
1125 return create<ast::BlockStatement>(
1126 ast::StatementList{std::forward<Statements>(statements)...});
1127 }
1128
1129 /// Creates a ast::ElseStatement with input condition and body
1130 /// @param condition the else condition expression
1131 /// @param body the else body
1132 /// @returns the else statement pointer
1133 ast::ElseStatement* Else(ast::Expression* condition,
1134 ast::BlockStatement* body) {
1135 return create<ast::ElseStatement>(condition, body);
1136 }
1137
1138 /// Creates a ast::IfStatement with input condition, body, and optional
1139 /// variadic else statements
1140 /// @param condition the if statement condition expression
1141 /// @param body the if statement body
1142 /// @param elseStatements optional variadic else statements
1143 /// @returns the if statement pointer
1144 template <typename... ElseStatements>
1145 ast::IfStatement* If(ast::Expression* condition,
1146 ast::BlockStatement* body,
1147 ElseStatements&&... elseStatements) {
1148 return create<ast::IfStatement>(
1149 condition, body,
1150 ast::ElseStatementList{
1151 std::forward<ElseStatements>(elseStatements)...});
1152 }
1153
1154 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001155 /// @param lhs the left hand side expression initializer
1156 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001157 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001158 template <typename LhsExpressionInit, typename RhsExpressionInit>
1159 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1160 RhsExpressionInit&& rhs) {
1161 return create<ast::AssignmentStatement>(
1162 Expr(std::forward<LhsExpressionInit>(lhs)),
1163 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001164 }
1165
1166 /// Creates a ast::LoopStatement with input body and optional continuing
1167 /// @param body the loop body
1168 /// @param continuing the optional continuing block
1169 /// @returns the loop statement pointer
1170 ast::LoopStatement* Loop(ast::BlockStatement* body,
1171 ast::BlockStatement* continuing = nullptr) {
1172 return create<ast::LoopStatement>(body, continuing);
1173 }
1174
1175 /// Creates a ast::VariableDeclStatement for the input variable
1176 /// @param var the variable to wrap in a decl statement
1177 /// @returns the variable decl statement pointer
1178 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1179 return create<ast::VariableDeclStatement>(var);
1180 }
1181
Antonio Maioranocea744d2021-03-25 12:55:27 +00001182 /// Creates a ast::SwitchStatement with input expression and cases
1183 /// @param condition the condition expression initializer
1184 /// @param cases case statements
1185 /// @returns the switch statement pointer
1186 template <typename ExpressionInit, typename... Cases>
1187 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1188 return create<ast::SwitchStatement>(
1189 Expr(std::forward<ExpressionInit>(condition)),
1190 ast::CaseStatementList{std::forward<Cases>(cases)...});
1191 }
1192
1193 /// Creates a ast::CaseStatement with input list of selectors, and body
1194 /// @param selectors list of selectors
1195 /// @param body the case body
1196 /// @returns the case statement pointer
1197 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1198 ast::BlockStatement* body = nullptr) {
1199 return create<ast::CaseStatement>(std::move(selectors),
1200 body ? body : Block());
1201 }
1202
1203 /// Convenient overload that takes a single selector
1204 /// @param selector a single case selector
1205 /// @param body the case body
1206 /// @returns the case statement pointer
1207 ast::CaseStatement* Case(ast::IntLiteral* selector,
1208 ast::BlockStatement* body = nullptr) {
1209 return Case(ast::CaseSelectorList{selector}, body);
1210 }
1211
1212 /// Convenience function that creates a 'default' ast::CaseStatement
1213 /// @param body the case body
1214 /// @returns the case statement pointer
1215 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1216 return Case(ast::CaseSelectorList{}, body);
1217 }
1218
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001219 /// Sets the current builder source to `src`
1220 /// @param src the Source used for future create() calls
1221 void SetSource(const Source& src) {
1222 AssertNotMoved();
1223 source_ = src;
1224 }
1225
1226 /// Sets the current builder source to `loc`
1227 /// @param loc the Source used for future create() calls
1228 void SetSource(const Source::Location& loc) {
1229 AssertNotMoved();
1230 source_ = Source(loc);
1231 }
1232
Ben Clayton33352542021-01-29 16:43:41 +00001233 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001234 /// @note As the Resolver is run when the Program is built, this will only be
1235 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001236 /// @param expr the AST expression
1237 /// @return the resolved semantic type for the expression, or nullptr if the
1238 /// expression has no resolved type.
1239 type::Type* TypeOf(ast::Expression* expr) const;
1240
Ben Clayton401b96b2021-02-03 17:19:59 +00001241 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001242 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001243 /// nodes.
1244 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1245 /// @return the ast::Statement that wraps the ast::Expression
1246 ast::Statement* WrapInStatement(ast::Expression* expr);
1247 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001248 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001249 /// these nodes.
1250 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1251 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1252 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1253 /// Returns the statement argument. Used as a passthrough-overload by
1254 /// WrapInFunction().
1255 /// @param stmt the ast::Statement
1256 /// @return `stmt`
1257 ast::Statement* WrapInStatement(ast::Statement* stmt);
1258 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001259 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001260 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001261 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001262 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001263 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001264 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001265 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001266 }
1267 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001268 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001269 /// @returns the function
1270 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001271
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001272 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001273 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001274
1275 protected:
1276 /// Asserts that the builder has not been moved.
1277 void AssertNotMoved() const;
1278
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001279 private:
1280 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001281 ASTNodeAllocator ast_nodes_;
1282 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001283 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001284 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001285 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001286 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001287
1288 /// The source to use when creating AST nodes without providing a Source as
1289 /// the first argument.
1290 Source source_;
1291
Ben Clayton5f0ea112021-03-09 10:54:37 +00001292 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001293 /// program when built.
1294 bool resolve_on_build_ = true;
1295
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001296 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1297 bool moved_ = false;
1298};
1299
1300//! @cond Doxygen_Suppress
1301// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1302template <>
1303struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1304 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1305 return t->i32();
1306 }
1307};
1308template <>
1309struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1310 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1311 return t->u32();
1312 }
1313};
1314template <>
1315struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1316 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1317 return t->f32();
1318 }
1319};
1320template <>
1321struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1322 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1323 return t->bool_();
1324 }
1325};
1326template <>
1327struct ProgramBuilder::TypesBuilder::CToAST<void> {
1328 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1329 return t->void_();
1330 }
1331};
1332//! @endcond
1333
1334} // namespace tint
1335
1336#endif // SRC_PROGRAM_BUILDER_H_