blob: fb261cc905489db08cb6bc639a57013fe72e570a [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
Antonio Maiorano25436862021-04-13 13:32:33 +0000341 /// @param type vector subtype
342 /// @return the tint AST type for a 2-element vector of `type`.
343 type::Vector* vec2(type::Type* type) const {
344 return builder->create<type::Vector>(type, 2u);
345 }
346
347 /// @param type vector subtype
348 /// @return the tint AST type for a 3-element vector of `type`.
349 type::Vector* vec3(type::Type* type) const {
350 return builder->create<type::Vector>(type, 3u);
351 }
352
353 /// @param type vector subtype
354 /// @return the tint AST type for a 4-element vector of `type`.
355 type::Type* vec4(type::Type* type) const {
356 return builder->create<type::Vector>(type, 4u);
357 }
358
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000359 /// @return the tint AST type for a 2-element vector of the C type `T`.
360 template <typename T>
361 type::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000362 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000363 }
364
365 /// @return the tint AST type for a 3-element vector of the C type `T`.
366 template <typename T>
367 type::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000368 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000369 }
370
371 /// @return the tint AST type for a 4-element vector of the C type `T`.
372 template <typename T>
373 type::Type* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000374 return vec4(Of<T>());
375 }
376
377 /// @param type matrix subtype
378 /// @return the tint AST type for a 2x3 matrix of `type`.
379 type::Matrix* mat2x2(type::Type* type) const {
380 return builder->create<type::Matrix>(type, 2u, 2u);
381 }
382
383 /// @param type matrix subtype
384 /// @return the tint AST type for a 2x3 matrix of `type`.
385 type::Matrix* mat2x3(type::Type* type) const {
386 return builder->create<type::Matrix>(type, 3u, 2u);
387 }
388
389 /// @param type matrix subtype
390 /// @return the tint AST type for a 2x4 matrix of `type`.
391 type::Matrix* mat2x4(type::Type* type) const {
392 return builder->create<type::Matrix>(type, 4u, 2u);
393 }
394
395 /// @param type matrix subtype
396 /// @return the tint AST type for a 3x2 matrix of `type`.
397 type::Matrix* mat3x2(type::Type* type) const {
398 return builder->create<type::Matrix>(type, 2u, 3u);
399 }
400
401 /// @param type matrix subtype
402 /// @return the tint AST type for a 3x3 matrix of `type`.
403 type::Matrix* mat3x3(type::Type* type) const {
404 return builder->create<type::Matrix>(type, 3u, 3u);
405 }
406
407 /// @param type matrix subtype
408 /// @return the tint AST type for a 3x4 matrix of `type`.
409 type::Matrix* mat3x4(type::Type* type) const {
410 return builder->create<type::Matrix>(type, 4u, 3u);
411 }
412
413 /// @param type matrix subtype
414 /// @return the tint AST type for a 4x2 matrix of `type`.
415 type::Matrix* mat4x2(type::Type* type) const {
416 return builder->create<type::Matrix>(type, 2u, 4u);
417 }
418
419 /// @param type matrix subtype
420 /// @return the tint AST type for a 4x3 matrix of `type`.
421 type::Matrix* mat4x3(type::Type* type) const {
422 return builder->create<type::Matrix>(type, 3u, 4u);
423 }
424
425 /// @param type matrix subtype
426 /// @return the tint AST type for a 4x4 matrix of `type`.
427 type::Matrix* mat4x4(type::Type* type) const {
428 return builder->create<type::Matrix>(type, 4u, 4u);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000429 }
430
431 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
432 template <typename T>
433 type::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000434 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000435 }
436
437 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
438 template <typename T>
439 type::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000440 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000441 }
442
443 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
444 template <typename T>
445 type::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000446 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000447 }
448
449 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
450 template <typename T>
451 type::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000452 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000453 }
454
455 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
456 template <typename T>
457 type::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000458 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000459 }
460
461 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
462 template <typename T>
463 type::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000464 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000465 }
466
467 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
468 template <typename T>
469 type::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000470 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000471 }
472
473 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
474 template <typename T>
475 type::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000476 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000477 }
478
479 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
480 template <typename T>
481 type::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000482 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000483 }
484
485 /// @param subtype the array element type
486 /// @param n the array size. 0 represents a runtime-array.
487 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000488 type::Array* array(type::Type* subtype, uint32_t n = 0) const {
James Price95d40772021-03-11 17:39:32 +0000489 return builder->create<type::Array>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000490 }
491
Ben Claytonbab31972021-02-08 21:16:21 +0000492 /// @param subtype the array element type
493 /// @param n the array size. 0 represents a runtime-array.
494 /// @param stride the array stride.
495 /// @return the tint AST type for a array of size `n` of type `T`
496 type::Array* array(type::Type* subtype, uint32_t n, uint32_t stride) const {
497 return builder->create<type::Array>(
498 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000499 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000500 builder->create<ast::StrideDecoration>(stride),
501 });
502 }
503
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000504 /// @return the tint AST type for an array of size `N` of type `T`
505 template <typename T, int N = 0>
506 type::Array* array() const {
507 return array(Of<T>(), N);
508 }
509
Ben Claytonbab31972021-02-08 21:16:21 +0000510 /// @param stride the array stride
511 /// @return the tint AST type for an array of size `N` of type `T`
512 template <typename T, int N = 0>
513 type::Array* array(uint32_t stride) const {
514 return array(Of<T>(), N, stride);
515 }
Ben Clayton42d1e092021-02-02 14:29:15 +0000516 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000517 /// @param name the alias name
518 /// @param type the alias type
519 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000520 template <typename NAME>
521 type::Alias* alias(NAME&& name, type::Type* type) const {
522 return builder->create<type::Alias>(
523 builder->Sym(std::forward<NAME>(name)), type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000524 }
525
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000526 /// @return the tint AST pointer to `type` with the given ast::StorageClass
527 /// @param type the type of the pointer
528 /// @param storage_class the storage class of the pointer
529 type::Pointer* pointer(type::Type* type,
530 ast::StorageClass storage_class) const {
531 return builder->create<type::Pointer>(type, storage_class);
532 }
533
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000534 /// @return the tint AST pointer to type `T` with the given
535 /// ast::StorageClass.
536 /// @param storage_class the storage class of the pointer
537 template <typename T>
538 type::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000539 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000540 }
541
542 /// @param name the struct name
543 /// @param impl the struct implementation
544 /// @returns a struct pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000545 template <typename NAME>
546 type::Struct* struct_(NAME&& name, ast::Struct* impl) const {
547 return builder->create<type::Struct>(
548 builder->Sym(std::forward<NAME>(name)), impl);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000549 }
550
551 private:
552 /// CToAST<T> is specialized for various `T` types and each specialization
553 /// contains a single static `get()` method for obtaining the corresponding
554 /// AST type for the C type `T`.
555 /// `get()` has the signature:
556 /// `static type::Type* get(Types* t)`
557 template <typename T>
558 struct CToAST {};
559
Ben Claytonc7ca7662021-02-17 16:23:52 +0000560 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000561 };
562
563 //////////////////////////////////////////////////////////////////////////////
564 // AST helper methods
565 //////////////////////////////////////////////////////////////////////////////
566
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000567 /// @param name the symbol string
568 /// @return a Symbol with the given name
569 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
570
571 /// @param sym the symbol
572 /// @return `sym`
573 Symbol Sym(Symbol sym) { return sym; }
574
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000575 /// @param expr the expression
576 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000577 template <typename T>
578 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
579 return expr;
580 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000581
582 /// @param name the identifier name
583 /// @return an ast::IdentifierExpression with the given name
584 ast::IdentifierExpression* Expr(const std::string& name) {
585 return create<ast::IdentifierExpression>(Symbols().Register(name));
586 }
587
Ben Clayton46d78d72021-02-10 21:34:26 +0000588 /// @param symbol the identifier symbol
589 /// @return an ast::IdentifierExpression with the given symbol
590 ast::IdentifierExpression* Expr(Symbol symbol) {
591 return create<ast::IdentifierExpression>(symbol);
592 }
593
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000594 /// @param source the source information
595 /// @param name the identifier name
596 /// @return an ast::IdentifierExpression with the given name
597 ast::IdentifierExpression* Expr(const Source& source,
598 const std::string& name) {
599 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
600 }
601
602 /// @param name the identifier name
603 /// @return an ast::IdentifierExpression with the given name
604 ast::IdentifierExpression* Expr(const char* name) {
605 return create<ast::IdentifierExpression>(Symbols().Register(name));
606 }
607
608 /// @param value the boolean value
609 /// @return a Scalar constructor for the given value
610 ast::ScalarConstructorExpression* Expr(bool value) {
611 return create<ast::ScalarConstructorExpression>(Literal(value));
612 }
613
614 /// @param value the float value
615 /// @return a Scalar constructor for the given value
616 ast::ScalarConstructorExpression* Expr(f32 value) {
617 return create<ast::ScalarConstructorExpression>(Literal(value));
618 }
619
620 /// @param value the integer value
621 /// @return a Scalar constructor for the given value
622 ast::ScalarConstructorExpression* Expr(i32 value) {
623 return create<ast::ScalarConstructorExpression>(Literal(value));
624 }
625
626 /// @param value the unsigned int value
627 /// @return a Scalar constructor for the given value
628 ast::ScalarConstructorExpression* Expr(u32 value) {
629 return create<ast::ScalarConstructorExpression>(Literal(value));
630 }
631
632 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
633 /// `list`.
634 /// @param list the list to append too
635 /// @param arg the arg to create
636 template <typename ARG>
637 void Append(ast::ExpressionList& list, ARG&& arg) {
638 list.emplace_back(Expr(std::forward<ARG>(arg)));
639 }
640
641 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
642 /// then appends them to `list`.
643 /// @param list the list to append too
644 /// @param arg0 the first argument
645 /// @param args the rest of the arguments
646 template <typename ARG0, typename... ARGS>
647 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
648 Append(list, std::forward<ARG0>(arg0));
649 Append(list, std::forward<ARGS>(args)...);
650 }
651
652 /// @return an empty list of expressions
653 ast::ExpressionList ExprList() { return {}; }
654
655 /// @param args the list of expressions
656 /// @return the list of expressions converted to `ast::Expression`s using
657 /// `Expr()`,
658 template <typename... ARGS>
659 ast::ExpressionList ExprList(ARGS&&... args) {
660 ast::ExpressionList list;
661 list.reserve(sizeof...(args));
662 Append(list, std::forward<ARGS>(args)...);
663 return list;
664 }
665
666 /// @param list the list of expressions
667 /// @return `list`
668 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
669
670 /// @param val the boolan value
671 /// @return a boolean literal with the given value
672 ast::BoolLiteral* Literal(bool val) {
673 return create<ast::BoolLiteral>(ty.bool_(), val);
674 }
675
676 /// @param val the float value
677 /// @return a float literal with the given value
678 ast::FloatLiteral* Literal(f32 val) {
679 return create<ast::FloatLiteral>(ty.f32(), val);
680 }
681
682 /// @param val the unsigned int value
683 /// @return a ast::UintLiteral with the given value
684 ast::UintLiteral* Literal(u32 val) {
685 return create<ast::UintLiteral>(ty.u32(), val);
686 }
687
688 /// @param val the integer value
689 /// @return the ast::SintLiteral with the given value
690 ast::SintLiteral* Literal(i32 val) {
691 return create<ast::SintLiteral>(ty.i32(), val);
692 }
693
694 /// @param args the arguments for the type constructor
695 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
696 /// of `args` converted to `ast::Expression`s using `Expr()`
697 template <typename T, typename... ARGS>
698 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
699 return create<ast::TypeConstructorExpression>(
700 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
701 }
702
703 /// @param type the type to construct
704 /// @param args the arguments for the constructor
705 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
706 /// values `args`.
707 template <typename... ARGS>
708 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
709 return create<ast::TypeConstructorExpression>(
710 type, ExprList(std::forward<ARGS>(args)...));
711 }
712
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000713 /// Creates a constructor expression that constructs an object of
714 /// `type` filled with `elem_value`. For example,
715 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
716 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
717 /// @param type the type to construct
718 /// @param elem_value the initial or element value (for vec and mat) to
719 /// construct with
720 /// @return the constructor expression
721 ast::ConstructorExpression* ConstructValueFilledWith(type::Type* type,
722 int elem_value = 0);
723
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000724 /// @param args the arguments for the vector constructor
725 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
726 /// `T`, constructed with the values `args`.
727 template <typename T, typename... ARGS>
728 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
729 return create<ast::TypeConstructorExpression>(
730 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
731 }
732
733 /// @param args the arguments for the vector constructor
734 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
735 /// `T`, constructed with the values `args`.
736 template <typename T, typename... ARGS>
737 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
738 return create<ast::TypeConstructorExpression>(
739 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
740 }
741
742 /// @param args the arguments for the vector constructor
743 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
744 /// `T`, constructed with the values `args`.
745 template <typename T, typename... ARGS>
746 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
747 return create<ast::TypeConstructorExpression>(
748 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
749 }
750
751 /// @param args the arguments for the matrix constructor
752 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
753 /// `T`, constructed with the values `args`.
754 template <typename T, typename... ARGS>
755 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
756 return create<ast::TypeConstructorExpression>(
757 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
758 }
759
760 /// @param args the arguments for the matrix constructor
761 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
762 /// `T`, constructed with the values `args`.
763 template <typename T, typename... ARGS>
764 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
765 return create<ast::TypeConstructorExpression>(
766 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
767 }
768
769 /// @param args the arguments for the matrix constructor
770 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
771 /// `T`, constructed with the values `args`.
772 template <typename T, typename... ARGS>
773 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
774 return create<ast::TypeConstructorExpression>(
775 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
776 }
777
778 /// @param args the arguments for the matrix constructor
779 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
780 /// `T`, constructed with the values `args`.
781 template <typename T, typename... ARGS>
782 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
783 return create<ast::TypeConstructorExpression>(
784 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
785 }
786
787 /// @param args the arguments for the matrix constructor
788 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
789 /// `T`, constructed with the values `args`.
790 template <typename T, typename... ARGS>
791 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
792 return create<ast::TypeConstructorExpression>(
793 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
794 }
795
796 /// @param args the arguments for the matrix constructor
797 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
798 /// `T`, constructed with the values `args`.
799 template <typename T, typename... ARGS>
800 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
801 return create<ast::TypeConstructorExpression>(
802 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
803 }
804
805 /// @param args the arguments for the matrix constructor
806 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
807 /// `T`, constructed with the values `args`.
808 template <typename T, typename... ARGS>
809 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
810 return create<ast::TypeConstructorExpression>(
811 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
812 }
813
814 /// @param args the arguments for the matrix constructor
815 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
816 /// `T`, constructed with the values `args`.
817 template <typename T, typename... ARGS>
818 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
819 return create<ast::TypeConstructorExpression>(
820 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
821 }
822
823 /// @param args the arguments for the matrix constructor
824 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
825 /// `T`, constructed with the values `args`.
826 template <typename T, typename... ARGS>
827 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
828 return create<ast::TypeConstructorExpression>(
829 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
830 }
831
832 /// @param args the arguments for the array constructor
833 /// @return an `ast::TypeConstructorExpression` of an array with element type
834 /// `T`, constructed with the values `args`.
835 template <typename T, int N = 0, typename... ARGS>
836 ast::TypeConstructorExpression* array(ARGS&&... args) {
837 return create<ast::TypeConstructorExpression>(
838 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
839 }
840
841 /// @param subtype the array element type
842 /// @param n the array size. 0 represents a runtime-array.
843 /// @param args the arguments for the array constructor
844 /// @return an `ast::TypeConstructorExpression` of an array with element type
845 /// `subtype`, constructed with the values `args`.
846 template <typename... ARGS>
847 ast::TypeConstructorExpression* array(type::Type* subtype,
848 uint32_t n,
849 ARGS&&... args) {
850 return create<ast::TypeConstructorExpression>(
851 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
852 }
853
854 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000855 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000856 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000857 /// @param constructor constructor expression
858 /// @param decorations variable decorations
859 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000860 template <typename NAME>
861 ast::Variable* Var(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000862 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000863 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000864 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000865 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000866 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
867 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000868 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000869
870 /// @param source the variable source
871 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000872 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000873 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000874 /// @param constructor constructor expression
875 /// @param decorations variable decorations
876 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000877 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000878 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000879 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000880 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000881 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000882 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000883 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000884 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000885 type, false, constructor, decorations);
886 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000887
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000888 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000889 /// @param type the variable type
890 /// @param constructor optional constructor expression
891 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000892 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000893 template <typename NAME>
894 ast::Variable* Const(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000895 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000896 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000897 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000898 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000899 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000900 constructor, decorations);
901 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000902
903 /// @param source the variable source
904 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000905 /// @param type the variable type
906 /// @param constructor optional constructor expression
907 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000908 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000909 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000910 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000911 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000912 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000913 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000914 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000915 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000916 ast::StorageClass::kNone, type, true,
917 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000918 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000919
James Pricedaf1f1c2021-04-08 22:15:48 +0000920 /// @param name the parameter name
921 /// @param type the parameter type
922 /// @param decorations optional parameter decorations
923 /// @returns a constant `ast::Variable` with the given name and type
924 template <typename NAME>
925 ast::Variable* Param(NAME&& name,
926 type::Type* type,
927 ast::DecorationList decorations = {}) {
928 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
929 ast::StorageClass::kNone, type, true, nullptr,
930 decorations);
931 }
932
933 /// @param source the parameter source
934 /// @param name the parameter name
935 /// @param type the parameter type
936 /// @param decorations optional parameter decorations
937 /// @returns a constant `ast::Variable` with the given name and type
938 template <typename NAME>
939 ast::Variable* Param(const Source& source,
940 NAME&& name,
941 type::Type* type,
942 ast::DecorationList decorations = {}) {
943 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
944 ast::StorageClass::kNone, type, true, nullptr,
945 decorations);
946 }
947
Ben Clayton401b96b2021-02-03 17:19:59 +0000948 /// @param args the arguments to pass to Var()
949 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
950 /// of `args`, which is automatically registered as a global variable with the
951 /// ast::Module.
952 template <typename... ARGS>
953 ast::Variable* Global(ARGS&&... args) {
954 auto* var = Var(std::forward<ARGS>(args)...);
955 AST().AddGlobalVariable(var);
956 return var;
957 }
958
959 /// @param args the arguments to pass to Const()
960 /// @returns a const `ast::Variable` constructed by calling Var() with the
961 /// arguments of `args`, which is automatically registered as a global
962 /// variable with the ast::Module.
963 template <typename... ARGS>
964 ast::Variable* GlobalConst(ARGS&&... args) {
965 auto* var = Const(std::forward<ARGS>(args)...);
966 AST().AddGlobalVariable(var);
967 return var;
968 }
969
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000970 /// @param func the function name
971 /// @param args the function call arguments
972 /// @returns a `ast::CallExpression` to the function `func`, with the
973 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
974 template <typename NAME, typename... ARGS>
975 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
976 return create<ast::CallExpression>(Expr(func),
977 ExprList(std::forward<ARGS>(args)...));
978 }
979
980 /// @param lhs the left hand argument to the addition operation
981 /// @param rhs the right hand argument to the addition operation
982 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
983 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000984 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000985 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
986 Expr(std::forward<LHS>(lhs)),
987 Expr(std::forward<RHS>(rhs)));
988 }
989
990 /// @param lhs the left hand argument to the subtraction operation
991 /// @param rhs the right hand argument to the subtraction operation
992 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
993 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000994 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000995 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
996 Expr(std::forward<LHS>(lhs)),
997 Expr(std::forward<RHS>(rhs)));
998 }
999
1000 /// @param lhs the left hand argument to the multiplication operation
1001 /// @param rhs the right hand argument to the multiplication operation
1002 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1003 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001004 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001005 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1006 Expr(std::forward<LHS>(lhs)),
1007 Expr(std::forward<RHS>(rhs)));
1008 }
1009
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001010 /// @param source the source information
1011 /// @param lhs the left hand argument to the multiplication operation
1012 /// @param rhs the right hand argument to the multiplication operation
1013 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1014 template <typename LHS, typename RHS>
1015 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1016 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1017 Expr(std::forward<LHS>(lhs)),
1018 Expr(std::forward<RHS>(rhs)));
1019 }
1020
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001021 /// @param lhs the left hand argument to the division operation
1022 /// @param rhs the right hand argument to the division operation
1023 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1024 template <typename LHS, typename RHS>
1025 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1026 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1027 Expr(std::forward<LHS>(lhs)),
1028 Expr(std::forward<RHS>(rhs)));
1029 }
1030
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001031 /// @param arr the array argument for the array accessor expression
1032 /// @param idx the index argument for the array accessor expression
1033 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1034 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001035 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001036 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1037 Expr(std::forward<IDX>(idx)));
1038 }
1039
1040 /// @param obj the object for the member accessor expression
1041 /// @param idx the index argument for the array accessor expression
1042 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1043 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001044 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001045 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1046 Expr(std::forward<IDX>(idx)));
1047 }
1048
Ben Clayton42d1e092021-02-02 14:29:15 +00001049 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001050 /// @param val the offset value
1051 /// @returns the offset decoration pointer
1052 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1053 return create<ast::StructMemberOffsetDecoration>(source_, val);
1054 }
1055
Ben Claytond614dd52021-03-15 10:43:11 +00001056 /// Creates a ast::StructMemberSizeDecoration
1057 /// @param source the source information
1058 /// @param val the size value
1059 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001060 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1061 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001062 return create<ast::StructMemberSizeDecoration>(source, val);
1063 }
1064
1065 /// Creates a ast::StructMemberSizeDecoration
1066 /// @param val the size value
1067 /// @returns the size decoration pointer
1068 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1069 return create<ast::StructMemberSizeDecoration>(source_, val);
1070 }
1071
1072 /// Creates a ast::StructMemberAlignDecoration
1073 /// @param source the source information
1074 /// @param val the align value
1075 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001076 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1077 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001078 return create<ast::StructMemberAlignDecoration>(source, val);
1079 }
1080
1081 /// Creates a ast::StructMemberAlignDecoration
1082 /// @param val the align value
1083 /// @returns the align decoration pointer
1084 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1085 return create<ast::StructMemberAlignDecoration>(source_, val);
1086 }
1087
Ben Clayton42d1e092021-02-02 14:29:15 +00001088 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001089 /// @param source the source information
1090 /// @param name the function name
1091 /// @param params the function parameters
1092 /// @param type the function return type
1093 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001094 /// @param decorations the optional function decorations
1095 /// @param return_type_decorations the optional function return type
1096 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001097 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001098 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001099 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001100 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001101 ast::VariableList params,
1102 type::Type* type,
1103 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001104 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001105 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001106 auto* func =
1107 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1108 type, create<ast::BlockStatement>(body),
1109 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001110 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001111 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001112 }
1113
Ben Clayton42d1e092021-02-02 14:29:15 +00001114 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001115 /// @param name the function name
1116 /// @param params the function parameters
1117 /// @param type the function return type
1118 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001119 /// @param decorations the optional function decorations
1120 /// @param return_type_decorations the optional function return type
1121 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001122 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001123 template <typename NAME>
1124 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001125 ast::VariableList params,
1126 type::Type* type,
1127 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001128 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001129 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001130 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1131 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001132 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001133 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001134 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001135 }
1136
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001137 /// Creates an ast::ReturnStatement with the input args
1138 /// @param args arguments to construct a return statement with
1139 /// @returns the return statement pointer
1140 template <typename... Args>
1141 ast::ReturnStatement* Return(Args&&... args) {
1142 return create<ast::ReturnStatement>(std::forward<Args>(args)...);
1143 }
1144
Ben Claytond614dd52021-03-15 10:43:11 +00001145 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1146 /// the AST().ConstructedTypes().
1147 /// @param source the source information
1148 /// @param name the struct name
1149 /// @param members the struct members
1150 /// @param decorations the optional struct decorations
1151 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001152 template <typename NAME>
Ben Claytond614dd52021-03-15 10:43:11 +00001153 type::Struct* Structure(const Source& source,
Ben Clayton1d618b12021-04-09 16:19:48 +00001154 NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001155 ast::StructMemberList members,
1156 ast::DecorationList decorations = {}) {
1157 auto* impl =
1158 create<ast::Struct>(source, std::move(members), std::move(decorations));
Ben Clayton1d618b12021-04-09 16:19:48 +00001159 auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001160 AST().AddConstructedType(type);
1161 return type;
1162 }
1163
1164 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1165 /// the AST().ConstructedTypes().
1166 /// @param name the struct name
1167 /// @param members the struct members
1168 /// @param decorations the optional struct decorations
1169 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001170 template <typename NAME>
1171 type::Struct* Structure(NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001172 ast::StructMemberList members,
1173 ast::DecorationList decorations = {}) {
1174 auto* impl =
1175 create<ast::Struct>(std::move(members), std::move(decorations));
Ben Clayton1d618b12021-04-09 16:19:48 +00001176 auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001177 AST().AddConstructedType(type);
1178 return type;
1179 }
1180
Ben Clayton42d1e092021-02-02 14:29:15 +00001181 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001182 /// @param source the source information
1183 /// @param name the struct member name
1184 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001185 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001186 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001187 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001188 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001189 NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001190 type::Type* type,
1191 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001192 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1193 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001194 }
1195
Ben Clayton42d1e092021-02-02 14:29:15 +00001196 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001197 /// @param name the struct member name
1198 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001199 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001200 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001201 template <typename NAME>
1202 ast::StructMember* Member(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001203 type::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001204 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001205 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1206 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001207 }
1208
Ben Claytonbab31972021-02-08 21:16:21 +00001209 /// Creates a ast::StructMember with the given byte offset
1210 /// @param offset the offset to use in the StructMemberOffsetDecoration
1211 /// @param name the struct member name
1212 /// @param type the struct member type
1213 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001214 template <typename NAME>
1215 ast::StructMember* Member(uint32_t offset, NAME&& name, type::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001216 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001217 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001218 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001219 create<ast::StructMemberOffsetDecoration>(offset),
1220 });
1221 }
1222
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001223 /// Creates a ast::BlockStatement with input statements
1224 /// @param statements statements of block
1225 /// @returns the block statement pointer
1226 template <typename... Statements>
1227 ast::BlockStatement* Block(Statements&&... statements) {
1228 return create<ast::BlockStatement>(
1229 ast::StatementList{std::forward<Statements>(statements)...});
1230 }
1231
1232 /// Creates a ast::ElseStatement with input condition and body
1233 /// @param condition the else condition expression
1234 /// @param body the else body
1235 /// @returns the else statement pointer
1236 ast::ElseStatement* Else(ast::Expression* condition,
1237 ast::BlockStatement* body) {
1238 return create<ast::ElseStatement>(condition, body);
1239 }
1240
1241 /// Creates a ast::IfStatement with input condition, body, and optional
1242 /// variadic else statements
1243 /// @param condition the if statement condition expression
1244 /// @param body the if statement body
1245 /// @param elseStatements optional variadic else statements
1246 /// @returns the if statement pointer
1247 template <typename... ElseStatements>
1248 ast::IfStatement* If(ast::Expression* condition,
1249 ast::BlockStatement* body,
1250 ElseStatements&&... elseStatements) {
1251 return create<ast::IfStatement>(
1252 condition, body,
1253 ast::ElseStatementList{
1254 std::forward<ElseStatements>(elseStatements)...});
1255 }
1256
1257 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001258 /// @param lhs the left hand side expression initializer
1259 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001260 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001261 template <typename LhsExpressionInit, typename RhsExpressionInit>
1262 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1263 RhsExpressionInit&& rhs) {
1264 return create<ast::AssignmentStatement>(
1265 Expr(std::forward<LhsExpressionInit>(lhs)),
1266 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001267 }
1268
1269 /// Creates a ast::LoopStatement with input body and optional continuing
1270 /// @param body the loop body
1271 /// @param continuing the optional continuing block
1272 /// @returns the loop statement pointer
1273 ast::LoopStatement* Loop(ast::BlockStatement* body,
1274 ast::BlockStatement* continuing = nullptr) {
1275 return create<ast::LoopStatement>(body, continuing);
1276 }
1277
1278 /// Creates a ast::VariableDeclStatement for the input variable
1279 /// @param var the variable to wrap in a decl statement
1280 /// @returns the variable decl statement pointer
1281 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1282 return create<ast::VariableDeclStatement>(var);
1283 }
1284
Antonio Maioranocea744d2021-03-25 12:55:27 +00001285 /// Creates a ast::SwitchStatement with input expression and cases
1286 /// @param condition the condition expression initializer
1287 /// @param cases case statements
1288 /// @returns the switch statement pointer
1289 template <typename ExpressionInit, typename... Cases>
1290 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1291 return create<ast::SwitchStatement>(
1292 Expr(std::forward<ExpressionInit>(condition)),
1293 ast::CaseStatementList{std::forward<Cases>(cases)...});
1294 }
1295
1296 /// Creates a ast::CaseStatement with input list of selectors, and body
1297 /// @param selectors list of selectors
1298 /// @param body the case body
1299 /// @returns the case statement pointer
1300 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1301 ast::BlockStatement* body = nullptr) {
1302 return create<ast::CaseStatement>(std::move(selectors),
1303 body ? body : Block());
1304 }
1305
1306 /// Convenient overload that takes a single selector
1307 /// @param selector a single case selector
1308 /// @param body the case body
1309 /// @returns the case statement pointer
1310 ast::CaseStatement* Case(ast::IntLiteral* selector,
1311 ast::BlockStatement* body = nullptr) {
1312 return Case(ast::CaseSelectorList{selector}, body);
1313 }
1314
1315 /// Convenience function that creates a 'default' ast::CaseStatement
1316 /// @param body the case body
1317 /// @returns the case statement pointer
1318 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1319 return Case(ast::CaseSelectorList{}, body);
1320 }
1321
James Price68f558f2021-04-06 15:51:47 +00001322 /// Creates an ast::BuiltinDecoration
1323 /// @param source the source information
1324 /// @param builtin the builtin value
1325 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001326 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001327 return create<ast::BuiltinDecoration>(source, builtin);
1328 }
1329
1330 /// Creates an ast::BuiltinDecoration
1331 /// @param builtin the builtin value
1332 /// @returns the builtin decoration pointer
1333 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1334 return create<ast::BuiltinDecoration>(source_, builtin);
1335 }
1336
1337 /// Creates an ast::LocationDecoration
1338 /// @param source the source information
1339 /// @param location the location value
1340 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001341 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001342 return create<ast::LocationDecoration>(source, location);
1343 }
1344
1345 /// Creates an ast::LocationDecoration
1346 /// @param location the location value
1347 /// @returns the location decoration pointer
1348 ast::LocationDecoration* Location(uint32_t location) {
1349 return create<ast::LocationDecoration>(source_, location);
1350 }
1351
1352 /// Creates an ast::StageDecoration
1353 /// @param source the source information
1354 /// @param stage the pipeline stage
1355 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001356 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001357 return create<ast::StageDecoration>(source, stage);
1358 }
1359
1360 /// Creates an ast::StageDecoration
1361 /// @param stage the pipeline stage
1362 /// @returns the stage decoration pointer
1363 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1364 return create<ast::StageDecoration>(source_, stage);
1365 }
1366
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001367 /// Sets the current builder source to `src`
1368 /// @param src the Source used for future create() calls
1369 void SetSource(const Source& src) {
1370 AssertNotMoved();
1371 source_ = src;
1372 }
1373
1374 /// Sets the current builder source to `loc`
1375 /// @param loc the Source used for future create() calls
1376 void SetSource(const Source::Location& loc) {
1377 AssertNotMoved();
1378 source_ = Source(loc);
1379 }
1380
Ben Clayton33352542021-01-29 16:43:41 +00001381 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001382 /// @note As the Resolver is run when the Program is built, this will only be
1383 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001384 /// @param expr the AST expression
1385 /// @return the resolved semantic type for the expression, or nullptr if the
1386 /// expression has no resolved type.
1387 type::Type* TypeOf(ast::Expression* expr) const;
1388
Ben Clayton401b96b2021-02-03 17:19:59 +00001389 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001390 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001391 /// nodes.
1392 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1393 /// @return the ast::Statement that wraps the ast::Expression
1394 ast::Statement* WrapInStatement(ast::Expression* expr);
1395 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001396 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001397 /// these nodes.
1398 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1399 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1400 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1401 /// Returns the statement argument. Used as a passthrough-overload by
1402 /// WrapInFunction().
1403 /// @param stmt the ast::Statement
1404 /// @return `stmt`
1405 ast::Statement* WrapInStatement(ast::Statement* stmt);
1406 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001407 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001408 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001409 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001410 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001411 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001412 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001413 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001414 }
1415 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001416 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001417 /// @returns the function
1418 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001419
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001420 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001421 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001422
1423 protected:
1424 /// Asserts that the builder has not been moved.
1425 void AssertNotMoved() const;
1426
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001427 private:
1428 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001429 ASTNodeAllocator ast_nodes_;
1430 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001431 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001432 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001433 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001434 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001435
1436 /// The source to use when creating AST nodes without providing a Source as
1437 /// the first argument.
1438 Source source_;
1439
Ben Clayton5f0ea112021-03-09 10:54:37 +00001440 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001441 /// program when built.
1442 bool resolve_on_build_ = true;
1443
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001444 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1445 bool moved_ = false;
1446};
1447
1448//! @cond Doxygen_Suppress
1449// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1450template <>
1451struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1452 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1453 return t->i32();
1454 }
1455};
1456template <>
1457struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1458 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1459 return t->u32();
1460 }
1461};
1462template <>
1463struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1464 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1465 return t->f32();
1466 }
1467};
1468template <>
1469struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1470 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1471 return t->bool_();
1472 }
1473};
1474template <>
1475struct ProgramBuilder::TypesBuilder::CToAST<void> {
1476 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1477 return t->void_();
1478 }
1479};
1480//! @endcond
1481
1482} // namespace tint
1483
1484#endif // SRC_PROGRAM_BUILDER_H_