blob: 5fddb560c3597ff8e83482764a310ef49a1f3186 [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
Ben Clayton1d618b12021-04-09 16:19:48 +0000448 template <typename NAME>
449 type::Alias* alias(NAME&& name, type::Type* type) const {
450 return builder->create<type::Alias>(
451 builder->Sym(std::forward<NAME>(name)), type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000452 }
453
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000454 /// @return the tint AST pointer to `type` with the given ast::StorageClass
455 /// @param type the type of the pointer
456 /// @param storage_class the storage class of the pointer
457 type::Pointer* pointer(type::Type* type,
458 ast::StorageClass storage_class) const {
459 return builder->create<type::Pointer>(type, storage_class);
460 }
461
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000462 /// @return the tint AST pointer to type `T` with the given
463 /// ast::StorageClass.
464 /// @param storage_class the storage class of the pointer
465 template <typename T>
466 type::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000467 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000468 }
469
470 /// @param name the struct name
471 /// @param impl the struct implementation
472 /// @returns a struct pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000473 template <typename NAME>
474 type::Struct* struct_(NAME&& name, ast::Struct* impl) const {
475 return builder->create<type::Struct>(
476 builder->Sym(std::forward<NAME>(name)), impl);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000477 }
478
479 private:
480 /// CToAST<T> is specialized for various `T` types and each specialization
481 /// contains a single static `get()` method for obtaining the corresponding
482 /// AST type for the C type `T`.
483 /// `get()` has the signature:
484 /// `static type::Type* get(Types* t)`
485 template <typename T>
486 struct CToAST {};
487
Ben Claytonc7ca7662021-02-17 16:23:52 +0000488 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000489 };
490
491 //////////////////////////////////////////////////////////////////////////////
492 // AST helper methods
493 //////////////////////////////////////////////////////////////////////////////
494
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000495 /// @param name the symbol string
496 /// @return a Symbol with the given name
497 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
498
499 /// @param sym the symbol
500 /// @return `sym`
501 Symbol Sym(Symbol sym) { return sym; }
502
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000503 /// @param expr the expression
504 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000505 template <typename T>
506 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
507 return expr;
508 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000509
510 /// @param name the identifier name
511 /// @return an ast::IdentifierExpression with the given name
512 ast::IdentifierExpression* Expr(const std::string& name) {
513 return create<ast::IdentifierExpression>(Symbols().Register(name));
514 }
515
Ben Clayton46d78d72021-02-10 21:34:26 +0000516 /// @param symbol the identifier symbol
517 /// @return an ast::IdentifierExpression with the given symbol
518 ast::IdentifierExpression* Expr(Symbol symbol) {
519 return create<ast::IdentifierExpression>(symbol);
520 }
521
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000522 /// @param source the source information
523 /// @param name the identifier name
524 /// @return an ast::IdentifierExpression with the given name
525 ast::IdentifierExpression* Expr(const Source& source,
526 const std::string& name) {
527 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
528 }
529
530 /// @param name the identifier name
531 /// @return an ast::IdentifierExpression with the given name
532 ast::IdentifierExpression* Expr(const char* name) {
533 return create<ast::IdentifierExpression>(Symbols().Register(name));
534 }
535
536 /// @param value the boolean value
537 /// @return a Scalar constructor for the given value
538 ast::ScalarConstructorExpression* Expr(bool value) {
539 return create<ast::ScalarConstructorExpression>(Literal(value));
540 }
541
542 /// @param value the float value
543 /// @return a Scalar constructor for the given value
544 ast::ScalarConstructorExpression* Expr(f32 value) {
545 return create<ast::ScalarConstructorExpression>(Literal(value));
546 }
547
548 /// @param value the integer value
549 /// @return a Scalar constructor for the given value
550 ast::ScalarConstructorExpression* Expr(i32 value) {
551 return create<ast::ScalarConstructorExpression>(Literal(value));
552 }
553
554 /// @param value the unsigned int value
555 /// @return a Scalar constructor for the given value
556 ast::ScalarConstructorExpression* Expr(u32 value) {
557 return create<ast::ScalarConstructorExpression>(Literal(value));
558 }
559
560 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
561 /// `list`.
562 /// @param list the list to append too
563 /// @param arg the arg to create
564 template <typename ARG>
565 void Append(ast::ExpressionList& list, ARG&& arg) {
566 list.emplace_back(Expr(std::forward<ARG>(arg)));
567 }
568
569 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
570 /// then appends them to `list`.
571 /// @param list the list to append too
572 /// @param arg0 the first argument
573 /// @param args the rest of the arguments
574 template <typename ARG0, typename... ARGS>
575 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
576 Append(list, std::forward<ARG0>(arg0));
577 Append(list, std::forward<ARGS>(args)...);
578 }
579
580 /// @return an empty list of expressions
581 ast::ExpressionList ExprList() { return {}; }
582
583 /// @param args the list of expressions
584 /// @return the list of expressions converted to `ast::Expression`s using
585 /// `Expr()`,
586 template <typename... ARGS>
587 ast::ExpressionList ExprList(ARGS&&... args) {
588 ast::ExpressionList list;
589 list.reserve(sizeof...(args));
590 Append(list, std::forward<ARGS>(args)...);
591 return list;
592 }
593
594 /// @param list the list of expressions
595 /// @return `list`
596 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
597
598 /// @param val the boolan value
599 /// @return a boolean literal with the given value
600 ast::BoolLiteral* Literal(bool val) {
601 return create<ast::BoolLiteral>(ty.bool_(), val);
602 }
603
604 /// @param val the float value
605 /// @return a float literal with the given value
606 ast::FloatLiteral* Literal(f32 val) {
607 return create<ast::FloatLiteral>(ty.f32(), val);
608 }
609
610 /// @param val the unsigned int value
611 /// @return a ast::UintLiteral with the given value
612 ast::UintLiteral* Literal(u32 val) {
613 return create<ast::UintLiteral>(ty.u32(), val);
614 }
615
616 /// @param val the integer value
617 /// @return the ast::SintLiteral with the given value
618 ast::SintLiteral* Literal(i32 val) {
619 return create<ast::SintLiteral>(ty.i32(), val);
620 }
621
622 /// @param args the arguments for the type constructor
623 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
624 /// of `args` converted to `ast::Expression`s using `Expr()`
625 template <typename T, typename... ARGS>
626 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
627 return create<ast::TypeConstructorExpression>(
628 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
629 }
630
631 /// @param type the type to construct
632 /// @param args the arguments for the constructor
633 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
634 /// values `args`.
635 template <typename... ARGS>
636 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
637 return create<ast::TypeConstructorExpression>(
638 type, ExprList(std::forward<ARGS>(args)...));
639 }
640
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000641 /// Creates a constructor expression that constructs an object of
642 /// `type` filled with `elem_value`. For example,
643 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
644 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
645 /// @param type the type to construct
646 /// @param elem_value the initial or element value (for vec and mat) to
647 /// construct with
648 /// @return the constructor expression
649 ast::ConstructorExpression* ConstructValueFilledWith(type::Type* type,
650 int elem_value = 0);
651
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000652 /// @param args the arguments for the vector constructor
653 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
654 /// `T`, constructed with the values `args`.
655 template <typename T, typename... ARGS>
656 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
657 return create<ast::TypeConstructorExpression>(
658 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
659 }
660
661 /// @param args the arguments for the vector constructor
662 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
663 /// `T`, constructed with the values `args`.
664 template <typename T, typename... ARGS>
665 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
666 return create<ast::TypeConstructorExpression>(
667 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
668 }
669
670 /// @param args the arguments for the vector constructor
671 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
672 /// `T`, constructed with the values `args`.
673 template <typename T, typename... ARGS>
674 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
675 return create<ast::TypeConstructorExpression>(
676 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
677 }
678
679 /// @param args the arguments for the matrix constructor
680 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
681 /// `T`, constructed with the values `args`.
682 template <typename T, typename... ARGS>
683 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
684 return create<ast::TypeConstructorExpression>(
685 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
686 }
687
688 /// @param args the arguments for the matrix constructor
689 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
690 /// `T`, constructed with the values `args`.
691 template <typename T, typename... ARGS>
692 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
693 return create<ast::TypeConstructorExpression>(
694 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
695 }
696
697 /// @param args the arguments for the matrix constructor
698 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
699 /// `T`, constructed with the values `args`.
700 template <typename T, typename... ARGS>
701 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
702 return create<ast::TypeConstructorExpression>(
703 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
704 }
705
706 /// @param args the arguments for the matrix constructor
707 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
708 /// `T`, constructed with the values `args`.
709 template <typename T, typename... ARGS>
710 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
711 return create<ast::TypeConstructorExpression>(
712 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
713 }
714
715 /// @param args the arguments for the matrix constructor
716 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
717 /// `T`, constructed with the values `args`.
718 template <typename T, typename... ARGS>
719 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
720 return create<ast::TypeConstructorExpression>(
721 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
722 }
723
724 /// @param args the arguments for the matrix constructor
725 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
726 /// `T`, constructed with the values `args`.
727 template <typename T, typename... ARGS>
728 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
729 return create<ast::TypeConstructorExpression>(
730 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
731 }
732
733 /// @param args the arguments for the matrix constructor
734 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
735 /// `T`, constructed with the values `args`.
736 template <typename T, typename... ARGS>
737 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
738 return create<ast::TypeConstructorExpression>(
739 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
740 }
741
742 /// @param args the arguments for the matrix constructor
743 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
744 /// `T`, constructed with the values `args`.
745 template <typename T, typename... ARGS>
746 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
747 return create<ast::TypeConstructorExpression>(
748 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
749 }
750
751 /// @param args the arguments for the matrix constructor
752 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
753 /// `T`, constructed with the values `args`.
754 template <typename T, typename... ARGS>
755 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
756 return create<ast::TypeConstructorExpression>(
757 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
758 }
759
760 /// @param args the arguments for the array constructor
761 /// @return an `ast::TypeConstructorExpression` of an array with element type
762 /// `T`, constructed with the values `args`.
763 template <typename T, int N = 0, typename... ARGS>
764 ast::TypeConstructorExpression* array(ARGS&&... args) {
765 return create<ast::TypeConstructorExpression>(
766 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
767 }
768
769 /// @param subtype the array element type
770 /// @param n the array size. 0 represents a runtime-array.
771 /// @param args the arguments for the array constructor
772 /// @return an `ast::TypeConstructorExpression` of an array with element type
773 /// `subtype`, constructed with the values `args`.
774 template <typename... ARGS>
775 ast::TypeConstructorExpression* array(type::Type* subtype,
776 uint32_t n,
777 ARGS&&... args) {
778 return create<ast::TypeConstructorExpression>(
779 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
780 }
781
782 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000783 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000784 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000785 /// @param constructor constructor expression
786 /// @param decorations variable decorations
787 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000788 template <typename NAME>
789 ast::Variable* Var(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000790 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000791 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000792 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000793 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000794 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
795 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000796 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000797
798 /// @param source the variable source
799 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000800 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000801 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000802 /// @param constructor constructor expression
803 /// @param decorations variable decorations
804 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000805 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000806 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000807 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000808 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000809 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000810 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000811 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000812 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000813 type, false, constructor, decorations);
814 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000815
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000816 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000817 /// @param type the variable type
818 /// @param constructor optional constructor expression
819 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000820 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000821 template <typename NAME>
822 ast::Variable* Const(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000823 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000824 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000825 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000826 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000827 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000828 constructor, decorations);
829 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000830
831 /// @param source the variable source
832 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000833 /// @param type the variable type
834 /// @param constructor optional constructor expression
835 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000836 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000837 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000838 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000839 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000840 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000841 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000842 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000843 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000844 ast::StorageClass::kNone, type, true,
845 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000846 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000847
James Pricedaf1f1c2021-04-08 22:15:48 +0000848 /// @param name the parameter name
849 /// @param type the parameter type
850 /// @param decorations optional parameter decorations
851 /// @returns a constant `ast::Variable` with the given name and type
852 template <typename NAME>
853 ast::Variable* Param(NAME&& name,
854 type::Type* type,
855 ast::DecorationList decorations = {}) {
856 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
857 ast::StorageClass::kNone, type, true, nullptr,
858 decorations);
859 }
860
861 /// @param source the parameter source
862 /// @param name the parameter name
863 /// @param type the parameter type
864 /// @param decorations optional parameter decorations
865 /// @returns a constant `ast::Variable` with the given name and type
866 template <typename NAME>
867 ast::Variable* Param(const Source& source,
868 NAME&& name,
869 type::Type* type,
870 ast::DecorationList decorations = {}) {
871 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
872 ast::StorageClass::kNone, type, true, nullptr,
873 decorations);
874 }
875
Ben Clayton401b96b2021-02-03 17:19:59 +0000876 /// @param args the arguments to pass to Var()
877 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
878 /// of `args`, which is automatically registered as a global variable with the
879 /// ast::Module.
880 template <typename... ARGS>
881 ast::Variable* Global(ARGS&&... args) {
882 auto* var = Var(std::forward<ARGS>(args)...);
883 AST().AddGlobalVariable(var);
884 return var;
885 }
886
887 /// @param args the arguments to pass to Const()
888 /// @returns a const `ast::Variable` constructed by calling Var() with the
889 /// arguments of `args`, which is automatically registered as a global
890 /// variable with the ast::Module.
891 template <typename... ARGS>
892 ast::Variable* GlobalConst(ARGS&&... args) {
893 auto* var = Const(std::forward<ARGS>(args)...);
894 AST().AddGlobalVariable(var);
895 return var;
896 }
897
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000898 /// @param func the function name
899 /// @param args the function call arguments
900 /// @returns a `ast::CallExpression` to the function `func`, with the
901 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
902 template <typename NAME, typename... ARGS>
903 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
904 return create<ast::CallExpression>(Expr(func),
905 ExprList(std::forward<ARGS>(args)...));
906 }
907
908 /// @param lhs the left hand argument to the addition operation
909 /// @param rhs the right hand argument to the addition operation
910 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
911 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000912 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000913 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
914 Expr(std::forward<LHS>(lhs)),
915 Expr(std::forward<RHS>(rhs)));
916 }
917
918 /// @param lhs the left hand argument to the subtraction operation
919 /// @param rhs the right hand argument to the subtraction operation
920 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
921 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000922 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000923 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
924 Expr(std::forward<LHS>(lhs)),
925 Expr(std::forward<RHS>(rhs)));
926 }
927
928 /// @param lhs the left hand argument to the multiplication operation
929 /// @param rhs the right hand argument to the multiplication operation
930 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
931 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000932 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000933 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
934 Expr(std::forward<LHS>(lhs)),
935 Expr(std::forward<RHS>(rhs)));
936 }
937
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000938 /// @param source the source information
939 /// @param lhs the left hand argument to the multiplication operation
940 /// @param rhs the right hand argument to the multiplication operation
941 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
942 template <typename LHS, typename RHS>
943 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
944 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
945 Expr(std::forward<LHS>(lhs)),
946 Expr(std::forward<RHS>(rhs)));
947 }
948
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000949 /// @param lhs the left hand argument to the division operation
950 /// @param rhs the right hand argument to the division operation
951 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
952 template <typename LHS, typename RHS>
953 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
954 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
955 Expr(std::forward<LHS>(lhs)),
956 Expr(std::forward<RHS>(rhs)));
957 }
958
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000959 /// @param arr the array argument for the array accessor expression
960 /// @param idx the index argument for the array accessor expression
961 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
962 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000963 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000964 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
965 Expr(std::forward<IDX>(idx)));
966 }
967
968 /// @param obj the object for the member accessor expression
969 /// @param idx the index argument for the array accessor expression
970 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
971 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +0000972 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000973 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
974 Expr(std::forward<IDX>(idx)));
975 }
976
Ben Clayton42d1e092021-02-02 14:29:15 +0000977 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000978 /// @param val the offset value
979 /// @returns the offset decoration pointer
980 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
981 return create<ast::StructMemberOffsetDecoration>(source_, val);
982 }
983
Ben Claytond614dd52021-03-15 10:43:11 +0000984 /// Creates a ast::StructMemberSizeDecoration
985 /// @param source the source information
986 /// @param val the size value
987 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +0000988 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
989 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +0000990 return create<ast::StructMemberSizeDecoration>(source, val);
991 }
992
993 /// Creates a ast::StructMemberSizeDecoration
994 /// @param val the size value
995 /// @returns the size decoration pointer
996 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
997 return create<ast::StructMemberSizeDecoration>(source_, val);
998 }
999
1000 /// Creates a ast::StructMemberAlignDecoration
1001 /// @param source the source information
1002 /// @param val the align value
1003 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001004 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1005 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001006 return create<ast::StructMemberAlignDecoration>(source, val);
1007 }
1008
1009 /// Creates a ast::StructMemberAlignDecoration
1010 /// @param val the align value
1011 /// @returns the align decoration pointer
1012 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1013 return create<ast::StructMemberAlignDecoration>(source_, val);
1014 }
1015
Ben Clayton42d1e092021-02-02 14:29:15 +00001016 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001017 /// @param source the source information
1018 /// @param name the function name
1019 /// @param params the function parameters
1020 /// @param type the function return type
1021 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001022 /// @param decorations the optional function decorations
1023 /// @param return_type_decorations the optional function return type
1024 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001025 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001026 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001027 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001028 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001029 ast::VariableList params,
1030 type::Type* type,
1031 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001032 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001033 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001034 auto* func =
1035 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1036 type, create<ast::BlockStatement>(body),
1037 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001038 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001039 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001040 }
1041
Ben Clayton42d1e092021-02-02 14:29:15 +00001042 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001043 /// @param name the function name
1044 /// @param params the function parameters
1045 /// @param type the function return type
1046 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001047 /// @param decorations the optional function decorations
1048 /// @param return_type_decorations the optional function return type
1049 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001050 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001051 template <typename NAME>
1052 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001053 ast::VariableList params,
1054 type::Type* type,
1055 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001056 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001057 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001058 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1059 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001060 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001061 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001062 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001063 }
1064
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001065 /// Creates an ast::ReturnStatement with the input args
1066 /// @param args arguments to construct a return statement with
1067 /// @returns the return statement pointer
1068 template <typename... Args>
1069 ast::ReturnStatement* Return(Args&&... args) {
1070 return create<ast::ReturnStatement>(std::forward<Args>(args)...);
1071 }
1072
Ben Claytond614dd52021-03-15 10:43:11 +00001073 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1074 /// the AST().ConstructedTypes().
1075 /// @param source the source information
1076 /// @param name the struct name
1077 /// @param members the struct members
1078 /// @param decorations the optional struct decorations
1079 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001080 template <typename NAME>
Ben Claytond614dd52021-03-15 10:43:11 +00001081 type::Struct* Structure(const Source& source,
Ben Clayton1d618b12021-04-09 16:19:48 +00001082 NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001083 ast::StructMemberList members,
1084 ast::DecorationList decorations = {}) {
1085 auto* impl =
1086 create<ast::Struct>(source, std::move(members), std::move(decorations));
Ben Clayton1d618b12021-04-09 16:19:48 +00001087 auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001088 AST().AddConstructedType(type);
1089 return type;
1090 }
1091
1092 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1093 /// the AST().ConstructedTypes().
1094 /// @param name the struct name
1095 /// @param members the struct members
1096 /// @param decorations the optional struct decorations
1097 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001098 template <typename NAME>
1099 type::Struct* Structure(NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001100 ast::StructMemberList members,
1101 ast::DecorationList decorations = {}) {
1102 auto* impl =
1103 create<ast::Struct>(std::move(members), std::move(decorations));
Ben Clayton1d618b12021-04-09 16:19:48 +00001104 auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001105 AST().AddConstructedType(type);
1106 return type;
1107 }
1108
Ben Clayton42d1e092021-02-02 14:29:15 +00001109 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001110 /// @param source the source information
1111 /// @param name the struct member name
1112 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001113 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001114 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001115 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001116 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001117 NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001118 type::Type* type,
1119 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001120 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1121 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001122 }
1123
Ben Clayton42d1e092021-02-02 14:29:15 +00001124 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001125 /// @param name the struct member name
1126 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001127 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001128 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001129 template <typename NAME>
1130 ast::StructMember* Member(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001131 type::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001132 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001133 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1134 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001135 }
1136
Ben Claytonbab31972021-02-08 21:16:21 +00001137 /// Creates a ast::StructMember with the given byte offset
1138 /// @param offset the offset to use in the StructMemberOffsetDecoration
1139 /// @param name the struct member name
1140 /// @param type the struct member type
1141 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001142 template <typename NAME>
1143 ast::StructMember* Member(uint32_t offset, NAME&& name, type::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001144 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001145 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001146 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001147 create<ast::StructMemberOffsetDecoration>(offset),
1148 });
1149 }
1150
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001151 /// Creates a ast::BlockStatement with input statements
1152 /// @param statements statements of block
1153 /// @returns the block statement pointer
1154 template <typename... Statements>
1155 ast::BlockStatement* Block(Statements&&... statements) {
1156 return create<ast::BlockStatement>(
1157 ast::StatementList{std::forward<Statements>(statements)...});
1158 }
1159
1160 /// Creates a ast::ElseStatement with input condition and body
1161 /// @param condition the else condition expression
1162 /// @param body the else body
1163 /// @returns the else statement pointer
1164 ast::ElseStatement* Else(ast::Expression* condition,
1165 ast::BlockStatement* body) {
1166 return create<ast::ElseStatement>(condition, body);
1167 }
1168
1169 /// Creates a ast::IfStatement with input condition, body, and optional
1170 /// variadic else statements
1171 /// @param condition the if statement condition expression
1172 /// @param body the if statement body
1173 /// @param elseStatements optional variadic else statements
1174 /// @returns the if statement pointer
1175 template <typename... ElseStatements>
1176 ast::IfStatement* If(ast::Expression* condition,
1177 ast::BlockStatement* body,
1178 ElseStatements&&... elseStatements) {
1179 return create<ast::IfStatement>(
1180 condition, body,
1181 ast::ElseStatementList{
1182 std::forward<ElseStatements>(elseStatements)...});
1183 }
1184
1185 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001186 /// @param lhs the left hand side expression initializer
1187 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001188 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001189 template <typename LhsExpressionInit, typename RhsExpressionInit>
1190 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1191 RhsExpressionInit&& rhs) {
1192 return create<ast::AssignmentStatement>(
1193 Expr(std::forward<LhsExpressionInit>(lhs)),
1194 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001195 }
1196
1197 /// Creates a ast::LoopStatement with input body and optional continuing
1198 /// @param body the loop body
1199 /// @param continuing the optional continuing block
1200 /// @returns the loop statement pointer
1201 ast::LoopStatement* Loop(ast::BlockStatement* body,
1202 ast::BlockStatement* continuing = nullptr) {
1203 return create<ast::LoopStatement>(body, continuing);
1204 }
1205
1206 /// Creates a ast::VariableDeclStatement for the input variable
1207 /// @param var the variable to wrap in a decl statement
1208 /// @returns the variable decl statement pointer
1209 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1210 return create<ast::VariableDeclStatement>(var);
1211 }
1212
Antonio Maioranocea744d2021-03-25 12:55:27 +00001213 /// Creates a ast::SwitchStatement with input expression and cases
1214 /// @param condition the condition expression initializer
1215 /// @param cases case statements
1216 /// @returns the switch statement pointer
1217 template <typename ExpressionInit, typename... Cases>
1218 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1219 return create<ast::SwitchStatement>(
1220 Expr(std::forward<ExpressionInit>(condition)),
1221 ast::CaseStatementList{std::forward<Cases>(cases)...});
1222 }
1223
1224 /// Creates a ast::CaseStatement with input list of selectors, and body
1225 /// @param selectors list of selectors
1226 /// @param body the case body
1227 /// @returns the case statement pointer
1228 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1229 ast::BlockStatement* body = nullptr) {
1230 return create<ast::CaseStatement>(std::move(selectors),
1231 body ? body : Block());
1232 }
1233
1234 /// Convenient overload that takes a single selector
1235 /// @param selector a single case selector
1236 /// @param body the case body
1237 /// @returns the case statement pointer
1238 ast::CaseStatement* Case(ast::IntLiteral* selector,
1239 ast::BlockStatement* body = nullptr) {
1240 return Case(ast::CaseSelectorList{selector}, body);
1241 }
1242
1243 /// Convenience function that creates a 'default' ast::CaseStatement
1244 /// @param body the case body
1245 /// @returns the case statement pointer
1246 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1247 return Case(ast::CaseSelectorList{}, body);
1248 }
1249
James Price68f558f2021-04-06 15:51:47 +00001250 /// Creates an ast::BuiltinDecoration
1251 /// @param source the source information
1252 /// @param builtin the builtin value
1253 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001254 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001255 return create<ast::BuiltinDecoration>(source, builtin);
1256 }
1257
1258 /// Creates an ast::BuiltinDecoration
1259 /// @param builtin the builtin value
1260 /// @returns the builtin decoration pointer
1261 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1262 return create<ast::BuiltinDecoration>(source_, builtin);
1263 }
1264
1265 /// Creates an ast::LocationDecoration
1266 /// @param source the source information
1267 /// @param location the location value
1268 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001269 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001270 return create<ast::LocationDecoration>(source, location);
1271 }
1272
1273 /// Creates an ast::LocationDecoration
1274 /// @param location the location value
1275 /// @returns the location decoration pointer
1276 ast::LocationDecoration* Location(uint32_t location) {
1277 return create<ast::LocationDecoration>(source_, location);
1278 }
1279
1280 /// Creates an ast::StageDecoration
1281 /// @param source the source information
1282 /// @param stage the pipeline stage
1283 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001284 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001285 return create<ast::StageDecoration>(source, stage);
1286 }
1287
1288 /// Creates an ast::StageDecoration
1289 /// @param stage the pipeline stage
1290 /// @returns the stage decoration pointer
1291 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1292 return create<ast::StageDecoration>(source_, stage);
1293 }
1294
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001295 /// Sets the current builder source to `src`
1296 /// @param src the Source used for future create() calls
1297 void SetSource(const Source& src) {
1298 AssertNotMoved();
1299 source_ = src;
1300 }
1301
1302 /// Sets the current builder source to `loc`
1303 /// @param loc the Source used for future create() calls
1304 void SetSource(const Source::Location& loc) {
1305 AssertNotMoved();
1306 source_ = Source(loc);
1307 }
1308
Ben Clayton33352542021-01-29 16:43:41 +00001309 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001310 /// @note As the Resolver is run when the Program is built, this will only be
1311 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001312 /// @param expr the AST expression
1313 /// @return the resolved semantic type for the expression, or nullptr if the
1314 /// expression has no resolved type.
1315 type::Type* TypeOf(ast::Expression* expr) const;
1316
Ben Clayton401b96b2021-02-03 17:19:59 +00001317 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001318 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001319 /// nodes.
1320 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1321 /// @return the ast::Statement that wraps the ast::Expression
1322 ast::Statement* WrapInStatement(ast::Expression* expr);
1323 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001324 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001325 /// these nodes.
1326 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1327 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1328 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1329 /// Returns the statement argument. Used as a passthrough-overload by
1330 /// WrapInFunction().
1331 /// @param stmt the ast::Statement
1332 /// @return `stmt`
1333 ast::Statement* WrapInStatement(ast::Statement* stmt);
1334 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001335 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001336 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001337 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001338 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001339 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001340 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001341 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001342 }
1343 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001344 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001345 /// @returns the function
1346 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001347
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001348 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001349 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001350
1351 protected:
1352 /// Asserts that the builder has not been moved.
1353 void AssertNotMoved() const;
1354
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001355 private:
1356 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001357 ASTNodeAllocator ast_nodes_;
1358 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001359 ast::Module* ast_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +00001360 semantic::Info sem_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001361 SymbolTable symbols_;
Ben Clayton844217f2021-01-27 18:49:05 +00001362 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001363
1364 /// The source to use when creating AST nodes without providing a Source as
1365 /// the first argument.
1366 Source source_;
1367
Ben Clayton5f0ea112021-03-09 10:54:37 +00001368 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001369 /// program when built.
1370 bool resolve_on_build_ = true;
1371
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001372 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1373 bool moved_ = false;
1374};
1375
1376//! @cond Doxygen_Suppress
1377// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1378template <>
1379struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1380 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1381 return t->i32();
1382 }
1383};
1384template <>
1385struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1386 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1387 return t->u32();
1388 }
1389};
1390template <>
1391struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1392 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1393 return t->f32();
1394 }
1395};
1396template <>
1397struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1398 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1399 return t->bool_();
1400 }
1401};
1402template <>
1403struct ProgramBuilder::TypesBuilder::CToAST<void> {
1404 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1405 return t->void_();
1406 }
1407};
1408//! @endcond
1409
1410} // namespace tint
1411
1412#endif // SRC_PROGRAM_BUILDER_H_