blob: d4a25cfe52c2b94ef85b680d7ca554096df6e77c [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 Claytoncd7eb4f2021-07-17 18:09:26 +0000108 /// A helper used to disable overloads if the first type in `TYPES` is a
109 /// Source. Used to avoid ambiguities in overloads that take a Source as the
110 /// first parameter and those that perfectly-forward the first argument.
111 template <typename... TYPES>
112 using DisableIfSource = traits::EnableIfIsNotType<
113 traits::Decay<traits::NthTypeOf<0, TYPES..., void>>,
114 Source>;
115
Ben Clayton620d77e2021-06-04 19:55:08 +0000116 /// VarOptionals is a helper for accepting a number of optional, extra
117 /// arguments for Var() and Global().
118 struct VarOptionals {
119 template <typename... ARGS>
120 explicit VarOptionals(ARGS&&... args) {
121 Apply(std::forward<ARGS>(args)...);
122 }
123 ~VarOptionals();
124
125 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000126 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000127 ast::Expression* constructor = nullptr;
128 ast::DecorationList decorations = {};
129
130 private:
131 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000132 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000133 void Set(ast::Expression* c) { constructor = c; }
134 void Set(const ast::DecorationList& l) { decorations = l; }
135
136 template <typename FIRST, typename... ARGS>
137 void Apply(FIRST&& first, ARGS&&... args) {
138 Set(std::forward<FIRST>(first));
139 Apply(std::forward<ARGS>(args)...);
140 }
141 void Apply() {}
142 };
143
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000144 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000145 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
146 using ASTNodeAllocator = BlockAllocator<ast::Node>;
147
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000148 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
149 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000150
151 /// `i32` is a type alias to `int`.
152 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
153 /// WGSL syntax.
154 /// Note: this is intentionally not aliased to uint32_t as we want integer
155 /// literals passed to the builder to match WGSL's integer literal types.
156 using i32 = decltype(1);
157 /// `u32` is a type alias to `unsigned int`.
158 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
159 /// WGSL syntax.
160 /// Note: this is intentionally not aliased to uint32_t as we want integer
161 /// literals passed to the builder to match WGSL's integer literal types.
162 using u32 = decltype(1u);
163 /// `f32` is a type alias to `float`
164 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
165 /// WGSL syntax.
166 using f32 = float;
167
168 /// Constructor
169 ProgramBuilder();
170
171 /// Move constructor
172 /// @param rhs the builder to move
173 ProgramBuilder(ProgramBuilder&& rhs);
174
175 /// Destructor
176 virtual ~ProgramBuilder();
177
178 /// Move assignment operator
179 /// @param rhs the builder to move
180 /// @return this builder
181 ProgramBuilder& operator=(ProgramBuilder&& rhs);
182
Ben Claytone43c8302021-01-29 11:59:32 +0000183 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
184 /// making a deep clone of the Program contents.
185 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
186 /// existing immutable program.
187 /// As the returned ProgramBuilder wraps `program`, `program` must not be
188 /// destructed or assigned while using the returned ProgramBuilder.
189 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
190 /// function. See crbug.com/tint/460.
191 /// @param program the immutable Program to wrap
192 /// @return the ProgramBuilder that wraps `program`
193 static ProgramBuilder Wrap(const Program* program);
194
Ben Claytone6995de2021-04-13 23:27:27 +0000195 /// @returns the unique identifier for this program
196 ProgramID ID() const { return id_; }
197
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000198 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000199 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000200 AssertNotMoved();
201 return types_;
202 }
203
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000204 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000205 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000206 AssertNotMoved();
207 return types_;
208 }
209
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000210 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000211 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000212 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000213 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000214 }
215
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000216 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000217 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000218 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000219 return ast_nodes_;
220 }
221
222 /// @returns a reference to the program's semantic nodes storage
223 SemNodeAllocator& SemNodes() {
224 AssertNotMoved();
225 return sem_nodes_;
226 }
227
228 /// @returns a reference to the program's semantic nodes storage
229 const SemNodeAllocator& SemNodes() const {
230 AssertNotMoved();
231 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000232 }
233
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000234 /// @returns a reference to the program's AST root Module
235 ast::Module& AST() {
236 AssertNotMoved();
237 return *ast_;
238 }
239
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000240 /// @returns a reference to the program's AST root Module
241 const ast::Module& AST() const {
242 AssertNotMoved();
243 return *ast_;
244 }
245
246 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000247 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000248 AssertNotMoved();
249 return sem_;
250 }
251
252 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000253 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000254 AssertNotMoved();
255 return sem_;
256 }
257
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000258 /// @returns a reference to the program's SymbolTable
259 SymbolTable& Symbols() {
260 AssertNotMoved();
261 return symbols_;
262 }
263
Ben Clayton708dc2d2021-01-29 11:22:40 +0000264 /// @returns a reference to the program's SymbolTable
265 const SymbolTable& Symbols() const {
266 AssertNotMoved();
267 return symbols_;
268 }
269
Ben Clayton844217f2021-01-27 18:49:05 +0000270 /// @returns a reference to the program's diagnostics
271 diag::List& Diagnostics() {
272 AssertNotMoved();
273 return diagnostics_;
274 }
275
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000276 /// @returns a reference to the program's diagnostics
277 const diag::List& Diagnostics() const {
278 AssertNotMoved();
279 return diagnostics_;
280 }
281
Ben Clayton5f0ea112021-03-09 10:54:37 +0000282 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000283 /// @param enable the new flag value (defaults to true)
284 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
285
Ben Clayton5f0ea112021-03-09 10:54:37 +0000286 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000287 /// built.
288 bool ResolveOnBuild() const { return resolve_on_build_; }
289
Ben Clayton844217f2021-01-27 18:49:05 +0000290 /// @returns true if the program has no error diagnostics and is not missing
291 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000292 bool IsValid() const;
293
Ben Clayton708dc2d2021-01-29 11:22:40 +0000294 /// Writes a representation of the node to the output stream
295 /// @note unlike str(), to_str() does not automatically demangle the string.
296 /// @param node the AST node
297 /// @param out the stream to write to
298 /// @param indent number of spaces to indent the node when writing
299 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
300 node->to_str(Sem(), out, indent);
301 }
302
303 /// Returns a demangled, string representation of `node`.
304 /// @param node the AST node
305 /// @returns a string representation of the node
306 std::string str(const ast::Node* node) const;
307
Ben Clayton7fdfff12021-01-29 15:17:30 +0000308 /// Creates a new ast::Node owned by the ProgramBuilder. When the
309 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000310 /// @param source the Source of the node
311 /// @param args the arguments to pass to the type constructor
312 /// @returns the node pointer
313 template <typename T, typename... ARGS>
314 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
315 ARGS&&... args) {
316 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000317 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000318 }
319
Ben Clayton7fdfff12021-01-29 15:17:30 +0000320 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
321 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000322 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000323 /// When the ProgramBuilder is destructed, the ast::Node will also be
324 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000325 /// @returns the node pointer
326 template <typename T>
327 traits::EnableIfIsType<T, ast::Node>* create() {
328 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000329 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000330 }
331
Ben Clayton7fdfff12021-01-29 15:17:30 +0000332 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
333 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000334 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000335 /// When the ProgramBuilder is destructed, the ast::Node will also be
336 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000337 /// @param arg0 the first arguments to pass to the type constructor
338 /// @param args the remaining arguments to pass to the type constructor
339 /// @returns the node pointer
340 template <typename T, typename ARG0, typename... ARGS>
341 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
342 traits::IsTypeOrDerived<T, ast::Node>::value &&
343 !traits::IsTypeOrDerived<ARG0, Source>::value,
344 T>*
345 create(ARG0&& arg0, ARGS&&... args) {
346 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000347 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000348 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000349 }
350
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000351 /// Creates a new sem::Node owned by the ProgramBuilder.
352 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000353 /// destructed.
354 /// @param args the arguments to pass to the type constructor
355 /// @returns the node pointer
356 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000357 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
358 !traits::IsTypeOrDerived<T, sem::Type>::value,
359 T>*
360 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000361 AssertNotMoved();
362 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
363 }
364
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000365 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000366 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
367 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000368 /// Types are unique (de-aliased), and so calling create() for the same `T`
369 /// and arguments will return the same pointer.
370 /// @warning Use this method to acquire a type only if all of its type
371 /// information is provided in the constructor arguments `args`.<br>
372 /// If the type requires additional configuration after construction that
373 /// affect its fundamental type, build the type with `std::make_unique`, make
374 /// any necessary alterations and then call unique_type() instead.
375 /// @param args the arguments to pass to the type constructor
376 /// @returns the de-aliased type pointer
377 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000378 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
379 static_assert(std::is_base_of<sem::Type, T>::value,
380 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000381 AssertNotMoved();
382 return types_.Get<T>(std::forward<ARGS>(args)...);
383 }
384
385 /// Marks this builder as moved, preventing any further use of the builder.
386 void MarkAsMoved();
387
388 //////////////////////////////////////////////////////////////////////////////
389 // TypesBuilder
390 //////////////////////////////////////////////////////////////////////////////
391
392 /// TypesBuilder holds basic `tint` types and methods for constructing
393 /// complex types.
394 class TypesBuilder {
395 public:
396 /// Constructor
397 /// @param builder the program builder
398 explicit TypesBuilder(ProgramBuilder* builder);
399
400 /// @return the tint AST type for the C type `T`.
401 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000402 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000403 return CToAST<T>::get(this);
404 }
405
406 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000407 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000408
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000409 /// @param source the Source of the node
410 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000411 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000412 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000413 }
414
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000415 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000416 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000417
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000418 /// @param source the Source of the node
419 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000420 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000421 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000422 }
423
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000424 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000425 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000426
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000427 /// @param source the Source of the node
428 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000429 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000430 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000431 }
432
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000433 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000434 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000435
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000436 /// @param source the Source of the node
437 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000438 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000439 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000440 }
441
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000442 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000443 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000444
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000445 /// @param source the Source of the node
446 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000447 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000448 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000449 }
450
Antonio Maiorano25436862021-04-13 13:32:33 +0000451 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000452 /// @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(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000455 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000456 }
457
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000458 /// @param source the Source of the node
459 /// @param type vector subtype
460 /// @param n vector width in elements
461 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000462 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000463 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000464 }
465
Antonio Maiorano25436862021-04-13 13:32:33 +0000466 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000467 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000468 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000469
470 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000471 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000472 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000473
474 /// @param type vector subtype
475 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000476 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000477
478 /// @param n vector width in elements
479 /// @return the tint AST type for a `n`-element vector of `type`.
480 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000481 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000482 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000483 }
484
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000485 /// @return the tint AST type for a 2-element vector of the C type `T`.
486 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000487 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000488 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000489 }
490
491 /// @return the tint AST type for a 3-element vector of the C type `T`.
492 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000493 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000494 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000495 }
496
497 /// @return the tint AST type for a 4-element vector of the C type `T`.
498 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000499 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000500 return vec4(Of<T>());
501 }
502
503 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000504 /// @param columns number of columns for the matrix
505 /// @param rows number of rows for the matrix
506 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000507 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000508 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000509 }
510
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000511 /// @param source the Source of the node
512 /// @param type matrix subtype
513 /// @param columns number of columns for the matrix
514 /// @param rows number of rows for the matrix
515 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000516 ast::Matrix* mat(const Source& source,
517 ast::Type* type,
518 uint32_t columns,
519 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000520 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000521 }
522
Ben Claytone204f272021-04-22 14:40:23 +0000523 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000524 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000525 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000526
527 /// @param type matrix subtype
528 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000529 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000530
531 /// @param type matrix subtype
532 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000533 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000534
535 /// @param type matrix subtype
536 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000537 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000538
539 /// @param type matrix subtype
540 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000541 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000542
543 /// @param type matrix subtype
544 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000545 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000546
547 /// @param type matrix subtype
548 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000549 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000550
551 /// @param type matrix subtype
552 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000553 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000554
555 /// @param type matrix subtype
556 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000557 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000558
Ben Claytone204f272021-04-22 14:40:23 +0000559 /// @param columns number of columns for the matrix
560 /// @param rows number of rows for the matrix
561 /// @return the tint AST type for a matrix of `type`
562 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000563 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000564 return mat(Of<T>(), columns, rows);
565 }
566
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000567 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
568 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000569 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000570 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000571 }
572
573 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
574 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000575 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000576 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000577 }
578
579 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
580 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000581 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000582 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000583 }
584
585 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
586 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000587 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000588 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000589 }
590
591 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
592 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000593 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000594 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000595 }
596
597 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
598 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000599 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000600 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000601 }
602
603 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
604 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000605 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000606 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000607 }
608
609 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
610 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000611 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000612 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000613 }
614
615 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
616 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000617 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000618 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000619 }
620
621 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000622 /// @param n the array size. 0 represents a runtime-array
623 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000624 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000625 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000626 uint32_t n = 0,
627 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000628 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000629 }
630
631 /// @param source the Source of the node
632 /// @param subtype the array element type
633 /// @param n the array size. 0 represents a runtime-array
634 /// @param decos the optional decorations for the array
635 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000636 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000637 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000638 uint32_t n = 0,
639 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000640 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000641 }
642
Ben Claytonbab31972021-02-08 21:16:21 +0000643 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000644 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000645 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000646 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000647 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000648 ast::DecorationList decos;
649 if (stride) {
650 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
651 }
652 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000653 }
654
Ben Clayton0f88b312021-05-04 17:36:31 +0000655 /// @param source the Source of the node
656 /// @param subtype the array element type
657 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000658 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000659 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000660 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000661 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000662 uint32_t n,
663 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000664 ast::DecorationList decos;
665 if (stride) {
666 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
667 }
668 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000669 }
670
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000671 /// @return the tint AST type for an array of size `N` of type `T`
672 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000673 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000674 return array(Of<T>(), N);
675 }
676
Ben Claytonbab31972021-02-08 21:16:21 +0000677 /// @param stride the array stride
678 /// @return the tint AST type for an array of size `N` of type `T`
679 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000680 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000681 return array(Of<T>(), N, stride);
682 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000683
Ben Clayton0f88b312021-05-04 17:36:31 +0000684 /// Creates a type name
685 /// @param name the name
686 /// @returns the type name
687 template <typename NAME>
688 ast::TypeName* type_name(NAME&& name) const {
689 return builder->create<ast::TypeName>(
690 builder->Sym(std::forward<NAME>(name)));
691 }
692
693 /// Creates a type name
694 /// @param source the Source of the node
695 /// @param name the name
696 /// @returns the type name
697 template <typename NAME>
698 ast::TypeName* type_name(const Source& source, NAME&& name) const {
699 return builder->create<ast::TypeName>(
700 source, builder->Sym(std::forward<NAME>(name)));
701 }
702
Ben Clayton42d1e092021-02-02 14:29:15 +0000703 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000704 /// @param name the alias name
705 /// @param type the alias type
706 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000707 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000708 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000709 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000710 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000711 }
712
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000713 /// Creates an alias type
714 /// @param source the Source of the node
715 /// @param name the alias name
716 /// @param type the alias type
717 /// @returns the alias pointer
718 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000719 ast::Alias* alias(const Source& source,
720 NAME&& name,
721 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000722 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000723 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000724 }
725
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000726 /// @param type the type of the pointer
727 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000728 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000729 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000730 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000731 ast::StorageClass storage_class,
732 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000733 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000734 }
735
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000736 /// @param source the Source of the node
737 /// @param type the type of the pointer
738 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000739 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000740 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000741 ast::Pointer* pointer(const Source& source,
742 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000743 ast::StorageClass storage_class,
744 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000745 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000746 }
747
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000748 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000749 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000750 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000751 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000752 ast::Pointer* pointer(ast::StorageClass storage_class,
753 ast::Access access = ast::Access::kUndefined) const {
754 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000755 }
756
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000757 /// @param source the Source of the node
758 /// @param type the type of the atomic
759 /// @return the atomic to `type`
760 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
761 return builder->create<ast::Atomic>(source, type);
762 }
763
Ben Clayton313e6182021-06-17 19:56:14 +0000764 /// @param type the type of the atomic
765 /// @return the atomic to `type`
766 ast::Atomic* atomic(ast::Type* type) const {
767 return builder->create<ast::Atomic>(type);
768 }
769
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000770 /// @return the atomic to type `T`
771 template <typename T>
772 ast::Atomic* atomic() const {
773 return atomic(Of<T>());
774 }
775
Ben Claytone204f272021-04-22 14:40:23 +0000776 /// @param kind the kind of sampler
777 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000778 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000779 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000780 }
781
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000782 /// @param source the Source of the node
783 /// @param kind the kind of sampler
784 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000785 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000786 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000787 }
788
Ben Claytone204f272021-04-22 14:40:23 +0000789 /// @param dims the dimensionality of the texture
790 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000791 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000792 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000793 }
794
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000795 /// @param source the Source of the node
796 /// @param dims the dimensionality of the texture
797 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000798 ast::DepthTexture* depth_texture(const Source& source,
799 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000800 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000801 }
802
Ben Claytone204f272021-04-22 14:40:23 +0000803 /// @param dims the dimensionality of the texture
804 /// @param subtype the texture subtype.
805 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000806 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
807 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000808 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000809 }
810
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000811 /// @param source the Source of the node
812 /// @param dims the dimensionality of the texture
813 /// @param subtype the texture subtype.
814 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000815 ast::SampledTexture* sampled_texture(const Source& source,
816 ast::TextureDimension dims,
817 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000818 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000819 }
820
Ben Claytone204f272021-04-22 14:40:23 +0000821 /// @param dims the dimensionality of the texture
822 /// @param subtype the texture subtype.
823 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000824 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
825 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000826 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000827 }
828
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000829 /// @param source the Source of the node
830 /// @param dims the dimensionality of the texture
831 /// @param subtype the texture subtype.
832 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000833 ast::MultisampledTexture* multisampled_texture(const Source& source,
834 ast::TextureDimension dims,
835 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000836 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000837 }
838
Ben Claytone204f272021-04-22 14:40:23 +0000839 /// @param dims the dimensionality of the texture
840 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000841 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000842 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000843 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000844 ast::ImageFormat format,
845 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000846 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000847 return builder->create<ast::StorageTexture>(dims, format, subtype,
848 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000849 }
850
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000851 /// @param source the Source of the node
852 /// @param dims the dimensionality of the texture
853 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000854 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000855 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000856 ast::StorageTexture* storage_texture(const Source& source,
857 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000858 ast::ImageFormat format,
859 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000860 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000861 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
862 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000863 }
864
Brandon Jones7b257692021-05-17 17:40:17 +0000865 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000866 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000867 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000868 }
869
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000870 /// @param source the Source of the node
871 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000872 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000873 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000874 }
875
Ben Clayton19b03192021-05-20 15:04:08 +0000876 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000877 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000878 /// @param type the type
879 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000880 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000881
Ben Clayton8758f102021-06-09 14:32:14 +0000882 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000883 /// @param type the type
884 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000885 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000886
Ben Claytonf5f311e2021-04-28 14:31:23 +0000887 /// The ProgramBuilder
888 ProgramBuilder* const builder;
889
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000890 private:
891 /// CToAST<T> is specialized for various `T` types and each specialization
892 /// contains a single static `get()` method for obtaining the corresponding
893 /// AST type for the C type `T`.
894 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000895 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000896 template <typename T>
897 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000898 };
899
900 //////////////////////////////////////////////////////////////////////////////
901 // AST helper methods
902 //////////////////////////////////////////////////////////////////////////////
903
James Price65ae64d2021-04-29 12:59:14 +0000904 /// @return a new unnamed symbol
905 Symbol Sym() { return Symbols().New(); }
906
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000907 /// @param name the symbol string
908 /// @return a Symbol with the given name
909 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
910
911 /// @param sym the symbol
912 /// @return `sym`
913 Symbol Sym(Symbol sym) { return sym; }
914
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000915 /// @param expr the expression
916 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000917 template <typename T>
918 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
919 return expr;
920 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000921
Ben Claytonb8ea5912021-04-19 16:52:42 +0000922 /// Passthrough for nullptr
923 /// @return nullptr
924 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
925
Ben Claytonfe0910f2021-05-17 15:51:47 +0000926 /// @param source the source information
927 /// @param symbol the identifier symbol
928 /// @return an ast::IdentifierExpression with the given symbol
929 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
930 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000931 }
932
Ben Clayton46d78d72021-02-10 21:34:26 +0000933 /// @param symbol the identifier symbol
934 /// @return an ast::IdentifierExpression with the given symbol
935 ast::IdentifierExpression* Expr(Symbol symbol) {
936 return create<ast::IdentifierExpression>(symbol);
937 }
938
Ben Claytonfe0910f2021-05-17 15:51:47 +0000939 /// @param source the source information
940 /// @param variable the AST variable
941 /// @return an ast::IdentifierExpression with the variable's symbol
942 ast::IdentifierExpression* Expr(const Source& source,
943 ast::Variable* variable) {
944 return create<ast::IdentifierExpression>(source, variable->symbol());
945 }
946
Ben Claytonb8ea5912021-04-19 16:52:42 +0000947 /// @param variable the AST variable
948 /// @return an ast::IdentifierExpression with the variable's symbol
949 ast::IdentifierExpression* Expr(ast::Variable* variable) {
950 return create<ast::IdentifierExpression>(variable->symbol());
951 }
952
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000953 /// @param source the source information
954 /// @param name the identifier name
955 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000956 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
957 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
958 }
959
960 /// @param name the identifier name
961 /// @return an ast::IdentifierExpression with the given name
962 ast::IdentifierExpression* Expr(const char* name) {
963 return create<ast::IdentifierExpression>(Symbols().Register(name));
964 }
965
966 /// @param source the source information
967 /// @param name the identifier name
968 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000969 ast::IdentifierExpression* Expr(const Source& source,
970 const std::string& name) {
971 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
972 }
973
974 /// @param name the identifier name
975 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000976 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000977 return create<ast::IdentifierExpression>(Symbols().Register(name));
978 }
979
Ben Claytonfe0910f2021-05-17 15:51:47 +0000980 /// @param source the source information
981 /// @param value the boolean value
982 /// @return a Scalar constructor for the given value
983 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
984 return create<ast::ScalarConstructorExpression>(source, Literal(value));
985 }
986
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000987 /// @param value the boolean value
988 /// @return a Scalar constructor for the given value
989 ast::ScalarConstructorExpression* Expr(bool value) {
990 return create<ast::ScalarConstructorExpression>(Literal(value));
991 }
992
Ben Claytonfe0910f2021-05-17 15:51:47 +0000993 /// @param source the source information
994 /// @param value the float value
995 /// @return a Scalar constructor for the given value
996 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
997 return create<ast::ScalarConstructorExpression>(source, Literal(value));
998 }
999
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001000 /// @param value the float value
1001 /// @return a Scalar constructor for the given value
1002 ast::ScalarConstructorExpression* Expr(f32 value) {
1003 return create<ast::ScalarConstructorExpression>(Literal(value));
1004 }
1005
Ben Claytonfe0910f2021-05-17 15:51:47 +00001006 /// @param source the source information
1007 /// @param value the integer value
1008 /// @return a Scalar constructor for the given value
1009 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1010 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1011 }
1012
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001013 /// @param value the integer value
1014 /// @return a Scalar constructor for the given value
1015 ast::ScalarConstructorExpression* Expr(i32 value) {
1016 return create<ast::ScalarConstructorExpression>(Literal(value));
1017 }
1018
Ben Claytonfe0910f2021-05-17 15:51:47 +00001019 /// @param source the source information
1020 /// @param value the unsigned int value
1021 /// @return a Scalar constructor for the given value
1022 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1023 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1024 }
1025
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001026 /// @param value the unsigned int value
1027 /// @return a Scalar constructor for the given value
1028 ast::ScalarConstructorExpression* Expr(u32 value) {
1029 return create<ast::ScalarConstructorExpression>(Literal(value));
1030 }
1031
1032 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1033 /// `list`.
1034 /// @param list the list to append too
1035 /// @param arg the arg to create
1036 template <typename ARG>
1037 void Append(ast::ExpressionList& list, ARG&& arg) {
1038 list.emplace_back(Expr(std::forward<ARG>(arg)));
1039 }
1040
1041 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1042 /// then appends them to `list`.
1043 /// @param list the list to append too
1044 /// @param arg0 the first argument
1045 /// @param args the rest of the arguments
1046 template <typename ARG0, typename... ARGS>
1047 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1048 Append(list, std::forward<ARG0>(arg0));
1049 Append(list, std::forward<ARGS>(args)...);
1050 }
1051
1052 /// @return an empty list of expressions
1053 ast::ExpressionList ExprList() { return {}; }
1054
1055 /// @param args the list of expressions
1056 /// @return the list of expressions converted to `ast::Expression`s using
1057 /// `Expr()`,
1058 template <typename... ARGS>
1059 ast::ExpressionList ExprList(ARGS&&... args) {
1060 ast::ExpressionList list;
1061 list.reserve(sizeof...(args));
1062 Append(list, std::forward<ARGS>(args)...);
1063 return list;
1064 }
1065
1066 /// @param list the list of expressions
1067 /// @return `list`
1068 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1069
1070 /// @param val the boolan value
1071 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001072 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001073
1074 /// @param val the float value
1075 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001076 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001077
1078 /// @param val the unsigned int value
1079 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001080 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001081
1082 /// @param val the integer value
1083 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001084 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001085
1086 /// @param args the arguments for the type constructor
1087 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1088 /// of `args` converted to `ast::Expression`s using `Expr()`
1089 template <typename T, typename... ARGS>
1090 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001091 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001092 }
1093
1094 /// @param type the type to construct
1095 /// @param args the arguments for the constructor
1096 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1097 /// values `args`.
1098 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001099 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001100 return create<ast::TypeConstructorExpression>(
1101 type, ExprList(std::forward<ARGS>(args)...));
1102 }
1103
Ben Clayton313e6182021-06-17 19:56:14 +00001104 /// @param source the source information
1105 /// @param type the type to construct
1106 /// @param args the arguments for the constructor
1107 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1108 /// values `args`.
1109 template <typename... ARGS>
1110 ast::TypeConstructorExpression* Construct(const Source& source,
1111 ast::Type* type,
1112 ARGS&&... args) {
1113 return create<ast::TypeConstructorExpression>(
1114 source, type, ExprList(std::forward<ARGS>(args)...));
1115 }
1116
Ben Claytond9603282021-06-29 12:37:45 +00001117 /// @param expr the expression for the bitcast
1118 /// @return an `ast::BitcastExpression` of type `ty`, with the values of
1119 /// `expr` converted to `ast::Expression`s using `Expr()`
1120 template <typename T, typename EXPR>
1121 ast::BitcastExpression* Bitcast(EXPR&& expr) {
1122 return Bitcast(ty.Of<T>(), std::forward<EXPR>(expr));
1123 }
1124
1125 /// @param type the type to cast to
1126 /// @param expr the expression for the bitcast
1127 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1128 /// `expr`.
1129 template <typename EXPR>
1130 ast::BitcastExpression* Bitcast(ast::Type* type, EXPR&& expr) {
1131 return create<ast::BitcastExpression>(type, Expr(std::forward<EXPR>(expr)));
1132 }
1133
1134 /// @param source the source information
1135 /// @param type the type to cast to
1136 /// @param expr the expression for the bitcast
1137 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1138 /// `expr`.
1139 template <typename EXPR>
1140 ast::BitcastExpression* Bitcast(const Source& source,
1141 ast::Type* type,
1142 EXPR&& expr) {
1143 return create<ast::BitcastExpression>(source, type,
1144 Expr(std::forward<EXPR>(expr)));
1145 }
1146
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001147 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001148 /// @param type the vector type
1149 /// @param size the vector size
1150 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1151 /// type `type`, constructed with the values `args`.
1152 template <typename... ARGS>
1153 ast::TypeConstructorExpression* vec(ast::Type* type,
1154 uint32_t size,
1155 ARGS&&... args) {
1156 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1157 }
1158
1159 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001160 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1161 /// `T`, constructed with the values `args`.
1162 template <typename T, typename... ARGS>
1163 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001164 return Construct(ty.vec2<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 3-element vector of type
1169 /// `T`, constructed with the values `args`.
1170 template <typename T, typename... ARGS>
1171 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001172 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001173 }
1174
1175 /// @param args the arguments for the vector constructor
1176 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1177 /// `T`, constructed with the values `args`.
1178 template <typename T, typename... ARGS>
1179 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001180 return Construct(ty.vec4<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 2x2 matrix of type
1185 /// `T`, constructed with the values `args`.
1186 template <typename T, typename... ARGS>
1187 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001188 return Construct(ty.mat2x2<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 2x3 matrix of type
1193 /// `T`, constructed with the values `args`.
1194 template <typename T, typename... ARGS>
1195 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001196 return Construct(ty.mat2x3<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 2x4 matrix of type
1201 /// `T`, constructed with the values `args`.
1202 template <typename T, typename... ARGS>
1203 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001204 return Construct(ty.mat2x4<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 3x2 matrix of type
1209 /// `T`, constructed with the values `args`.
1210 template <typename T, typename... ARGS>
1211 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001212 return Construct(ty.mat3x2<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 3x3 matrix of type
1217 /// `T`, constructed with the values `args`.
1218 template <typename T, typename... ARGS>
1219 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001220 return Construct(ty.mat3x3<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 3x4 matrix of type
1225 /// `T`, constructed with the values `args`.
1226 template <typename T, typename... ARGS>
1227 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001228 return Construct(ty.mat3x4<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 4x2 matrix of type
1233 /// `T`, constructed with the values `args`.
1234 template <typename T, typename... ARGS>
1235 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001236 return Construct(ty.mat4x2<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 4x3 matrix of type
1241 /// `T`, constructed with the values `args`.
1242 template <typename T, typename... ARGS>
1243 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001244 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001245 }
1246
1247 /// @param args the arguments for the matrix constructor
1248 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1249 /// `T`, constructed with the values `args`.
1250 template <typename T, typename... ARGS>
1251 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001252 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001253 }
1254
1255 /// @param args the arguments for the array constructor
1256 /// @return an `ast::TypeConstructorExpression` of an array with element type
1257 /// `T`, constructed with the values `args`.
1258 template <typename T, int N = 0, typename... ARGS>
1259 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001260 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001261 }
1262
1263 /// @param subtype the array element type
1264 /// @param n the array size. 0 represents a runtime-array.
1265 /// @param args the arguments for the array constructor
1266 /// @return an `ast::TypeConstructorExpression` of an array with element type
1267 /// `subtype`, constructed with the values `args`.
1268 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001269 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001270 uint32_t n,
1271 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001272 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001273 }
1274
1275 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001276 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001277 /// @param optional the optional variable settings.
1278 /// Can be any of the following, in any order:
1279 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001280 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001281 /// * ast::Expression* - specifies the variable's initializer expression
1282 /// * ast::DecorationList - specifies the variable's decorations
1283 /// Note that repeated arguments of the same type will use the last argument's
1284 /// value.
1285 /// @returns a `ast::Variable` with the given name, type and additional
1286 /// options
1287 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001288 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001289 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001290 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001291 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1292 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001293 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001294 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001295 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001296
1297 /// @param source the variable source
1298 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001299 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001300 /// @param optional the optional variable settings.
1301 /// Can be any of the following, in any order:
1302 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001303 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001304 /// * ast::Expression* - specifies the variable's initializer expression
1305 /// * ast::DecorationList - specifies the variable's decorations
1306 /// Note that repeated arguments of the same type will use the last argument's
1307 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001308 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001309 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001310 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001311 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001312 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001313 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001314 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1315 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001316 opts.storage, opts.access, type, false,
1317 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001318 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001319
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001320 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001321 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001322 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001323 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001324 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001325 template <typename NAME>
1326 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001327 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001328 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001329 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001330 return create<ast::Variable>(
1331 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1332 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001333 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001334
1335 /// @param source the variable source
1336 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001337 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001338 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001339 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001340 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001341 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001342 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001343 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001344 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001345 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001346 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001347 return create<ast::Variable>(
1348 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1349 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001350 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001351
James Pricedaf1f1c2021-04-08 22:15:48 +00001352 /// @param name the parameter name
1353 /// @param type the parameter type
1354 /// @param decorations optional parameter decorations
1355 /// @returns a constant `ast::Variable` with the given name and type
1356 template <typename NAME>
1357 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001358 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001359 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001360 return create<ast::Variable>(
1361 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1362 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001363 }
1364
1365 /// @param source the parameter source
1366 /// @param name the parameter name
1367 /// @param type the parameter type
1368 /// @param decorations optional parameter decorations
1369 /// @returns a constant `ast::Variable` with the given name and type
1370 template <typename NAME>
1371 ast::Variable* Param(const Source& source,
1372 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001373 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001374 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001375 return create<ast::Variable>(
1376 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1377 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001378 }
1379
Ben Clayton42708342021-04-21 17:55:12 +00001380 /// @param name the variable name
1381 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001382 /// @param optional the optional variable settings.
1383 /// Can be any of the following, in any order:
1384 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001385 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001386 /// * ast::Expression* - specifies the variable's initializer expression
1387 /// * ast::DecorationList - specifies the variable's decorations
1388 /// Note that repeated arguments of the same type will use the last argument's
1389 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001390 /// @returns a new `ast::Variable`, which is automatically registered as a
1391 /// global variable with the ast::Module.
Antonio Maiorano60dae242021-07-15 19:09:25 +00001392 template <typename NAME,
1393 typename... OPTIONAL,
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001394 typename = DisableIfSource<NAME>>
Ben Clayton42708342021-04-21 17:55:12 +00001395 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001396 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001397 OPTIONAL&&... optional) {
1398 auto* var = Var(std::forward<NAME>(name), type,
1399 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001400 AST().AddGlobalVariable(var);
1401 return var;
1402 }
1403
1404 /// @param source the variable source
1405 /// @param name the variable name
1406 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001407 /// @param optional the optional variable settings.
1408 /// Can be any of the following, in any order:
1409 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001410 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001411 /// * ast::Expression* - specifies the variable's initializer expression
1412 /// * ast::DecorationList - specifies the variable's decorations
1413 /// Note that repeated arguments of the same type will use the last argument's
1414 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001415 /// @returns a new `ast::Variable`, which is automatically registered as a
1416 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001417 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001418 ast::Variable* Global(const Source& source,
1419 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001420 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001421 OPTIONAL&&... optional) {
1422 auto* var = Var(source, std::forward<NAME>(name), type,
1423 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001424 AST().AddGlobalVariable(var);
1425 return var;
1426 }
1427
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001428 /// @param name the variable name
1429 /// @param type the variable type
1430 /// @param constructor constructor expression
1431 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001432 /// @returns a const `ast::Variable` constructed by calling Var() with the
1433 /// arguments of `args`, which is automatically registered as a global
1434 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001435 template <typename NAME>
1436 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001437 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001438 ast::Expression* constructor,
1439 ast::DecorationList decorations = {}) {
1440 auto* var = Const(std::forward<NAME>(name), type, constructor,
1441 std::move(decorations));
1442 AST().AddGlobalVariable(var);
1443 return var;
1444 }
1445
1446 /// @param source the variable source
1447 /// @param name the variable name
1448 /// @param type the variable type
1449 /// @param constructor constructor expression
1450 /// @param decorations optional variable decorations
1451 /// @returns a const `ast::Variable` constructed by calling Var() with the
1452 /// arguments of `args`, which is automatically registered as a global
1453 /// variable with the ast::Module.
1454 template <typename NAME>
1455 ast::Variable* GlobalConst(const Source& source,
1456 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001457 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001458 ast::Expression* constructor,
1459 ast::DecorationList decorations = {}) {
1460 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1461 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001462 AST().AddGlobalVariable(var);
1463 return var;
1464 }
1465
Ben Claytonfe0910f2021-05-17 15:51:47 +00001466 /// @param source the source information
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(const Source& source, EXPR&& expr) {
1471 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1472 Expr(std::forward<EXPR>(expr)));
1473 }
1474
1475 /// @param expr the expression to take the address of
1476 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1477 template <typename EXPR>
1478 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1479 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1480 Expr(std::forward<EXPR>(expr)));
1481 }
1482
1483 /// @param source the source information
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(const Source& source, EXPR&& expr) {
1488 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1489 Expr(std::forward<EXPR>(expr)));
1490 }
1491
1492 /// @param expr the expression to perform an indirection on
1493 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1494 template <typename EXPR>
1495 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1496 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1497 Expr(std::forward<EXPR>(expr)));
1498 }
1499
Antonio Maiorano14b34032021-06-09 20:17:59 +00001500 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001501 /// @param func the function name
1502 /// @param args the function call arguments
1503 /// @returns a `ast::CallExpression` to the function `func`, with the
1504 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1505 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001506 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1507 return create<ast::CallExpression>(source, Expr(func),
1508 ExprList(std::forward<ARGS>(args)...));
1509 }
1510
1511 /// @param func the function name
1512 /// @param args the function call arguments
1513 /// @returns a `ast::CallExpression` to the function `func`, with the
1514 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001515 template <typename NAME, typename... ARGS, typename = DisableIfSource<NAME>>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001516 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1517 return create<ast::CallExpression>(Expr(func),
1518 ExprList(std::forward<ARGS>(args)...));
1519 }
1520
Ben Clayton7fe01062021-06-11 13:22:27 +00001521 /// @param expr the expression to ignore
1522 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1523 /// passed the single `expr` argument
1524 template <typename EXPR>
1525 ast::CallStatement* Ignore(EXPR&& expr) {
1526 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1527 }
1528
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001529 /// @param lhs the left hand argument to the addition operation
1530 /// @param rhs the right hand argument to the addition operation
1531 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1532 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001533 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001534 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1535 Expr(std::forward<LHS>(lhs)),
1536 Expr(std::forward<RHS>(rhs)));
1537 }
1538
James Pricec32e8f62021-06-22 20:08:29 +00001539 /// @param lhs the left hand argument to the and operation
1540 /// @param rhs the right hand argument to the and operation
1541 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
1542 template <typename LHS, typename RHS>
1543 ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
1544 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
1545 Expr(std::forward<LHS>(lhs)),
1546 Expr(std::forward<RHS>(rhs)));
1547 }
1548
Ben Claytond9603282021-06-29 12:37:45 +00001549 /// @param lhs the left hand argument to the or operation
1550 /// @param rhs the right hand argument to the or operation
1551 /// @returns a `ast::BinaryExpression` bitwise or-ing `lhs` and `rhs`
1552 template <typename LHS, typename RHS>
1553 ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) {
1554 return create<ast::BinaryExpression>(ast::BinaryOp::kOr,
1555 Expr(std::forward<LHS>(lhs)),
1556 Expr(std::forward<RHS>(rhs)));
1557 }
1558
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001559 /// @param lhs the left hand argument to the subtraction operation
1560 /// @param rhs the right hand argument to the subtraction operation
1561 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1562 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001563 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001564 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1565 Expr(std::forward<LHS>(lhs)),
1566 Expr(std::forward<RHS>(rhs)));
1567 }
1568
1569 /// @param lhs the left hand argument to the multiplication operation
1570 /// @param rhs the right hand argument to the multiplication operation
1571 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1572 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001573 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001574 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1575 Expr(std::forward<LHS>(lhs)),
1576 Expr(std::forward<RHS>(rhs)));
1577 }
1578
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001579 /// @param source the source information
1580 /// @param lhs the left hand argument to the multiplication operation
1581 /// @param rhs the right hand argument to the multiplication operation
1582 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1583 template <typename LHS, typename RHS>
1584 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1585 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1586 Expr(std::forward<LHS>(lhs)),
1587 Expr(std::forward<RHS>(rhs)));
1588 }
1589
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001590 /// @param lhs the left hand argument to the division operation
1591 /// @param rhs the right hand argument to the division operation
1592 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1593 template <typename LHS, typename RHS>
1594 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1595 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1596 Expr(std::forward<LHS>(lhs)),
1597 Expr(std::forward<RHS>(rhs)));
1598 }
1599
Ben Claytond9603282021-06-29 12:37:45 +00001600 /// @param lhs the left hand argument to the bit shift right operation
1601 /// @param rhs the right hand argument to the bit shift right operation
1602 /// @returns a `ast::BinaryExpression` bit shifting right `lhs` by `rhs`
1603 template <typename LHS, typename RHS>
1604 ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) {
1605 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftRight,
1606 Expr(std::forward<LHS>(lhs)),
1607 Expr(std::forward<RHS>(rhs)));
1608 }
1609
1610 /// @param lhs the left hand argument to the bit shift left operation
1611 /// @param rhs the right hand argument to the bit shift left operation
1612 /// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs`
1613 template <typename LHS, typename RHS>
1614 ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) {
1615 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftLeft,
1616 Expr(std::forward<LHS>(lhs)),
1617 Expr(std::forward<RHS>(rhs)));
1618 }
1619
Ben Clayton0597a2b2021-06-16 09:19:36 +00001620 /// @param source the source information
1621 /// @param arr the array argument for the array accessor expression
1622 /// @param idx the index argument for the array accessor expression
1623 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1624 template <typename ARR, typename IDX>
1625 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1626 ARR&& arr,
1627 IDX&& idx) {
1628 return create<ast::ArrayAccessorExpression>(
1629 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1630 }
1631
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001632 /// @param arr the array argument for the array accessor expression
1633 /// @param idx the index argument for the array accessor expression
1634 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1635 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001636 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001637 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1638 Expr(std::forward<IDX>(idx)));
1639 }
1640
1641 /// @param obj the object for the member accessor expression
1642 /// @param idx the index argument for the array accessor expression
1643 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1644 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001645 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001646 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1647 Expr(std::forward<IDX>(idx)));
1648 }
1649
Ben Clayton42d1e092021-02-02 14:29:15 +00001650 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001651 /// @param val the offset value
1652 /// @returns the offset decoration pointer
1653 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1654 return create<ast::StructMemberOffsetDecoration>(source_, val);
1655 }
1656
Ben Claytond614dd52021-03-15 10:43:11 +00001657 /// Creates a ast::StructMemberSizeDecoration
1658 /// @param source the source information
1659 /// @param val the size value
1660 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001661 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1662 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001663 return create<ast::StructMemberSizeDecoration>(source, val);
1664 }
1665
1666 /// Creates a ast::StructMemberSizeDecoration
1667 /// @param val the size value
1668 /// @returns the size decoration pointer
1669 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1670 return create<ast::StructMemberSizeDecoration>(source_, val);
1671 }
1672
1673 /// Creates a ast::StructMemberAlignDecoration
1674 /// @param source the source information
1675 /// @param val the align value
1676 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001677 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1678 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001679 return create<ast::StructMemberAlignDecoration>(source, val);
1680 }
1681
1682 /// Creates a ast::StructMemberAlignDecoration
1683 /// @param val the align value
1684 /// @returns the align decoration pointer
1685 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1686 return create<ast::StructMemberAlignDecoration>(source_, val);
1687 }
1688
Antonio Maiorano60dae242021-07-15 19:09:25 +00001689 /// Creates a ast::StructBlockDecoration
1690 /// @returns the struct block decoration pointer
1691 ast::StructBlockDecoration* StructBlock() {
1692 return create<ast::StructBlockDecoration>();
1693 }
1694
1695 /// Creates the ast::GroupDecoration
1696 /// @param value group decoration index
1697 /// @returns the group decoration pointer
1698 ast::GroupDecoration* Group(uint32_t value) {
1699 return create<ast::GroupDecoration>(value);
1700 }
1701
1702 /// Creates the ast::BindingDecoration
1703 /// @param value the binding index
1704 /// @returns the binding deocration pointer
1705 ast::BindingDecoration* Binding(uint32_t value) {
1706 return create<ast::BindingDecoration>(value);
1707 }
1708
1709 /// Convenience function to create both a ast::GroupDecoration and
1710 /// ast::BindingDecoration
1711 /// @param group the group index
1712 /// @param binding the binding index
1713 /// @returns a decoration list with both the group and binding decorations
1714 ast::DecorationList GroupAndBinding(uint32_t group, uint32_t binding) {
1715 return {Group(group), Binding(binding)};
1716 }
1717
Ben Clayton42d1e092021-02-02 14:29:15 +00001718 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001719 /// @param source the source information
1720 /// @param name the function name
1721 /// @param params the function parameters
1722 /// @param type the function return type
1723 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001724 /// @param decorations the optional function decorations
1725 /// @param return_type_decorations the optional function return type
1726 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001727 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001728 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001729 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001730 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001731 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001732 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001733 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001734 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001735 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001736 auto* func =
1737 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1738 type, create<ast::BlockStatement>(body),
1739 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001740 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001741 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001742 }
1743
Ben Clayton42d1e092021-02-02 14:29:15 +00001744 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001745 /// @param name the function name
1746 /// @param params the function parameters
1747 /// @param type the function return type
1748 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001749 /// @param decorations the optional function decorations
1750 /// @param return_type_decorations the optional function return type
1751 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001752 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001753 template <typename NAME>
1754 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001755 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001756 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001757 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001758 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001759 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001760 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1761 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001762 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001763 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001764 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001765 }
1766
Ben Clayton49668bb2021-04-17 05:32:01 +00001767 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001768 /// @param source the source information
1769 /// @returns the return statement pointer
1770 ast::ReturnStatement* Return(const Source& source) {
1771 return create<ast::ReturnStatement>(source);
1772 }
1773
1774 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001775 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001776 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1777
1778 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001779 /// @param source the source information
1780 /// @param val the return value
1781 /// @returns the return statement pointer
1782 template <typename EXPR>
1783 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1784 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1785 }
1786
1787 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001788 /// @param val the return value
1789 /// @returns the return statement pointer
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001790 template <typename EXPR, typename = DisableIfSource<EXPR>>
Ben Clayton49668bb2021-04-17 05:32:01 +00001791 ast::ReturnStatement* Return(EXPR&& val) {
1792 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001793 }
1794
Ben Clayton950809f2021-06-09 14:32:14 +00001795 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001796 /// @param source the source information
1797 /// @param name the alias name
1798 /// @param type the alias target type
1799 /// @returns the alias type
1800 template <typename NAME>
1801 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1802 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001803 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001804 return out;
1805 }
1806
Ben Clayton950809f2021-06-09 14:32:14 +00001807 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001808 /// @param name the alias name
1809 /// @param type the alias target type
1810 /// @returns the alias type
1811 template <typename NAME>
1812 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1813 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001814 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001815 return out;
1816 }
1817
Ben Clayton950809f2021-06-09 14:32:14 +00001818 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001819 /// @param source the source information
1820 /// @param name the struct name
1821 /// @param members the struct members
1822 /// @param decorations the optional struct decorations
1823 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001824 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001825 ast::Struct* Structure(const Source& source,
1826 NAME&& name,
1827 ast::StructMemberList members,
1828 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001829 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001830 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001831 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001832 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001833 return type;
1834 }
1835
Ben Clayton950809f2021-06-09 14:32:14 +00001836 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001837 /// @param name the struct name
1838 /// @param members the struct members
1839 /// @param decorations the optional struct decorations
1840 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001841 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001842 ast::Struct* Structure(NAME&& name,
1843 ast::StructMemberList members,
1844 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001845 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001846 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001847 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001848 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001849 return type;
1850 }
1851
Ben Clayton42d1e092021-02-02 14:29:15 +00001852 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001853 /// @param source the source information
1854 /// @param name the struct member name
1855 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001856 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001857 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001858 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001859 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001860 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001861 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001862 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001863 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1864 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001865 }
1866
Ben Clayton42d1e092021-02-02 14:29:15 +00001867 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001868 /// @param name the struct member name
1869 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001870 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001871 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001872 template <typename NAME>
1873 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001874 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001875 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001876 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1877 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001878 }
1879
Ben Claytonbab31972021-02-08 21:16:21 +00001880 /// Creates a ast::StructMember with the given byte offset
1881 /// @param offset the offset to use in the StructMemberOffsetDecoration
1882 /// @param name the struct member name
1883 /// @param type the struct member type
1884 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001885 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001886 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001887 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001888 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001889 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001890 create<ast::StructMemberOffsetDecoration>(offset),
1891 });
1892 }
1893
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001894 /// Creates a ast::BlockStatement with input statements
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001895 /// @param source the source information for the block
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001896 /// @param statements statements of block
1897 /// @returns the block statement pointer
1898 template <typename... Statements>
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001899 ast::BlockStatement* Block(const Source& source, Statements&&... statements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001900 return create<ast::BlockStatement>(
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001901 source, ast::StatementList{std::forward<Statements>(statements)...});
1902 }
1903
1904 /// Creates a ast::BlockStatement with input statements
1905 /// @param statements statements of block
1906 /// @returns the block statement pointer
1907 template <typename... STATEMENTS, typename = DisableIfSource<STATEMENTS...>>
1908 ast::BlockStatement* Block(STATEMENTS&&... statements) {
1909 return create<ast::BlockStatement>(
1910 ast::StatementList{std::forward<STATEMENTS>(statements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001911 }
1912
1913 /// Creates a ast::ElseStatement with input condition and body
1914 /// @param condition the else condition expression
1915 /// @param body the else body
1916 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001917 template <typename CONDITION>
1918 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1919 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1920 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001921 }
1922
1923 /// Creates a ast::IfStatement with input condition, body, and optional
1924 /// variadic else statements
1925 /// @param condition the if statement condition expression
1926 /// @param body the if statement body
1927 /// @param elseStatements optional variadic else statements
1928 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001929 template <typename CONDITION, typename... ELSE_STATEMENTS>
1930 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001931 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001932 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001933 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001934 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001935 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001936 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001937 }
1938
1939 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001940 /// @param source the source information
1941 /// @param lhs the left hand side expression initializer
1942 /// @param rhs the right hand side expression initializer
1943 /// @returns the assignment statement pointer
1944 template <typename LhsExpressionInit, typename RhsExpressionInit>
1945 ast::AssignmentStatement* Assign(const Source& source,
1946 LhsExpressionInit&& lhs,
1947 RhsExpressionInit&& rhs) {
1948 return create<ast::AssignmentStatement>(
1949 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1950 Expr(std::forward<RhsExpressionInit>(rhs)));
1951 }
1952
1953 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001954 /// @param lhs the left hand side expression initializer
1955 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001956 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001957 template <typename LhsExpressionInit, typename RhsExpressionInit>
1958 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1959 RhsExpressionInit&& rhs) {
1960 return create<ast::AssignmentStatement>(
1961 Expr(std::forward<LhsExpressionInit>(lhs)),
1962 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001963 }
1964
1965 /// Creates a ast::LoopStatement with input body and optional continuing
1966 /// @param body the loop body
1967 /// @param continuing the optional continuing block
1968 /// @returns the loop statement pointer
1969 ast::LoopStatement* Loop(ast::BlockStatement* body,
1970 ast::BlockStatement* continuing = nullptr) {
1971 return create<ast::LoopStatement>(body, continuing);
1972 }
1973
Ben Clayton65cd2592021-07-02 19:27:42 +00001974 /// Creates a ast::ForLoopStatement with input body and optional initializer,
1975 /// condition and continuing.
1976 /// @param source the source information
1977 /// @param init the optional loop initializer
1978 /// @param cond the optional loop condition
1979 /// @param cont the optional loop continuing
1980 /// @param body the loop body
1981 /// @returns the for loop statement pointer
1982 template <typename COND>
1983 ast::ForLoopStatement* For(const Source& source,
1984 ast::Statement* init,
1985 COND&& cond,
1986 ast::Statement* cont,
1987 ast::BlockStatement* body) {
1988 return create<ast::ForLoopStatement>(
1989 source, init, Expr(std::forward<COND>(cond)), cont, body);
1990 }
1991
1992 /// Creates a ast::ForLoopStatement with input body and optional initializer,
1993 /// condition and continuing.
1994 /// @param init the optional loop initializer
1995 /// @param cond the optional loop condition
1996 /// @param cont the optional loop continuing
1997 /// @param body the loop body
1998 /// @returns the for loop statement pointer
1999 template <typename COND>
2000 ast::ForLoopStatement* For(ast::Statement* init,
2001 COND&& cond,
2002 ast::Statement* cont,
2003 ast::BlockStatement* body) {
2004 return create<ast::ForLoopStatement>(init, Expr(std::forward<COND>(cond)),
2005 cont, body);
2006 }
2007
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00002008 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00002009 /// @param source the source information
2010 /// @param var the variable to wrap in a decl statement
2011 /// @returns the variable decl statement pointer
2012 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
2013 return create<ast::VariableDeclStatement>(source, var);
2014 }
2015
2016 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00002017 /// @param var the variable to wrap in a decl statement
2018 /// @returns the variable decl statement pointer
2019 ast::VariableDeclStatement* Decl(ast::Variable* var) {
2020 return create<ast::VariableDeclStatement>(var);
2021 }
2022
Antonio Maioranocea744d2021-03-25 12:55:27 +00002023 /// Creates a ast::SwitchStatement with input expression and cases
2024 /// @param condition the condition expression initializer
2025 /// @param cases case statements
2026 /// @returns the switch statement pointer
2027 template <typename ExpressionInit, typename... Cases>
2028 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
2029 return create<ast::SwitchStatement>(
2030 Expr(std::forward<ExpressionInit>(condition)),
2031 ast::CaseStatementList{std::forward<Cases>(cases)...});
2032 }
2033
2034 /// Creates a ast::CaseStatement with input list of selectors, and body
2035 /// @param selectors list of selectors
2036 /// @param body the case body
2037 /// @returns the case statement pointer
2038 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
2039 ast::BlockStatement* body = nullptr) {
2040 return create<ast::CaseStatement>(std::move(selectors),
2041 body ? body : Block());
2042 }
2043
2044 /// Convenient overload that takes a single selector
2045 /// @param selector a single case selector
2046 /// @param body the case body
2047 /// @returns the case statement pointer
2048 ast::CaseStatement* Case(ast::IntLiteral* selector,
2049 ast::BlockStatement* body = nullptr) {
2050 return Case(ast::CaseSelectorList{selector}, body);
2051 }
2052
2053 /// Convenience function that creates a 'default' ast::CaseStatement
2054 /// @param body the case body
2055 /// @returns the case statement pointer
2056 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
2057 return Case(ast::CaseSelectorList{}, body);
2058 }
2059
James Price68f558f2021-04-06 15:51:47 +00002060 /// Creates an ast::BuiltinDecoration
2061 /// @param source the source information
2062 /// @param builtin the builtin value
2063 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002064 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00002065 return create<ast::BuiltinDecoration>(source, builtin);
2066 }
2067
2068 /// Creates an ast::BuiltinDecoration
2069 /// @param builtin the builtin value
2070 /// @returns the builtin decoration pointer
2071 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
2072 return create<ast::BuiltinDecoration>(source_, builtin);
2073 }
2074
James Price989a8e42021-06-28 23:04:43 +00002075 /// Creates an ast::InterpolateDecoration
2076 /// @param source the source information
2077 /// @param type the interpolation type
2078 /// @param sampling the interpolation sampling
2079 /// @returns the interpolate decoration pointer
2080 ast::InterpolateDecoration* Interpolate(const Source& source,
2081 ast::InterpolationType type,
2082 ast::InterpolationSampling sampling) {
2083 return create<ast::InterpolateDecoration>(source, type, sampling);
2084 }
2085
2086 /// Creates an ast::InterpolateDecoration
2087 /// @param type the interpolation type
2088 /// @param sampling the interpolation sampling
2089 /// @returns the interpolate decoration pointer
2090 ast::InterpolateDecoration* Interpolate(ast::InterpolationType type,
2091 ast::InterpolationSampling sampling) {
2092 return create<ast::InterpolateDecoration>(source_, type, sampling);
2093 }
2094
James Price508d2492021-07-12 12:12:32 +00002095 /// Creates an ast::InvariantDecoration
2096 /// @param source the source information
2097 /// @returns the invariant decoration pointer
2098 ast::InvariantDecoration* Invariant(const Source& source) {
2099 return create<ast::InvariantDecoration>(source);
2100 }
2101
2102 /// Creates an ast::InvariantDecoration
2103 /// @returns the invariant decoration pointer
2104 ast::InvariantDecoration* Invariant() {
2105 return create<ast::InvariantDecoration>(source_);
2106 }
2107
James Price68f558f2021-04-06 15:51:47 +00002108 /// Creates an ast::LocationDecoration
2109 /// @param source the source information
2110 /// @param location the location value
2111 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002112 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00002113 return create<ast::LocationDecoration>(source, location);
2114 }
2115
2116 /// Creates an ast::LocationDecoration
2117 /// @param location the location value
2118 /// @returns the location decoration pointer
2119 ast::LocationDecoration* Location(uint32_t location) {
2120 return create<ast::LocationDecoration>(source_, location);
2121 }
2122
James Pricef2f3bfc2021-05-13 20:32:32 +00002123 /// Creates an ast::OverrideDecoration with a specific constant ID
2124 /// @param source the source information
2125 /// @param id the id value
2126 /// @returns the override decoration pointer
2127 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
2128 return create<ast::OverrideDecoration>(source, id);
2129 }
2130
2131 /// Creates an ast::OverrideDecoration with a specific constant ID
2132 /// @param id the optional id value
2133 /// @returns the override decoration pointer
2134 ast::OverrideDecoration* Override(uint32_t id) {
2135 return Override(source_, id);
2136 }
2137
2138 /// Creates an ast::OverrideDecoration without a constant ID
2139 /// @param source the source information
2140 /// @returns the override decoration pointer
2141 ast::OverrideDecoration* Override(const Source& source) {
2142 return create<ast::OverrideDecoration>(source);
2143 }
2144
2145 /// Creates an ast::OverrideDecoration without a constant ID
2146 /// @returns the override decoration pointer
2147 ast::OverrideDecoration* Override() { return Override(source_); }
2148
James Price68f558f2021-04-06 15:51:47 +00002149 /// Creates an ast::StageDecoration
2150 /// @param source the source information
2151 /// @param stage the pipeline stage
2152 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002153 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00002154 return create<ast::StageDecoration>(source, stage);
2155 }
2156
2157 /// Creates an ast::StageDecoration
2158 /// @param stage the pipeline stage
2159 /// @returns the stage decoration pointer
2160 ast::StageDecoration* Stage(ast::PipelineStage stage) {
2161 return create<ast::StageDecoration>(source_, stage);
2162 }
2163
James Price70f80bb2021-05-19 13:40:08 +00002164 /// Creates an ast::WorkgroupDecoration
2165 /// @param x the x dimension expression
2166 /// @returns the workgroup decoration pointer
2167 template <typename EXPR_X>
2168 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
2169 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
2170 }
2171
2172 /// Creates an ast::WorkgroupDecoration
2173 /// @param x the x dimension expression
2174 /// @param y the y dimension expression
2175 /// @returns the workgroup decoration pointer
2176 template <typename EXPR_X, typename EXPR_Y>
2177 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
2178 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
2179 nullptr);
2180 }
2181
2182 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00002183 /// @param source the source information
2184 /// @param x the x dimension expression
2185 /// @param y the y dimension expression
2186 /// @param z the z dimension expression
2187 /// @returns the workgroup decoration pointer
2188 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2189 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
2190 EXPR_X&& x,
2191 EXPR_Y&& y,
2192 EXPR_Z&& z) {
2193 return create<ast::WorkgroupDecoration>(
2194 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2195 Expr(std::forward<EXPR_Z>(z)));
2196 }
2197
2198 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002199 /// @param x the x dimension expression
2200 /// @param y the y dimension expression
2201 /// @param z the z dimension expression
2202 /// @returns the workgroup decoration pointer
2203 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2204 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2205 return create<ast::WorkgroupDecoration>(
2206 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2207 Expr(std::forward<EXPR_Z>(z)));
2208 }
2209
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002210 /// Sets the current builder source to `src`
2211 /// @param src the Source used for future create() calls
2212 void SetSource(const Source& src) {
2213 AssertNotMoved();
2214 source_ = src;
2215 }
2216
2217 /// Sets the current builder source to `loc`
2218 /// @param loc the Source used for future create() calls
2219 void SetSource(const Source::Location& loc) {
2220 AssertNotMoved();
2221 source_ = Source(loc);
2222 }
2223
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002224 /// Marks that the given transform has been applied to this program.
2225 /// @param transform the transform that has been applied
2226 void SetTransformApplied(const CastableBase* transform) {
2227 transforms_applied_.emplace(&transform->TypeInfo());
2228 }
2229
2230 /// Marks that the given transform `T` has been applied to this program.
2231 template <typename T>
2232 void SetTransformApplied() {
2233 transforms_applied_.emplace(&TypeInfo::Of<T>());
2234 }
2235
2236 /// Marks that the transforms with the given TypeInfos have been applied to
2237 /// this program.
2238 /// @param transforms the set of transform TypeInfos that has been applied
2239 void SetTransformApplied(
2240 const std::unordered_set<const TypeInfo*>& transforms) {
2241 for (auto* transform : transforms) {
2242 transforms_applied_.emplace(transform);
2243 }
2244 }
2245
2246 /// @returns true if the transform of type `T` was applied.
2247 template <typename T>
2248 bool HasTransformApplied() {
2249 return transforms_applied_.count(&TypeInfo::Of<T>());
2250 }
2251
2252 /// @return the TypeInfo pointers of all transforms that have been applied to
2253 /// this program.
2254 std::unordered_set<const TypeInfo*> TransformsApplied() const {
2255 return transforms_applied_;
2256 }
2257
Ben Clayton33352542021-01-29 16:43:41 +00002258 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002259 /// @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 Clayton33352542021-01-29 16:43:41 +00002261 /// @param expr the AST expression
2262 /// @return the resolved semantic type for the expression, or nullptr if the
2263 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002264 sem::Type* TypeOf(const ast::Expression* expr) const;
2265
Ben Claytonfe0910f2021-05-17 15:51:47 +00002266 /// Helper for returning the resolved semantic type of the variable `var`.
2267 /// @note As the Resolver is run when the Program is built, this will only be
2268 /// useful for the Resolver itself and tests that use their own Resolver.
2269 /// @param var the AST variable
2270 /// @return the resolved semantic type for the variable, or nullptr if the
2271 /// variable has no resolved type.
2272 sem::Type* TypeOf(const ast::Variable* var) const;
2273
Ben Claytonfbec46f2021-04-30 20:20:19 +00002274 /// Helper for returning the resolved semantic type of the AST type `type`.
2275 /// @note As the Resolver is run when the Program is built, this will only be
2276 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002277 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002278 /// @return the resolved semantic type for the type, or nullptr if the type
2279 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002280 const sem::Type* TypeOf(const ast::Type* type) const;
2281
2282 /// Helper for returning the resolved semantic type of the AST type
2283 /// declaration `type_decl`.
2284 /// @note As the Resolver is run when the Program is built, this will only be
2285 /// useful for the Resolver itself and tests that use their own Resolver.
2286 /// @param type_decl the AST type declaration
2287 /// @return the resolved semantic type for the type declaration, or nullptr if
2288 /// the type declaration has no resolved type.
2289 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002290
Ben Clayton169512e2021-04-17 05:52:11 +00002291 /// Wraps the ast::Literal in a statement. This is used by tests that
2292 /// construct a partial AST and require the Resolver to reach these
2293 /// nodes.
2294 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2295 /// @return the ast::Statement that wraps the ast::Statement
2296 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002297 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002298 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002299 /// nodes.
2300 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2301 /// @return the ast::Statement that wraps the ast::Expression
2302 ast::Statement* WrapInStatement(ast::Expression* expr);
2303 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002304 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002305 /// these nodes.
2306 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2307 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2308 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2309 /// Returns the statement argument. Used as a passthrough-overload by
2310 /// WrapInFunction().
2311 /// @param stmt the ast::Statement
2312 /// @return `stmt`
2313 ast::Statement* WrapInStatement(ast::Statement* stmt);
2314 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002315 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002316 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002317 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002318 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002319 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002320 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002321 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002322 }
2323 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002324 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002325 /// @returns the function
2326 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002327
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002328 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002329 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002330
2331 protected:
2332 /// Asserts that the builder has not been moved.
2333 void AssertNotMoved() const;
2334
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002335 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002336 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002337 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002338 ASTNodeAllocator ast_nodes_;
2339 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002340 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002341 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002342 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002343 diag::List diagnostics_;
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002344 std::unordered_set<const TypeInfo*> transforms_applied_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002345
2346 /// The source to use when creating AST nodes without providing a Source as
2347 /// the first argument.
2348 Source source_;
2349
Ben Clayton5f0ea112021-03-09 10:54:37 +00002350 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002351 /// program when built.
2352 bool resolve_on_build_ = true;
2353
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002354 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2355 bool moved_ = false;
2356};
2357
2358//! @cond Doxygen_Suppress
2359// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2360template <>
2361struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002362 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002363 return t->i32();
2364 }
2365};
2366template <>
2367struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002368 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002369 return t->u32();
2370 }
2371};
2372template <>
2373struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002374 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002375 return t->f32();
2376 }
2377};
2378template <>
2379struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002380 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002381 return t->bool_();
2382 }
2383};
2384template <>
2385struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002386 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002387 return t->void_();
2388 }
2389};
2390//! @endcond
2391
Ben Clayton917b14b2021-04-19 16:50:23 +00002392/// @param builder the ProgramBuilder
2393/// @returns the ProgramID of the ProgramBuilder
2394inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2395 return builder->ID();
2396}
2397
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002398} // namespace tint
2399
2400#endif // SRC_PROGRAM_BUILDER_H_