blob: a7d8f90aa91641eb915fe1f0abf16c3c6eb24921 [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 Clayton0f2d95d2021-07-22 13:24:59 +000053#include "src/ast/sampler.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000054#include "src/ast/scalar_constructor_expression.h"
55#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000056#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000057#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000058#include "src/ast/stride_decoration.h"
Antonio Maiorano60dae242021-07-15 19:09:25 +000059#include "src/ast/struct_block_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000060#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000061#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000062#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000063#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000064#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000065#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000066#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000067#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000068#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000069#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000070#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000071#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000072#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000073#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000074#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000075#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000076#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000077#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000078#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000079#include "src/sem/f32_type.h"
80#include "src/sem/i32_type.h"
81#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000082#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000083#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000084#include "src/sem/sampled_texture_type.h"
85#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000086#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000087#include "src/sem/u32_type.h"
88#include "src/sem/vector_type.h"
89#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000090
Ben Clayton03d10722021-06-05 12:45:50 +000091#ifdef INCLUDE_TINT_TINT_H_
92#error "internal tint header being #included from tint.h"
93#endif
94
Ben Clayton401b96b2021-02-03 17:19:59 +000095// Forward declarations
Ben Claytonb5cd10c2021-06-25 10:26:26 +000096namespace tint {
Ben Clayton401b96b2021-02-03 17:19:59 +000097namespace ast {
98class VariableDeclStatement;
99} // namespace ast
Ben Claytonb5cd10c2021-06-25 10:26:26 +0000100} // namespace tint
Ben Clayton401b96b2021-02-03 17:19:59 +0000101
Ben Claytonb5cd10c2021-06-25 10:26:26 +0000102namespace tint {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000103class CloneContext;
104
105/// ProgramBuilder is a mutable builder for a Program.
106/// To construct a Program, populate the builder and then `std::move` it to a
107/// Program.
108class ProgramBuilder {
Ben Claytoncd7eb4f2021-07-17 18:09:26 +0000109 /// A helper used to disable overloads if the first type in `TYPES` is a
110 /// Source. Used to avoid ambiguities in overloads that take a Source as the
111 /// first parameter and those that perfectly-forward the first argument.
112 template <typename... TYPES>
113 using DisableIfSource = traits::EnableIfIsNotType<
114 traits::Decay<traits::NthTypeOf<0, TYPES..., void>>,
115 Source>;
116
Ben Clayton620d77e2021-06-04 19:55:08 +0000117 /// VarOptionals is a helper for accepting a number of optional, extra
118 /// arguments for Var() and Global().
119 struct VarOptionals {
120 template <typename... ARGS>
121 explicit VarOptionals(ARGS&&... args) {
122 Apply(std::forward<ARGS>(args)...);
123 }
124 ~VarOptionals();
125
126 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000127 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000128 ast::Expression* constructor = nullptr;
129 ast::DecorationList decorations = {};
130
131 private:
132 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000133 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000134 void Set(ast::Expression* c) { constructor = c; }
135 void Set(const ast::DecorationList& l) { decorations = l; }
136
137 template <typename FIRST, typename... ARGS>
138 void Apply(FIRST&& first, ARGS&&... args) {
139 Set(std::forward<FIRST>(first));
140 Apply(std::forward<ARGS>(args)...);
141 }
142 void Apply() {}
143 };
144
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000145 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000146 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
147 using ASTNodeAllocator = BlockAllocator<ast::Node>;
148
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000149 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
150 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000151
152 /// `i32` is a type alias to `int`.
153 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
154 /// WGSL syntax.
155 /// Note: this is intentionally not aliased to uint32_t as we want integer
156 /// literals passed to the builder to match WGSL's integer literal types.
157 using i32 = decltype(1);
158 /// `u32` is a type alias to `unsigned int`.
159 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
160 /// WGSL syntax.
161 /// Note: this is intentionally not aliased to uint32_t as we want integer
162 /// literals passed to the builder to match WGSL's integer literal types.
163 using u32 = decltype(1u);
164 /// `f32` is a type alias to `float`
165 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
166 /// WGSL syntax.
167 using f32 = float;
168
169 /// Constructor
170 ProgramBuilder();
171
172 /// Move constructor
173 /// @param rhs the builder to move
174 ProgramBuilder(ProgramBuilder&& rhs);
175
176 /// Destructor
177 virtual ~ProgramBuilder();
178
179 /// Move assignment operator
180 /// @param rhs the builder to move
181 /// @return this builder
182 ProgramBuilder& operator=(ProgramBuilder&& rhs);
183
Ben Claytone43c8302021-01-29 11:59:32 +0000184 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
185 /// making a deep clone of the Program contents.
186 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
187 /// existing immutable program.
188 /// As the returned ProgramBuilder wraps `program`, `program` must not be
189 /// destructed or assigned while using the returned ProgramBuilder.
190 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
191 /// function. See crbug.com/tint/460.
192 /// @param program the immutable Program to wrap
193 /// @return the ProgramBuilder that wraps `program`
194 static ProgramBuilder Wrap(const Program* program);
195
Ben Claytone6995de2021-04-13 23:27:27 +0000196 /// @returns the unique identifier for this program
197 ProgramID ID() const { return id_; }
198
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000199 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000200 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000201 AssertNotMoved();
202 return types_;
203 }
204
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000205 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000206 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000207 AssertNotMoved();
208 return types_;
209 }
210
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000211 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000212 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000213 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000214 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000215 }
216
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000217 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000218 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000219 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000220 return ast_nodes_;
221 }
222
223 /// @returns a reference to the program's semantic nodes storage
224 SemNodeAllocator& SemNodes() {
225 AssertNotMoved();
226 return sem_nodes_;
227 }
228
229 /// @returns a reference to the program's semantic nodes storage
230 const SemNodeAllocator& SemNodes() const {
231 AssertNotMoved();
232 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000233 }
234
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000235 /// @returns a reference to the program's AST root Module
236 ast::Module& AST() {
237 AssertNotMoved();
238 return *ast_;
239 }
240
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000241 /// @returns a reference to the program's AST root Module
242 const ast::Module& AST() const {
243 AssertNotMoved();
244 return *ast_;
245 }
246
247 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000248 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000249 AssertNotMoved();
250 return sem_;
251 }
252
253 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000254 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000255 AssertNotMoved();
256 return sem_;
257 }
258
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000259 /// @returns a reference to the program's SymbolTable
260 SymbolTable& Symbols() {
261 AssertNotMoved();
262 return symbols_;
263 }
264
Ben Clayton708dc2d2021-01-29 11:22:40 +0000265 /// @returns a reference to the program's SymbolTable
266 const SymbolTable& Symbols() const {
267 AssertNotMoved();
268 return symbols_;
269 }
270
Ben Clayton844217f2021-01-27 18:49:05 +0000271 /// @returns a reference to the program's diagnostics
272 diag::List& Diagnostics() {
273 AssertNotMoved();
274 return diagnostics_;
275 }
276
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000277 /// @returns a reference to the program's diagnostics
278 const diag::List& Diagnostics() const {
279 AssertNotMoved();
280 return diagnostics_;
281 }
282
Ben Clayton5f0ea112021-03-09 10:54:37 +0000283 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000284 /// @param enable the new flag value (defaults to true)
285 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
286
Ben Clayton5f0ea112021-03-09 10:54:37 +0000287 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000288 /// built.
289 bool ResolveOnBuild() const { return resolve_on_build_; }
290
Ben Clayton844217f2021-01-27 18:49:05 +0000291 /// @returns true if the program has no error diagnostics and is not missing
292 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000293 bool IsValid() const;
294
Ben Clayton708dc2d2021-01-29 11:22:40 +0000295 /// Writes a representation of the node to the output stream
296 /// @note unlike str(), to_str() does not automatically demangle the string.
297 /// @param node the AST node
298 /// @param out the stream to write to
299 /// @param indent number of spaces to indent the node when writing
300 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
301 node->to_str(Sem(), out, indent);
302 }
303
304 /// Returns a demangled, string representation of `node`.
305 /// @param node the AST node
306 /// @returns a string representation of the node
307 std::string str(const ast::Node* node) const;
308
Ben Clayton7fdfff12021-01-29 15:17:30 +0000309 /// Creates a new ast::Node owned by the ProgramBuilder. When the
310 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000311 /// @param source the Source of the node
312 /// @param args the arguments to pass to the type constructor
313 /// @returns the node pointer
314 template <typename T, typename... ARGS>
315 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
316 ARGS&&... args) {
317 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000318 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000319 }
320
Ben Clayton7fdfff12021-01-29 15:17:30 +0000321 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
322 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000323 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000324 /// When the ProgramBuilder is destructed, the ast::Node will also be
325 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000326 /// @returns the node pointer
327 template <typename T>
328 traits::EnableIfIsType<T, ast::Node>* create() {
329 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000330 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000331 }
332
Ben Clayton7fdfff12021-01-29 15:17:30 +0000333 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
334 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000335 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000336 /// When the ProgramBuilder is destructed, the ast::Node will also be
337 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000338 /// @param arg0 the first arguments to pass to the type constructor
339 /// @param args the remaining arguments to pass to the type constructor
340 /// @returns the node pointer
341 template <typename T, typename ARG0, typename... ARGS>
342 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
343 traits::IsTypeOrDerived<T, ast::Node>::value &&
344 !traits::IsTypeOrDerived<ARG0, Source>::value,
345 T>*
346 create(ARG0&& arg0, ARGS&&... args) {
347 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000348 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000349 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000350 }
351
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000352 /// Creates a new sem::Node owned by the ProgramBuilder.
353 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000354 /// destructed.
355 /// @param args the arguments to pass to the type constructor
356 /// @returns the node pointer
357 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000358 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
359 !traits::IsTypeOrDerived<T, sem::Type>::value,
360 T>*
361 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000362 AssertNotMoved();
363 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
364 }
365
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000366 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000367 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
368 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000369 /// Types are unique (de-aliased), and so calling create() for the same `T`
370 /// and arguments will return the same pointer.
371 /// @warning Use this method to acquire a type only if all of its type
372 /// information is provided in the constructor arguments `args`.<br>
373 /// If the type requires additional configuration after construction that
374 /// affect its fundamental type, build the type with `std::make_unique`, make
375 /// any necessary alterations and then call unique_type() instead.
376 /// @param args the arguments to pass to the type constructor
377 /// @returns the de-aliased type pointer
378 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000379 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
380 static_assert(std::is_base_of<sem::Type, T>::value,
381 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000382 AssertNotMoved();
383 return types_.Get<T>(std::forward<ARGS>(args)...);
384 }
385
386 /// Marks this builder as moved, preventing any further use of the builder.
387 void MarkAsMoved();
388
389 //////////////////////////////////////////////////////////////////////////////
390 // TypesBuilder
391 //////////////////////////////////////////////////////////////////////////////
392
393 /// TypesBuilder holds basic `tint` types and methods for constructing
394 /// complex types.
395 class TypesBuilder {
396 public:
397 /// Constructor
398 /// @param builder the program builder
399 explicit TypesBuilder(ProgramBuilder* builder);
400
401 /// @return the tint AST type for the C type `T`.
402 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000403 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000404 return CToAST<T>::get(this);
405 }
406
407 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000408 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000409
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000410 /// @param source the Source of the node
411 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000412 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000413 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000414 }
415
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000416 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000417 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000418
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000419 /// @param source the Source of the node
420 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000421 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000422 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000423 }
424
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000425 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000426 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000427
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000428 /// @param source the Source of the node
429 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000430 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000431 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000432 }
433
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000434 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000435 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000436
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000437 /// @param source the Source of the node
438 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000439 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000440 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000441 }
442
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000443 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000444 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000445
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000446 /// @param source the Source of the node
447 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000448 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000449 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000450 }
451
Antonio Maiorano25436862021-04-13 13:32:33 +0000452 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000453 /// @param n vector width in elements
454 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000455 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000456 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000457 }
458
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000459 /// @param source the Source of the node
460 /// @param type vector subtype
461 /// @param n vector width in elements
462 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000463 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000464 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000465 }
466
Antonio Maiorano25436862021-04-13 13:32:33 +0000467 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000468 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000469 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000470
471 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000472 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000473 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000474
475 /// @param type vector subtype
476 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000477 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000478
479 /// @param n vector width in elements
480 /// @return the tint AST type for a `n`-element vector of `type`.
481 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000482 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000483 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000484 }
485
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000486 /// @return the tint AST type for a 2-element vector of the C type `T`.
487 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000488 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000489 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000490 }
491
492 /// @return the tint AST type for a 3-element vector of the C type `T`.
493 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000494 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000495 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000496 }
497
498 /// @return the tint AST type for a 4-element vector of the C type `T`.
499 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000500 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000501 return vec4(Of<T>());
502 }
503
504 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000505 /// @param columns number of columns for the matrix
506 /// @param rows number of rows for the matrix
507 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000508 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000509 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000510 }
511
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000512 /// @param source the Source of the node
513 /// @param type matrix subtype
514 /// @param columns number of columns for the matrix
515 /// @param rows number of rows for the matrix
516 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000517 ast::Matrix* mat(const Source& source,
518 ast::Type* type,
519 uint32_t columns,
520 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000521 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000522 }
523
Ben Claytone204f272021-04-22 14:40:23 +0000524 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000525 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000526 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000527
528 /// @param type matrix subtype
529 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000530 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000531
532 /// @param type matrix subtype
533 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000534 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000535
536 /// @param type matrix subtype
537 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000538 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000539
540 /// @param type matrix subtype
541 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000542 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000543
544 /// @param type matrix subtype
545 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000546 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000547
548 /// @param type matrix subtype
549 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000550 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000551
552 /// @param type matrix subtype
553 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000554 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000555
556 /// @param type matrix subtype
557 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000558 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000559
Ben Claytone204f272021-04-22 14:40:23 +0000560 /// @param columns number of columns for the matrix
561 /// @param rows number of rows for the matrix
562 /// @return the tint AST type for a matrix of `type`
563 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000564 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000565 return mat(Of<T>(), columns, rows);
566 }
567
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000568 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
569 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000570 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000571 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000572 }
573
574 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
575 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000576 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000577 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000578 }
579
580 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
581 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000582 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000583 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000584 }
585
586 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
587 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000588 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000589 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000590 }
591
592 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
593 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000594 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000595 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000596 }
597
598 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
599 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000600 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000601 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000602 }
603
604 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
605 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000606 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000607 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000608 }
609
610 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
611 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000612 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000613 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000614 }
615
616 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
617 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000618 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000619 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000620 }
621
622 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000623 /// @param n the array size. 0 represents a runtime-array
624 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000625 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000626 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000627 uint32_t n = 0,
628 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000629 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000630 }
631
632 /// @param source the Source of the node
633 /// @param subtype the array element type
634 /// @param n the array size. 0 represents a runtime-array
635 /// @param decos the optional decorations for the array
636 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000637 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000638 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000639 uint32_t n = 0,
640 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000641 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000642 }
643
Ben Claytonbab31972021-02-08 21:16:21 +0000644 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000645 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000646 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000647 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000648 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000649 ast::DecorationList decos;
650 if (stride) {
651 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
652 }
653 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000654 }
655
Ben Clayton0f88b312021-05-04 17:36:31 +0000656 /// @param source the Source of the node
657 /// @param subtype the array element type
658 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000659 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000660 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000661 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000662 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000663 uint32_t n,
664 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000665 ast::DecorationList decos;
666 if (stride) {
667 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
668 }
669 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000670 }
671
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000672 /// @return the tint AST type for an array of size `N` of type `T`
673 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000674 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000675 return array(Of<T>(), N);
676 }
677
Ben Claytonbab31972021-02-08 21:16:21 +0000678 /// @param stride the array stride
679 /// @return the tint AST type for an array of size `N` of type `T`
680 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000681 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000682 return array(Of<T>(), N, stride);
683 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000684
Ben Clayton0f88b312021-05-04 17:36:31 +0000685 /// Creates a type name
686 /// @param name the name
687 /// @returns the type name
688 template <typename NAME>
689 ast::TypeName* type_name(NAME&& name) const {
690 return builder->create<ast::TypeName>(
691 builder->Sym(std::forward<NAME>(name)));
692 }
693
694 /// Creates a type name
695 /// @param source the Source of the node
696 /// @param name the name
697 /// @returns the type name
698 template <typename NAME>
699 ast::TypeName* type_name(const Source& source, NAME&& name) const {
700 return builder->create<ast::TypeName>(
701 source, builder->Sym(std::forward<NAME>(name)));
702 }
703
Ben Clayton42d1e092021-02-02 14:29:15 +0000704 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000705 /// @param name the alias name
706 /// @param type the alias type
707 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000708 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000709 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000710 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000711 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000712 }
713
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000714 /// Creates an alias type
715 /// @param source the Source of the node
716 /// @param name the alias name
717 /// @param type the alias type
718 /// @returns the alias pointer
719 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000720 ast::Alias* alias(const Source& source,
721 NAME&& name,
722 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000723 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000724 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000725 }
726
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000727 /// @param type the type of the pointer
728 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000729 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000730 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000731 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000732 ast::StorageClass storage_class,
733 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000734 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000735 }
736
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000737 /// @param source the Source of the node
738 /// @param type the type of the pointer
739 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000740 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000741 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000742 ast::Pointer* pointer(const Source& source,
743 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000744 ast::StorageClass storage_class,
745 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000746 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000747 }
748
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000749 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000750 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000751 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000752 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000753 ast::Pointer* pointer(ast::StorageClass storage_class,
754 ast::Access access = ast::Access::kUndefined) const {
755 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000756 }
757
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000758 /// @param source the Source of the node
759 /// @param type the type of the atomic
760 /// @return the atomic to `type`
761 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
762 return builder->create<ast::Atomic>(source, type);
763 }
764
Ben Clayton313e6182021-06-17 19:56:14 +0000765 /// @param type the type of the atomic
766 /// @return the atomic to `type`
767 ast::Atomic* atomic(ast::Type* type) const {
768 return builder->create<ast::Atomic>(type);
769 }
770
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000771 /// @return the atomic to type `T`
772 template <typename T>
773 ast::Atomic* atomic() const {
774 return atomic(Of<T>());
775 }
776
Ben Claytone204f272021-04-22 14:40:23 +0000777 /// @param kind the kind of sampler
778 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000779 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000780 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000781 }
782
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000783 /// @param source the Source of the node
784 /// @param kind the kind of sampler
785 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000786 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000787 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000788 }
789
Ben Claytone204f272021-04-22 14:40:23 +0000790 /// @param dims the dimensionality of the texture
791 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000792 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000793 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000794 }
795
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000796 /// @param source the Source of the node
797 /// @param dims the dimensionality of the texture
798 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000799 ast::DepthTexture* depth_texture(const Source& source,
800 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000801 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000802 }
803
Ben Claytone204f272021-04-22 14:40:23 +0000804 /// @param dims the dimensionality of the texture
805 /// @param subtype the texture subtype.
806 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000807 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
808 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000809 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000810 }
811
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000812 /// @param source the Source of the node
813 /// @param dims the dimensionality of the texture
814 /// @param subtype the texture subtype.
815 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000816 ast::SampledTexture* sampled_texture(const Source& source,
817 ast::TextureDimension dims,
818 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000819 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000820 }
821
Ben Claytone204f272021-04-22 14:40:23 +0000822 /// @param dims the dimensionality of the texture
823 /// @param subtype the texture subtype.
824 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000825 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
826 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000827 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000828 }
829
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000830 /// @param source the Source of the node
831 /// @param dims the dimensionality of the texture
832 /// @param subtype the texture subtype.
833 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000834 ast::MultisampledTexture* multisampled_texture(const Source& source,
835 ast::TextureDimension dims,
836 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000837 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000838 }
839
Ben Claytone204f272021-04-22 14:40:23 +0000840 /// @param dims the dimensionality of the texture
841 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000842 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000843 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000844 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000845 ast::ImageFormat format,
846 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000847 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000848 return builder->create<ast::StorageTexture>(dims, format, subtype,
849 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000850 }
851
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000852 /// @param source the Source of the node
853 /// @param dims the dimensionality of the texture
854 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000855 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000856 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000857 ast::StorageTexture* storage_texture(const Source& source,
858 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000859 ast::ImageFormat format,
860 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000861 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000862 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
863 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000864 }
865
Brandon Jones7b257692021-05-17 17:40:17 +0000866 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000867 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000868 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000869 }
870
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000871 /// @param source the Source of the node
872 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000873 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000874 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000875 }
876
Ben Clayton19b03192021-05-20 15:04:08 +0000877 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000878 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000879 /// @param type the type
880 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000881 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000882
Ben Clayton8758f102021-06-09 14:32:14 +0000883 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000884 /// @param type the type
885 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000886 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000887
Ben Claytonf5f311e2021-04-28 14:31:23 +0000888 /// The ProgramBuilder
889 ProgramBuilder* const builder;
890
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000891 private:
892 /// CToAST<T> is specialized for various `T` types and each specialization
893 /// contains a single static `get()` method for obtaining the corresponding
894 /// AST type for the C type `T`.
895 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000896 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000897 template <typename T>
898 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000899 };
900
901 //////////////////////////////////////////////////////////////////////////////
902 // AST helper methods
903 //////////////////////////////////////////////////////////////////////////////
904
James Price65ae64d2021-04-29 12:59:14 +0000905 /// @return a new unnamed symbol
906 Symbol Sym() { return Symbols().New(); }
907
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000908 /// @param name the symbol string
909 /// @return a Symbol with the given name
910 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
911
912 /// @param sym the symbol
913 /// @return `sym`
914 Symbol Sym(Symbol sym) { return sym; }
915
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000916 /// @param expr the expression
917 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000918 template <typename T>
919 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
920 return expr;
921 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000922
Ben Claytonb8ea5912021-04-19 16:52:42 +0000923 /// Passthrough for nullptr
924 /// @return nullptr
925 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
926
Ben Claytonfe0910f2021-05-17 15:51:47 +0000927 /// @param source the source information
928 /// @param symbol the identifier symbol
929 /// @return an ast::IdentifierExpression with the given symbol
930 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
931 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000932 }
933
Ben Clayton46d78d72021-02-10 21:34:26 +0000934 /// @param symbol the identifier symbol
935 /// @return an ast::IdentifierExpression with the given symbol
936 ast::IdentifierExpression* Expr(Symbol symbol) {
937 return create<ast::IdentifierExpression>(symbol);
938 }
939
Ben Claytonfe0910f2021-05-17 15:51:47 +0000940 /// @param source the source information
941 /// @param variable the AST variable
942 /// @return an ast::IdentifierExpression with the variable's symbol
943 ast::IdentifierExpression* Expr(const Source& source,
944 ast::Variable* variable) {
945 return create<ast::IdentifierExpression>(source, variable->symbol());
946 }
947
Ben Claytonb8ea5912021-04-19 16:52:42 +0000948 /// @param variable the AST variable
949 /// @return an ast::IdentifierExpression with the variable's symbol
950 ast::IdentifierExpression* Expr(ast::Variable* variable) {
951 return create<ast::IdentifierExpression>(variable->symbol());
952 }
953
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000954 /// @param source the source information
955 /// @param name the identifier name
956 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000957 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
958 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
959 }
960
961 /// @param name the identifier name
962 /// @return an ast::IdentifierExpression with the given name
963 ast::IdentifierExpression* Expr(const char* name) {
964 return create<ast::IdentifierExpression>(Symbols().Register(name));
965 }
966
967 /// @param source the source information
968 /// @param name the identifier name
969 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000970 ast::IdentifierExpression* Expr(const Source& source,
971 const std::string& name) {
972 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
973 }
974
975 /// @param name the identifier name
976 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000977 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000978 return create<ast::IdentifierExpression>(Symbols().Register(name));
979 }
980
Ben Claytonfe0910f2021-05-17 15:51:47 +0000981 /// @param source the source information
982 /// @param value the boolean value
983 /// @return a Scalar constructor for the given value
984 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
985 return create<ast::ScalarConstructorExpression>(source, Literal(value));
986 }
987
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000988 /// @param value the boolean value
989 /// @return a Scalar constructor for the given value
990 ast::ScalarConstructorExpression* Expr(bool value) {
991 return create<ast::ScalarConstructorExpression>(Literal(value));
992 }
993
Ben Claytonfe0910f2021-05-17 15:51:47 +0000994 /// @param source the source information
995 /// @param value the float value
996 /// @return a Scalar constructor for the given value
997 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
998 return create<ast::ScalarConstructorExpression>(source, Literal(value));
999 }
1000
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001001 /// @param value the float value
1002 /// @return a Scalar constructor for the given value
1003 ast::ScalarConstructorExpression* Expr(f32 value) {
1004 return create<ast::ScalarConstructorExpression>(Literal(value));
1005 }
1006
Ben Claytonfe0910f2021-05-17 15:51:47 +00001007 /// @param source the source information
1008 /// @param value the integer value
1009 /// @return a Scalar constructor for the given value
1010 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1011 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1012 }
1013
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001014 /// @param value the integer value
1015 /// @return a Scalar constructor for the given value
1016 ast::ScalarConstructorExpression* Expr(i32 value) {
1017 return create<ast::ScalarConstructorExpression>(Literal(value));
1018 }
1019
Ben Claytonfe0910f2021-05-17 15:51:47 +00001020 /// @param source the source information
1021 /// @param value the unsigned int value
1022 /// @return a Scalar constructor for the given value
1023 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1024 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1025 }
1026
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001027 /// @param value the unsigned int value
1028 /// @return a Scalar constructor for the given value
1029 ast::ScalarConstructorExpression* Expr(u32 value) {
1030 return create<ast::ScalarConstructorExpression>(Literal(value));
1031 }
1032
1033 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1034 /// `list`.
1035 /// @param list the list to append too
1036 /// @param arg the arg to create
1037 template <typename ARG>
1038 void Append(ast::ExpressionList& list, ARG&& arg) {
1039 list.emplace_back(Expr(std::forward<ARG>(arg)));
1040 }
1041
1042 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1043 /// then appends them to `list`.
1044 /// @param list the list to append too
1045 /// @param arg0 the first argument
1046 /// @param args the rest of the arguments
1047 template <typename ARG0, typename... ARGS>
1048 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1049 Append(list, std::forward<ARG0>(arg0));
1050 Append(list, std::forward<ARGS>(args)...);
1051 }
1052
1053 /// @return an empty list of expressions
1054 ast::ExpressionList ExprList() { return {}; }
1055
1056 /// @param args the list of expressions
1057 /// @return the list of expressions converted to `ast::Expression`s using
1058 /// `Expr()`,
1059 template <typename... ARGS>
1060 ast::ExpressionList ExprList(ARGS&&... args) {
1061 ast::ExpressionList list;
1062 list.reserve(sizeof...(args));
1063 Append(list, std::forward<ARGS>(args)...);
1064 return list;
1065 }
1066
1067 /// @param list the list of expressions
1068 /// @return `list`
1069 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1070
1071 /// @param val the boolan value
1072 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001073 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001074
1075 /// @param val the float value
1076 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001077 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078
1079 /// @param val the unsigned int value
1080 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001081 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001082
1083 /// @param val the integer value
1084 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001085 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001086
1087 /// @param args the arguments for the type constructor
1088 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1089 /// of `args` converted to `ast::Expression`s using `Expr()`
1090 template <typename T, typename... ARGS>
1091 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001092 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001093 }
1094
1095 /// @param type the type to construct
1096 /// @param args the arguments for the constructor
1097 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1098 /// values `args`.
1099 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001100 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001101 return create<ast::TypeConstructorExpression>(
1102 type, ExprList(std::forward<ARGS>(args)...));
1103 }
1104
Ben Clayton313e6182021-06-17 19:56:14 +00001105 /// @param source the source information
1106 /// @param type the type to construct
1107 /// @param args the arguments for the constructor
1108 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1109 /// values `args`.
1110 template <typename... ARGS>
1111 ast::TypeConstructorExpression* Construct(const Source& source,
1112 ast::Type* type,
1113 ARGS&&... args) {
1114 return create<ast::TypeConstructorExpression>(
1115 source, type, ExprList(std::forward<ARGS>(args)...));
1116 }
1117
Ben Claytond9603282021-06-29 12:37:45 +00001118 /// @param expr the expression for the bitcast
1119 /// @return an `ast::BitcastExpression` of type `ty`, with the values of
1120 /// `expr` converted to `ast::Expression`s using `Expr()`
1121 template <typename T, typename EXPR>
1122 ast::BitcastExpression* Bitcast(EXPR&& expr) {
1123 return Bitcast(ty.Of<T>(), std::forward<EXPR>(expr));
1124 }
1125
1126 /// @param type the type to cast to
1127 /// @param expr the expression for the bitcast
1128 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1129 /// `expr`.
1130 template <typename EXPR>
1131 ast::BitcastExpression* Bitcast(ast::Type* type, EXPR&& expr) {
1132 return create<ast::BitcastExpression>(type, Expr(std::forward<EXPR>(expr)));
1133 }
1134
1135 /// @param source the source information
1136 /// @param type the type to cast to
1137 /// @param expr the expression for the bitcast
1138 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1139 /// `expr`.
1140 template <typename EXPR>
1141 ast::BitcastExpression* Bitcast(const Source& source,
1142 ast::Type* type,
1143 EXPR&& expr) {
1144 return create<ast::BitcastExpression>(source, type,
1145 Expr(std::forward<EXPR>(expr)));
1146 }
1147
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001148 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001149 /// @param type the vector type
1150 /// @param size the vector size
1151 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1152 /// type `type`, constructed with the values `args`.
1153 template <typename... ARGS>
1154 ast::TypeConstructorExpression* vec(ast::Type* type,
1155 uint32_t size,
1156 ARGS&&... args) {
1157 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1158 }
1159
1160 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001161 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1162 /// `T`, constructed with the values `args`.
1163 template <typename T, typename... ARGS>
1164 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001165 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001166 }
1167
1168 /// @param args the arguments for the vector constructor
1169 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1170 /// `T`, constructed with the values `args`.
1171 template <typename T, typename... ARGS>
1172 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001173 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001174 }
1175
1176 /// @param args the arguments for the vector constructor
1177 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1178 /// `T`, constructed with the values `args`.
1179 template <typename T, typename... ARGS>
1180 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001181 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001182 }
1183
1184 /// @param args the arguments for the matrix constructor
1185 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1186 /// `T`, constructed with the values `args`.
1187 template <typename T, typename... ARGS>
1188 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001189 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001190 }
1191
1192 /// @param args the arguments for the matrix constructor
1193 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1194 /// `T`, constructed with the values `args`.
1195 template <typename T, typename... ARGS>
1196 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001197 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001198 }
1199
1200 /// @param args the arguments for the matrix constructor
1201 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1202 /// `T`, constructed with the values `args`.
1203 template <typename T, typename... ARGS>
1204 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001205 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001206 }
1207
1208 /// @param args the arguments for the matrix constructor
1209 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1210 /// `T`, constructed with the values `args`.
1211 template <typename T, typename... ARGS>
1212 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001213 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001214 }
1215
1216 /// @param args the arguments for the matrix constructor
1217 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1218 /// `T`, constructed with the values `args`.
1219 template <typename T, typename... ARGS>
1220 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001221 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001222 }
1223
1224 /// @param args the arguments for the matrix constructor
1225 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1226 /// `T`, constructed with the values `args`.
1227 template <typename T, typename... ARGS>
1228 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001229 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001230 }
1231
1232 /// @param args the arguments for the matrix constructor
1233 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1234 /// `T`, constructed with the values `args`.
1235 template <typename T, typename... ARGS>
1236 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001237 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001238 }
1239
1240 /// @param args the arguments for the matrix constructor
1241 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1242 /// `T`, constructed with the values `args`.
1243 template <typename T, typename... ARGS>
1244 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001245 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001246 }
1247
1248 /// @param args the arguments for the matrix constructor
1249 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1250 /// `T`, constructed with the values `args`.
1251 template <typename T, typename... ARGS>
1252 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001253 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001254 }
1255
1256 /// @param args the arguments for the array constructor
1257 /// @return an `ast::TypeConstructorExpression` of an array with element type
1258 /// `T`, constructed with the values `args`.
1259 template <typename T, int N = 0, typename... ARGS>
1260 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001261 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001262 }
1263
1264 /// @param subtype the array element type
1265 /// @param n the array size. 0 represents a runtime-array.
1266 /// @param args the arguments for the array constructor
1267 /// @return an `ast::TypeConstructorExpression` of an array with element type
1268 /// `subtype`, constructed with the values `args`.
1269 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001270 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001271 uint32_t n,
1272 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001273 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001274 }
1275
1276 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001277 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001278 /// @param optional the optional variable settings.
1279 /// Can be any of the following, in any order:
1280 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001281 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001282 /// * ast::Expression* - specifies the variable's initializer expression
1283 /// * ast::DecorationList - specifies the variable's decorations
1284 /// Note that repeated arguments of the same type will use the last argument's
1285 /// value.
1286 /// @returns a `ast::Variable` with the given name, type and additional
1287 /// options
1288 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001289 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001290 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001291 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001292 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1293 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001294 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001295 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001296 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001297
1298 /// @param source the variable source
1299 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001300 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001301 /// @param optional the optional variable settings.
1302 /// Can be any of the following, in any order:
1303 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001304 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001305 /// * ast::Expression* - specifies the variable's initializer expression
1306 /// * ast::DecorationList - specifies the variable's decorations
1307 /// Note that repeated arguments of the same type will use the last argument's
1308 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001309 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001310 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001311 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001312 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001313 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001314 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001315 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1316 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001317 opts.storage, opts.access, type, false,
1318 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001319 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001320
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001321 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001322 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001323 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001324 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001325 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001326 template <typename NAME>
1327 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001328 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001329 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001330 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001331 return create<ast::Variable>(
1332 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1333 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001334 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001335
1336 /// @param source the variable source
1337 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001338 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001339 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001340 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001341 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001342 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001343 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001344 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001345 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001346 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001347 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001348 return create<ast::Variable>(
1349 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1350 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001351 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001352
James Pricedaf1f1c2021-04-08 22:15:48 +00001353 /// @param name the parameter name
1354 /// @param type the parameter type
1355 /// @param decorations optional parameter decorations
1356 /// @returns a constant `ast::Variable` with the given name and type
1357 template <typename NAME>
1358 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001359 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001360 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001361 return create<ast::Variable>(
1362 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1363 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001364 }
1365
1366 /// @param source the parameter source
1367 /// @param name the parameter name
1368 /// @param type the parameter type
1369 /// @param decorations optional parameter decorations
1370 /// @returns a constant `ast::Variable` with the given name and type
1371 template <typename NAME>
1372 ast::Variable* Param(const Source& source,
1373 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001374 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001375 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001376 return create<ast::Variable>(
1377 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1378 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001379 }
1380
Ben Clayton42708342021-04-21 17:55:12 +00001381 /// @param name the variable name
1382 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001383 /// @param optional the optional variable settings.
1384 /// Can be any of the following, in any order:
1385 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001386 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001387 /// * ast::Expression* - specifies the variable's initializer expression
1388 /// * ast::DecorationList - specifies the variable's decorations
1389 /// Note that repeated arguments of the same type will use the last argument's
1390 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001391 /// @returns a new `ast::Variable`, which is automatically registered as a
1392 /// global variable with the ast::Module.
Antonio Maiorano60dae242021-07-15 19:09:25 +00001393 template <typename NAME,
1394 typename... OPTIONAL,
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001395 typename = DisableIfSource<NAME>>
Ben Clayton42708342021-04-21 17:55:12 +00001396 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001397 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001398 OPTIONAL&&... optional) {
1399 auto* var = Var(std::forward<NAME>(name), type,
1400 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001401 AST().AddGlobalVariable(var);
1402 return var;
1403 }
1404
1405 /// @param source the variable source
1406 /// @param name the variable name
1407 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001408 /// @param optional the optional variable settings.
1409 /// Can be any of the following, in any order:
1410 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001411 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001412 /// * ast::Expression* - specifies the variable's initializer expression
1413 /// * ast::DecorationList - specifies the variable's decorations
1414 /// Note that repeated arguments of the same type will use the last argument's
1415 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001416 /// @returns a new `ast::Variable`, which is automatically registered as a
1417 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001418 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001419 ast::Variable* Global(const Source& source,
1420 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001421 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001422 OPTIONAL&&... optional) {
1423 auto* var = Var(source, std::forward<NAME>(name), type,
1424 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001425 AST().AddGlobalVariable(var);
1426 return var;
1427 }
1428
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001429 /// @param name the variable name
1430 /// @param type the variable type
1431 /// @param constructor constructor expression
1432 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001433 /// @returns a const `ast::Variable` constructed by calling Var() with the
1434 /// arguments of `args`, which is automatically registered as a global
1435 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001436 template <typename NAME>
1437 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001438 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001439 ast::Expression* constructor,
1440 ast::DecorationList decorations = {}) {
1441 auto* var = Const(std::forward<NAME>(name), type, constructor,
1442 std::move(decorations));
1443 AST().AddGlobalVariable(var);
1444 return var;
1445 }
1446
1447 /// @param source the variable source
1448 /// @param name the variable name
1449 /// @param type the variable type
1450 /// @param constructor constructor expression
1451 /// @param decorations optional variable decorations
1452 /// @returns a const `ast::Variable` constructed by calling Var() with the
1453 /// arguments of `args`, which is automatically registered as a global
1454 /// variable with the ast::Module.
1455 template <typename NAME>
1456 ast::Variable* GlobalConst(const Source& source,
1457 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001458 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001459 ast::Expression* constructor,
1460 ast::DecorationList decorations = {}) {
1461 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1462 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001463 AST().AddGlobalVariable(var);
1464 return var;
1465 }
1466
Ben Claytonfe0910f2021-05-17 15:51:47 +00001467 /// @param source the source information
1468 /// @param expr the expression to take the address of
1469 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1470 template <typename EXPR>
1471 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1472 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1473 Expr(std::forward<EXPR>(expr)));
1474 }
1475
1476 /// @param expr the expression to take the address of
1477 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1478 template <typename EXPR>
1479 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1480 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1481 Expr(std::forward<EXPR>(expr)));
1482 }
1483
1484 /// @param source the source information
1485 /// @param expr the expression to perform an indirection on
1486 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1487 template <typename EXPR>
1488 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1489 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1490 Expr(std::forward<EXPR>(expr)));
1491 }
1492
1493 /// @param expr the expression to perform an indirection on
1494 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1495 template <typename EXPR>
1496 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1497 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1498 Expr(std::forward<EXPR>(expr)));
1499 }
1500
Antonio Maiorano14b34032021-06-09 20:17:59 +00001501 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001502 /// @param func the function name
1503 /// @param args the function call arguments
1504 /// @returns a `ast::CallExpression` to the function `func`, with the
1505 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1506 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001507 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1508 return create<ast::CallExpression>(source, Expr(func),
1509 ExprList(std::forward<ARGS>(args)...));
1510 }
1511
1512 /// @param func the function name
1513 /// @param args the function call arguments
1514 /// @returns a `ast::CallExpression` to the function `func`, with the
1515 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001516 template <typename NAME, typename... ARGS, typename = DisableIfSource<NAME>>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001517 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1518 return create<ast::CallExpression>(Expr(func),
1519 ExprList(std::forward<ARGS>(args)...));
1520 }
1521
Ben Clayton7fe01062021-06-11 13:22:27 +00001522 /// @param expr the expression to ignore
1523 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1524 /// passed the single `expr` argument
1525 template <typename EXPR>
1526 ast::CallStatement* Ignore(EXPR&& expr) {
1527 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1528 }
1529
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001530 /// @param lhs the left hand argument to the addition operation
1531 /// @param rhs the right hand argument to the addition operation
1532 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1533 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001534 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001535 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1536 Expr(std::forward<LHS>(lhs)),
1537 Expr(std::forward<RHS>(rhs)));
1538 }
1539
James Pricec32e8f62021-06-22 20:08:29 +00001540 /// @param lhs the left hand argument to the and operation
1541 /// @param rhs the right hand argument to the and operation
1542 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
1543 template <typename LHS, typename RHS>
1544 ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
1545 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
1546 Expr(std::forward<LHS>(lhs)),
1547 Expr(std::forward<RHS>(rhs)));
1548 }
1549
Ben Claytond9603282021-06-29 12:37:45 +00001550 /// @param lhs the left hand argument to the or operation
1551 /// @param rhs the right hand argument to the or operation
1552 /// @returns a `ast::BinaryExpression` bitwise or-ing `lhs` and `rhs`
1553 template <typename LHS, typename RHS>
1554 ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) {
1555 return create<ast::BinaryExpression>(ast::BinaryOp::kOr,
1556 Expr(std::forward<LHS>(lhs)),
1557 Expr(std::forward<RHS>(rhs)));
1558 }
1559
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001560 /// @param lhs the left hand argument to the subtraction operation
1561 /// @param rhs the right hand argument to the subtraction operation
1562 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1563 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001564 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001565 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1566 Expr(std::forward<LHS>(lhs)),
1567 Expr(std::forward<RHS>(rhs)));
1568 }
1569
1570 /// @param lhs the left hand argument to the multiplication operation
1571 /// @param rhs the right hand argument to the multiplication operation
1572 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1573 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001574 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001575 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1576 Expr(std::forward<LHS>(lhs)),
1577 Expr(std::forward<RHS>(rhs)));
1578 }
1579
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001580 /// @param source the source information
1581 /// @param lhs the left hand argument to the multiplication operation
1582 /// @param rhs the right hand argument to the multiplication operation
1583 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1584 template <typename LHS, typename RHS>
1585 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1586 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1587 Expr(std::forward<LHS>(lhs)),
1588 Expr(std::forward<RHS>(rhs)));
1589 }
1590
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001591 /// @param lhs the left hand argument to the division operation
1592 /// @param rhs the right hand argument to the division operation
1593 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1594 template <typename LHS, typename RHS>
1595 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1596 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1597 Expr(std::forward<LHS>(lhs)),
1598 Expr(std::forward<RHS>(rhs)));
1599 }
1600
Ben Claytond9603282021-06-29 12:37:45 +00001601 /// @param lhs the left hand argument to the bit shift right operation
1602 /// @param rhs the right hand argument to the bit shift right operation
1603 /// @returns a `ast::BinaryExpression` bit shifting right `lhs` by `rhs`
1604 template <typename LHS, typename RHS>
1605 ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) {
1606 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftRight,
1607 Expr(std::forward<LHS>(lhs)),
1608 Expr(std::forward<RHS>(rhs)));
1609 }
1610
1611 /// @param lhs the left hand argument to the bit shift left operation
1612 /// @param rhs the right hand argument to the bit shift left operation
1613 /// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs`
1614 template <typename LHS, typename RHS>
1615 ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) {
1616 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftLeft,
1617 Expr(std::forward<LHS>(lhs)),
1618 Expr(std::forward<RHS>(rhs)));
1619 }
1620
Ben Clayton0597a2b2021-06-16 09:19:36 +00001621 /// @param source the source information
1622 /// @param arr the array argument for the array accessor expression
1623 /// @param idx the index argument for the array accessor expression
1624 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1625 template <typename ARR, typename IDX>
1626 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1627 ARR&& arr,
1628 IDX&& idx) {
1629 return create<ast::ArrayAccessorExpression>(
1630 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1631 }
1632
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001633 /// @param arr the array argument for the array accessor expression
1634 /// @param idx the index argument for the array accessor expression
1635 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1636 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001637 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001638 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1639 Expr(std::forward<IDX>(idx)));
1640 }
1641
1642 /// @param obj the object for the member accessor expression
1643 /// @param idx the index argument for the array accessor expression
1644 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1645 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001646 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001647 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1648 Expr(std::forward<IDX>(idx)));
1649 }
1650
Ben Clayton42d1e092021-02-02 14:29:15 +00001651 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001652 /// @param val the offset value
1653 /// @returns the offset decoration pointer
1654 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1655 return create<ast::StructMemberOffsetDecoration>(source_, val);
1656 }
1657
Ben Claytond614dd52021-03-15 10:43:11 +00001658 /// Creates a ast::StructMemberSizeDecoration
1659 /// @param source the source information
1660 /// @param val the size value
1661 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001662 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1663 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001664 return create<ast::StructMemberSizeDecoration>(source, val);
1665 }
1666
1667 /// Creates a ast::StructMemberSizeDecoration
1668 /// @param val the size value
1669 /// @returns the size decoration pointer
1670 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1671 return create<ast::StructMemberSizeDecoration>(source_, val);
1672 }
1673
1674 /// Creates a ast::StructMemberAlignDecoration
1675 /// @param source the source information
1676 /// @param val the align value
1677 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001678 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1679 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001680 return create<ast::StructMemberAlignDecoration>(source, val);
1681 }
1682
1683 /// Creates a ast::StructMemberAlignDecoration
1684 /// @param val the align value
1685 /// @returns the align decoration pointer
1686 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1687 return create<ast::StructMemberAlignDecoration>(source_, val);
1688 }
1689
Antonio Maiorano60dae242021-07-15 19:09:25 +00001690 /// Creates a ast::StructBlockDecoration
1691 /// @returns the struct block decoration pointer
1692 ast::StructBlockDecoration* StructBlock() {
1693 return create<ast::StructBlockDecoration>();
1694 }
1695
1696 /// Creates the ast::GroupDecoration
1697 /// @param value group decoration index
1698 /// @returns the group decoration pointer
1699 ast::GroupDecoration* Group(uint32_t value) {
1700 return create<ast::GroupDecoration>(value);
1701 }
1702
1703 /// Creates the ast::BindingDecoration
1704 /// @param value the binding index
1705 /// @returns the binding deocration pointer
1706 ast::BindingDecoration* Binding(uint32_t value) {
1707 return create<ast::BindingDecoration>(value);
1708 }
1709
1710 /// Convenience function to create both a ast::GroupDecoration and
1711 /// ast::BindingDecoration
1712 /// @param group the group index
1713 /// @param binding the binding index
1714 /// @returns a decoration list with both the group and binding decorations
1715 ast::DecorationList GroupAndBinding(uint32_t group, uint32_t binding) {
1716 return {Group(group), Binding(binding)};
1717 }
1718
Ben Clayton42d1e092021-02-02 14:29:15 +00001719 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001720 /// @param source the source information
1721 /// @param name the function name
1722 /// @param params the function parameters
1723 /// @param type the function return type
1724 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001725 /// @param decorations the optional function decorations
1726 /// @param return_type_decorations the optional function return type
1727 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001728 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001729 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001730 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001731 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001732 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001733 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001734 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001735 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001736 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001737 auto* func =
1738 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1739 type, create<ast::BlockStatement>(body),
1740 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001741 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001742 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001743 }
1744
Ben Clayton42d1e092021-02-02 14:29:15 +00001745 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001746 /// @param name the function name
1747 /// @param params the function parameters
1748 /// @param type the function return type
1749 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001750 /// @param decorations the optional function decorations
1751 /// @param return_type_decorations the optional function return type
1752 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001753 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001754 template <typename NAME>
1755 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001756 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001757 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001758 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001759 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001760 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001761 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1762 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001763 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001764 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001765 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001766 }
1767
Ben Clayton49668bb2021-04-17 05:32:01 +00001768 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001769 /// @param source the source information
1770 /// @returns the return statement pointer
1771 ast::ReturnStatement* Return(const Source& source) {
1772 return create<ast::ReturnStatement>(source);
1773 }
1774
1775 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001776 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001777 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1778
1779 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001780 /// @param source the source information
1781 /// @param val the return value
1782 /// @returns the return statement pointer
1783 template <typename EXPR>
1784 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1785 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1786 }
1787
1788 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001789 /// @param val the return value
1790 /// @returns the return statement pointer
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001791 template <typename EXPR, typename = DisableIfSource<EXPR>>
Ben Clayton49668bb2021-04-17 05:32:01 +00001792 ast::ReturnStatement* Return(EXPR&& val) {
1793 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001794 }
1795
Ben Clayton950809f2021-06-09 14:32:14 +00001796 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001797 /// @param source the source information
1798 /// @param name the alias name
1799 /// @param type the alias target type
1800 /// @returns the alias type
1801 template <typename NAME>
1802 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1803 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001804 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001805 return out;
1806 }
1807
Ben Clayton950809f2021-06-09 14:32:14 +00001808 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001809 /// @param name the alias name
1810 /// @param type the alias target type
1811 /// @returns the alias type
1812 template <typename NAME>
1813 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1814 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001815 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001816 return out;
1817 }
1818
Ben Clayton950809f2021-06-09 14:32:14 +00001819 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001820 /// @param source the source information
1821 /// @param name the struct name
1822 /// @param members the struct members
1823 /// @param decorations the optional struct decorations
1824 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001825 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001826 ast::Struct* Structure(const Source& source,
1827 NAME&& name,
1828 ast::StructMemberList members,
1829 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001830 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001831 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001832 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001833 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001834 return type;
1835 }
1836
Ben Clayton950809f2021-06-09 14:32:14 +00001837 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001838 /// @param name the struct name
1839 /// @param members the struct members
1840 /// @param decorations the optional struct decorations
1841 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001842 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001843 ast::Struct* Structure(NAME&& name,
1844 ast::StructMemberList members,
1845 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001846 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001847 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001848 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001849 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001850 return type;
1851 }
1852
Ben Clayton42d1e092021-02-02 14:29:15 +00001853 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001854 /// @param source the source information
1855 /// @param name the struct member name
1856 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001857 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001858 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001859 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001860 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001861 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001862 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001863 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001864 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1865 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001866 }
1867
Ben Clayton42d1e092021-02-02 14:29:15 +00001868 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001869 /// @param name the struct member name
1870 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001871 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001872 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001873 template <typename NAME>
1874 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001875 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001876 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001877 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1878 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001879 }
1880
Ben Claytonbab31972021-02-08 21:16:21 +00001881 /// Creates a ast::StructMember with the given byte offset
1882 /// @param offset the offset to use in the StructMemberOffsetDecoration
1883 /// @param name the struct member name
1884 /// @param type the struct member type
1885 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001886 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001887 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001888 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001889 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001890 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001891 create<ast::StructMemberOffsetDecoration>(offset),
1892 });
1893 }
1894
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001895 /// Creates a ast::BlockStatement with input statements
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001896 /// @param source the source information for the block
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001897 /// @param statements statements of block
1898 /// @returns the block statement pointer
1899 template <typename... Statements>
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001900 ast::BlockStatement* Block(const Source& source, Statements&&... statements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001901 return create<ast::BlockStatement>(
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001902 source, ast::StatementList{std::forward<Statements>(statements)...});
1903 }
1904
1905 /// Creates a ast::BlockStatement with input statements
1906 /// @param statements statements of block
1907 /// @returns the block statement pointer
1908 template <typename... STATEMENTS, typename = DisableIfSource<STATEMENTS...>>
1909 ast::BlockStatement* Block(STATEMENTS&&... statements) {
1910 return create<ast::BlockStatement>(
1911 ast::StatementList{std::forward<STATEMENTS>(statements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001912 }
1913
1914 /// Creates a ast::ElseStatement with input condition and body
1915 /// @param condition the else condition expression
1916 /// @param body the else body
1917 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001918 template <typename CONDITION>
1919 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1920 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1921 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001922 }
1923
1924 /// Creates a ast::IfStatement with input condition, body, and optional
1925 /// variadic else statements
1926 /// @param condition the if statement condition expression
1927 /// @param body the if statement body
1928 /// @param elseStatements optional variadic else statements
1929 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001930 template <typename CONDITION, typename... ELSE_STATEMENTS>
1931 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001932 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001933 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001934 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001935 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001936 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001937 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001938 }
1939
1940 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001941 /// @param source the source information
1942 /// @param lhs the left hand side expression initializer
1943 /// @param rhs the right hand side expression initializer
1944 /// @returns the assignment statement pointer
1945 template <typename LhsExpressionInit, typename RhsExpressionInit>
1946 ast::AssignmentStatement* Assign(const Source& source,
1947 LhsExpressionInit&& lhs,
1948 RhsExpressionInit&& rhs) {
1949 return create<ast::AssignmentStatement>(
1950 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1951 Expr(std::forward<RhsExpressionInit>(rhs)));
1952 }
1953
1954 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001955 /// @param lhs the left hand side expression initializer
1956 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001957 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001958 template <typename LhsExpressionInit, typename RhsExpressionInit>
1959 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1960 RhsExpressionInit&& rhs) {
1961 return create<ast::AssignmentStatement>(
1962 Expr(std::forward<LhsExpressionInit>(lhs)),
1963 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001964 }
1965
1966 /// Creates a ast::LoopStatement with input body and optional continuing
1967 /// @param body the loop body
1968 /// @param continuing the optional continuing block
1969 /// @returns the loop statement pointer
1970 ast::LoopStatement* Loop(ast::BlockStatement* body,
1971 ast::BlockStatement* continuing = nullptr) {
1972 return create<ast::LoopStatement>(body, continuing);
1973 }
1974
Ben Clayton65cd2592021-07-02 19:27:42 +00001975 /// Creates a ast::ForLoopStatement with input body and optional initializer,
1976 /// condition and continuing.
1977 /// @param source the source information
1978 /// @param init the optional loop initializer
1979 /// @param cond the optional loop condition
1980 /// @param cont the optional loop continuing
1981 /// @param body the loop body
1982 /// @returns the for loop statement pointer
1983 template <typename COND>
1984 ast::ForLoopStatement* For(const Source& source,
1985 ast::Statement* init,
1986 COND&& cond,
1987 ast::Statement* cont,
1988 ast::BlockStatement* body) {
1989 return create<ast::ForLoopStatement>(
1990 source, init, Expr(std::forward<COND>(cond)), cont, body);
1991 }
1992
1993 /// Creates a ast::ForLoopStatement with input body and optional initializer,
1994 /// condition and continuing.
1995 /// @param init the optional loop initializer
1996 /// @param cond the optional loop condition
1997 /// @param cont the optional loop continuing
1998 /// @param body the loop body
1999 /// @returns the for loop statement pointer
2000 template <typename COND>
2001 ast::ForLoopStatement* For(ast::Statement* init,
2002 COND&& cond,
2003 ast::Statement* cont,
2004 ast::BlockStatement* body) {
2005 return create<ast::ForLoopStatement>(init, Expr(std::forward<COND>(cond)),
2006 cont, body);
2007 }
2008
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00002009 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00002010 /// @param source the source information
2011 /// @param var the variable to wrap in a decl statement
2012 /// @returns the variable decl statement pointer
2013 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
2014 return create<ast::VariableDeclStatement>(source, var);
2015 }
2016
2017 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00002018 /// @param var the variable to wrap in a decl statement
2019 /// @returns the variable decl statement pointer
2020 ast::VariableDeclStatement* Decl(ast::Variable* var) {
2021 return create<ast::VariableDeclStatement>(var);
2022 }
2023
Antonio Maioranocea744d2021-03-25 12:55:27 +00002024 /// Creates a ast::SwitchStatement with input expression and cases
2025 /// @param condition the condition expression initializer
2026 /// @param cases case statements
2027 /// @returns the switch statement pointer
2028 template <typename ExpressionInit, typename... Cases>
2029 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
2030 return create<ast::SwitchStatement>(
2031 Expr(std::forward<ExpressionInit>(condition)),
2032 ast::CaseStatementList{std::forward<Cases>(cases)...});
2033 }
2034
2035 /// Creates a ast::CaseStatement with input list of selectors, and body
2036 /// @param selectors list of selectors
2037 /// @param body the case body
2038 /// @returns the case statement pointer
2039 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
2040 ast::BlockStatement* body = nullptr) {
2041 return create<ast::CaseStatement>(std::move(selectors),
2042 body ? body : Block());
2043 }
2044
2045 /// Convenient overload that takes a single selector
2046 /// @param selector a single case selector
2047 /// @param body the case body
2048 /// @returns the case statement pointer
2049 ast::CaseStatement* Case(ast::IntLiteral* selector,
2050 ast::BlockStatement* body = nullptr) {
2051 return Case(ast::CaseSelectorList{selector}, body);
2052 }
2053
2054 /// Convenience function that creates a 'default' ast::CaseStatement
2055 /// @param body the case body
2056 /// @returns the case statement pointer
2057 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
2058 return Case(ast::CaseSelectorList{}, body);
2059 }
2060
James Price68f558f2021-04-06 15:51:47 +00002061 /// Creates an ast::BuiltinDecoration
2062 /// @param source the source information
2063 /// @param builtin the builtin value
2064 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002065 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00002066 return create<ast::BuiltinDecoration>(source, builtin);
2067 }
2068
2069 /// Creates an ast::BuiltinDecoration
2070 /// @param builtin the builtin value
2071 /// @returns the builtin decoration pointer
2072 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
2073 return create<ast::BuiltinDecoration>(source_, builtin);
2074 }
2075
James Price989a8e42021-06-28 23:04:43 +00002076 /// Creates an ast::InterpolateDecoration
2077 /// @param source the source information
2078 /// @param type the interpolation type
2079 /// @param sampling the interpolation sampling
2080 /// @returns the interpolate decoration pointer
2081 ast::InterpolateDecoration* Interpolate(const Source& source,
2082 ast::InterpolationType type,
2083 ast::InterpolationSampling sampling) {
2084 return create<ast::InterpolateDecoration>(source, type, sampling);
2085 }
2086
2087 /// Creates an ast::InterpolateDecoration
2088 /// @param type the interpolation type
2089 /// @param sampling the interpolation sampling
2090 /// @returns the interpolate decoration pointer
2091 ast::InterpolateDecoration* Interpolate(ast::InterpolationType type,
2092 ast::InterpolationSampling sampling) {
2093 return create<ast::InterpolateDecoration>(source_, type, sampling);
2094 }
2095
James Price508d2492021-07-12 12:12:32 +00002096 /// Creates an ast::InvariantDecoration
2097 /// @param source the source information
2098 /// @returns the invariant decoration pointer
2099 ast::InvariantDecoration* Invariant(const Source& source) {
2100 return create<ast::InvariantDecoration>(source);
2101 }
2102
2103 /// Creates an ast::InvariantDecoration
2104 /// @returns the invariant decoration pointer
2105 ast::InvariantDecoration* Invariant() {
2106 return create<ast::InvariantDecoration>(source_);
2107 }
2108
James Price68f558f2021-04-06 15:51:47 +00002109 /// Creates an ast::LocationDecoration
2110 /// @param source the source information
2111 /// @param location the location value
2112 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002113 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00002114 return create<ast::LocationDecoration>(source, location);
2115 }
2116
2117 /// Creates an ast::LocationDecoration
2118 /// @param location the location value
2119 /// @returns the location decoration pointer
2120 ast::LocationDecoration* Location(uint32_t location) {
2121 return create<ast::LocationDecoration>(source_, location);
2122 }
2123
James Pricef2f3bfc2021-05-13 20:32:32 +00002124 /// Creates an ast::OverrideDecoration with a specific constant ID
2125 /// @param source the source information
2126 /// @param id the id value
2127 /// @returns the override decoration pointer
2128 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
2129 return create<ast::OverrideDecoration>(source, id);
2130 }
2131
2132 /// Creates an ast::OverrideDecoration with a specific constant ID
2133 /// @param id the optional id value
2134 /// @returns the override decoration pointer
2135 ast::OverrideDecoration* Override(uint32_t id) {
2136 return Override(source_, id);
2137 }
2138
2139 /// Creates an ast::OverrideDecoration without a constant ID
2140 /// @param source the source information
2141 /// @returns the override decoration pointer
2142 ast::OverrideDecoration* Override(const Source& source) {
2143 return create<ast::OverrideDecoration>(source);
2144 }
2145
2146 /// Creates an ast::OverrideDecoration without a constant ID
2147 /// @returns the override decoration pointer
2148 ast::OverrideDecoration* Override() { return Override(source_); }
2149
James Price68f558f2021-04-06 15:51:47 +00002150 /// Creates an ast::StageDecoration
2151 /// @param source the source information
2152 /// @param stage the pipeline stage
2153 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002154 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00002155 return create<ast::StageDecoration>(source, stage);
2156 }
2157
2158 /// Creates an ast::StageDecoration
2159 /// @param stage the pipeline stage
2160 /// @returns the stage decoration pointer
2161 ast::StageDecoration* Stage(ast::PipelineStage stage) {
2162 return create<ast::StageDecoration>(source_, stage);
2163 }
2164
James Price70f80bb2021-05-19 13:40:08 +00002165 /// Creates an ast::WorkgroupDecoration
2166 /// @param x the x dimension expression
2167 /// @returns the workgroup decoration pointer
2168 template <typename EXPR_X>
2169 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
2170 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
2171 }
2172
2173 /// Creates an ast::WorkgroupDecoration
2174 /// @param x the x dimension expression
2175 /// @param y the y dimension expression
2176 /// @returns the workgroup decoration pointer
2177 template <typename EXPR_X, typename EXPR_Y>
2178 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
2179 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
2180 nullptr);
2181 }
2182
2183 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00002184 /// @param source the source information
2185 /// @param x the x dimension expression
2186 /// @param y the y dimension expression
2187 /// @param z the z dimension expression
2188 /// @returns the workgroup decoration pointer
2189 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2190 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
2191 EXPR_X&& x,
2192 EXPR_Y&& y,
2193 EXPR_Z&& z) {
2194 return create<ast::WorkgroupDecoration>(
2195 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2196 Expr(std::forward<EXPR_Z>(z)));
2197 }
2198
2199 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002200 /// @param x the x dimension expression
2201 /// @param y the y dimension expression
2202 /// @param z the z dimension expression
2203 /// @returns the workgroup decoration pointer
2204 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2205 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2206 return create<ast::WorkgroupDecoration>(
2207 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2208 Expr(std::forward<EXPR_Z>(z)));
2209 }
2210
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002211 /// Sets the current builder source to `src`
2212 /// @param src the Source used for future create() calls
2213 void SetSource(const Source& src) {
2214 AssertNotMoved();
2215 source_ = src;
2216 }
2217
2218 /// Sets the current builder source to `loc`
2219 /// @param loc the Source used for future create() calls
2220 void SetSource(const Source::Location& loc) {
2221 AssertNotMoved();
2222 source_ = Source(loc);
2223 }
2224
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002225 /// Marks that the given transform has been applied to this program.
2226 /// @param transform the transform that has been applied
2227 void SetTransformApplied(const CastableBase* transform) {
2228 transforms_applied_.emplace(&transform->TypeInfo());
2229 }
2230
2231 /// Marks that the given transform `T` has been applied to this program.
2232 template <typename T>
2233 void SetTransformApplied() {
2234 transforms_applied_.emplace(&TypeInfo::Of<T>());
2235 }
2236
2237 /// Marks that the transforms with the given TypeInfos have been applied to
2238 /// this program.
2239 /// @param transforms the set of transform TypeInfos that has been applied
2240 void SetTransformApplied(
2241 const std::unordered_set<const TypeInfo*>& transforms) {
2242 for (auto* transform : transforms) {
2243 transforms_applied_.emplace(transform);
2244 }
2245 }
2246
2247 /// @returns true if the transform of type `T` was applied.
2248 template <typename T>
2249 bool HasTransformApplied() {
2250 return transforms_applied_.count(&TypeInfo::Of<T>());
2251 }
2252
2253 /// @return the TypeInfo pointers of all transforms that have been applied to
2254 /// this program.
2255 std::unordered_set<const TypeInfo*> TransformsApplied() const {
2256 return transforms_applied_;
2257 }
2258
Ben Clayton33352542021-01-29 16:43:41 +00002259 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002260 /// @note As the Resolver is run when the Program is built, this will only be
2261 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002262 /// @param expr the AST expression
2263 /// @return the resolved semantic type for the expression, or nullptr if the
2264 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002265 sem::Type* TypeOf(const ast::Expression* expr) const;
2266
Ben Claytonfe0910f2021-05-17 15:51:47 +00002267 /// Helper for returning the resolved semantic type of the variable `var`.
2268 /// @note As the Resolver is run when the Program is built, this will only be
2269 /// useful for the Resolver itself and tests that use their own Resolver.
2270 /// @param var the AST variable
2271 /// @return the resolved semantic type for the variable, or nullptr if the
2272 /// variable has no resolved type.
2273 sem::Type* TypeOf(const ast::Variable* var) const;
2274
Ben Claytonfbec46f2021-04-30 20:20:19 +00002275 /// Helper for returning the resolved semantic type of the AST type `type`.
2276 /// @note As the Resolver is run when the Program is built, this will only be
2277 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002278 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002279 /// @return the resolved semantic type for the type, or nullptr if the type
2280 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002281 const sem::Type* TypeOf(const ast::Type* type) const;
2282
2283 /// Helper for returning the resolved semantic type of the AST type
2284 /// declaration `type_decl`.
2285 /// @note As the Resolver is run when the Program is built, this will only be
2286 /// useful for the Resolver itself and tests that use their own Resolver.
2287 /// @param type_decl the AST type declaration
2288 /// @return the resolved semantic type for the type declaration, or nullptr if
2289 /// the type declaration has no resolved type.
2290 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002291
Ben Clayton169512e2021-04-17 05:52:11 +00002292 /// Wraps the ast::Literal in a statement. This is used by tests that
2293 /// construct a partial AST and require the Resolver to reach these
2294 /// nodes.
2295 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2296 /// @return the ast::Statement that wraps the ast::Statement
2297 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002298 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002299 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002300 /// nodes.
2301 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2302 /// @return the ast::Statement that wraps the ast::Expression
2303 ast::Statement* WrapInStatement(ast::Expression* expr);
2304 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002305 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002306 /// these nodes.
2307 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2308 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2309 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2310 /// Returns the statement argument. Used as a passthrough-overload by
2311 /// WrapInFunction().
2312 /// @param stmt the ast::Statement
2313 /// @return `stmt`
2314 ast::Statement* WrapInStatement(ast::Statement* stmt);
2315 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002316 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002317 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002318 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002319 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002320 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002321 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002322 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002323 }
2324 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002325 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002326 /// @returns the function
2327 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002328
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002329 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002330 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002331
2332 protected:
2333 /// Asserts that the builder has not been moved.
2334 void AssertNotMoved() const;
2335
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002336 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002337 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002338 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002339 ASTNodeAllocator ast_nodes_;
2340 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002341 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002342 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002343 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002344 diag::List diagnostics_;
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002345 std::unordered_set<const TypeInfo*> transforms_applied_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002346
2347 /// The source to use when creating AST nodes without providing a Source as
2348 /// the first argument.
2349 Source source_;
2350
Ben Clayton5f0ea112021-03-09 10:54:37 +00002351 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002352 /// program when built.
2353 bool resolve_on_build_ = true;
2354
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002355 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2356 bool moved_ = false;
2357};
2358
2359//! @cond Doxygen_Suppress
2360// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2361template <>
2362struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002363 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002364 return t->i32();
2365 }
2366};
2367template <>
2368struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002369 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002370 return t->u32();
2371 }
2372};
2373template <>
2374struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002375 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002376 return t->f32();
2377 }
2378};
2379template <>
2380struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002381 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002382 return t->bool_();
2383 }
2384};
2385template <>
2386struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002387 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002388 return t->void_();
2389 }
2390};
2391//! @endcond
2392
Ben Clayton917b14b2021-04-19 16:50:23 +00002393/// @param builder the ProgramBuilder
2394/// @returns the ProgramID of the ProgramBuilder
2395inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2396 return builder->ID();
2397}
2398
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002399} // namespace tint
2400
2401#endif // SRC_PROGRAM_BUILDER_H_