blob: ae197ccac4d9a89711641486390a3dff5aec46d1 [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
21#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000022#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/binary_expression.h"
24#include "src/ast/bool_literal.h"
25#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000026#include "src/ast/case_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/float_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000028#include "src/ast/if_statement.h"
29#include "src/ast/loop_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000030#include "src/ast/member_accessor_expression.h"
31#include "src/ast/module.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000032#include "src/ast/return_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/scalar_constructor_expression.h"
34#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000035#include "src/ast/stage_decoration.h"
Ben Claytonbab31972021-02-08 21:16:21 +000036#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000037#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000039#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000040#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000041#include "src/ast/type_constructor_expression.h"
42#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000043#include "src/ast/variable_decl_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/program.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/type/alias_type.h"
46#include "src/type/array_type.h"
47#include "src/type/bool_type.h"
48#include "src/type/f32_type.h"
49#include "src/type/i32_type.h"
50#include "src/type/matrix_type.h"
51#include "src/type/pointer_type.h"
52#include "src/type/struct_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000053#include "src/type/u32_type.h"
54#include "src/type/vector_type.h"
55#include "src/type/void_type.h"
56
57namespace tint {
58
Ben Clayton401b96b2021-02-03 17:19:59 +000059// Forward declarations
60namespace ast {
61class VariableDeclStatement;
62} // namespace ast
63
Ben Claytona6b9a8e2021-01-26 16:57:10 +000064class CloneContext;
65
66/// ProgramBuilder is a mutable builder for a Program.
67/// To construct a Program, populate the builder and then `std::move` it to a
68/// Program.
69class ProgramBuilder {
70 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000071 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
72 using ASTNodeAllocator = BlockAllocator<ast::Node>;
73
74 /// SemNodeAllocator is an alias to BlockAllocator<semantic::Node>
75 using SemNodeAllocator = BlockAllocator<semantic::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000076
77 /// `i32` is a type alias to `int`.
78 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
79 /// WGSL syntax.
80 /// Note: this is intentionally not aliased to uint32_t as we want integer
81 /// literals passed to the builder to match WGSL's integer literal types.
82 using i32 = decltype(1);
83 /// `u32` is a type alias to `unsigned int`.
84 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
85 /// WGSL syntax.
86 /// Note: this is intentionally not aliased to uint32_t as we want integer
87 /// literals passed to the builder to match WGSL's integer literal types.
88 using u32 = decltype(1u);
89 /// `f32` is a type alias to `float`
90 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
91 /// WGSL syntax.
92 using f32 = float;
93
94 /// Constructor
95 ProgramBuilder();
96
97 /// Move constructor
98 /// @param rhs the builder to move
99 ProgramBuilder(ProgramBuilder&& rhs);
100
101 /// Destructor
102 virtual ~ProgramBuilder();
103
104 /// Move assignment operator
105 /// @param rhs the builder to move
106 /// @return this builder
107 ProgramBuilder& operator=(ProgramBuilder&& rhs);
108
Ben Claytone43c8302021-01-29 11:59:32 +0000109 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
110 /// making a deep clone of the Program contents.
111 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
112 /// existing immutable program.
113 /// As the returned ProgramBuilder wraps `program`, `program` must not be
114 /// destructed or assigned while using the returned ProgramBuilder.
115 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
116 /// function. See crbug.com/tint/460.
117 /// @param program the immutable Program to wrap
118 /// @return the ProgramBuilder that wraps `program`
119 static ProgramBuilder Wrap(const Program* program);
120
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000121 /// @returns a reference to the program's types
122 type::Manager& Types() {
123 AssertNotMoved();
124 return types_;
125 }
126
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000127 /// @returns a reference to the program's types
128 const type::Manager& Types() const {
129 AssertNotMoved();
130 return types_;
131 }
132
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000133 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000134 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000135 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000136 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000137 }
138
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000139 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000140 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000141 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000142 return ast_nodes_;
143 }
144
145 /// @returns a reference to the program's semantic nodes storage
146 SemNodeAllocator& SemNodes() {
147 AssertNotMoved();
148 return sem_nodes_;
149 }
150
151 /// @returns a reference to the program's semantic nodes storage
152 const SemNodeAllocator& SemNodes() const {
153 AssertNotMoved();
154 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000155 }
156
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000157 /// @returns a reference to the program's AST root Module
158 ast::Module& AST() {
159 AssertNotMoved();
160 return *ast_;
161 }
162
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000163 /// @returns a reference to the program's AST root Module
164 const ast::Module& AST() const {
165 AssertNotMoved();
166 return *ast_;
167 }
168
169 /// @returns a reference to the program's semantic info
170 semantic::Info& Sem() {
171 AssertNotMoved();
172 return sem_;
173 }
174
175 /// @returns a reference to the program's semantic info
176 const semantic::Info& Sem() const {
177 AssertNotMoved();
178 return sem_;
179 }
180
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000181 /// @returns a reference to the program's SymbolTable
182 SymbolTable& Symbols() {
183 AssertNotMoved();
184 return symbols_;
185 }
186
Ben Clayton708dc2d2021-01-29 11:22:40 +0000187 /// @returns a reference to the program's SymbolTable
188 const SymbolTable& Symbols() const {
189 AssertNotMoved();
190 return symbols_;
191 }
192
Ben Clayton844217f2021-01-27 18:49:05 +0000193 /// @returns a reference to the program's diagnostics
194 diag::List& Diagnostics() {
195 AssertNotMoved();
196 return diagnostics_;
197 }
198
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000199 /// @returns a reference to the program's diagnostics
200 const diag::List& Diagnostics() const {
201 AssertNotMoved();
202 return diagnostics_;
203 }
204
Ben Clayton5f0ea112021-03-09 10:54:37 +0000205 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000206 /// @param enable the new flag value (defaults to true)
207 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
208
Ben Clayton5f0ea112021-03-09 10:54:37 +0000209 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000210 /// built.
211 bool ResolveOnBuild() const { return resolve_on_build_; }
212
Ben Clayton844217f2021-01-27 18:49:05 +0000213 /// @returns true if the program has no error diagnostics and is not missing
214 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000215 bool IsValid() const;
216
Ben Clayton708dc2d2021-01-29 11:22:40 +0000217 /// Writes a representation of the node to the output stream
218 /// @note unlike str(), to_str() does not automatically demangle the string.
219 /// @param node the AST node
220 /// @param out the stream to write to
221 /// @param indent number of spaces to indent the node when writing
222 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
223 node->to_str(Sem(), out, indent);
224 }
225
226 /// Returns a demangled, string representation of `node`.
227 /// @param node the AST node
228 /// @returns a string representation of the node
229 std::string str(const ast::Node* node) const;
230
Ben Clayton7fdfff12021-01-29 15:17:30 +0000231 /// Creates a new ast::Node owned by the ProgramBuilder. When the
232 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000233 /// @param source the Source of the node
234 /// @param args the arguments to pass to the type constructor
235 /// @returns the node pointer
236 template <typename T, typename... ARGS>
237 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
238 ARGS&&... args) {
239 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000240 return ast_nodes_.Create<T>(source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000241 }
242
Ben Clayton7fdfff12021-01-29 15:17:30 +0000243 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
244 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000245 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000246 /// When the ProgramBuilder is destructed, the ast::Node will also be
247 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000248 /// @returns the node pointer
249 template <typename T>
250 traits::EnableIfIsType<T, ast::Node>* create() {
251 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000252 return ast_nodes_.Create<T>(source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000253 }
254
Ben Clayton7fdfff12021-01-29 15:17:30 +0000255 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
256 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000257 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000258 /// When the ProgramBuilder is destructed, the ast::Node will also be
259 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000260 /// @param arg0 the first arguments to pass to the type constructor
261 /// @param args the remaining arguments to pass to the type constructor
262 /// @returns the node pointer
263 template <typename T, typename ARG0, typename... ARGS>
264 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
265 traits::IsTypeOrDerived<T, ast::Node>::value &&
266 !traits::IsTypeOrDerived<ARG0, Source>::value,
267 T>*
268 create(ARG0&& arg0, ARGS&&... args) {
269 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000270 return ast_nodes_.Create<T>(source_, std::forward<ARG0>(arg0),
271 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000272 }
273
Ben Clayton7fdfff12021-01-29 15:17:30 +0000274 /// Creates a new semantic::Node owned by the ProgramBuilder.
275 /// When the ProgramBuilder is destructed, the semantic::Node will also be
276 /// destructed.
277 /// @param args the arguments to pass to the type constructor
278 /// @returns the node pointer
279 template <typename T, typename... ARGS>
280 traits::EnableIfIsType<T, semantic::Node>* create(ARGS&&... args) {
281 AssertNotMoved();
282 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
283 }
284
285 /// Creates a new type::Type owned by the ProgramBuilder.
286 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
287 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000288 /// Types are unique (de-aliased), and so calling create() for the same `T`
289 /// and arguments will return the same pointer.
290 /// @warning Use this method to acquire a type only if all of its type
291 /// information is provided in the constructor arguments `args`.<br>
292 /// If the type requires additional configuration after construction that
293 /// affect its fundamental type, build the type with `std::make_unique`, make
294 /// any necessary alterations and then call unique_type() instead.
295 /// @param args the arguments to pass to the type constructor
296 /// @returns the de-aliased type pointer
297 template <typename T, typename... ARGS>
298 traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) {
299 static_assert(std::is_base_of<type::Type, T>::value,
300 "T does not derive from type::Type");
301 AssertNotMoved();
302 return types_.Get<T>(std::forward<ARGS>(args)...);
303 }
304
305 /// Marks this builder as moved, preventing any further use of the builder.
306 void MarkAsMoved();
307
308 //////////////////////////////////////////////////////////////////////////////
309 // TypesBuilder
310 //////////////////////////////////////////////////////////////////////////////
311
312 /// TypesBuilder holds basic `tint` types and methods for constructing
313 /// complex types.
314 class TypesBuilder {
315 public:
316 /// Constructor
317 /// @param builder the program builder
318 explicit TypesBuilder(ProgramBuilder* builder);
319
320 /// @return the tint AST type for the C type `T`.
321 template <typename T>
322 type::Type* Of() const {
323 return CToAST<T>::get(this);
324 }
325
326 /// @returns a boolean type
327 type::Bool* bool_() const { return builder->create<type::Bool>(); }
328
329 /// @returns a f32 type
330 type::F32* f32() const { return builder->create<type::F32>(); }
331
332 /// @returns a i32 type
333 type::I32* i32() const { return builder->create<type::I32>(); }
334
335 /// @returns a u32 type
336 type::U32* u32() const { return builder->create<type::U32>(); }
337
338 /// @returns a void type
339 type::Void* void_() const { return builder->create<type::Void>(); }
340
341 /// @return the tint AST type for a 2-element vector of the C type `T`.
342 template <typename T>
343 type::Vector* vec2() const {
344 return builder->create<type::Vector>(Of<T>(), 2);
345 }
346
347 /// @return the tint AST type for a 3-element vector of the C type `T`.
348 template <typename T>
349 type::Vector* vec3() const {
350 return builder->create<type::Vector>(Of<T>(), 3);
351 }
352
353 /// @return the tint AST type for a 4-element vector of the C type `T`.
354 template <typename T>
355 type::Type* vec4() const {
356 return builder->create<type::Vector>(Of<T>(), 4);
357 }
358
359 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
360 template <typename T>
361 type::Matrix* mat2x2() const {
362 return builder->create<type::Matrix>(Of<T>(), 2, 2);
363 }
364
365 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
366 template <typename T>
367 type::Matrix* mat2x3() const {
368 return builder->create<type::Matrix>(Of<T>(), 3, 2);
369 }
370
371 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
372 template <typename T>
373 type::Matrix* mat2x4() const {
374 return builder->create<type::Matrix>(Of<T>(), 4, 2);
375 }
376
377 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
378 template <typename T>
379 type::Matrix* mat3x2() const {
380 return builder->create<type::Matrix>(Of<T>(), 2, 3);
381 }
382
383 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
384 template <typename T>
385 type::Matrix* mat3x3() const {
386 return builder->create<type::Matrix>(Of<T>(), 3, 3);
387 }
388
389 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
390 template <typename T>
391 type::Matrix* mat3x4() const {
392 return builder->create<type::Matrix>(Of<T>(), 4, 3);
393 }
394
395 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
396 template <typename T>
397 type::Matrix* mat4x2() const {
398 return builder->create<type::Matrix>(Of<T>(), 2, 4);
399 }
400
401 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
402 template <typename T>
403 type::Matrix* mat4x3() const {
404 return builder->create<type::Matrix>(Of<T>(), 3, 4);
405 }
406
407 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
408 template <typename T>
409 type::Matrix* mat4x4() const {
410 return builder->create<type::Matrix>(Of<T>(), 4, 4);
411 }
412
413 /// @param subtype the array element type
414 /// @param n the array size. 0 represents a runtime-array.
415 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000416 type::Array* array(type::Type* subtype, uint32_t n = 0) const {
James Price95d40772021-03-11 17:39:32 +0000417 return builder->create<type::Array>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000418 }
419
Ben Claytonbab31972021-02-08 21:16:21 +0000420 /// @param subtype the array element type
421 /// @param n the array size. 0 represents a runtime-array.
422 /// @param stride the array stride.
423 /// @return the tint AST type for a array of size `n` of type `T`
424 type::Array* array(type::Type* subtype, uint32_t n, uint32_t stride) const {
425 return builder->create<type::Array>(
426 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000427 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000428 builder->create<ast::StrideDecoration>(stride),
429 });
430 }
431
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000432 /// @return the tint AST type for an array of size `N` of type `T`
433 template <typename T, int N = 0>
434 type::Array* array() const {
435 return array(Of<T>(), N);
436 }
437
Ben Claytonbab31972021-02-08 21:16:21 +0000438 /// @param stride the array stride
439 /// @return the tint AST type for an array of size `N` of type `T`
440 template <typename T, int N = 0>
441 type::Array* array(uint32_t stride) const {
442 return array(Of<T>(), N, stride);
443 }
Ben Clayton42d1e092021-02-02 14:29:15 +0000444 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000445 /// @param name the alias name
446 /// @param type the alias type
447 /// @returns the alias pointer
448 type::Alias* alias(const std::string& name, type::Type* type) const {
449 return builder->create<type::Alias>(builder->Symbols().Register(name),
450 type);
451 }
452
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000453 /// @return the tint AST pointer to `type` with the given ast::StorageClass
454 /// @param type the type of the pointer
455 /// @param storage_class the storage class of the pointer
456 type::Pointer* pointer(type::Type* type,
457 ast::StorageClass storage_class) const {
458 return builder->create<type::Pointer>(type, storage_class);
459 }
460
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000461 /// @return the tint AST pointer to type `T` with the given
462 /// ast::StorageClass.
463 /// @param storage_class the storage class of the pointer
464 template <typename T>
465 type::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000466 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000467 }
468
469 /// @param name the struct name
470 /// @param impl the struct implementation
471 /// @returns a struct pointer
472 type::Struct* struct_(const std::string& name, ast::Struct* impl) const {
473 return builder->create<type::Struct>(builder->Symbols().Register(name),
474 impl);
475 }
476
477 private:
478 /// CToAST<T> is specialized for various `T` types and each specialization
479 /// contains a single static `get()` method for obtaining the corresponding
480 /// AST type for the C type `T`.
481 /// `get()` has the signature:
482 /// `static type::Type* get(Types* t)`
483 template <typename T>
484 struct CToAST {};
485
Ben Claytonc7ca7662021-02-17 16:23:52 +0000486 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000487 };
488
489 //////////////////////////////////////////////////////////////////////////////
490 // AST helper methods
491 //////////////////////////////////////////////////////////////////////////////
492
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000493 /// @param name the symbol string
494 /// @return a Symbol with the given name
495 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
496
497 /// @param sym the symbol
498 /// @return `sym`
499 Symbol Sym(Symbol sym) { return sym; }
500
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000501 /// @param expr the expression
502 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000503 template <typename T>
504 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
505 return expr;
506 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000507
508 /// @param name the identifier name
509 /// @return an ast::IdentifierExpression with the given name
510 ast::IdentifierExpression* Expr(const std::string& name) {
511 return create<ast::IdentifierExpression>(Symbols().Register(name));
512 }
513
Ben Clayton46d78d72021-02-10 21:34:26 +0000514 /// @param symbol the identifier symbol
515 /// @return an ast::IdentifierExpression with the given symbol
516 ast::IdentifierExpression* Expr(Symbol symbol) {
517 return create<ast::IdentifierExpression>(symbol);
518 }
519
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000520 /// @param source the source information
521 /// @param name the identifier name
522 /// @return an ast::IdentifierExpression with the given name
523 ast::IdentifierExpression* Expr(const Source& source,
524 const std::string& name) {
525 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
526 }
527
528 /// @param name the identifier name
529 /// @return an ast::IdentifierExpression with the given name
530 ast::IdentifierExpression* Expr(const char* name) {
531 return create<ast::IdentifierExpression>(Symbols().Register(name));
532 }
533
534 /// @param value the boolean value
535 /// @return a Scalar constructor for the given value
536 ast::ScalarConstructorExpression* Expr(bool value) {
537 return create<ast::ScalarConstructorExpression>(Literal(value));
538 }
539
540 /// @param value the float value
541 /// @return a Scalar constructor for the given value
542 ast::ScalarConstructorExpression* Expr(f32 value) {
543 return create<ast::ScalarConstructorExpression>(Literal(value));
544 }
545
546 /// @param value the integer value
547 /// @return a Scalar constructor for the given value
548 ast::ScalarConstructorExpression* Expr(i32 value) {
549 return create<ast::ScalarConstructorExpression>(Literal(value));
550 }
551
552 /// @param value the unsigned int value
553 /// @return a Scalar constructor for the given value
554 ast::ScalarConstructorExpression* Expr(u32 value) {
555 return create<ast::ScalarConstructorExpression>(Literal(value));
556 }
557
558 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
559 /// `list`.
560 /// @param list the list to append too
561 /// @param arg the arg to create
562 template <typename ARG>
563 void Append(ast::ExpressionList& list, ARG&& arg) {
564 list.emplace_back(Expr(std::forward<ARG>(arg)));
565 }
566
567 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
568 /// then appends them to `list`.
569 /// @param list the list to append too
570 /// @param arg0 the first argument
571 /// @param args the rest of the arguments
572 template <typename ARG0, typename... ARGS>
573 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
574 Append(list, std::forward<ARG0>(arg0));
575 Append(list, std::forward<ARGS>(args)...);
576 }
577
578 /// @return an empty list of expressions
579 ast::ExpressionList ExprList() { return {}; }
580
581 /// @param args the list of expressions
582 /// @return the list of expressions converted to `ast::Expression`s using
583 /// `Expr()`,
584 template <typename... ARGS>
585 ast::ExpressionList ExprList(ARGS&&... args) {
586 ast::ExpressionList list;
587 list.reserve(sizeof...(args));
588 Append(list, std::forward<ARGS>(args)...);
589 return list;
590 }
591
592 /// @param list the list of expressions
593 /// @return `list`
594 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
595
596 /// @param val the boolan value
597 /// @return a boolean literal with the given value
598 ast::BoolLiteral* Literal(bool val) {
599 return create<ast::BoolLiteral>(ty.bool_(), val);
600 }
601
602 /// @param val the float value
603 /// @return a float literal with the given value
604 ast::FloatLiteral* Literal(f32 val) {
605 return create<ast::FloatLiteral>(ty.f32(), val);
606 }
607
608 /// @param val the unsigned int value
609 /// @return a ast::UintLiteral with the given value
610 ast::UintLiteral* Literal(u32 val) {
611 return create<ast::UintLiteral>(ty.u32(), val);
612 }
613
614 /// @param val the integer value
615 /// @return the ast::SintLiteral with the given value
616 ast::SintLiteral* Literal(i32 val) {
617 return create<ast::SintLiteral>(ty.i32(), val);
618 }
619
620 /// @param args the arguments for the type constructor
621 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
622 /// of `args` converted to `ast::Expression`s using `Expr()`
623 template <typename T, typename... ARGS>
624 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
625 return create<ast::TypeConstructorExpression>(
626 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
627 }
628
629 /// @param type the type to construct
630 /// @param args the arguments for the constructor
631 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
632 /// values `args`.
633 template <typename... ARGS>
634 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
635 return create<ast::TypeConstructorExpression>(
636 type, ExprList(std::forward<ARGS>(args)...));
637 }
638
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000639 /// Creates a constructor expression that constructs an object of
640 /// `type` filled with `elem_value`. For example,
641 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
642 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
643 /// @param type the type to construct
644 /// @param elem_value the initial or element value (for vec and mat) to
645 /// construct with
646 /// @return the constructor expression
647 ast::ConstructorExpression* ConstructValueFilledWith(type::Type* type,
648 int elem_value = 0);
649
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000650 /// @param args the arguments for the vector constructor
651 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
652 /// `T`, constructed with the values `args`.
653 template <typename T, typename... ARGS>
654 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
655 return create<ast::TypeConstructorExpression>(
656 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
657 }
658
659 /// @param args the arguments for the vector constructor
660 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
661 /// `T`, constructed with the values `args`.
662 template <typename T, typename... ARGS>
663 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
664 return create<ast::TypeConstructorExpression>(
665 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
666 }
667
668 /// @param args the arguments for the vector constructor
669 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
670 /// `T`, constructed with the values `args`.
671 template <typename T, typename... ARGS>
672 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
673 return create<ast::TypeConstructorExpression>(
674 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
675 }
676
677 /// @param args the arguments for the matrix constructor
678 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
679 /// `T`, constructed with the values `args`.
680 template <typename T, typename... ARGS>
681 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
682 return create<ast::TypeConstructorExpression>(
683 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
684 }
685
686 /// @param args the arguments for the matrix constructor
687 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
688 /// `T`, constructed with the values `args`.
689 template <typename T, typename... ARGS>
690 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
691 return create<ast::TypeConstructorExpression>(
692 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
693 }
694
695 /// @param args the arguments for the matrix constructor
696 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
697 /// `T`, constructed with the values `args`.
698 template <typename T, typename... ARGS>
699 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
700 return create<ast::TypeConstructorExpression>(
701 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
702 }
703
704 /// @param args the arguments for the matrix constructor
705 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
706 /// `T`, constructed with the values `args`.
707 template <typename T, typename... ARGS>
708 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
709 return create<ast::TypeConstructorExpression>(
710 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
711 }
712
713 /// @param args the arguments for the matrix constructor
714 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
715 /// `T`, constructed with the values `args`.
716 template <typename T, typename... ARGS>
717 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
718 return create<ast::TypeConstructorExpression>(
719 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
720 }
721
722 /// @param args the arguments for the matrix constructor
723 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
724 /// `T`, constructed with the values `args`.
725 template <typename T, typename... ARGS>
726 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
727 return create<ast::TypeConstructorExpression>(
728 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
729 }
730
731 /// @param args the arguments for the matrix constructor
732 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
733 /// `T`, constructed with the values `args`.
734 template <typename T, typename... ARGS>
735 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
736 return create<ast::TypeConstructorExpression>(
737 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
738 }
739
740 /// @param args the arguments for the matrix constructor
741 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
742 /// `T`, constructed with the values `args`.
743 template <typename T, typename... ARGS>
744 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
745 return create<ast::TypeConstructorExpression>(
746 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
747 }
748
749 /// @param args the arguments for the matrix constructor
750 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
751 /// `T`, constructed with the values `args`.
752 template <typename T, typename... ARGS>
753 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
754 return create<ast::TypeConstructorExpression>(
755 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
756 }
757
758 /// @param args the arguments for the array constructor
759 /// @return an `ast::TypeConstructorExpression` of an array with element type
760 /// `T`, constructed with the values `args`.
761 template <typename T, int N = 0, typename... ARGS>
762 ast::TypeConstructorExpression* array(ARGS&&... args) {
763 return create<ast::TypeConstructorExpression>(
764 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
765 }
766
767 /// @param subtype the array element type
768 /// @param n the array size. 0 represents a runtime-array.
769 /// @param args the arguments for the array constructor
770 /// @return an `ast::TypeConstructorExpression` of an array with element type
771 /// `subtype`, constructed with the values `args`.
772 template <typename... ARGS>
773 ast::TypeConstructorExpression* array(type::Type* subtype,
774 uint32_t n,
775 ARGS&&... args) {
776 return create<ast::TypeConstructorExpression>(
777 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
778 }
779
780 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000781 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000782 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000783 /// @param constructor constructor expression
784 /// @param decorations variable decorations
785 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000786 template <typename NAME>
787 ast::Variable* Var(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000788 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000789 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000790 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000791 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000792 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
793 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000794 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000795
796 /// @param source the variable source
797 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000798 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000799 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000800 /// @param constructor constructor expression
801 /// @param decorations variable decorations
802 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000803 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000804 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000805 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000806 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000807 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000808 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000809 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000810 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000811 type, false, constructor, decorations);
812 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000813
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000814 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000815 /// @param type the variable type
816 /// @param constructor optional constructor expression
817 /// @param decorations optional variable decorations
818 /// @returns a constant `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000819 template <typename NAME>
820 ast::Variable* Const(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000821 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000822 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000823 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000824 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000825 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000826 constructor, decorations);
827 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000828
829 /// @param source the variable source
830 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000831 /// @param type the variable type
832 /// @param constructor optional constructor expression
833 /// @param decorations optional variable decorations
834 /// @returns a constant `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000835 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000836 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000837 NAME&& 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 Clayton1b8d9f22021-04-07 11:16:01 +0000841 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000842 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 Clayton401b96b2021-02-03 17:19:59 +0000846 /// @param args the arguments to pass to Var()
847 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
848 /// of `args`, which is automatically registered as a global variable with the
849 /// ast::Module.
850 template <typename... ARGS>
851 ast::Variable* Global(ARGS&&... args) {
852 auto* var = Var(std::forward<ARGS>(args)...);
853 AST().AddGlobalVariable(var);
854 return var;
855 }
856
857 /// @param args the arguments to pass to Const()
858 /// @returns a const `ast::Variable` constructed by calling Var() with the
859 /// arguments of `args`, which is automatically registered as a global
860 /// variable with the ast::Module.
861 template <typename... ARGS>
862 ast::Variable* GlobalConst(ARGS&&... args) {
863 auto* var = Const(std::forward<ARGS>(args)...);
864 AST().AddGlobalVariable(var);
865 return var;
866 }
867
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000868 /// @param func the function name
869 /// @param args the function call arguments
870 /// @returns a `ast::CallExpression` to the function `func`, with the
871 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
872 template <typename NAME, typename... ARGS>
873 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
874 return create<ast::CallExpression>(Expr(func),
875 ExprList(std::forward<ARGS>(args)...));
876 }
877
878 /// @param lhs the left hand argument to the addition operation
879 /// @param rhs the right hand argument to the addition operation
880 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
881 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000882 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000883 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
884 Expr(std::forward<LHS>(lhs)),
885 Expr(std::forward<RHS>(rhs)));
886 }
887
888 /// @param lhs the left hand argument to the subtraction operation
889 /// @param rhs the right hand argument to the subtraction operation
890 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
891 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000892 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000893 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
894 Expr(std::forward<LHS>(lhs)),
895 Expr(std::forward<RHS>(rhs)));
896 }
897
898 /// @param lhs the left hand argument to the multiplication operation
899 /// @param rhs the right hand argument to the multiplication operation
900 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
901 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000902 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000903 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
904 Expr(std::forward<LHS>(lhs)),
905 Expr(std::forward<RHS>(rhs)));
906 }
907
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000908 /// @param source the source information
909 /// @param lhs the left hand argument to the multiplication operation
910 /// @param rhs the right hand argument to the multiplication operation
911 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
912 template <typename LHS, typename RHS>
913 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
914 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
915 Expr(std::forward<LHS>(lhs)),
916 Expr(std::forward<RHS>(rhs)));
917 }
918
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000919 /// @param lhs the left hand argument to the division operation
920 /// @param rhs the right hand argument to the division operation
921 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
922 template <typename LHS, typename RHS>
923 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
924 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
925 Expr(std::forward<LHS>(lhs)),
926 Expr(std::forward<RHS>(rhs)));
927 }
928
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000929 /// @param arr the array argument for the array accessor expression
930 /// @param idx the index argument for the array accessor expression
931 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
932 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000933 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000934 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
935 Expr(std::forward<IDX>(idx)));
936 }
937
938 /// @param obj the object for the member accessor expression
939 /// @param idx the index argument for the array accessor expression
940 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
941 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +0000942 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000943 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
944 Expr(std::forward<IDX>(idx)));
945 }
946
Ben Clayton42d1e092021-02-02 14:29:15 +0000947 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000948 /// @param val the offset value
949 /// @returns the offset decoration pointer
950 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
951 return create<ast::StructMemberOffsetDecoration>(source_, val);
952 }
953
Ben Claytond614dd52021-03-15 10:43:11 +0000954 /// Creates a ast::StructMemberSizeDecoration
955 /// @param source the source information
956 /// @param val the size value
957 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +0000958 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
959 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +0000960 return create<ast::StructMemberSizeDecoration>(source, val);
961 }
962
963 /// Creates a ast::StructMemberSizeDecoration
964 /// @param val the size value
965 /// @returns the size decoration pointer
966 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
967 return create<ast::StructMemberSizeDecoration>(source_, val);
968 }
969
970 /// Creates a ast::StructMemberAlignDecoration
971 /// @param source the source information
972 /// @param val the align value
973 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +0000974 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
975 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +0000976 return create<ast::StructMemberAlignDecoration>(source, val);
977 }
978
979 /// Creates a ast::StructMemberAlignDecoration
980 /// @param val the align value
981 /// @returns the align decoration pointer
982 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
983 return create<ast::StructMemberAlignDecoration>(source_, val);
984 }
985
Ben Clayton42d1e092021-02-02 14:29:15 +0000986 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000987 /// @param source the source information
988 /// @param name the function name
989 /// @param params the function parameters
990 /// @param type the function return type
991 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000992 /// @param decorations the optional function decorations
993 /// @param return_type_decorations the optional function return type
994 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000995 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000996 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +0000997 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000998 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000999 ast::VariableList params,
1000 type::Type* type,
1001 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001002 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001003 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001004 auto* func =
1005 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1006 type, create<ast::BlockStatement>(body),
1007 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001008 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001009 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001010 }
1011
Ben Clayton42d1e092021-02-02 14:29:15 +00001012 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001013 /// @param name the function name
1014 /// @param params the function parameters
1015 /// @param type the function return type
1016 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001017 /// @param decorations the optional function decorations
1018 /// @param return_type_decorations the optional function return type
1019 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001020 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001021 template <typename NAME>
1022 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001023 ast::VariableList params,
1024 type::Type* type,
1025 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001026 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001027 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001028 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1029 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001030 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001031 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001032 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001033 }
1034
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001035 /// Creates an ast::ReturnStatement with the input args
1036 /// @param args arguments to construct a return statement with
1037 /// @returns the return statement pointer
1038 template <typename... Args>
1039 ast::ReturnStatement* Return(Args&&... args) {
1040 return create<ast::ReturnStatement>(std::forward<Args>(args)...);
1041 }
1042
Ben Claytond614dd52021-03-15 10:43:11 +00001043 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1044 /// the AST().ConstructedTypes().
1045 /// @param source the source information
1046 /// @param name the struct name
1047 /// @param members the struct members
1048 /// @param decorations the optional struct decorations
1049 /// @returns the struct type
1050 type::Struct* Structure(const Source& source,
1051 const std::string& name,
1052 ast::StructMemberList members,
1053 ast::DecorationList decorations = {}) {
1054 auto* impl =
1055 create<ast::Struct>(source, std::move(members), std::move(decorations));
1056 auto* type = ty.struct_(name, impl);
1057 AST().AddConstructedType(type);
1058 return type;
1059 }
1060
1061 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1062 /// the AST().ConstructedTypes().
1063 /// @param name the struct name
1064 /// @param members the struct members
1065 /// @param decorations the optional struct decorations
1066 /// @returns the struct type
1067 type::Struct* Structure(const std::string& name,
1068 ast::StructMemberList members,
1069 ast::DecorationList decorations = {}) {
1070 auto* impl =
1071 create<ast::Struct>(std::move(members), std::move(decorations));
1072 auto* type = ty.struct_(name, impl);
1073 AST().AddConstructedType(type);
1074 return type;
1075 }
1076
Ben Clayton42d1e092021-02-02 14:29:15 +00001077 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078 /// @param source the source information
1079 /// @param name the struct member name
1080 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001081 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001082 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001083 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001084 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001085 NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001086 type::Type* type,
1087 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001088 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1089 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001090 }
1091
Ben Clayton42d1e092021-02-02 14:29:15 +00001092 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001093 /// @param name the struct member name
1094 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001095 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001096 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001097 template <typename NAME>
1098 ast::StructMember* Member(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001099 type::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001100 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001101 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1102 type, 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
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001110 template <typename NAME>
1111 ast::StructMember* Member(uint32_t offset, NAME&& name, type::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001112 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001113 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001114 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001115 create<ast::StructMemberOffsetDecoration>(offset),
1116 });
1117 }
1118
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001119 /// Creates a ast::BlockStatement with input statements
1120 /// @param statements statements of block
1121 /// @returns the block statement pointer
1122 template <typename... Statements>
1123 ast::BlockStatement* Block(Statements&&... statements) {
1124 return create<ast::BlockStatement>(
1125 ast::StatementList{std::forward<Statements>(statements)...});
1126 }
1127
1128 /// Creates a ast::ElseStatement with input condition and body
1129 /// @param condition the else condition expression
1130 /// @param body the else body
1131 /// @returns the else statement pointer
1132 ast::ElseStatement* Else(ast::Expression* condition,
1133 ast::BlockStatement* body) {
1134 return create<ast::ElseStatement>(condition, body);
1135 }
1136
1137 /// Creates a ast::IfStatement with input condition, body, and optional
1138 /// variadic else statements
1139 /// @param condition the if statement condition expression
1140 /// @param body the if statement body
1141 /// @param elseStatements optional variadic else statements
1142 /// @returns the if statement pointer
1143 template <typename... ElseStatements>
1144 ast::IfStatement* If(ast::Expression* condition,
1145 ast::BlockStatement* body,
1146 ElseStatements&&... elseStatements) {
1147 return create<ast::IfStatement>(
1148 condition, body,
1149 ast::ElseStatementList{
1150 std::forward<ElseStatements>(elseStatements)...});
1151 }
1152
1153 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001154 /// @param lhs the left hand side expression initializer
1155 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001156 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001157 template <typename LhsExpressionInit, typename RhsExpressionInit>
1158 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1159 RhsExpressionInit&& rhs) {
1160 return create<ast::AssignmentStatement>(
1161 Expr(std::forward<LhsExpressionInit>(lhs)),
1162 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001163 }
1164
1165 /// Creates a ast::LoopStatement with input body and optional continuing
1166 /// @param body the loop body
1167 /// @param continuing the optional continuing block
1168 /// @returns the loop statement pointer
1169 ast::LoopStatement* Loop(ast::BlockStatement* body,
1170 ast::BlockStatement* continuing = nullptr) {
1171 return create<ast::LoopStatement>(body, continuing);
1172 }
1173
1174 /// Creates a ast::VariableDeclStatement for the input variable
1175 /// @param var the variable to wrap in a decl statement
1176 /// @returns the variable decl statement pointer
1177 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1178 return create<ast::VariableDeclStatement>(var);
1179 }
1180
Antonio Maioranocea744d2021-03-25 12:55:27 +00001181 /// Creates a ast::SwitchStatement with input expression and cases
1182 /// @param condition the condition expression initializer
1183 /// @param cases case statements
1184 /// @returns the switch statement pointer
1185 template <typename ExpressionInit, typename... Cases>
1186 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1187 return create<ast::SwitchStatement>(
1188 Expr(std::forward<ExpressionInit>(condition)),
1189 ast::CaseStatementList{std::forward<Cases>(cases)...});
1190 }
1191
1192 /// Creates a ast::CaseStatement with input list of selectors, and body
1193 /// @param selectors list of selectors
1194 /// @param body the case body
1195 /// @returns the case statement pointer
1196 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1197 ast::BlockStatement* body = nullptr) {
1198 return create<ast::CaseStatement>(std::move(selectors),
1199 body ? body : Block());
1200 }
1201
1202 /// Convenient overload that takes a single selector
1203 /// @param selector a single case selector
1204 /// @param body the case body
1205 /// @returns the case statement pointer
1206 ast::CaseStatement* Case(ast::IntLiteral* selector,
1207 ast::BlockStatement* body = nullptr) {
1208 return Case(ast::CaseSelectorList{selector}, body);
1209 }
1210
1211 /// Convenience function that creates a 'default' ast::CaseStatement
1212 /// @param body the case body
1213 /// @returns the case statement pointer
1214 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1215 return Case(ast::CaseSelectorList{}, body);
1216 }
1217
James Price68f558f2021-04-06 15:51:47 +00001218 /// Creates an ast::BuiltinDecoration
1219 /// @param source the source information
1220 /// @param builtin the builtin value
1221 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001222 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001223 return create<ast::BuiltinDecoration>(source, builtin);
1224 }
1225
1226 /// Creates an ast::BuiltinDecoration
1227 /// @param builtin the builtin value
1228 /// @returns the builtin decoration pointer
1229 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1230 return create<ast::BuiltinDecoration>(source_, builtin);
1231 }
1232
1233 /// Creates an ast::LocationDecoration
1234 /// @param source the source information
1235 /// @param location the location value
1236 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001237 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001238 return create<ast::LocationDecoration>(source, location);
1239 }
1240
1241 /// Creates an ast::LocationDecoration
1242 /// @param location the location value
1243 /// @returns the location decoration pointer
1244 ast::LocationDecoration* Location(uint32_t location) {
1245 return create<ast::LocationDecoration>(source_, location);
1246 }
1247
1248 /// Creates an ast::StageDecoration
1249 /// @param source the source information
1250 /// @param stage the pipeline stage
1251 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001252 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001253 return create<ast::StageDecoration>(source, stage);
1254 }
1255
1256 /// Creates an ast::StageDecoration
1257 /// @param stage the pipeline stage
1258 /// @returns the stage decoration pointer
1259 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1260 return create<ast::StageDecoration>(source_, stage);
1261 }
1262
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001263 /// Sets the current builder source to `src`
1264 /// @param src the Source used for future create() calls
1265 void SetSource(const Source& src) {
1266 AssertNotMoved();
1267 source_ = src;
1268 }
1269
1270 /// Sets the current builder source to `loc`
1271 /// @param loc the Source used for future create() calls
1272 void SetSource(const Source::Location& loc) {
1273 AssertNotMoved();
1274 source_ = Source(loc);
1275 }
1276
Ben Clayton33352542021-01-29 16:43:41 +00001277 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001278 /// @note As the Resolver is run when the Program is built, this will only be
1279 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001280 /// @param expr the AST expression
1281 /// @return the resolved semantic type for the expression, or nullptr if the
1282 /// expression has no resolved type.
1283 type::Type* TypeOf(ast::Expression* expr) const;
1284
Ben Clayton401b96b2021-02-03 17:19:59 +00001285 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001286 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001287 /// nodes.
1288 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1289 /// @return the ast::Statement that wraps the ast::Expression
1290 ast::Statement* WrapInStatement(ast::Expression* expr);
1291 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001292 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001293 /// these nodes.
1294 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1295 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1296 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1297 /// Returns the statement argument. Used as a passthrough-overload by
1298 /// WrapInFunction().
1299 /// @param stmt the ast::Statement
1300 /// @return `stmt`
1301 ast::Statement* WrapInStatement(ast::Statement* stmt);
1302 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001303 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001304 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001305 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001306 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001307 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001308 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001309 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001310 }
1311 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001312 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001313 /// @returns the function
1314 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001315
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001316 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001317 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001318
1319 protected:
1320 /// Asserts that the builder has not been moved.
1321 void AssertNotMoved() const;
1322
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001323 private:
1324 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001325 ASTNodeAllocator ast_nodes_;
1326 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001327 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001328 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001329 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001330 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001331
1332 /// The source to use when creating AST nodes without providing a Source as
1333 /// the first argument.
1334 Source source_;
1335
Ben Clayton5f0ea112021-03-09 10:54:37 +00001336 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001337 /// program when built.
1338 bool resolve_on_build_ = true;
1339
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001340 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1341 bool moved_ = false;
1342};
1343
1344//! @cond Doxygen_Suppress
1345// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1346template <>
1347struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1348 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1349 return t->i32();
1350 }
1351};
1352template <>
1353struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1354 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1355 return t->u32();
1356 }
1357};
1358template <>
1359struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1360 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1361 return t->f32();
1362 }
1363};
1364template <>
1365struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1366 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1367 return t->bool_();
1368 }
1369};
1370template <>
1371struct ProgramBuilder::TypesBuilder::CToAST<void> {
1372 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1373 return t->void_();
1374 }
1375};
1376//! @endcond
1377
1378} // namespace tint
1379
1380#endif // SRC_PROGRAM_BUILDER_H_