blob: 70b356d9483a0e9ae5c2ece92835ccb68990dea7 [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>
Ben Claytonb5cd10c2021-06-25 10:26:26 +000019#include <unordered_set>
Ben Claytona6b9a8e2021-01-26 16:57:10 +000020#include <utility>
21
Ben Claytone204f272021-04-22 14:40:23 +000022#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000023#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000024#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000025#include "src/ast/assignment_statement.h"
Ben Clayton8c7f0da2021-06-17 15:48:39 +000026#include "src/ast/atomic.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/binary_expression.h"
Antonio Maiorano60dae242021-07-15 19:09:25 +000028#include "src/ast/binding_decoration.h"
Ben Claytond9603282021-06-29 12:37:45 +000029#include "src/ast/bitcast_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000030#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000031#include "src/ast/bool_literal.h"
32#include "src/ast/call_expression.h"
Ben Clayton7fe01062021-06-11 13:22:27 +000033#include "src/ast/call_statement.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000034#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000035#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000036#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000037#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/float_literal.h"
Ben Clayton65cd2592021-07-02 19:27:42 +000039#include "src/ast/for_loop_statement.h"
Ben Claytona922e652021-04-21 13:37:22 +000040#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000041#include "src/ast/if_statement.h"
James Price989a8e42021-06-28 23:04:43 +000042#include "src/ast/interpolate_decoration.h"
James Price508d2492021-07-12 12:12:32 +000043#include "src/ast/invariant_decoration.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000044#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000045#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000046#include "src/ast/member_accessor_expression.h"
47#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000048#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000049#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000050#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000051#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000052#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000053#include "src/ast/scalar_constructor_expression.h"
54#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000055#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000056#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000057#include "src/ast/stride_decoration.h"
Antonio Maiorano60dae242021-07-15 19:09:25 +000058#include "src/ast/struct_block_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000059#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000060#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000061#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000062#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000064#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000065#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000066#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000067#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000068#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000069#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000070#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000071#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000072#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000073#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000074#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000075#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000076#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000077#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000078#include "src/sem/f32_type.h"
79#include "src/sem/i32_type.h"
80#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000081#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000082#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000083#include "src/sem/sampled_texture_type.h"
84#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000085#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000086#include "src/sem/u32_type.h"
87#include "src/sem/vector_type.h"
88#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000089
Ben Clayton03d10722021-06-05 12:45:50 +000090#ifdef INCLUDE_TINT_TINT_H_
91#error "internal tint header being #included from tint.h"
92#endif
93
Ben Clayton401b96b2021-02-03 17:19:59 +000094// Forward declarations
Ben Claytonb5cd10c2021-06-25 10:26:26 +000095namespace tint {
Ben Clayton401b96b2021-02-03 17:19:59 +000096namespace ast {
97class VariableDeclStatement;
98} // namespace ast
Ben Claytonb5cd10c2021-06-25 10:26:26 +000099} // namespace tint
Ben Clayton401b96b2021-02-03 17:19:59 +0000100
Ben Claytonb5cd10c2021-06-25 10:26:26 +0000101namespace tint {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000102class CloneContext;
103
104/// ProgramBuilder is a mutable builder for a Program.
105/// To construct a Program, populate the builder and then `std::move` it to a
106/// Program.
107class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +0000108 /// VarOptionals is a helper for accepting a number of optional, extra
109 /// arguments for Var() and Global().
110 struct VarOptionals {
111 template <typename... ARGS>
112 explicit VarOptionals(ARGS&&... args) {
113 Apply(std::forward<ARGS>(args)...);
114 }
115 ~VarOptionals();
116
117 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000118 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000119 ast::Expression* constructor = nullptr;
120 ast::DecorationList decorations = {};
121
122 private:
123 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000124 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000125 void Set(ast::Expression* c) { constructor = c; }
126 void Set(const ast::DecorationList& l) { decorations = l; }
127
128 template <typename FIRST, typename... ARGS>
129 void Apply(FIRST&& first, ARGS&&... args) {
130 Set(std::forward<FIRST>(first));
131 Apply(std::forward<ARGS>(args)...);
132 }
133 void Apply() {}
134 };
135
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000136 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000137 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
138 using ASTNodeAllocator = BlockAllocator<ast::Node>;
139
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000140 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
141 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000142
143 /// `i32` is a type alias to `int`.
144 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
145 /// WGSL syntax.
146 /// Note: this is intentionally not aliased to uint32_t as we want integer
147 /// literals passed to the builder to match WGSL's integer literal types.
148 using i32 = decltype(1);
149 /// `u32` is a type alias to `unsigned int`.
150 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
151 /// WGSL syntax.
152 /// Note: this is intentionally not aliased to uint32_t as we want integer
153 /// literals passed to the builder to match WGSL's integer literal types.
154 using u32 = decltype(1u);
155 /// `f32` is a type alias to `float`
156 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
157 /// WGSL syntax.
158 using f32 = float;
159
160 /// Constructor
161 ProgramBuilder();
162
163 /// Move constructor
164 /// @param rhs the builder to move
165 ProgramBuilder(ProgramBuilder&& rhs);
166
167 /// Destructor
168 virtual ~ProgramBuilder();
169
170 /// Move assignment operator
171 /// @param rhs the builder to move
172 /// @return this builder
173 ProgramBuilder& operator=(ProgramBuilder&& rhs);
174
Ben Claytone43c8302021-01-29 11:59:32 +0000175 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
176 /// making a deep clone of the Program contents.
177 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
178 /// existing immutable program.
179 /// As the returned ProgramBuilder wraps `program`, `program` must not be
180 /// destructed or assigned while using the returned ProgramBuilder.
181 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
182 /// function. See crbug.com/tint/460.
183 /// @param program the immutable Program to wrap
184 /// @return the ProgramBuilder that wraps `program`
185 static ProgramBuilder Wrap(const Program* program);
186
Ben Claytone6995de2021-04-13 23:27:27 +0000187 /// @returns the unique identifier for this program
188 ProgramID ID() const { return id_; }
189
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000190 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000191 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000192 AssertNotMoved();
193 return types_;
194 }
195
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000196 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000197 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000198 AssertNotMoved();
199 return types_;
200 }
201
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000202 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000203 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000204 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000205 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000206 }
207
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000208 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000209 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000210 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000211 return ast_nodes_;
212 }
213
214 /// @returns a reference to the program's semantic nodes storage
215 SemNodeAllocator& SemNodes() {
216 AssertNotMoved();
217 return sem_nodes_;
218 }
219
220 /// @returns a reference to the program's semantic nodes storage
221 const SemNodeAllocator& SemNodes() const {
222 AssertNotMoved();
223 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000224 }
225
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000226 /// @returns a reference to the program's AST root Module
227 ast::Module& AST() {
228 AssertNotMoved();
229 return *ast_;
230 }
231
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000232 /// @returns a reference to the program's AST root Module
233 const ast::Module& AST() const {
234 AssertNotMoved();
235 return *ast_;
236 }
237
238 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000239 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000240 AssertNotMoved();
241 return sem_;
242 }
243
244 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000245 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000246 AssertNotMoved();
247 return sem_;
248 }
249
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000250 /// @returns a reference to the program's SymbolTable
251 SymbolTable& Symbols() {
252 AssertNotMoved();
253 return symbols_;
254 }
255
Ben Clayton708dc2d2021-01-29 11:22:40 +0000256 /// @returns a reference to the program's SymbolTable
257 const SymbolTable& Symbols() const {
258 AssertNotMoved();
259 return symbols_;
260 }
261
Ben Clayton844217f2021-01-27 18:49:05 +0000262 /// @returns a reference to the program's diagnostics
263 diag::List& Diagnostics() {
264 AssertNotMoved();
265 return diagnostics_;
266 }
267
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000268 /// @returns a reference to the program's diagnostics
269 const diag::List& Diagnostics() const {
270 AssertNotMoved();
271 return diagnostics_;
272 }
273
Ben Clayton5f0ea112021-03-09 10:54:37 +0000274 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000275 /// @param enable the new flag value (defaults to true)
276 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
277
Ben Clayton5f0ea112021-03-09 10:54:37 +0000278 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000279 /// built.
280 bool ResolveOnBuild() const { return resolve_on_build_; }
281
Ben Clayton844217f2021-01-27 18:49:05 +0000282 /// @returns true if the program has no error diagnostics and is not missing
283 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000284 bool IsValid() const;
285
Ben Clayton708dc2d2021-01-29 11:22:40 +0000286 /// Writes a representation of the node to the output stream
287 /// @note unlike str(), to_str() does not automatically demangle the string.
288 /// @param node the AST node
289 /// @param out the stream to write to
290 /// @param indent number of spaces to indent the node when writing
291 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
292 node->to_str(Sem(), out, indent);
293 }
294
295 /// Returns a demangled, string representation of `node`.
296 /// @param node the AST node
297 /// @returns a string representation of the node
298 std::string str(const ast::Node* node) const;
299
Ben Clayton7fdfff12021-01-29 15:17:30 +0000300 /// Creates a new ast::Node owned by the ProgramBuilder. When the
301 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000302 /// @param source the Source of the node
303 /// @param args the arguments to pass to the type constructor
304 /// @returns the node pointer
305 template <typename T, typename... ARGS>
306 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
307 ARGS&&... args) {
308 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000309 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000310 }
311
Ben Clayton7fdfff12021-01-29 15:17:30 +0000312 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
313 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000314 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000315 /// When the ProgramBuilder is destructed, the ast::Node will also be
316 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000317 /// @returns the node pointer
318 template <typename T>
319 traits::EnableIfIsType<T, ast::Node>* create() {
320 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000321 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000322 }
323
Ben Clayton7fdfff12021-01-29 15:17:30 +0000324 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
325 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000326 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000327 /// When the ProgramBuilder is destructed, the ast::Node will also be
328 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000329 /// @param arg0 the first arguments to pass to the type constructor
330 /// @param args the remaining arguments to pass to the type constructor
331 /// @returns the node pointer
332 template <typename T, typename ARG0, typename... ARGS>
333 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
334 traits::IsTypeOrDerived<T, ast::Node>::value &&
335 !traits::IsTypeOrDerived<ARG0, Source>::value,
336 T>*
337 create(ARG0&& arg0, ARGS&&... args) {
338 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000339 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000340 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000341 }
342
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000343 /// Creates a new sem::Node owned by the ProgramBuilder.
344 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000345 /// destructed.
346 /// @param args the arguments to pass to the type constructor
347 /// @returns the node pointer
348 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000349 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
350 !traits::IsTypeOrDerived<T, sem::Type>::value,
351 T>*
352 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000353 AssertNotMoved();
354 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
355 }
356
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000357 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000358 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
359 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000360 /// Types are unique (de-aliased), and so calling create() for the same `T`
361 /// and arguments will return the same pointer.
362 /// @warning Use this method to acquire a type only if all of its type
363 /// information is provided in the constructor arguments `args`.<br>
364 /// If the type requires additional configuration after construction that
365 /// affect its fundamental type, build the type with `std::make_unique`, make
366 /// any necessary alterations and then call unique_type() instead.
367 /// @param args the arguments to pass to the type constructor
368 /// @returns the de-aliased type pointer
369 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000370 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
371 static_assert(std::is_base_of<sem::Type, T>::value,
372 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000373 AssertNotMoved();
374 return types_.Get<T>(std::forward<ARGS>(args)...);
375 }
376
377 /// Marks this builder as moved, preventing any further use of the builder.
378 void MarkAsMoved();
379
380 //////////////////////////////////////////////////////////////////////////////
381 // TypesBuilder
382 //////////////////////////////////////////////////////////////////////////////
383
384 /// TypesBuilder holds basic `tint` types and methods for constructing
385 /// complex types.
386 class TypesBuilder {
387 public:
388 /// Constructor
389 /// @param builder the program builder
390 explicit TypesBuilder(ProgramBuilder* builder);
391
392 /// @return the tint AST type for the C type `T`.
393 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000394 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000395 return CToAST<T>::get(this);
396 }
397
398 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000399 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000400
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000401 /// @param source the Source of the node
402 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000403 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000404 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000405 }
406
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000407 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000408 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000409
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000410 /// @param source the Source of the node
411 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000412 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000413 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000414 }
415
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000416 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000417 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000418
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000419 /// @param source the Source of the node
420 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000421 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000422 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000423 }
424
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000425 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000426 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000427
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000428 /// @param source the Source of the node
429 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000430 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000431 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000432 }
433
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000434 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000435 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000436
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000437 /// @param source the Source of the node
438 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000439 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000440 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000441 }
442
Antonio Maiorano25436862021-04-13 13:32:33 +0000443 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000444 /// @param n vector width in elements
445 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000446 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000447 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000448 }
449
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000450 /// @param source the Source of the node
451 /// @param type vector subtype
452 /// @param n vector width in elements
453 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000454 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000455 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000456 }
457
Antonio Maiorano25436862021-04-13 13:32:33 +0000458 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000459 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000460 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000461
462 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000463 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000464 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000465
466 /// @param type vector subtype
467 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000468 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000469
470 /// @param n vector width in elements
471 /// @return the tint AST type for a `n`-element vector of `type`.
472 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000473 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000474 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000475 }
476
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000477 /// @return the tint AST type for a 2-element vector of the C type `T`.
478 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000479 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000480 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000481 }
482
483 /// @return the tint AST type for a 3-element vector of the C type `T`.
484 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000485 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000486 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000487 }
488
489 /// @return the tint AST type for a 4-element vector of the C type `T`.
490 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000491 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000492 return vec4(Of<T>());
493 }
494
495 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000496 /// @param columns number of columns for the matrix
497 /// @param rows number of rows for the matrix
498 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000499 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000500 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000501 }
502
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000503 /// @param source the Source of the node
504 /// @param type matrix subtype
505 /// @param columns number of columns for the matrix
506 /// @param rows number of rows for the matrix
507 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000508 ast::Matrix* mat(const Source& source,
509 ast::Type* type,
510 uint32_t columns,
511 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000512 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000513 }
514
Ben Claytone204f272021-04-22 14:40:23 +0000515 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000516 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000517 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000518
519 /// @param type matrix subtype
520 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000521 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000522
523 /// @param type matrix subtype
524 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000525 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000526
527 /// @param type matrix subtype
528 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000529 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000530
531 /// @param type matrix subtype
532 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000533 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000534
535 /// @param type matrix subtype
536 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000537 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000538
539 /// @param type matrix subtype
540 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000541 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000542
543 /// @param type matrix subtype
544 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000545 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000546
547 /// @param type matrix subtype
548 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000549 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000550
Ben Claytone204f272021-04-22 14:40:23 +0000551 /// @param columns number of columns for the matrix
552 /// @param rows number of rows for the matrix
553 /// @return the tint AST type for a matrix of `type`
554 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000555 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000556 return mat(Of<T>(), columns, rows);
557 }
558
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000559 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
560 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000561 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000562 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000563 }
564
565 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
566 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000567 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000568 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000569 }
570
571 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
572 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000573 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000574 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000575 }
576
577 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
578 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000579 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000580 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000581 }
582
583 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
584 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000585 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000586 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000587 }
588
589 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
590 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000591 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000592 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000593 }
594
595 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
596 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000597 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000598 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000599 }
600
601 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
602 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000603 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000604 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000605 }
606
607 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
608 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000609 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000610 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000611 }
612
613 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000614 /// @param n the array size. 0 represents a runtime-array
615 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000616 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000617 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000618 uint32_t n = 0,
619 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000620 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000621 }
622
623 /// @param source the Source of the node
624 /// @param subtype the array element type
625 /// @param n the array size. 0 represents a runtime-array
626 /// @param decos the optional decorations for the array
627 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000628 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000629 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000630 uint32_t n = 0,
631 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000632 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000633 }
634
Ben Claytonbab31972021-02-08 21:16:21 +0000635 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000636 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000637 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000638 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000639 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000640 ast::DecorationList decos;
641 if (stride) {
642 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
643 }
644 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000645 }
646
Ben Clayton0f88b312021-05-04 17:36:31 +0000647 /// @param source the Source of the node
648 /// @param subtype the array element type
649 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000650 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000651 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000652 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000653 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000654 uint32_t n,
655 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000656 ast::DecorationList decos;
657 if (stride) {
658 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
659 }
660 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000661 }
662
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000663 /// @return the tint AST type for an array of size `N` of type `T`
664 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000665 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000666 return array(Of<T>(), N);
667 }
668
Ben Claytonbab31972021-02-08 21:16:21 +0000669 /// @param stride the array stride
670 /// @return the tint AST type for an array of size `N` of type `T`
671 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000672 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000673 return array(Of<T>(), N, stride);
674 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000675
Ben Clayton0f88b312021-05-04 17:36:31 +0000676 /// Creates a type name
677 /// @param name the name
678 /// @returns the type name
679 template <typename NAME>
680 ast::TypeName* type_name(NAME&& name) const {
681 return builder->create<ast::TypeName>(
682 builder->Sym(std::forward<NAME>(name)));
683 }
684
685 /// Creates a type name
686 /// @param source the Source of the node
687 /// @param name the name
688 /// @returns the type name
689 template <typename NAME>
690 ast::TypeName* type_name(const Source& source, NAME&& name) const {
691 return builder->create<ast::TypeName>(
692 source, builder->Sym(std::forward<NAME>(name)));
693 }
694
Ben Clayton42d1e092021-02-02 14:29:15 +0000695 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000696 /// @param name the alias name
697 /// @param type the alias type
698 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000699 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000700 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000701 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000702 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000703 }
704
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000705 /// Creates an alias type
706 /// @param source the Source of the node
707 /// @param name the alias name
708 /// @param type the alias type
709 /// @returns the alias pointer
710 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000711 ast::Alias* alias(const Source& source,
712 NAME&& name,
713 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000714 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000715 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000716 }
717
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000718 /// @param type the type of the pointer
719 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000720 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000721 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000722 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000723 ast::StorageClass storage_class,
724 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000725 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000726 }
727
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000728 /// @param source the Source of the node
729 /// @param type the type of the pointer
730 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000731 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000732 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000733 ast::Pointer* pointer(const Source& source,
734 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000735 ast::StorageClass storage_class,
736 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000737 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000738 }
739
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000740 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000741 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000742 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000743 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000744 ast::Pointer* pointer(ast::StorageClass storage_class,
745 ast::Access access = ast::Access::kUndefined) const {
746 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000747 }
748
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000749 /// @param source the Source of the node
750 /// @param type the type of the atomic
751 /// @return the atomic to `type`
752 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
753 return builder->create<ast::Atomic>(source, type);
754 }
755
Ben Clayton313e6182021-06-17 19:56:14 +0000756 /// @param type the type of the atomic
757 /// @return the atomic to `type`
758 ast::Atomic* atomic(ast::Type* type) const {
759 return builder->create<ast::Atomic>(type);
760 }
761
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000762 /// @return the atomic to type `T`
763 template <typename T>
764 ast::Atomic* atomic() const {
765 return atomic(Of<T>());
766 }
767
Ben Claytone204f272021-04-22 14:40:23 +0000768 /// @param kind the kind of sampler
769 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000770 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000771 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000772 }
773
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000774 /// @param source the Source of the node
775 /// @param kind the kind of sampler
776 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000777 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000778 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000779 }
780
Ben Claytone204f272021-04-22 14:40:23 +0000781 /// @param dims the dimensionality of the texture
782 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000783 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000784 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000785 }
786
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000787 /// @param source the Source of the node
788 /// @param dims the dimensionality of the texture
789 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000790 ast::DepthTexture* depth_texture(const Source& source,
791 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000792 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000793 }
794
Ben Claytone204f272021-04-22 14:40:23 +0000795 /// @param dims the dimensionality of the texture
796 /// @param subtype the texture subtype.
797 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000798 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
799 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000800 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000801 }
802
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000803 /// @param source the Source of the node
804 /// @param dims the dimensionality of the texture
805 /// @param subtype the texture subtype.
806 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000807 ast::SampledTexture* sampled_texture(const Source& source,
808 ast::TextureDimension dims,
809 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000810 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000811 }
812
Ben Claytone204f272021-04-22 14:40:23 +0000813 /// @param dims the dimensionality of the texture
814 /// @param subtype the texture subtype.
815 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000816 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
817 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000818 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000819 }
820
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000821 /// @param source the Source of the node
822 /// @param dims the dimensionality of the texture
823 /// @param subtype the texture subtype.
824 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000825 ast::MultisampledTexture* multisampled_texture(const Source& source,
826 ast::TextureDimension dims,
827 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000828 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000829 }
830
Ben Claytone204f272021-04-22 14:40:23 +0000831 /// @param dims the dimensionality of the texture
832 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000833 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000834 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000835 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000836 ast::ImageFormat format,
837 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000838 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000839 return builder->create<ast::StorageTexture>(dims, format, subtype,
840 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000841 }
842
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000843 /// @param source the Source of the node
844 /// @param dims the dimensionality of the texture
845 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000846 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000847 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000848 ast::StorageTexture* storage_texture(const Source& source,
849 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000850 ast::ImageFormat format,
851 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000852 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000853 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
854 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000855 }
856
Brandon Jones7b257692021-05-17 17:40:17 +0000857 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000858 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000859 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000860 }
861
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000862 /// @param source the Source of the node
863 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000864 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000865 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000866 }
867
Ben Clayton19b03192021-05-20 15:04:08 +0000868 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000869 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000870 /// @param type the type
871 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000872 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000873
Ben Clayton8758f102021-06-09 14:32:14 +0000874 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000875 /// @param type the type
876 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000877 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000878
Ben Claytonf5f311e2021-04-28 14:31:23 +0000879 /// The ProgramBuilder
880 ProgramBuilder* const builder;
881
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000882 private:
883 /// CToAST<T> is specialized for various `T` types and each specialization
884 /// contains a single static `get()` method for obtaining the corresponding
885 /// AST type for the C type `T`.
886 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000887 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000888 template <typename T>
889 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000890 };
891
892 //////////////////////////////////////////////////////////////////////////////
893 // AST helper methods
894 //////////////////////////////////////////////////////////////////////////////
895
James Price65ae64d2021-04-29 12:59:14 +0000896 /// @return a new unnamed symbol
897 Symbol Sym() { return Symbols().New(); }
898
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000899 /// @param name the symbol string
900 /// @return a Symbol with the given name
901 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
902
903 /// @param sym the symbol
904 /// @return `sym`
905 Symbol Sym(Symbol sym) { return sym; }
906
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000907 /// @param expr the expression
908 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000909 template <typename T>
910 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
911 return expr;
912 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000913
Ben Claytonb8ea5912021-04-19 16:52:42 +0000914 /// Passthrough for nullptr
915 /// @return nullptr
916 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
917
Ben Claytonfe0910f2021-05-17 15:51:47 +0000918 /// @param source the source information
919 /// @param symbol the identifier symbol
920 /// @return an ast::IdentifierExpression with the given symbol
921 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
922 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000923 }
924
Ben Clayton46d78d72021-02-10 21:34:26 +0000925 /// @param symbol the identifier symbol
926 /// @return an ast::IdentifierExpression with the given symbol
927 ast::IdentifierExpression* Expr(Symbol symbol) {
928 return create<ast::IdentifierExpression>(symbol);
929 }
930
Ben Claytonfe0910f2021-05-17 15:51:47 +0000931 /// @param source the source information
932 /// @param variable the AST variable
933 /// @return an ast::IdentifierExpression with the variable's symbol
934 ast::IdentifierExpression* Expr(const Source& source,
935 ast::Variable* variable) {
936 return create<ast::IdentifierExpression>(source, variable->symbol());
937 }
938
Ben Claytonb8ea5912021-04-19 16:52:42 +0000939 /// @param variable the AST variable
940 /// @return an ast::IdentifierExpression with the variable's symbol
941 ast::IdentifierExpression* Expr(ast::Variable* variable) {
942 return create<ast::IdentifierExpression>(variable->symbol());
943 }
944
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000945 /// @param source the source information
946 /// @param name the identifier name
947 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000948 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
949 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
950 }
951
952 /// @param name the identifier name
953 /// @return an ast::IdentifierExpression with the given name
954 ast::IdentifierExpression* Expr(const char* name) {
955 return create<ast::IdentifierExpression>(Symbols().Register(name));
956 }
957
958 /// @param source the source information
959 /// @param name the identifier name
960 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000961 ast::IdentifierExpression* Expr(const Source& source,
962 const std::string& name) {
963 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
964 }
965
966 /// @param name the identifier name
967 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000968 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000969 return create<ast::IdentifierExpression>(Symbols().Register(name));
970 }
971
Ben Claytonfe0910f2021-05-17 15:51:47 +0000972 /// @param source the source information
973 /// @param value the boolean value
974 /// @return a Scalar constructor for the given value
975 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
976 return create<ast::ScalarConstructorExpression>(source, Literal(value));
977 }
978
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000979 /// @param value the boolean value
980 /// @return a Scalar constructor for the given value
981 ast::ScalarConstructorExpression* Expr(bool value) {
982 return create<ast::ScalarConstructorExpression>(Literal(value));
983 }
984
Ben Claytonfe0910f2021-05-17 15:51:47 +0000985 /// @param source the source information
986 /// @param value the float value
987 /// @return a Scalar constructor for the given value
988 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
989 return create<ast::ScalarConstructorExpression>(source, Literal(value));
990 }
991
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000992 /// @param value the float value
993 /// @return a Scalar constructor for the given value
994 ast::ScalarConstructorExpression* Expr(f32 value) {
995 return create<ast::ScalarConstructorExpression>(Literal(value));
996 }
997
Ben Claytonfe0910f2021-05-17 15:51:47 +0000998 /// @param source the source information
999 /// @param value the integer value
1000 /// @return a Scalar constructor for the given value
1001 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1002 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1003 }
1004
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001005 /// @param value the integer value
1006 /// @return a Scalar constructor for the given value
1007 ast::ScalarConstructorExpression* Expr(i32 value) {
1008 return create<ast::ScalarConstructorExpression>(Literal(value));
1009 }
1010
Ben Claytonfe0910f2021-05-17 15:51:47 +00001011 /// @param source the source information
1012 /// @param value the unsigned int value
1013 /// @return a Scalar constructor for the given value
1014 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1015 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1016 }
1017
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001018 /// @param value the unsigned int value
1019 /// @return a Scalar constructor for the given value
1020 ast::ScalarConstructorExpression* Expr(u32 value) {
1021 return create<ast::ScalarConstructorExpression>(Literal(value));
1022 }
1023
1024 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1025 /// `list`.
1026 /// @param list the list to append too
1027 /// @param arg the arg to create
1028 template <typename ARG>
1029 void Append(ast::ExpressionList& list, ARG&& arg) {
1030 list.emplace_back(Expr(std::forward<ARG>(arg)));
1031 }
1032
1033 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1034 /// then appends them to `list`.
1035 /// @param list the list to append too
1036 /// @param arg0 the first argument
1037 /// @param args the rest of the arguments
1038 template <typename ARG0, typename... ARGS>
1039 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1040 Append(list, std::forward<ARG0>(arg0));
1041 Append(list, std::forward<ARGS>(args)...);
1042 }
1043
1044 /// @return an empty list of expressions
1045 ast::ExpressionList ExprList() { return {}; }
1046
1047 /// @param args the list of expressions
1048 /// @return the list of expressions converted to `ast::Expression`s using
1049 /// `Expr()`,
1050 template <typename... ARGS>
1051 ast::ExpressionList ExprList(ARGS&&... args) {
1052 ast::ExpressionList list;
1053 list.reserve(sizeof...(args));
1054 Append(list, std::forward<ARGS>(args)...);
1055 return list;
1056 }
1057
1058 /// @param list the list of expressions
1059 /// @return `list`
1060 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1061
1062 /// @param val the boolan value
1063 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001064 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001065
1066 /// @param val the float value
1067 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001068 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001069
1070 /// @param val the unsigned int value
1071 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001072 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001073
1074 /// @param val the integer value
1075 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001076 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001077
1078 /// @param args the arguments for the type constructor
1079 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1080 /// of `args` converted to `ast::Expression`s using `Expr()`
1081 template <typename T, typename... ARGS>
1082 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001083 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001084 }
1085
1086 /// @param type the type to construct
1087 /// @param args the arguments for the constructor
1088 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1089 /// values `args`.
1090 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001091 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001092 return create<ast::TypeConstructorExpression>(
1093 type, ExprList(std::forward<ARGS>(args)...));
1094 }
1095
Ben Clayton313e6182021-06-17 19:56:14 +00001096 /// @param source the source information
1097 /// @param type the type to construct
1098 /// @param args the arguments for the constructor
1099 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1100 /// values `args`.
1101 template <typename... ARGS>
1102 ast::TypeConstructorExpression* Construct(const Source& source,
1103 ast::Type* type,
1104 ARGS&&... args) {
1105 return create<ast::TypeConstructorExpression>(
1106 source, type, ExprList(std::forward<ARGS>(args)...));
1107 }
1108
Ben Claytond9603282021-06-29 12:37:45 +00001109 /// @param expr the expression for the bitcast
1110 /// @return an `ast::BitcastExpression` of type `ty`, with the values of
1111 /// `expr` converted to `ast::Expression`s using `Expr()`
1112 template <typename T, typename EXPR>
1113 ast::BitcastExpression* Bitcast(EXPR&& expr) {
1114 return Bitcast(ty.Of<T>(), std::forward<EXPR>(expr));
1115 }
1116
1117 /// @param type the type to cast to
1118 /// @param expr the expression for the bitcast
1119 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1120 /// `expr`.
1121 template <typename EXPR>
1122 ast::BitcastExpression* Bitcast(ast::Type* type, EXPR&& expr) {
1123 return create<ast::BitcastExpression>(type, Expr(std::forward<EXPR>(expr)));
1124 }
1125
1126 /// @param source the source information
1127 /// @param type the type to cast to
1128 /// @param expr the expression for the bitcast
1129 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1130 /// `expr`.
1131 template <typename EXPR>
1132 ast::BitcastExpression* Bitcast(const Source& source,
1133 ast::Type* type,
1134 EXPR&& expr) {
1135 return create<ast::BitcastExpression>(source, type,
1136 Expr(std::forward<EXPR>(expr)));
1137 }
1138
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001139 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001140 /// @param type the vector type
1141 /// @param size the vector size
1142 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1143 /// type `type`, constructed with the values `args`.
1144 template <typename... ARGS>
1145 ast::TypeConstructorExpression* vec(ast::Type* type,
1146 uint32_t size,
1147 ARGS&&... args) {
1148 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1149 }
1150
1151 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001152 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1153 /// `T`, constructed with the values `args`.
1154 template <typename T, typename... ARGS>
1155 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001156 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001157 }
1158
1159 /// @param args the arguments for the vector constructor
1160 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1161 /// `T`, constructed with the values `args`.
1162 template <typename T, typename... ARGS>
1163 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001164 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001165 }
1166
1167 /// @param args the arguments for the vector constructor
1168 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1169 /// `T`, constructed with the values `args`.
1170 template <typename T, typename... ARGS>
1171 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001172 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001173 }
1174
1175 /// @param args the arguments for the matrix constructor
1176 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1177 /// `T`, constructed with the values `args`.
1178 template <typename T, typename... ARGS>
1179 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001180 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001181 }
1182
1183 /// @param args the arguments for the matrix constructor
1184 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1185 /// `T`, constructed with the values `args`.
1186 template <typename T, typename... ARGS>
1187 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001188 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001189 }
1190
1191 /// @param args the arguments for the matrix constructor
1192 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1193 /// `T`, constructed with the values `args`.
1194 template <typename T, typename... ARGS>
1195 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001196 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001197 }
1198
1199 /// @param args the arguments for the matrix constructor
1200 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1201 /// `T`, constructed with the values `args`.
1202 template <typename T, typename... ARGS>
1203 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001204 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001205 }
1206
1207 /// @param args the arguments for the matrix constructor
1208 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1209 /// `T`, constructed with the values `args`.
1210 template <typename T, typename... ARGS>
1211 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001212 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001213 }
1214
1215 /// @param args the arguments for the matrix constructor
1216 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1217 /// `T`, constructed with the values `args`.
1218 template <typename T, typename... ARGS>
1219 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001220 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001221 }
1222
1223 /// @param args the arguments for the matrix constructor
1224 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1225 /// `T`, constructed with the values `args`.
1226 template <typename T, typename... ARGS>
1227 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001228 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001229 }
1230
1231 /// @param args the arguments for the matrix constructor
1232 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1233 /// `T`, constructed with the values `args`.
1234 template <typename T, typename... ARGS>
1235 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001236 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001237 }
1238
1239 /// @param args the arguments for the matrix constructor
1240 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1241 /// `T`, constructed with the values `args`.
1242 template <typename T, typename... ARGS>
1243 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001244 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001245 }
1246
1247 /// @param args the arguments for the array constructor
1248 /// @return an `ast::TypeConstructorExpression` of an array with element type
1249 /// `T`, constructed with the values `args`.
1250 template <typename T, int N = 0, typename... ARGS>
1251 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001252 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001253 }
1254
1255 /// @param subtype the array element type
1256 /// @param n the array size. 0 represents a runtime-array.
1257 /// @param args the arguments for the array constructor
1258 /// @return an `ast::TypeConstructorExpression` of an array with element type
1259 /// `subtype`, constructed with the values `args`.
1260 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001261 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001262 uint32_t n,
1263 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001264 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001265 }
1266
1267 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001268 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001269 /// @param optional the optional variable settings.
1270 /// Can be any of the following, in any order:
1271 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001272 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001273 /// * ast::Expression* - specifies the variable's initializer expression
1274 /// * ast::DecorationList - specifies the variable's decorations
1275 /// Note that repeated arguments of the same type will use the last argument's
1276 /// value.
1277 /// @returns a `ast::Variable` with the given name, type and additional
1278 /// options
1279 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001280 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001281 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001282 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001283 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1284 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001285 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001286 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001287 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001288
1289 /// @param source the variable source
1290 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001291 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001292 /// @param optional the optional variable settings.
1293 /// Can be any of the following, in any order:
1294 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001295 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001296 /// * ast::Expression* - specifies the variable's initializer expression
1297 /// * ast::DecorationList - specifies the variable's decorations
1298 /// Note that repeated arguments of the same type will use the last argument's
1299 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001300 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001301 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001302 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001303 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001304 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001305 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001306 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1307 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001308 opts.storage, opts.access, type, false,
1309 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001310 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001311
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001312 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001313 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001314 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001315 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001316 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001317 template <typename NAME>
1318 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001319 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001320 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001321 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001322 return create<ast::Variable>(
1323 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1324 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001325 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001326
1327 /// @param source the variable source
1328 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001329 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001330 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001331 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001332 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001333 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001334 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001335 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001336 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001337 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001338 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001339 return create<ast::Variable>(
1340 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1341 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001342 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001343
James Pricedaf1f1c2021-04-08 22:15:48 +00001344 /// @param name the parameter name
1345 /// @param type the parameter type
1346 /// @param decorations optional parameter decorations
1347 /// @returns a constant `ast::Variable` with the given name and type
1348 template <typename NAME>
1349 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001350 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001351 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001352 return create<ast::Variable>(
1353 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1354 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001355 }
1356
1357 /// @param source the parameter source
1358 /// @param name the parameter name
1359 /// @param type the parameter type
1360 /// @param decorations optional parameter decorations
1361 /// @returns a constant `ast::Variable` with the given name and type
1362 template <typename NAME>
1363 ast::Variable* Param(const Source& source,
1364 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001365 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001366 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001367 return create<ast::Variable>(
1368 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1369 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001370 }
1371
Ben Clayton42708342021-04-21 17:55:12 +00001372 /// @param name the variable name
1373 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001374 /// @param optional the optional variable settings.
1375 /// Can be any of the following, in any order:
1376 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001377 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001378 /// * ast::Expression* - specifies the variable's initializer expression
1379 /// * ast::DecorationList - specifies the variable's decorations
1380 /// Note that repeated arguments of the same type will use the last argument's
1381 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001382 /// @returns a new `ast::Variable`, which is automatically registered as a
1383 /// global variable with the ast::Module.
Antonio Maiorano60dae242021-07-15 19:09:25 +00001384 template <typename NAME,
1385 typename... OPTIONAL,
1386 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Clayton42708342021-04-21 17:55:12 +00001387 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001388 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001389 OPTIONAL&&... optional) {
1390 auto* var = Var(std::forward<NAME>(name), type,
1391 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001392 AST().AddGlobalVariable(var);
1393 return var;
1394 }
1395
1396 /// @param source the variable source
1397 /// @param name the variable name
1398 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001399 /// @param optional the optional variable settings.
1400 /// Can be any of the following, in any order:
1401 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001402 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001403 /// * ast::Expression* - specifies the variable's initializer expression
1404 /// * ast::DecorationList - specifies the variable's decorations
1405 /// Note that repeated arguments of the same type will use the last argument's
1406 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001407 /// @returns a new `ast::Variable`, which is automatically registered as a
1408 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001409 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001410 ast::Variable* Global(const Source& source,
1411 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001412 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001413 OPTIONAL&&... optional) {
1414 auto* var = Var(source, std::forward<NAME>(name), type,
1415 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001416 AST().AddGlobalVariable(var);
1417 return var;
1418 }
1419
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001420 /// @param name the variable name
1421 /// @param type the variable type
1422 /// @param constructor constructor expression
1423 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001424 /// @returns a const `ast::Variable` constructed by calling Var() with the
1425 /// arguments of `args`, which is automatically registered as a global
1426 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001427 template <typename NAME>
1428 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001429 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001430 ast::Expression* constructor,
1431 ast::DecorationList decorations = {}) {
1432 auto* var = Const(std::forward<NAME>(name), type, constructor,
1433 std::move(decorations));
1434 AST().AddGlobalVariable(var);
1435 return var;
1436 }
1437
1438 /// @param source the variable source
1439 /// @param name the variable name
1440 /// @param type the variable type
1441 /// @param constructor constructor expression
1442 /// @param decorations optional variable decorations
1443 /// @returns a const `ast::Variable` constructed by calling Var() with the
1444 /// arguments of `args`, which is automatically registered as a global
1445 /// variable with the ast::Module.
1446 template <typename NAME>
1447 ast::Variable* GlobalConst(const Source& source,
1448 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001449 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001450 ast::Expression* constructor,
1451 ast::DecorationList decorations = {}) {
1452 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1453 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001454 AST().AddGlobalVariable(var);
1455 return var;
1456 }
1457
Ben Claytonfe0910f2021-05-17 15:51:47 +00001458 /// @param source the source information
1459 /// @param expr the expression to take the address of
1460 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1461 template <typename EXPR>
1462 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1463 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1464 Expr(std::forward<EXPR>(expr)));
1465 }
1466
1467 /// @param expr the expression to take the address of
1468 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1469 template <typename EXPR>
1470 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1471 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1472 Expr(std::forward<EXPR>(expr)));
1473 }
1474
1475 /// @param source the source information
1476 /// @param expr the expression to perform an indirection on
1477 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1478 template <typename EXPR>
1479 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1480 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1481 Expr(std::forward<EXPR>(expr)));
1482 }
1483
1484 /// @param expr the expression to perform an indirection on
1485 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1486 template <typename EXPR>
1487 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1488 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1489 Expr(std::forward<EXPR>(expr)));
1490 }
1491
Antonio Maiorano14b34032021-06-09 20:17:59 +00001492 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001493 /// @param func the function name
1494 /// @param args the function call arguments
1495 /// @returns a `ast::CallExpression` to the function `func`, with the
1496 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1497 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001498 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1499 return create<ast::CallExpression>(source, Expr(func),
1500 ExprList(std::forward<ARGS>(args)...));
1501 }
1502
1503 /// @param func the function name
1504 /// @param args the function call arguments
1505 /// @returns a `ast::CallExpression` to the function `func`, with the
1506 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1507 template <typename NAME,
1508 typename... ARGS,
1509 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001510 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1511 return create<ast::CallExpression>(Expr(func),
1512 ExprList(std::forward<ARGS>(args)...));
1513 }
1514
Ben Clayton7fe01062021-06-11 13:22:27 +00001515 /// @param expr the expression to ignore
1516 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1517 /// passed the single `expr` argument
1518 template <typename EXPR>
1519 ast::CallStatement* Ignore(EXPR&& expr) {
1520 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1521 }
1522
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001523 /// @param lhs the left hand argument to the addition operation
1524 /// @param rhs the right hand argument to the addition operation
1525 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1526 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001527 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001528 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1529 Expr(std::forward<LHS>(lhs)),
1530 Expr(std::forward<RHS>(rhs)));
1531 }
1532
James Pricec32e8f62021-06-22 20:08:29 +00001533 /// @param lhs the left hand argument to the and operation
1534 /// @param rhs the right hand argument to the and operation
1535 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
1536 template <typename LHS, typename RHS>
1537 ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
1538 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
1539 Expr(std::forward<LHS>(lhs)),
1540 Expr(std::forward<RHS>(rhs)));
1541 }
1542
Ben Claytond9603282021-06-29 12:37:45 +00001543 /// @param lhs the left hand argument to the or operation
1544 /// @param rhs the right hand argument to the or operation
1545 /// @returns a `ast::BinaryExpression` bitwise or-ing `lhs` and `rhs`
1546 template <typename LHS, typename RHS>
1547 ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) {
1548 return create<ast::BinaryExpression>(ast::BinaryOp::kOr,
1549 Expr(std::forward<LHS>(lhs)),
1550 Expr(std::forward<RHS>(rhs)));
1551 }
1552
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001553 /// @param lhs the left hand argument to the subtraction operation
1554 /// @param rhs the right hand argument to the subtraction operation
1555 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1556 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001557 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001558 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1559 Expr(std::forward<LHS>(lhs)),
1560 Expr(std::forward<RHS>(rhs)));
1561 }
1562
1563 /// @param lhs the left hand argument to the multiplication operation
1564 /// @param rhs the right hand argument to the multiplication operation
1565 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1566 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001567 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001568 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1569 Expr(std::forward<LHS>(lhs)),
1570 Expr(std::forward<RHS>(rhs)));
1571 }
1572
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001573 /// @param source the source information
1574 /// @param lhs the left hand argument to the multiplication operation
1575 /// @param rhs the right hand argument to the multiplication operation
1576 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1577 template <typename LHS, typename RHS>
1578 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1579 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1580 Expr(std::forward<LHS>(lhs)),
1581 Expr(std::forward<RHS>(rhs)));
1582 }
1583
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001584 /// @param lhs the left hand argument to the division operation
1585 /// @param rhs the right hand argument to the division operation
1586 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1587 template <typename LHS, typename RHS>
1588 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1589 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1590 Expr(std::forward<LHS>(lhs)),
1591 Expr(std::forward<RHS>(rhs)));
1592 }
1593
Ben Claytond9603282021-06-29 12:37:45 +00001594 /// @param lhs the left hand argument to the bit shift right operation
1595 /// @param rhs the right hand argument to the bit shift right operation
1596 /// @returns a `ast::BinaryExpression` bit shifting right `lhs` by `rhs`
1597 template <typename LHS, typename RHS>
1598 ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) {
1599 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftRight,
1600 Expr(std::forward<LHS>(lhs)),
1601 Expr(std::forward<RHS>(rhs)));
1602 }
1603
1604 /// @param lhs the left hand argument to the bit shift left operation
1605 /// @param rhs the right hand argument to the bit shift left operation
1606 /// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs`
1607 template <typename LHS, typename RHS>
1608 ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) {
1609 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftLeft,
1610 Expr(std::forward<LHS>(lhs)),
1611 Expr(std::forward<RHS>(rhs)));
1612 }
1613
Ben Clayton0597a2b2021-06-16 09:19:36 +00001614 /// @param source the source information
1615 /// @param arr the array argument for the array accessor expression
1616 /// @param idx the index argument for the array accessor expression
1617 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1618 template <typename ARR, typename IDX>
1619 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1620 ARR&& arr,
1621 IDX&& idx) {
1622 return create<ast::ArrayAccessorExpression>(
1623 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1624 }
1625
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001626 /// @param arr the array argument for the array accessor expression
1627 /// @param idx the index argument for the array accessor expression
1628 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1629 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001630 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001631 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1632 Expr(std::forward<IDX>(idx)));
1633 }
1634
1635 /// @param obj the object for the member accessor expression
1636 /// @param idx the index argument for the array accessor expression
1637 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1638 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001639 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001640 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1641 Expr(std::forward<IDX>(idx)));
1642 }
1643
Ben Clayton42d1e092021-02-02 14:29:15 +00001644 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001645 /// @param val the offset value
1646 /// @returns the offset decoration pointer
1647 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1648 return create<ast::StructMemberOffsetDecoration>(source_, val);
1649 }
1650
Ben Claytond614dd52021-03-15 10:43:11 +00001651 /// Creates a ast::StructMemberSizeDecoration
1652 /// @param source the source information
1653 /// @param val the size value
1654 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001655 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1656 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001657 return create<ast::StructMemberSizeDecoration>(source, val);
1658 }
1659
1660 /// Creates a ast::StructMemberSizeDecoration
1661 /// @param val the size value
1662 /// @returns the size decoration pointer
1663 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1664 return create<ast::StructMemberSizeDecoration>(source_, val);
1665 }
1666
1667 /// Creates a ast::StructMemberAlignDecoration
1668 /// @param source the source information
1669 /// @param val the align value
1670 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001671 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1672 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001673 return create<ast::StructMemberAlignDecoration>(source, val);
1674 }
1675
1676 /// Creates a ast::StructMemberAlignDecoration
1677 /// @param val the align value
1678 /// @returns the align decoration pointer
1679 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1680 return create<ast::StructMemberAlignDecoration>(source_, val);
1681 }
1682
Antonio Maiorano60dae242021-07-15 19:09:25 +00001683 /// Creates a ast::StructBlockDecoration
1684 /// @returns the struct block decoration pointer
1685 ast::StructBlockDecoration* StructBlock() {
1686 return create<ast::StructBlockDecoration>();
1687 }
1688
1689 /// Creates the ast::GroupDecoration
1690 /// @param value group decoration index
1691 /// @returns the group decoration pointer
1692 ast::GroupDecoration* Group(uint32_t value) {
1693 return create<ast::GroupDecoration>(value);
1694 }
1695
1696 /// Creates the ast::BindingDecoration
1697 /// @param value the binding index
1698 /// @returns the binding deocration pointer
1699 ast::BindingDecoration* Binding(uint32_t value) {
1700 return create<ast::BindingDecoration>(value);
1701 }
1702
1703 /// Convenience function to create both a ast::GroupDecoration and
1704 /// ast::BindingDecoration
1705 /// @param group the group index
1706 /// @param binding the binding index
1707 /// @returns a decoration list with both the group and binding decorations
1708 ast::DecorationList GroupAndBinding(uint32_t group, uint32_t binding) {
1709 return {Group(group), Binding(binding)};
1710 }
1711
Ben Clayton42d1e092021-02-02 14:29:15 +00001712 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001713 /// @param source the source information
1714 /// @param name the function name
1715 /// @param params the function parameters
1716 /// @param type the function return type
1717 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001718 /// @param decorations the optional function decorations
1719 /// @param return_type_decorations the optional function return type
1720 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001721 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001722 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001723 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001724 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001725 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001726 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001727 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001728 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001729 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001730 auto* func =
1731 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1732 type, create<ast::BlockStatement>(body),
1733 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001734 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001735 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001736 }
1737
Ben Clayton42d1e092021-02-02 14:29:15 +00001738 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001739 /// @param name the function name
1740 /// @param params the function parameters
1741 /// @param type the function return type
1742 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001743 /// @param decorations the optional function decorations
1744 /// @param return_type_decorations the optional function return type
1745 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001746 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001747 template <typename NAME>
1748 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001749 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001750 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001751 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001752 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001753 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001754 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1755 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001756 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001757 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001758 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001759 }
1760
Ben Clayton49668bb2021-04-17 05:32:01 +00001761 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001762 /// @param source the source information
1763 /// @returns the return statement pointer
1764 ast::ReturnStatement* Return(const Source& source) {
1765 return create<ast::ReturnStatement>(source);
1766 }
1767
1768 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001769 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001770 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1771
1772 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001773 /// @param source the source information
1774 /// @param val the return value
1775 /// @returns the return statement pointer
1776 template <typename EXPR>
1777 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1778 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1779 }
1780
1781 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001782 /// @param val the return value
1783 /// @returns the return statement pointer
1784 template <typename EXPR>
1785 ast::ReturnStatement* Return(EXPR&& val) {
1786 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001787 }
1788
Ben Clayton950809f2021-06-09 14:32:14 +00001789 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001790 /// @param source the source information
1791 /// @param name the alias name
1792 /// @param type the alias target type
1793 /// @returns the alias type
1794 template <typename NAME>
1795 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1796 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001797 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001798 return out;
1799 }
1800
Ben Clayton950809f2021-06-09 14:32:14 +00001801 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001802 /// @param name the alias name
1803 /// @param type the alias target type
1804 /// @returns the alias type
1805 template <typename NAME>
1806 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1807 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001808 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001809 return out;
1810 }
1811
Ben Clayton950809f2021-06-09 14:32:14 +00001812 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001813 /// @param source the source information
1814 /// @param name the struct name
1815 /// @param members the struct members
1816 /// @param decorations the optional struct decorations
1817 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001818 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001819 ast::Struct* Structure(const Source& source,
1820 NAME&& name,
1821 ast::StructMemberList members,
1822 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001823 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001824 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001825 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001826 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001827 return type;
1828 }
1829
Ben Clayton950809f2021-06-09 14:32:14 +00001830 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001831 /// @param name the struct name
1832 /// @param members the struct members
1833 /// @param decorations the optional struct decorations
1834 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001835 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001836 ast::Struct* Structure(NAME&& name,
1837 ast::StructMemberList members,
1838 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001839 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001840 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001841 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001842 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001843 return type;
1844 }
1845
Ben Clayton42d1e092021-02-02 14:29:15 +00001846 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001847 /// @param source the source information
1848 /// @param name the struct member name
1849 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001850 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001851 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001852 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001853 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001854 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001855 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001856 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001857 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1858 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001859 }
1860
Ben Clayton42d1e092021-02-02 14:29:15 +00001861 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001862 /// @param name the struct member name
1863 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001864 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001865 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001866 template <typename NAME>
1867 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001868 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001869 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001870 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1871 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001872 }
1873
Ben Claytonbab31972021-02-08 21:16:21 +00001874 /// Creates a ast::StructMember with the given byte offset
1875 /// @param offset the offset to use in the StructMemberOffsetDecoration
1876 /// @param name the struct member name
1877 /// @param type the struct member type
1878 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001879 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001880 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001881 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001882 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001883 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001884 create<ast::StructMemberOffsetDecoration>(offset),
1885 });
1886 }
1887
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001888 /// Creates a ast::BlockStatement with input statements
1889 /// @param statements statements of block
1890 /// @returns the block statement pointer
1891 template <typename... Statements>
1892 ast::BlockStatement* Block(Statements&&... statements) {
1893 return create<ast::BlockStatement>(
1894 ast::StatementList{std::forward<Statements>(statements)...});
1895 }
1896
1897 /// Creates a ast::ElseStatement with input condition and body
1898 /// @param condition the else condition expression
1899 /// @param body the else body
1900 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001901 template <typename CONDITION>
1902 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1903 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1904 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001905 }
1906
1907 /// Creates a ast::IfStatement with input condition, body, and optional
1908 /// variadic else statements
1909 /// @param condition the if statement condition expression
1910 /// @param body the if statement body
1911 /// @param elseStatements optional variadic else statements
1912 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001913 template <typename CONDITION, typename... ELSE_STATEMENTS>
1914 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001915 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001916 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001917 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001918 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001919 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001920 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001921 }
1922
1923 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001924 /// @param source the source information
1925 /// @param lhs the left hand side expression initializer
1926 /// @param rhs the right hand side expression initializer
1927 /// @returns the assignment statement pointer
1928 template <typename LhsExpressionInit, typename RhsExpressionInit>
1929 ast::AssignmentStatement* Assign(const Source& source,
1930 LhsExpressionInit&& lhs,
1931 RhsExpressionInit&& rhs) {
1932 return create<ast::AssignmentStatement>(
1933 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1934 Expr(std::forward<RhsExpressionInit>(rhs)));
1935 }
1936
1937 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001938 /// @param lhs the left hand side expression initializer
1939 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001940 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001941 template <typename LhsExpressionInit, typename RhsExpressionInit>
1942 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1943 RhsExpressionInit&& rhs) {
1944 return create<ast::AssignmentStatement>(
1945 Expr(std::forward<LhsExpressionInit>(lhs)),
1946 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001947 }
1948
1949 /// Creates a ast::LoopStatement with input body and optional continuing
1950 /// @param body the loop body
1951 /// @param continuing the optional continuing block
1952 /// @returns the loop statement pointer
1953 ast::LoopStatement* Loop(ast::BlockStatement* body,
1954 ast::BlockStatement* continuing = nullptr) {
1955 return create<ast::LoopStatement>(body, continuing);
1956 }
1957
Ben Clayton65cd2592021-07-02 19:27:42 +00001958 /// Creates a ast::ForLoopStatement with input body and optional initializer,
1959 /// condition and continuing.
1960 /// @param source the source information
1961 /// @param init the optional loop initializer
1962 /// @param cond the optional loop condition
1963 /// @param cont the optional loop continuing
1964 /// @param body the loop body
1965 /// @returns the for loop statement pointer
1966 template <typename COND>
1967 ast::ForLoopStatement* For(const Source& source,
1968 ast::Statement* init,
1969 COND&& cond,
1970 ast::Statement* cont,
1971 ast::BlockStatement* body) {
1972 return create<ast::ForLoopStatement>(
1973 source, init, Expr(std::forward<COND>(cond)), cont, body);
1974 }
1975
1976 /// Creates a ast::ForLoopStatement with input body and optional initializer,
1977 /// condition and continuing.
1978 /// @param init the optional loop initializer
1979 /// @param cond the optional loop condition
1980 /// @param cont the optional loop continuing
1981 /// @param body the loop body
1982 /// @returns the for loop statement pointer
1983 template <typename COND>
1984 ast::ForLoopStatement* For(ast::Statement* init,
1985 COND&& cond,
1986 ast::Statement* cont,
1987 ast::BlockStatement* body) {
1988 return create<ast::ForLoopStatement>(init, Expr(std::forward<COND>(cond)),
1989 cont, body);
1990 }
1991
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001992 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001993 /// @param source the source information
1994 /// @param var the variable to wrap in a decl statement
1995 /// @returns the variable decl statement pointer
1996 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1997 return create<ast::VariableDeclStatement>(source, var);
1998 }
1999
2000 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00002001 /// @param var the variable to wrap in a decl statement
2002 /// @returns the variable decl statement pointer
2003 ast::VariableDeclStatement* Decl(ast::Variable* var) {
2004 return create<ast::VariableDeclStatement>(var);
2005 }
2006
Antonio Maioranocea744d2021-03-25 12:55:27 +00002007 /// Creates a ast::SwitchStatement with input expression and cases
2008 /// @param condition the condition expression initializer
2009 /// @param cases case statements
2010 /// @returns the switch statement pointer
2011 template <typename ExpressionInit, typename... Cases>
2012 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
2013 return create<ast::SwitchStatement>(
2014 Expr(std::forward<ExpressionInit>(condition)),
2015 ast::CaseStatementList{std::forward<Cases>(cases)...});
2016 }
2017
2018 /// Creates a ast::CaseStatement with input list of selectors, and body
2019 /// @param selectors list of selectors
2020 /// @param body the case body
2021 /// @returns the case statement pointer
2022 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
2023 ast::BlockStatement* body = nullptr) {
2024 return create<ast::CaseStatement>(std::move(selectors),
2025 body ? body : Block());
2026 }
2027
2028 /// Convenient overload that takes a single selector
2029 /// @param selector a single case selector
2030 /// @param body the case body
2031 /// @returns the case statement pointer
2032 ast::CaseStatement* Case(ast::IntLiteral* selector,
2033 ast::BlockStatement* body = nullptr) {
2034 return Case(ast::CaseSelectorList{selector}, body);
2035 }
2036
2037 /// Convenience function that creates a 'default' ast::CaseStatement
2038 /// @param body the case body
2039 /// @returns the case statement pointer
2040 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
2041 return Case(ast::CaseSelectorList{}, body);
2042 }
2043
James Price68f558f2021-04-06 15:51:47 +00002044 /// Creates an ast::BuiltinDecoration
2045 /// @param source the source information
2046 /// @param builtin the builtin value
2047 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002048 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00002049 return create<ast::BuiltinDecoration>(source, builtin);
2050 }
2051
2052 /// Creates an ast::BuiltinDecoration
2053 /// @param builtin the builtin value
2054 /// @returns the builtin decoration pointer
2055 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
2056 return create<ast::BuiltinDecoration>(source_, builtin);
2057 }
2058
James Price989a8e42021-06-28 23:04:43 +00002059 /// Creates an ast::InterpolateDecoration
2060 /// @param source the source information
2061 /// @param type the interpolation type
2062 /// @param sampling the interpolation sampling
2063 /// @returns the interpolate decoration pointer
2064 ast::InterpolateDecoration* Interpolate(const Source& source,
2065 ast::InterpolationType type,
2066 ast::InterpolationSampling sampling) {
2067 return create<ast::InterpolateDecoration>(source, type, sampling);
2068 }
2069
2070 /// Creates an ast::InterpolateDecoration
2071 /// @param type the interpolation type
2072 /// @param sampling the interpolation sampling
2073 /// @returns the interpolate decoration pointer
2074 ast::InterpolateDecoration* Interpolate(ast::InterpolationType type,
2075 ast::InterpolationSampling sampling) {
2076 return create<ast::InterpolateDecoration>(source_, type, sampling);
2077 }
2078
James Price508d2492021-07-12 12:12:32 +00002079 /// Creates an ast::InvariantDecoration
2080 /// @param source the source information
2081 /// @returns the invariant decoration pointer
2082 ast::InvariantDecoration* Invariant(const Source& source) {
2083 return create<ast::InvariantDecoration>(source);
2084 }
2085
2086 /// Creates an ast::InvariantDecoration
2087 /// @returns the invariant decoration pointer
2088 ast::InvariantDecoration* Invariant() {
2089 return create<ast::InvariantDecoration>(source_);
2090 }
2091
James Price68f558f2021-04-06 15:51:47 +00002092 /// Creates an ast::LocationDecoration
2093 /// @param source the source information
2094 /// @param location the location value
2095 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002096 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00002097 return create<ast::LocationDecoration>(source, location);
2098 }
2099
2100 /// Creates an ast::LocationDecoration
2101 /// @param location the location value
2102 /// @returns the location decoration pointer
2103 ast::LocationDecoration* Location(uint32_t location) {
2104 return create<ast::LocationDecoration>(source_, location);
2105 }
2106
James Pricef2f3bfc2021-05-13 20:32:32 +00002107 /// Creates an ast::OverrideDecoration with a specific constant ID
2108 /// @param source the source information
2109 /// @param id the id value
2110 /// @returns the override decoration pointer
2111 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
2112 return create<ast::OverrideDecoration>(source, id);
2113 }
2114
2115 /// Creates an ast::OverrideDecoration with a specific constant ID
2116 /// @param id the optional id value
2117 /// @returns the override decoration pointer
2118 ast::OverrideDecoration* Override(uint32_t id) {
2119 return Override(source_, id);
2120 }
2121
2122 /// Creates an ast::OverrideDecoration without a constant ID
2123 /// @param source the source information
2124 /// @returns the override decoration pointer
2125 ast::OverrideDecoration* Override(const Source& source) {
2126 return create<ast::OverrideDecoration>(source);
2127 }
2128
2129 /// Creates an ast::OverrideDecoration without a constant ID
2130 /// @returns the override decoration pointer
2131 ast::OverrideDecoration* Override() { return Override(source_); }
2132
James Price68f558f2021-04-06 15:51:47 +00002133 /// Creates an ast::StageDecoration
2134 /// @param source the source information
2135 /// @param stage the pipeline stage
2136 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002137 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00002138 return create<ast::StageDecoration>(source, stage);
2139 }
2140
2141 /// Creates an ast::StageDecoration
2142 /// @param stage the pipeline stage
2143 /// @returns the stage decoration pointer
2144 ast::StageDecoration* Stage(ast::PipelineStage stage) {
2145 return create<ast::StageDecoration>(source_, stage);
2146 }
2147
James Price70f80bb2021-05-19 13:40:08 +00002148 /// Creates an ast::WorkgroupDecoration
2149 /// @param x the x dimension expression
2150 /// @returns the workgroup decoration pointer
2151 template <typename EXPR_X>
2152 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
2153 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
2154 }
2155
2156 /// Creates an ast::WorkgroupDecoration
2157 /// @param x the x dimension expression
2158 /// @param y the y dimension expression
2159 /// @returns the workgroup decoration pointer
2160 template <typename EXPR_X, typename EXPR_Y>
2161 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
2162 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
2163 nullptr);
2164 }
2165
2166 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00002167 /// @param source the source information
2168 /// @param x the x dimension expression
2169 /// @param y the y dimension expression
2170 /// @param z the z dimension expression
2171 /// @returns the workgroup decoration pointer
2172 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2173 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
2174 EXPR_X&& x,
2175 EXPR_Y&& y,
2176 EXPR_Z&& z) {
2177 return create<ast::WorkgroupDecoration>(
2178 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2179 Expr(std::forward<EXPR_Z>(z)));
2180 }
2181
2182 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002183 /// @param x the x dimension expression
2184 /// @param y the y dimension expression
2185 /// @param z the z dimension expression
2186 /// @returns the workgroup decoration pointer
2187 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2188 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2189 return create<ast::WorkgroupDecoration>(
2190 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2191 Expr(std::forward<EXPR_Z>(z)));
2192 }
2193
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002194 /// Sets the current builder source to `src`
2195 /// @param src the Source used for future create() calls
2196 void SetSource(const Source& src) {
2197 AssertNotMoved();
2198 source_ = src;
2199 }
2200
2201 /// Sets the current builder source to `loc`
2202 /// @param loc the Source used for future create() calls
2203 void SetSource(const Source::Location& loc) {
2204 AssertNotMoved();
2205 source_ = Source(loc);
2206 }
2207
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002208 /// Marks that the given transform has been applied to this program.
2209 /// @param transform the transform that has been applied
2210 void SetTransformApplied(const CastableBase* transform) {
2211 transforms_applied_.emplace(&transform->TypeInfo());
2212 }
2213
2214 /// Marks that the given transform `T` has been applied to this program.
2215 template <typename T>
2216 void SetTransformApplied() {
2217 transforms_applied_.emplace(&TypeInfo::Of<T>());
2218 }
2219
2220 /// Marks that the transforms with the given TypeInfos have been applied to
2221 /// this program.
2222 /// @param transforms the set of transform TypeInfos that has been applied
2223 void SetTransformApplied(
2224 const std::unordered_set<const TypeInfo*>& transforms) {
2225 for (auto* transform : transforms) {
2226 transforms_applied_.emplace(transform);
2227 }
2228 }
2229
2230 /// @returns true if the transform of type `T` was applied.
2231 template <typename T>
2232 bool HasTransformApplied() {
2233 return transforms_applied_.count(&TypeInfo::Of<T>());
2234 }
2235
2236 /// @return the TypeInfo pointers of all transforms that have been applied to
2237 /// this program.
2238 std::unordered_set<const TypeInfo*> TransformsApplied() const {
2239 return transforms_applied_;
2240 }
2241
Ben Clayton33352542021-01-29 16:43:41 +00002242 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002243 /// @note As the Resolver is run when the Program is built, this will only be
2244 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002245 /// @param expr the AST expression
2246 /// @return the resolved semantic type for the expression, or nullptr if the
2247 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002248 sem::Type* TypeOf(const ast::Expression* expr) const;
2249
Ben Claytonfe0910f2021-05-17 15:51:47 +00002250 /// Helper for returning the resolved semantic type of the variable `var`.
2251 /// @note As the Resolver is run when the Program is built, this will only be
2252 /// useful for the Resolver itself and tests that use their own Resolver.
2253 /// @param var the AST variable
2254 /// @return the resolved semantic type for the variable, or nullptr if the
2255 /// variable has no resolved type.
2256 sem::Type* TypeOf(const ast::Variable* var) const;
2257
Ben Claytonfbec46f2021-04-30 20:20:19 +00002258 /// Helper for returning the resolved semantic type of the AST type `type`.
2259 /// @note As the Resolver is run when the Program is built, this will only be
2260 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002261 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002262 /// @return the resolved semantic type for the type, or nullptr if the type
2263 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002264 const sem::Type* TypeOf(const ast::Type* type) const;
2265
2266 /// Helper for returning the resolved semantic type of the AST type
2267 /// declaration `type_decl`.
2268 /// @note As the Resolver is run when the Program is built, this will only be
2269 /// useful for the Resolver itself and tests that use their own Resolver.
2270 /// @param type_decl the AST type declaration
2271 /// @return the resolved semantic type for the type declaration, or nullptr if
2272 /// the type declaration has no resolved type.
2273 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002274
Ben Clayton169512e2021-04-17 05:52:11 +00002275 /// Wraps the ast::Literal in a statement. This is used by tests that
2276 /// construct a partial AST and require the Resolver to reach these
2277 /// nodes.
2278 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2279 /// @return the ast::Statement that wraps the ast::Statement
2280 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002281 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002282 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002283 /// nodes.
2284 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2285 /// @return the ast::Statement that wraps the ast::Expression
2286 ast::Statement* WrapInStatement(ast::Expression* expr);
2287 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002288 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002289 /// these nodes.
2290 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2291 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2292 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2293 /// Returns the statement argument. Used as a passthrough-overload by
2294 /// WrapInFunction().
2295 /// @param stmt the ast::Statement
2296 /// @return `stmt`
2297 ast::Statement* WrapInStatement(ast::Statement* stmt);
2298 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002299 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002300 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002301 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002302 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002303 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002304 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002305 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002306 }
2307 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002308 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002309 /// @returns the function
2310 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002311
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002312 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002313 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002314
2315 protected:
2316 /// Asserts that the builder has not been moved.
2317 void AssertNotMoved() const;
2318
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002319 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002320 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002321 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002322 ASTNodeAllocator ast_nodes_;
2323 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002324 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002325 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002326 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002327 diag::List diagnostics_;
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002328 std::unordered_set<const TypeInfo*> transforms_applied_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002329
2330 /// The source to use when creating AST nodes without providing a Source as
2331 /// the first argument.
2332 Source source_;
2333
Ben Clayton5f0ea112021-03-09 10:54:37 +00002334 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002335 /// program when built.
2336 bool resolve_on_build_ = true;
2337
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002338 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2339 bool moved_ = false;
2340};
2341
2342//! @cond Doxygen_Suppress
2343// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2344template <>
2345struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002346 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002347 return t->i32();
2348 }
2349};
2350template <>
2351struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002352 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002353 return t->u32();
2354 }
2355};
2356template <>
2357struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002358 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002359 return t->f32();
2360 }
2361};
2362template <>
2363struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002364 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002365 return t->bool_();
2366 }
2367};
2368template <>
2369struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002370 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002371 return t->void_();
2372 }
2373};
2374//! @endcond
2375
Ben Clayton917b14b2021-04-19 16:50:23 +00002376/// @param builder the ProgramBuilder
2377/// @returns the ProgramID of the ProgramBuilder
2378inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2379 return builder->ID();
2380}
2381
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002382} // namespace tint
2383
2384#endif // SRC_PROGRAM_BUILDER_H_