blob: 5a775337ccbc52ccb9e1fcc838afe1ddb13d3176 [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 Claytonfd35aa82021-07-26 22:19:48 +000035#include "src/ast/depth_multisampled_texture.h"
Ben Claytone204f272021-04-22 14:40:23 +000036#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000037#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000038#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000039#include "src/ast/float_literal.h"
Ben Clayton65cd2592021-07-02 19:27:42 +000040#include "src/ast/for_loop_statement.h"
Ben Claytona922e652021-04-21 13:37:22 +000041#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000042#include "src/ast/if_statement.h"
James Price989a8e42021-06-28 23:04:43 +000043#include "src/ast/interpolate_decoration.h"
James Price508d2492021-07-12 12:12:32 +000044#include "src/ast/invariant_decoration.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000045#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000046#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000047#include "src/ast/member_accessor_expression.h"
48#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000049#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000050#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000051#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000052#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000053#include "src/ast/sampled_texture.h"
Ben Clayton0f2d95d2021-07-22 13:24:59 +000054#include "src/ast/sampler.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000055#include "src/ast/scalar_constructor_expression.h"
56#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000057#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000058#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000059#include "src/ast/stride_decoration.h"
Antonio Maiorano60dae242021-07-15 19:09:25 +000060#include "src/ast/struct_block_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000061#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000062#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000063#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000064#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000065#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000066#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000067#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000068#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000069#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000070#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000071#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000072#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000073#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000074#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000075#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000076#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000077#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000078#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000079#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000080#include "src/sem/f32_type.h"
81#include "src/sem/i32_type.h"
82#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000083#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000084#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000085#include "src/sem/sampled_texture_type.h"
86#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000087#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000088#include "src/sem/u32_type.h"
89#include "src/sem/vector_type.h"
90#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000091
Ben Clayton03d10722021-06-05 12:45:50 +000092#ifdef INCLUDE_TINT_TINT_H_
93#error "internal tint header being #included from tint.h"
94#endif
95
Ben Clayton401b96b2021-02-03 17:19:59 +000096// Forward declarations
Ben Claytonb5cd10c2021-06-25 10:26:26 +000097namespace tint {
Ben Clayton401b96b2021-02-03 17:19:59 +000098namespace ast {
99class VariableDeclStatement;
100} // namespace ast
Ben Claytonb5cd10c2021-06-25 10:26:26 +0000101} // namespace tint
Ben Clayton401b96b2021-02-03 17:19:59 +0000102
Ben Claytonb5cd10c2021-06-25 10:26:26 +0000103namespace tint {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000104class CloneContext;
105
106/// ProgramBuilder is a mutable builder for a Program.
107/// To construct a Program, populate the builder and then `std::move` it to a
108/// Program.
109class ProgramBuilder {
Ben Claytoncd7eb4f2021-07-17 18:09:26 +0000110 /// A helper used to disable overloads if the first type in `TYPES` is a
111 /// Source. Used to avoid ambiguities in overloads that take a Source as the
112 /// first parameter and those that perfectly-forward the first argument.
113 template <typename... TYPES>
114 using DisableIfSource = traits::EnableIfIsNotType<
115 traits::Decay<traits::NthTypeOf<0, TYPES..., void>>,
116 Source>;
117
Ben Clayton620d77e2021-06-04 19:55:08 +0000118 /// VarOptionals is a helper for accepting a number of optional, extra
119 /// arguments for Var() and Global().
120 struct VarOptionals {
121 template <typename... ARGS>
122 explicit VarOptionals(ARGS&&... args) {
123 Apply(std::forward<ARGS>(args)...);
124 }
125 ~VarOptionals();
126
127 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000128 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000129 ast::Expression* constructor = nullptr;
130 ast::DecorationList decorations = {};
131
132 private:
133 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000134 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000135 void Set(ast::Expression* c) { constructor = c; }
136 void Set(const ast::DecorationList& l) { decorations = l; }
137
138 template <typename FIRST, typename... ARGS>
139 void Apply(FIRST&& first, ARGS&&... args) {
140 Set(std::forward<FIRST>(first));
141 Apply(std::forward<ARGS>(args)...);
142 }
143 void Apply() {}
144 };
145
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000146 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000147 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
148 using ASTNodeAllocator = BlockAllocator<ast::Node>;
149
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000150 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
151 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000152
153 /// `i32` is a type alias to `int`.
154 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
155 /// WGSL syntax.
156 /// Note: this is intentionally not aliased to uint32_t as we want integer
157 /// literals passed to the builder to match WGSL's integer literal types.
158 using i32 = decltype(1);
159 /// `u32` is a type alias to `unsigned int`.
160 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
161 /// WGSL syntax.
162 /// Note: this is intentionally not aliased to uint32_t as we want integer
163 /// literals passed to the builder to match WGSL's integer literal types.
164 using u32 = decltype(1u);
165 /// `f32` is a type alias to `float`
166 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
167 /// WGSL syntax.
168 using f32 = float;
169
170 /// Constructor
171 ProgramBuilder();
172
173 /// Move constructor
174 /// @param rhs the builder to move
175 ProgramBuilder(ProgramBuilder&& rhs);
176
177 /// Destructor
178 virtual ~ProgramBuilder();
179
180 /// Move assignment operator
181 /// @param rhs the builder to move
182 /// @return this builder
183 ProgramBuilder& operator=(ProgramBuilder&& rhs);
184
Ben Claytone43c8302021-01-29 11:59:32 +0000185 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
186 /// making a deep clone of the Program contents.
187 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
188 /// existing immutable program.
189 /// As the returned ProgramBuilder wraps `program`, `program` must not be
190 /// destructed or assigned while using the returned ProgramBuilder.
191 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
192 /// function. See crbug.com/tint/460.
193 /// @param program the immutable Program to wrap
194 /// @return the ProgramBuilder that wraps `program`
195 static ProgramBuilder Wrap(const Program* program);
196
Ben Claytone6995de2021-04-13 23:27:27 +0000197 /// @returns the unique identifier for this program
198 ProgramID ID() const { return id_; }
199
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000200 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000201 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000202 AssertNotMoved();
203 return types_;
204 }
205
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000206 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000207 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000208 AssertNotMoved();
209 return types_;
210 }
211
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000212 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000213 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000214 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000215 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000216 }
217
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000218 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000219 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000220 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000221 return ast_nodes_;
222 }
223
224 /// @returns a reference to the program's semantic nodes storage
225 SemNodeAllocator& SemNodes() {
226 AssertNotMoved();
227 return sem_nodes_;
228 }
229
230 /// @returns a reference to the program's semantic nodes storage
231 const SemNodeAllocator& SemNodes() const {
232 AssertNotMoved();
233 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000234 }
235
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000236 /// @returns a reference to the program's AST root Module
237 ast::Module& AST() {
238 AssertNotMoved();
239 return *ast_;
240 }
241
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000242 /// @returns a reference to the program's AST root Module
243 const ast::Module& AST() const {
244 AssertNotMoved();
245 return *ast_;
246 }
247
248 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000249 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000250 AssertNotMoved();
251 return sem_;
252 }
253
254 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000255 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000256 AssertNotMoved();
257 return sem_;
258 }
259
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000260 /// @returns a reference to the program's SymbolTable
261 SymbolTable& Symbols() {
262 AssertNotMoved();
263 return symbols_;
264 }
265
Ben Clayton708dc2d2021-01-29 11:22:40 +0000266 /// @returns a reference to the program's SymbolTable
267 const SymbolTable& Symbols() const {
268 AssertNotMoved();
269 return symbols_;
270 }
271
Ben Clayton844217f2021-01-27 18:49:05 +0000272 /// @returns a reference to the program's diagnostics
273 diag::List& Diagnostics() {
274 AssertNotMoved();
275 return diagnostics_;
276 }
277
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000278 /// @returns a reference to the program's diagnostics
279 const diag::List& Diagnostics() const {
280 AssertNotMoved();
281 return diagnostics_;
282 }
283
Ben Clayton5f0ea112021-03-09 10:54:37 +0000284 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000285 /// @param enable the new flag value (defaults to true)
286 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
287
Ben Clayton5f0ea112021-03-09 10:54:37 +0000288 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000289 /// built.
290 bool ResolveOnBuild() const { return resolve_on_build_; }
291
Ben Clayton844217f2021-01-27 18:49:05 +0000292 /// @returns true if the program has no error diagnostics and is not missing
293 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000294 bool IsValid() const;
295
Ben Clayton708dc2d2021-01-29 11:22:40 +0000296 /// Writes a representation of the node to the output stream
297 /// @note unlike str(), to_str() does not automatically demangle the string.
298 /// @param node the AST node
299 /// @param out the stream to write to
300 /// @param indent number of spaces to indent the node when writing
301 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
302 node->to_str(Sem(), out, indent);
303 }
304
305 /// Returns a demangled, string representation of `node`.
306 /// @param node the AST node
307 /// @returns a string representation of the node
308 std::string str(const ast::Node* node) const;
309
Ben Clayton7fdfff12021-01-29 15:17:30 +0000310 /// Creates a new ast::Node owned by the ProgramBuilder. When the
311 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000312 /// @param source the Source of the node
313 /// @param args the arguments to pass to the type constructor
314 /// @returns the node pointer
315 template <typename T, typename... ARGS>
316 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
317 ARGS&&... args) {
318 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000319 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000320 }
321
Ben Clayton7fdfff12021-01-29 15:17:30 +0000322 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
323 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000324 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000325 /// When the ProgramBuilder is destructed, the ast::Node will also be
326 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000327 /// @returns the node pointer
328 template <typename T>
329 traits::EnableIfIsType<T, ast::Node>* create() {
330 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000331 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000332 }
333
Ben Clayton7fdfff12021-01-29 15:17:30 +0000334 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
335 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000336 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000337 /// When the ProgramBuilder is destructed, the ast::Node will also be
338 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000339 /// @param arg0 the first arguments to pass to the type constructor
340 /// @param args the remaining arguments to pass to the type constructor
341 /// @returns the node pointer
342 template <typename T, typename ARG0, typename... ARGS>
343 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
344 traits::IsTypeOrDerived<T, ast::Node>::value &&
345 !traits::IsTypeOrDerived<ARG0, Source>::value,
346 T>*
347 create(ARG0&& arg0, ARGS&&... args) {
348 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000349 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000350 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000351 }
352
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000353 /// Creates a new sem::Node owned by the ProgramBuilder.
354 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000355 /// destructed.
356 /// @param args the arguments to pass to the type constructor
357 /// @returns the node pointer
358 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000359 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
360 !traits::IsTypeOrDerived<T, sem::Type>::value,
361 T>*
362 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000363 AssertNotMoved();
364 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
365 }
366
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000367 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000368 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
369 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000370 /// Types are unique (de-aliased), and so calling create() for the same `T`
371 /// and arguments will return the same pointer.
372 /// @warning Use this method to acquire a type only if all of its type
373 /// information is provided in the constructor arguments `args`.<br>
374 /// If the type requires additional configuration after construction that
375 /// affect its fundamental type, build the type with `std::make_unique`, make
376 /// any necessary alterations and then call unique_type() instead.
377 /// @param args the arguments to pass to the type constructor
378 /// @returns the de-aliased type pointer
379 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000380 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
381 static_assert(std::is_base_of<sem::Type, T>::value,
382 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000383 AssertNotMoved();
384 return types_.Get<T>(std::forward<ARGS>(args)...);
385 }
386
387 /// Marks this builder as moved, preventing any further use of the builder.
388 void MarkAsMoved();
389
390 //////////////////////////////////////////////////////////////////////////////
391 // TypesBuilder
392 //////////////////////////////////////////////////////////////////////////////
393
394 /// TypesBuilder holds basic `tint` types and methods for constructing
395 /// complex types.
396 class TypesBuilder {
397 public:
398 /// Constructor
399 /// @param builder the program builder
400 explicit TypesBuilder(ProgramBuilder* builder);
401
402 /// @return the tint AST type for the C type `T`.
403 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000404 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000405 return CToAST<T>::get(this);
406 }
407
408 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000409 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000410
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000411 /// @param source the Source of the node
412 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000413 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000414 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000415 }
416
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000417 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000418 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000419
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000420 /// @param source the Source of the node
421 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000422 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000423 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000424 }
425
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000426 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000427 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000428
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000429 /// @param source the Source of the node
430 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000431 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000432 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000433 }
434
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000435 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000436 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000437
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000438 /// @param source the Source of the node
439 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000440 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000441 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000442 }
443
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000444 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000445 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000446
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000447 /// @param source the Source of the node
448 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000449 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000450 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000451 }
452
Antonio Maiorano25436862021-04-13 13:32:33 +0000453 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000454 /// @param n vector width in elements
455 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000456 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000457 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000458 }
459
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000460 /// @param source the Source of the node
461 /// @param type vector subtype
462 /// @param n vector width in elements
463 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000464 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000465 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000466 }
467
Antonio Maiorano25436862021-04-13 13:32:33 +0000468 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000469 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000470 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000471
472 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000473 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000474 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000475
476 /// @param type vector subtype
477 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000478 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000479
480 /// @param n vector width in elements
481 /// @return the tint AST type for a `n`-element vector of `type`.
482 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000483 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000484 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000485 }
486
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000487 /// @return the tint AST type for a 2-element vector of the C type `T`.
488 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000489 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000490 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000491 }
492
493 /// @return the tint AST type for a 3-element vector of the C type `T`.
494 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000495 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000496 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000497 }
498
499 /// @return the tint AST type for a 4-element vector of the C type `T`.
500 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000501 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000502 return vec4(Of<T>());
503 }
504
505 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000506 /// @param columns number of columns for the matrix
507 /// @param rows number of rows for the matrix
508 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000509 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000510 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000511 }
512
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000513 /// @param source the Source of the node
514 /// @param type matrix subtype
515 /// @param columns number of columns for the matrix
516 /// @param rows number of rows for the matrix
517 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000518 ast::Matrix* mat(const Source& source,
519 ast::Type* type,
520 uint32_t columns,
521 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000522 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000523 }
524
Ben Claytone204f272021-04-22 14:40:23 +0000525 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000526 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000527 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000528
529 /// @param type matrix subtype
530 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000531 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000532
533 /// @param type matrix subtype
534 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000535 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000536
537 /// @param type matrix subtype
538 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000539 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000540
541 /// @param type matrix subtype
542 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000543 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000544
545 /// @param type matrix subtype
546 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000547 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000548
549 /// @param type matrix subtype
550 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000551 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000552
553 /// @param type matrix subtype
554 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000555 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000556
557 /// @param type matrix subtype
558 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000559 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000560
Ben Claytone204f272021-04-22 14:40:23 +0000561 /// @param columns number of columns for the matrix
562 /// @param rows number of rows for the matrix
563 /// @return the tint AST type for a matrix of `type`
564 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000565 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000566 return mat(Of<T>(), columns, rows);
567 }
568
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000569 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
570 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000571 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000572 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000573 }
574
575 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
576 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000577 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000578 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000579 }
580
581 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
582 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000583 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000584 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000585 }
586
587 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
588 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000589 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000590 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000591 }
592
593 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
594 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000595 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000596 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000597 }
598
599 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
600 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000601 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000602 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000603 }
604
605 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
606 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000607 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000608 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000609 }
610
611 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
612 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000613 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000614 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000615 }
616
617 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
618 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000619 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000620 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000621 }
622
623 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000624 /// @param n the array size. 0 represents a runtime-array
625 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000626 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000627 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000628 uint32_t n = 0,
629 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000630 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000631 }
632
633 /// @param source the Source of the node
634 /// @param subtype the array element type
635 /// @param n the array size. 0 represents a runtime-array
636 /// @param decos the optional decorations for the array
637 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000638 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000639 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000640 uint32_t n = 0,
641 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000642 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000643 }
644
Ben Claytonbab31972021-02-08 21:16:21 +0000645 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000646 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000647 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000648 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000649 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000650 ast::DecorationList decos;
651 if (stride) {
652 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
653 }
654 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000655 }
656
Ben Clayton0f88b312021-05-04 17:36:31 +0000657 /// @param source the Source of the node
658 /// @param subtype the array element type
659 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000660 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000661 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000662 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000663 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000664 uint32_t n,
665 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000666 ast::DecorationList decos;
667 if (stride) {
668 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
669 }
670 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000671 }
672
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000673 /// @return the tint AST type for an array of size `N` of type `T`
674 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000675 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000676 return array(Of<T>(), N);
677 }
678
Ben Claytonbab31972021-02-08 21:16:21 +0000679 /// @param stride the array stride
680 /// @return the tint AST type for an array of size `N` of type `T`
681 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000682 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000683 return array(Of<T>(), N, stride);
684 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000685
Ben Clayton0f88b312021-05-04 17:36:31 +0000686 /// Creates a type name
687 /// @param name the name
688 /// @returns the type name
689 template <typename NAME>
690 ast::TypeName* type_name(NAME&& name) const {
691 return builder->create<ast::TypeName>(
692 builder->Sym(std::forward<NAME>(name)));
693 }
694
695 /// Creates a type name
696 /// @param source the Source of the node
697 /// @param name the name
698 /// @returns the type name
699 template <typename NAME>
700 ast::TypeName* type_name(const Source& source, NAME&& name) const {
701 return builder->create<ast::TypeName>(
702 source, builder->Sym(std::forward<NAME>(name)));
703 }
704
Ben Clayton42d1e092021-02-02 14:29:15 +0000705 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000706 /// @param name the alias name
707 /// @param type the alias type
708 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000709 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000710 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000711 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000712 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000713 }
714
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000715 /// Creates an alias type
716 /// @param source the Source of the node
717 /// @param name the alias name
718 /// @param type the alias type
719 /// @returns the alias pointer
720 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000721 ast::Alias* alias(const Source& source,
722 NAME&& name,
723 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000724 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000725 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000726 }
727
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000728 /// @param type the type of the pointer
729 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000730 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000731 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000732 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000733 ast::StorageClass storage_class,
734 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000735 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000736 }
737
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000738 /// @param source the Source of the node
739 /// @param type the type of the pointer
740 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000741 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000742 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000743 ast::Pointer* pointer(const Source& source,
744 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000745 ast::StorageClass storage_class,
746 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000747 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000748 }
749
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000750 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000751 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000752 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000753 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000754 ast::Pointer* pointer(ast::StorageClass storage_class,
755 ast::Access access = ast::Access::kUndefined) const {
756 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000757 }
758
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000759 /// @param source the Source of the node
760 /// @param type the type of the atomic
761 /// @return the atomic to `type`
762 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
763 return builder->create<ast::Atomic>(source, type);
764 }
765
Ben Clayton313e6182021-06-17 19:56:14 +0000766 /// @param type the type of the atomic
767 /// @return the atomic to `type`
768 ast::Atomic* atomic(ast::Type* type) const {
769 return builder->create<ast::Atomic>(type);
770 }
771
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000772 /// @return the atomic to type `T`
773 template <typename T>
774 ast::Atomic* atomic() const {
775 return atomic(Of<T>());
776 }
777
Ben Claytone204f272021-04-22 14:40:23 +0000778 /// @param kind the kind of sampler
779 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000780 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000781 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000782 }
783
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000784 /// @param source the Source of the node
785 /// @param kind the kind of sampler
786 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000787 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000788 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000789 }
790
Ben Claytone204f272021-04-22 14:40:23 +0000791 /// @param dims the dimensionality of the texture
792 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000793 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000794 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000795 }
796
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000797 /// @param source the Source of the node
798 /// @param dims the dimensionality of the texture
799 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000800 ast::DepthTexture* depth_texture(const Source& source,
801 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000802 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000803 }
804
Ben Claytone204f272021-04-22 14:40:23 +0000805 /// @param dims the dimensionality of the texture
Ben Claytonfd35aa82021-07-26 22:19:48 +0000806 /// @returns the multisampled depth texture
807 ast::DepthMultisampledTexture* depth_multisampled_texture(
808 ast::TextureDimension dims) const {
809 return builder->create<ast::DepthMultisampledTexture>(dims);
810 }
811
812 /// @param source the Source of the node
813 /// @param dims the dimensionality of the texture
814 /// @returns the multisampled depth texture
815 ast::DepthMultisampledTexture* depth_multisampled_texture(
816 const Source& source,
817 ast::TextureDimension dims) const {
818 return builder->create<ast::DepthMultisampledTexture>(source, dims);
819 }
820
821 /// @param dims the dimensionality of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000822 /// @param subtype the texture subtype.
823 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000824 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
825 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000826 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000827 }
828
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000829 /// @param source the Source of the node
830 /// @param dims the dimensionality of the texture
831 /// @param subtype the texture subtype.
832 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000833 ast::SampledTexture* sampled_texture(const Source& source,
834 ast::TextureDimension dims,
835 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000836 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000837 }
838
Ben Claytone204f272021-04-22 14:40:23 +0000839 /// @param dims the dimensionality of the texture
840 /// @param subtype the texture subtype.
841 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000842 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
843 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000844 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000845 }
846
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000847 /// @param source the Source of the node
848 /// @param dims the dimensionality of the texture
849 /// @param subtype the texture subtype.
850 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000851 ast::MultisampledTexture* multisampled_texture(const Source& source,
852 ast::TextureDimension dims,
853 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000854 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000855 }
856
Ben Claytone204f272021-04-22 14:40:23 +0000857 /// @param dims the dimensionality of the texture
858 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000859 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000860 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000861 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000862 ast::ImageFormat format,
863 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000864 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000865 return builder->create<ast::StorageTexture>(dims, format, subtype,
866 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000867 }
868
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000869 /// @param source the Source of the node
870 /// @param dims the dimensionality of the texture
871 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000872 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000873 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000874 ast::StorageTexture* storage_texture(const Source& source,
875 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000876 ast::ImageFormat format,
877 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000878 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000879 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
880 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000881 }
882
Brandon Jones7b257692021-05-17 17:40:17 +0000883 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000884 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000885 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000886 }
887
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000888 /// @param source the Source of the node
889 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000890 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000891 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000892 }
893
Ben Clayton19b03192021-05-20 15:04:08 +0000894 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000895 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000896 /// @param type the type
897 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000898 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000899
Ben Clayton8758f102021-06-09 14:32:14 +0000900 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000901 /// @param type the type
902 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000903 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000904
Ben Claytonf5f311e2021-04-28 14:31:23 +0000905 /// The ProgramBuilder
906 ProgramBuilder* const builder;
907
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000908 private:
909 /// CToAST<T> is specialized for various `T` types and each specialization
910 /// contains a single static `get()` method for obtaining the corresponding
911 /// AST type for the C type `T`.
912 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000913 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000914 template <typename T>
915 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000916 };
917
918 //////////////////////////////////////////////////////////////////////////////
919 // AST helper methods
920 //////////////////////////////////////////////////////////////////////////////
921
James Price65ae64d2021-04-29 12:59:14 +0000922 /// @return a new unnamed symbol
923 Symbol Sym() { return Symbols().New(); }
924
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000925 /// @param name the symbol string
926 /// @return a Symbol with the given name
927 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
928
929 /// @param sym the symbol
930 /// @return `sym`
931 Symbol Sym(Symbol sym) { return sym; }
932
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000933 /// @param expr the expression
934 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000935 template <typename T>
936 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
937 return expr;
938 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000939
Ben Claytonb8ea5912021-04-19 16:52:42 +0000940 /// Passthrough for nullptr
941 /// @return nullptr
942 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
943
Ben Claytonfe0910f2021-05-17 15:51:47 +0000944 /// @param source the source information
945 /// @param symbol the identifier symbol
946 /// @return an ast::IdentifierExpression with the given symbol
947 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
948 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000949 }
950
Ben Clayton46d78d72021-02-10 21:34:26 +0000951 /// @param symbol the identifier symbol
952 /// @return an ast::IdentifierExpression with the given symbol
953 ast::IdentifierExpression* Expr(Symbol symbol) {
954 return create<ast::IdentifierExpression>(symbol);
955 }
956
Ben Claytonfe0910f2021-05-17 15:51:47 +0000957 /// @param source the source information
958 /// @param variable the AST variable
959 /// @return an ast::IdentifierExpression with the variable's symbol
960 ast::IdentifierExpression* Expr(const Source& source,
961 ast::Variable* variable) {
962 return create<ast::IdentifierExpression>(source, variable->symbol());
963 }
964
Ben Claytonb8ea5912021-04-19 16:52:42 +0000965 /// @param variable the AST variable
966 /// @return an ast::IdentifierExpression with the variable's symbol
967 ast::IdentifierExpression* Expr(ast::Variable* variable) {
968 return create<ast::IdentifierExpression>(variable->symbol());
969 }
970
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000971 /// @param source the source information
972 /// @param name the identifier name
973 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000974 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
975 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
976 }
977
978 /// @param name the identifier name
979 /// @return an ast::IdentifierExpression with the given name
980 ast::IdentifierExpression* Expr(const char* name) {
981 return create<ast::IdentifierExpression>(Symbols().Register(name));
982 }
983
984 /// @param source the source information
985 /// @param name the identifier name
986 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000987 ast::IdentifierExpression* Expr(const Source& source,
988 const std::string& name) {
989 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
990 }
991
992 /// @param name the identifier name
993 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000994 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000995 return create<ast::IdentifierExpression>(Symbols().Register(name));
996 }
997
Ben Claytonfe0910f2021-05-17 15:51:47 +0000998 /// @param source the source information
999 /// @param value the boolean value
1000 /// @return a Scalar constructor for the given value
1001 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
1002 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1003 }
1004
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001005 /// @param value the boolean value
1006 /// @return a Scalar constructor for the given value
1007 ast::ScalarConstructorExpression* Expr(bool value) {
1008 return create<ast::ScalarConstructorExpression>(Literal(value));
1009 }
1010
Ben Claytonfe0910f2021-05-17 15:51:47 +00001011 /// @param source the source information
1012 /// @param value the float value
1013 /// @return a Scalar constructor for the given value
1014 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
1015 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1016 }
1017
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001018 /// @param value the float value
1019 /// @return a Scalar constructor for the given value
1020 ast::ScalarConstructorExpression* Expr(f32 value) {
1021 return create<ast::ScalarConstructorExpression>(Literal(value));
1022 }
1023
Ben Claytonfe0910f2021-05-17 15:51:47 +00001024 /// @param source the source information
1025 /// @param value the integer value
1026 /// @return a Scalar constructor for the given value
1027 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1028 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1029 }
1030
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001031 /// @param value the integer value
1032 /// @return a Scalar constructor for the given value
1033 ast::ScalarConstructorExpression* Expr(i32 value) {
1034 return create<ast::ScalarConstructorExpression>(Literal(value));
1035 }
1036
Ben Claytonfe0910f2021-05-17 15:51:47 +00001037 /// @param source the source information
1038 /// @param value the unsigned int value
1039 /// @return a Scalar constructor for the given value
1040 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1041 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1042 }
1043
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001044 /// @param value the unsigned int value
1045 /// @return a Scalar constructor for the given value
1046 ast::ScalarConstructorExpression* Expr(u32 value) {
1047 return create<ast::ScalarConstructorExpression>(Literal(value));
1048 }
1049
1050 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1051 /// `list`.
1052 /// @param list the list to append too
1053 /// @param arg the arg to create
1054 template <typename ARG>
1055 void Append(ast::ExpressionList& list, ARG&& arg) {
1056 list.emplace_back(Expr(std::forward<ARG>(arg)));
1057 }
1058
1059 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1060 /// then appends them to `list`.
1061 /// @param list the list to append too
1062 /// @param arg0 the first argument
1063 /// @param args the rest of the arguments
1064 template <typename ARG0, typename... ARGS>
1065 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1066 Append(list, std::forward<ARG0>(arg0));
1067 Append(list, std::forward<ARGS>(args)...);
1068 }
1069
1070 /// @return an empty list of expressions
1071 ast::ExpressionList ExprList() { return {}; }
1072
1073 /// @param args the list of expressions
1074 /// @return the list of expressions converted to `ast::Expression`s using
1075 /// `Expr()`,
1076 template <typename... ARGS>
1077 ast::ExpressionList ExprList(ARGS&&... args) {
1078 ast::ExpressionList list;
1079 list.reserve(sizeof...(args));
1080 Append(list, std::forward<ARGS>(args)...);
1081 return list;
1082 }
1083
1084 /// @param list the list of expressions
1085 /// @return `list`
1086 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1087
1088 /// @param val the boolan value
1089 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001090 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001091
1092 /// @param val the float value
1093 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001094 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001095
1096 /// @param val the unsigned int value
1097 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001098 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001099
1100 /// @param val the integer value
1101 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001102 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001103
1104 /// @param args the arguments for the type constructor
1105 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1106 /// of `args` converted to `ast::Expression`s using `Expr()`
1107 template <typename T, typename... ARGS>
1108 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001109 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001110 }
1111
1112 /// @param type the type to construct
1113 /// @param args the arguments for the constructor
1114 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1115 /// values `args`.
1116 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001117 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001118 return create<ast::TypeConstructorExpression>(
1119 type, ExprList(std::forward<ARGS>(args)...));
1120 }
1121
Ben Clayton313e6182021-06-17 19:56:14 +00001122 /// @param source the source information
1123 /// @param type the type to construct
1124 /// @param args the arguments for the constructor
1125 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1126 /// values `args`.
1127 template <typename... ARGS>
1128 ast::TypeConstructorExpression* Construct(const Source& source,
1129 ast::Type* type,
1130 ARGS&&... args) {
1131 return create<ast::TypeConstructorExpression>(
1132 source, type, ExprList(std::forward<ARGS>(args)...));
1133 }
1134
Ben Claytond9603282021-06-29 12:37:45 +00001135 /// @param expr the expression for the bitcast
1136 /// @return an `ast::BitcastExpression` of type `ty`, with the values of
1137 /// `expr` converted to `ast::Expression`s using `Expr()`
1138 template <typename T, typename EXPR>
1139 ast::BitcastExpression* Bitcast(EXPR&& expr) {
1140 return Bitcast(ty.Of<T>(), std::forward<EXPR>(expr));
1141 }
1142
1143 /// @param type the type to cast to
1144 /// @param expr the expression for the bitcast
1145 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1146 /// `expr`.
1147 template <typename EXPR>
1148 ast::BitcastExpression* Bitcast(ast::Type* type, EXPR&& expr) {
1149 return create<ast::BitcastExpression>(type, Expr(std::forward<EXPR>(expr)));
1150 }
1151
1152 /// @param source the source information
1153 /// @param type the type to cast to
1154 /// @param expr the expression for the bitcast
1155 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1156 /// `expr`.
1157 template <typename EXPR>
1158 ast::BitcastExpression* Bitcast(const Source& source,
1159 ast::Type* type,
1160 EXPR&& expr) {
1161 return create<ast::BitcastExpression>(source, type,
1162 Expr(std::forward<EXPR>(expr)));
1163 }
1164
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001165 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001166 /// @param type the vector type
1167 /// @param size the vector size
1168 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1169 /// type `type`, constructed with the values `args`.
1170 template <typename... ARGS>
1171 ast::TypeConstructorExpression* vec(ast::Type* type,
1172 uint32_t size,
1173 ARGS&&... args) {
1174 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1175 }
1176
1177 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001178 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1179 /// `T`, constructed with the values `args`.
1180 template <typename T, typename... ARGS>
1181 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001182 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001183 }
1184
1185 /// @param args the arguments for the vector constructor
1186 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1187 /// `T`, constructed with the values `args`.
1188 template <typename T, typename... ARGS>
1189 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001190 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001191 }
1192
1193 /// @param args the arguments for the vector constructor
1194 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1195 /// `T`, constructed with the values `args`.
1196 template <typename T, typename... ARGS>
1197 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001198 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001199 }
1200
1201 /// @param args the arguments for the matrix constructor
1202 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1203 /// `T`, constructed with the values `args`.
1204 template <typename T, typename... ARGS>
1205 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001206 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001207 }
1208
1209 /// @param args the arguments for the matrix constructor
1210 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1211 /// `T`, constructed with the values `args`.
1212 template <typename T, typename... ARGS>
1213 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001214 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215 }
1216
1217 /// @param args the arguments for the matrix constructor
1218 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1219 /// `T`, constructed with the values `args`.
1220 template <typename T, typename... ARGS>
1221 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001222 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001223 }
1224
1225 /// @param args the arguments for the matrix constructor
1226 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1227 /// `T`, constructed with the values `args`.
1228 template <typename T, typename... ARGS>
1229 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001230 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231 }
1232
1233 /// @param args the arguments for the matrix constructor
1234 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1235 /// `T`, constructed with the values `args`.
1236 template <typename T, typename... ARGS>
1237 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001238 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001239 }
1240
1241 /// @param args the arguments for the matrix constructor
1242 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1243 /// `T`, constructed with the values `args`.
1244 template <typename T, typename... ARGS>
1245 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001246 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001247 }
1248
1249 /// @param args the arguments for the matrix constructor
1250 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1251 /// `T`, constructed with the values `args`.
1252 template <typename T, typename... ARGS>
1253 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001254 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001255 }
1256
1257 /// @param args the arguments for the matrix constructor
1258 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1259 /// `T`, constructed with the values `args`.
1260 template <typename T, typename... ARGS>
1261 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001262 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001263 }
1264
1265 /// @param args the arguments for the matrix constructor
1266 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1267 /// `T`, constructed with the values `args`.
1268 template <typename T, typename... ARGS>
1269 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001270 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001271 }
1272
1273 /// @param args the arguments for the array constructor
1274 /// @return an `ast::TypeConstructorExpression` of an array with element type
1275 /// `T`, constructed with the values `args`.
1276 template <typename T, int N = 0, typename... ARGS>
1277 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001278 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001279 }
1280
1281 /// @param subtype the array element type
1282 /// @param n the array size. 0 represents a runtime-array.
1283 /// @param args the arguments for the array constructor
1284 /// @return an `ast::TypeConstructorExpression` of an array with element type
1285 /// `subtype`, constructed with the values `args`.
1286 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001287 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001288 uint32_t n,
1289 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001290 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001291 }
1292
1293 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001294 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001295 /// @param optional the optional variable settings.
1296 /// Can be any of the following, in any order:
1297 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001298 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001299 /// * ast::Expression* - specifies the variable's initializer expression
1300 /// * ast::DecorationList - specifies the variable's decorations
1301 /// Note that repeated arguments of the same type will use the last argument's
1302 /// value.
1303 /// @returns a `ast::Variable` with the given name, type and additional
1304 /// options
1305 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001306 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001307 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001308 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001309 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1310 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001311 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001312 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001313 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001314
1315 /// @param source the variable source
1316 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001317 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001318 /// @param optional the optional variable settings.
1319 /// Can be any of the following, in any order:
1320 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001321 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001322 /// * ast::Expression* - specifies the variable's initializer expression
1323 /// * ast::DecorationList - specifies the variable's decorations
1324 /// Note that repeated arguments of the same type will use the last argument's
1325 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001326 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001327 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001328 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001329 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001330 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001331 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001332 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1333 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001334 opts.storage, opts.access, type, false,
1335 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001336 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001337
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001338 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001339 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001340 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001341 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001342 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001343 template <typename NAME>
1344 ast::Variable* Const(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 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
1353 /// @param source the variable source
1354 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001355 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001356 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001357 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001358 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001359 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001360 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001361 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001362 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001363 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001364 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001365 return create<ast::Variable>(
1366 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1367 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001368 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001369
James Pricedaf1f1c2021-04-08 22:15:48 +00001370 /// @param name the parameter name
1371 /// @param type the parameter type
1372 /// @param decorations optional parameter decorations
1373 /// @returns a constant `ast::Variable` with the given name and type
1374 template <typename NAME>
1375 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001376 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001377 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001378 return create<ast::Variable>(
1379 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1380 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001381 }
1382
1383 /// @param source the parameter source
1384 /// @param name the parameter name
1385 /// @param type the parameter type
1386 /// @param decorations optional parameter decorations
1387 /// @returns a constant `ast::Variable` with the given name and type
1388 template <typename NAME>
1389 ast::Variable* Param(const Source& source,
1390 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001391 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001392 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001393 return create<ast::Variable>(
1394 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1395 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001396 }
1397
Ben Clayton42708342021-04-21 17:55:12 +00001398 /// @param name the variable name
1399 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001400 /// @param optional the optional variable settings.
1401 /// Can be any of the following, in any order:
1402 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001403 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001404 /// * ast::Expression* - specifies the variable's initializer expression
1405 /// * ast::DecorationList - specifies the variable's decorations
1406 /// Note that repeated arguments of the same type will use the last argument's
1407 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001408 /// @returns a new `ast::Variable`, which is automatically registered as a
1409 /// global variable with the ast::Module.
Antonio Maiorano60dae242021-07-15 19:09:25 +00001410 template <typename NAME,
1411 typename... OPTIONAL,
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001412 typename = DisableIfSource<NAME>>
Ben Clayton42708342021-04-21 17:55:12 +00001413 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001414 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001415 OPTIONAL&&... optional) {
1416 auto* var = Var(std::forward<NAME>(name), type,
1417 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001418 AST().AddGlobalVariable(var);
1419 return var;
1420 }
1421
1422 /// @param source the variable source
1423 /// @param name the variable name
1424 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001425 /// @param optional the optional variable settings.
1426 /// Can be any of the following, in any order:
1427 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001428 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001429 /// * ast::Expression* - specifies the variable's initializer expression
1430 /// * ast::DecorationList - specifies the variable's decorations
1431 /// Note that repeated arguments of the same type will use the last argument's
1432 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001433 /// @returns a new `ast::Variable`, which is automatically registered as a
1434 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001435 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001436 ast::Variable* Global(const Source& source,
1437 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001438 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001439 OPTIONAL&&... optional) {
1440 auto* var = Var(source, std::forward<NAME>(name), type,
1441 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001442 AST().AddGlobalVariable(var);
1443 return var;
1444 }
1445
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001446 /// @param name the variable name
1447 /// @param type the variable type
1448 /// @param constructor constructor expression
1449 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001450 /// @returns a const `ast::Variable` constructed by calling Var() with the
1451 /// arguments of `args`, which is automatically registered as a global
1452 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001453 template <typename NAME>
1454 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001455 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001456 ast::Expression* constructor,
1457 ast::DecorationList decorations = {}) {
1458 auto* var = Const(std::forward<NAME>(name), type, constructor,
1459 std::move(decorations));
1460 AST().AddGlobalVariable(var);
1461 return var;
1462 }
1463
1464 /// @param source the variable source
1465 /// @param name the variable name
1466 /// @param type the variable type
1467 /// @param constructor constructor expression
1468 /// @param decorations optional variable decorations
1469 /// @returns a const `ast::Variable` constructed by calling Var() with the
1470 /// arguments of `args`, which is automatically registered as a global
1471 /// variable with the ast::Module.
1472 template <typename NAME>
1473 ast::Variable* GlobalConst(const Source& source,
1474 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001475 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001476 ast::Expression* constructor,
1477 ast::DecorationList decorations = {}) {
1478 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1479 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001480 AST().AddGlobalVariable(var);
1481 return var;
1482 }
1483
Ben Claytonfe0910f2021-05-17 15:51:47 +00001484 /// @param source the source information
1485 /// @param expr the expression to take the address of
1486 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1487 template <typename EXPR>
1488 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1489 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1490 Expr(std::forward<EXPR>(expr)));
1491 }
1492
1493 /// @param expr the expression to take the address of
1494 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1495 template <typename EXPR>
1496 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1497 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1498 Expr(std::forward<EXPR>(expr)));
1499 }
1500
1501 /// @param source the source information
1502 /// @param expr the expression to perform an indirection on
1503 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1504 template <typename EXPR>
1505 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1506 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1507 Expr(std::forward<EXPR>(expr)));
1508 }
1509
1510 /// @param expr the expression to perform an indirection on
1511 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1512 template <typename EXPR>
1513 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1514 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1515 Expr(std::forward<EXPR>(expr)));
1516 }
1517
Antonio Maiorano14b34032021-06-09 20:17:59 +00001518 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001519 /// @param func the function name
1520 /// @param args the function call arguments
1521 /// @returns a `ast::CallExpression` to the function `func`, with the
1522 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1523 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001524 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1525 return create<ast::CallExpression>(source, Expr(func),
1526 ExprList(std::forward<ARGS>(args)...));
1527 }
1528
1529 /// @param func the function name
1530 /// @param args the function call arguments
1531 /// @returns a `ast::CallExpression` to the function `func`, with the
1532 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001533 template <typename NAME, typename... ARGS, typename = DisableIfSource<NAME>>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001534 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1535 return create<ast::CallExpression>(Expr(func),
1536 ExprList(std::forward<ARGS>(args)...));
1537 }
1538
Ben Clayton7fe01062021-06-11 13:22:27 +00001539 /// @param expr the expression to ignore
1540 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1541 /// passed the single `expr` argument
1542 template <typename EXPR>
1543 ast::CallStatement* Ignore(EXPR&& expr) {
1544 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1545 }
1546
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001547 /// @param lhs the left hand argument to the addition operation
1548 /// @param rhs the right hand argument to the addition operation
1549 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1550 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001551 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001552 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1553 Expr(std::forward<LHS>(lhs)),
1554 Expr(std::forward<RHS>(rhs)));
1555 }
1556
James Pricec32e8f62021-06-22 20:08:29 +00001557 /// @param lhs the left hand argument to the and operation
1558 /// @param rhs the right hand argument to the and operation
1559 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
1560 template <typename LHS, typename RHS>
1561 ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
1562 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
1563 Expr(std::forward<LHS>(lhs)),
1564 Expr(std::forward<RHS>(rhs)));
1565 }
1566
Ben Claytond9603282021-06-29 12:37:45 +00001567 /// @param lhs the left hand argument to the or operation
1568 /// @param rhs the right hand argument to the or operation
1569 /// @returns a `ast::BinaryExpression` bitwise or-ing `lhs` and `rhs`
1570 template <typename LHS, typename RHS>
1571 ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) {
1572 return create<ast::BinaryExpression>(ast::BinaryOp::kOr,
1573 Expr(std::forward<LHS>(lhs)),
1574 Expr(std::forward<RHS>(rhs)));
1575 }
1576
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001577 /// @param lhs the left hand argument to the subtraction operation
1578 /// @param rhs the right hand argument to the subtraction operation
1579 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1580 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001581 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001582 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1583 Expr(std::forward<LHS>(lhs)),
1584 Expr(std::forward<RHS>(rhs)));
1585 }
1586
1587 /// @param lhs the left hand argument to the multiplication operation
1588 /// @param rhs the right hand argument to the multiplication operation
1589 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1590 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001591 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001592 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1593 Expr(std::forward<LHS>(lhs)),
1594 Expr(std::forward<RHS>(rhs)));
1595 }
1596
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001597 /// @param source the source information
1598 /// @param lhs the left hand argument to the multiplication operation
1599 /// @param rhs the right hand argument to the multiplication operation
1600 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1601 template <typename LHS, typename RHS>
1602 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1603 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1604 Expr(std::forward<LHS>(lhs)),
1605 Expr(std::forward<RHS>(rhs)));
1606 }
1607
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001608 /// @param lhs the left hand argument to the division operation
1609 /// @param rhs the right hand argument to the division operation
1610 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1611 template <typename LHS, typename RHS>
1612 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1613 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1614 Expr(std::forward<LHS>(lhs)),
1615 Expr(std::forward<RHS>(rhs)));
1616 }
1617
Ben Claytond9603282021-06-29 12:37:45 +00001618 /// @param lhs the left hand argument to the bit shift right operation
1619 /// @param rhs the right hand argument to the bit shift right operation
1620 /// @returns a `ast::BinaryExpression` bit shifting right `lhs` by `rhs`
1621 template <typename LHS, typename RHS>
1622 ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) {
1623 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftRight,
1624 Expr(std::forward<LHS>(lhs)),
1625 Expr(std::forward<RHS>(rhs)));
1626 }
1627
1628 /// @param lhs the left hand argument to the bit shift left operation
1629 /// @param rhs the right hand argument to the bit shift left operation
1630 /// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs`
1631 template <typename LHS, typename RHS>
1632 ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) {
1633 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftLeft,
1634 Expr(std::forward<LHS>(lhs)),
1635 Expr(std::forward<RHS>(rhs)));
1636 }
1637
Ben Clayton0597a2b2021-06-16 09:19:36 +00001638 /// @param source the source information
1639 /// @param arr the array argument for the array accessor expression
1640 /// @param idx the index argument for the array accessor expression
1641 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1642 template <typename ARR, typename IDX>
1643 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1644 ARR&& arr,
1645 IDX&& idx) {
1646 return create<ast::ArrayAccessorExpression>(
1647 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1648 }
1649
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001650 /// @param arr the array argument for the array accessor expression
1651 /// @param idx the index argument for the array accessor expression
1652 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1653 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001654 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001655 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1656 Expr(std::forward<IDX>(idx)));
1657 }
1658
1659 /// @param obj the object for the member accessor expression
1660 /// @param idx the index argument for the array accessor expression
1661 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1662 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001663 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001664 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1665 Expr(std::forward<IDX>(idx)));
1666 }
1667
Ben Clayton42d1e092021-02-02 14:29:15 +00001668 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001669 /// @param val the offset value
1670 /// @returns the offset decoration pointer
1671 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1672 return create<ast::StructMemberOffsetDecoration>(source_, val);
1673 }
1674
Ben Claytond614dd52021-03-15 10:43:11 +00001675 /// Creates a ast::StructMemberSizeDecoration
1676 /// @param source the source information
1677 /// @param val the size value
1678 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001679 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1680 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001681 return create<ast::StructMemberSizeDecoration>(source, val);
1682 }
1683
1684 /// Creates a ast::StructMemberSizeDecoration
1685 /// @param val the size value
1686 /// @returns the size decoration pointer
1687 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1688 return create<ast::StructMemberSizeDecoration>(source_, val);
1689 }
1690
1691 /// Creates a ast::StructMemberAlignDecoration
1692 /// @param source the source information
1693 /// @param val the align value
1694 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001695 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1696 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001697 return create<ast::StructMemberAlignDecoration>(source, val);
1698 }
1699
1700 /// Creates a ast::StructMemberAlignDecoration
1701 /// @param val the align value
1702 /// @returns the align decoration pointer
1703 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1704 return create<ast::StructMemberAlignDecoration>(source_, val);
1705 }
1706
Antonio Maiorano60dae242021-07-15 19:09:25 +00001707 /// Creates a ast::StructBlockDecoration
1708 /// @returns the struct block decoration pointer
1709 ast::StructBlockDecoration* StructBlock() {
1710 return create<ast::StructBlockDecoration>();
1711 }
1712
1713 /// Creates the ast::GroupDecoration
1714 /// @param value group decoration index
1715 /// @returns the group decoration pointer
1716 ast::GroupDecoration* Group(uint32_t value) {
1717 return create<ast::GroupDecoration>(value);
1718 }
1719
1720 /// Creates the ast::BindingDecoration
1721 /// @param value the binding index
1722 /// @returns the binding deocration pointer
1723 ast::BindingDecoration* Binding(uint32_t value) {
1724 return create<ast::BindingDecoration>(value);
1725 }
1726
1727 /// Convenience function to create both a ast::GroupDecoration and
1728 /// ast::BindingDecoration
1729 /// @param group the group index
1730 /// @param binding the binding index
1731 /// @returns a decoration list with both the group and binding decorations
1732 ast::DecorationList GroupAndBinding(uint32_t group, uint32_t binding) {
1733 return {Group(group), Binding(binding)};
1734 }
1735
Ben Clayton42d1e092021-02-02 14:29:15 +00001736 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001737 /// @param source the source information
1738 /// @param name the function name
1739 /// @param params the function parameters
1740 /// @param type the function return type
1741 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001742 /// @param decorations the optional function decorations
1743 /// @param return_type_decorations the optional function return type
1744 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001745 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001746 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001747 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001748 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001749 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001750 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001751 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001752 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001753 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001754 auto* func =
1755 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1756 type, create<ast::BlockStatement>(body),
1757 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001758 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001759 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001760 }
1761
Ben Clayton42d1e092021-02-02 14:29:15 +00001762 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001763 /// @param name the function name
1764 /// @param params the function parameters
1765 /// @param type the function return type
1766 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001767 /// @param decorations the optional function decorations
1768 /// @param return_type_decorations the optional function return type
1769 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001770 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001771 template <typename NAME>
1772 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001773 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001774 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001775 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001776 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001777 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001778 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1779 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001780 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001781 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001782 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001783 }
1784
Ben Clayton49668bb2021-04-17 05:32:01 +00001785 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001786 /// @param source the source information
1787 /// @returns the return statement pointer
1788 ast::ReturnStatement* Return(const Source& source) {
1789 return create<ast::ReturnStatement>(source);
1790 }
1791
1792 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001793 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001794 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1795
1796 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001797 /// @param source the source information
1798 /// @param val the return value
1799 /// @returns the return statement pointer
1800 template <typename EXPR>
1801 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1802 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1803 }
1804
1805 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001806 /// @param val the return value
1807 /// @returns the return statement pointer
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001808 template <typename EXPR, typename = DisableIfSource<EXPR>>
Ben Clayton49668bb2021-04-17 05:32:01 +00001809 ast::ReturnStatement* Return(EXPR&& val) {
1810 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001811 }
1812
Ben Clayton950809f2021-06-09 14:32:14 +00001813 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001814 /// @param source the source information
1815 /// @param name the alias name
1816 /// @param type the alias target type
1817 /// @returns the alias type
1818 template <typename NAME>
1819 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1820 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001821 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001822 return out;
1823 }
1824
Ben Clayton950809f2021-06-09 14:32:14 +00001825 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001826 /// @param name the alias name
1827 /// @param type the alias target type
1828 /// @returns the alias type
1829 template <typename NAME>
1830 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1831 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001832 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001833 return out;
1834 }
1835
Ben Clayton950809f2021-06-09 14:32:14 +00001836 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001837 /// @param source the source information
1838 /// @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(const Source& source,
1844 NAME&& name,
1845 ast::StructMemberList members,
1846 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001847 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001848 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001849 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001850 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001851 return type;
1852 }
1853
Ben Clayton950809f2021-06-09 14:32:14 +00001854 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001855 /// @param name the struct name
1856 /// @param members the struct members
1857 /// @param decorations the optional struct decorations
1858 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001859 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001860 ast::Struct* Structure(NAME&& name,
1861 ast::StructMemberList members,
1862 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001863 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001864 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001865 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001866 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001867 return type;
1868 }
1869
Ben Clayton42d1e092021-02-02 14:29:15 +00001870 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001871 /// @param source the source information
1872 /// @param name the struct member name
1873 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001874 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001875 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001876 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001877 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001878 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001879 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001880 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001881 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1882 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001883 }
1884
Ben Clayton42d1e092021-02-02 14:29:15 +00001885 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001886 /// @param name the struct member name
1887 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001888 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001889 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001890 template <typename NAME>
1891 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001892 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001893 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001894 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1895 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001896 }
1897
Ben Claytonbab31972021-02-08 21:16:21 +00001898 /// Creates a ast::StructMember with the given byte offset
1899 /// @param offset the offset to use in the StructMemberOffsetDecoration
1900 /// @param name the struct member name
1901 /// @param type the struct member type
1902 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001903 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001904 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001905 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001906 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001907 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001908 create<ast::StructMemberOffsetDecoration>(offset),
1909 });
1910 }
1911
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001912 /// Creates a ast::BlockStatement with input statements
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001913 /// @param source the source information for the block
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001914 /// @param statements statements of block
1915 /// @returns the block statement pointer
1916 template <typename... Statements>
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001917 ast::BlockStatement* Block(const Source& source, Statements&&... statements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001918 return create<ast::BlockStatement>(
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001919 source, ast::StatementList{std::forward<Statements>(statements)...});
1920 }
1921
1922 /// Creates a ast::BlockStatement with input statements
1923 /// @param statements statements of block
1924 /// @returns the block statement pointer
1925 template <typename... STATEMENTS, typename = DisableIfSource<STATEMENTS...>>
1926 ast::BlockStatement* Block(STATEMENTS&&... statements) {
1927 return create<ast::BlockStatement>(
1928 ast::StatementList{std::forward<STATEMENTS>(statements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001929 }
1930
1931 /// Creates a ast::ElseStatement with input condition and body
1932 /// @param condition the else condition expression
1933 /// @param body the else body
1934 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001935 template <typename CONDITION>
1936 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1937 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1938 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001939 }
1940
1941 /// Creates a ast::IfStatement with input condition, body, and optional
1942 /// variadic else statements
1943 /// @param condition the if statement condition expression
1944 /// @param body the if statement body
1945 /// @param elseStatements optional variadic else statements
1946 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001947 template <typename CONDITION, typename... ELSE_STATEMENTS>
1948 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001949 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001950 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001951 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001952 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001953 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001954 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001955 }
1956
1957 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001958 /// @param source the source information
1959 /// @param lhs the left hand side expression initializer
1960 /// @param rhs the right hand side expression initializer
1961 /// @returns the assignment statement pointer
1962 template <typename LhsExpressionInit, typename RhsExpressionInit>
1963 ast::AssignmentStatement* Assign(const Source& source,
1964 LhsExpressionInit&& lhs,
1965 RhsExpressionInit&& rhs) {
1966 return create<ast::AssignmentStatement>(
1967 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1968 Expr(std::forward<RhsExpressionInit>(rhs)));
1969 }
1970
1971 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001972 /// @param lhs the left hand side expression initializer
1973 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001974 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001975 template <typename LhsExpressionInit, typename RhsExpressionInit>
1976 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1977 RhsExpressionInit&& rhs) {
1978 return create<ast::AssignmentStatement>(
1979 Expr(std::forward<LhsExpressionInit>(lhs)),
1980 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001981 }
1982
1983 /// Creates a ast::LoopStatement with input body and optional continuing
1984 /// @param body the loop body
1985 /// @param continuing the optional continuing block
1986 /// @returns the loop statement pointer
1987 ast::LoopStatement* Loop(ast::BlockStatement* body,
1988 ast::BlockStatement* continuing = nullptr) {
1989 return create<ast::LoopStatement>(body, continuing);
1990 }
1991
Ben Clayton65cd2592021-07-02 19:27:42 +00001992 /// Creates a ast::ForLoopStatement with input body and optional initializer,
1993 /// condition and continuing.
1994 /// @param source the source information
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(const Source& source,
2002 ast::Statement* init,
2003 COND&& cond,
2004 ast::Statement* cont,
2005 ast::BlockStatement* body) {
2006 return create<ast::ForLoopStatement>(
2007 source, init, Expr(std::forward<COND>(cond)), cont, body);
2008 }
2009
2010 /// Creates a ast::ForLoopStatement with input body and optional initializer,
2011 /// condition and continuing.
2012 /// @param init the optional loop initializer
2013 /// @param cond the optional loop condition
2014 /// @param cont the optional loop continuing
2015 /// @param body the loop body
2016 /// @returns the for loop statement pointer
2017 template <typename COND>
2018 ast::ForLoopStatement* For(ast::Statement* init,
2019 COND&& cond,
2020 ast::Statement* cont,
2021 ast::BlockStatement* body) {
2022 return create<ast::ForLoopStatement>(init, Expr(std::forward<COND>(cond)),
2023 cont, body);
2024 }
2025
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00002026 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00002027 /// @param source the source information
2028 /// @param var the variable to wrap in a decl statement
2029 /// @returns the variable decl statement pointer
2030 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
2031 return create<ast::VariableDeclStatement>(source, var);
2032 }
2033
2034 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00002035 /// @param var the variable to wrap in a decl statement
2036 /// @returns the variable decl statement pointer
2037 ast::VariableDeclStatement* Decl(ast::Variable* var) {
2038 return create<ast::VariableDeclStatement>(var);
2039 }
2040
Antonio Maioranocea744d2021-03-25 12:55:27 +00002041 /// Creates a ast::SwitchStatement with input expression and cases
2042 /// @param condition the condition expression initializer
2043 /// @param cases case statements
2044 /// @returns the switch statement pointer
2045 template <typename ExpressionInit, typename... Cases>
2046 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
2047 return create<ast::SwitchStatement>(
2048 Expr(std::forward<ExpressionInit>(condition)),
2049 ast::CaseStatementList{std::forward<Cases>(cases)...});
2050 }
2051
2052 /// Creates a ast::CaseStatement with input list of selectors, and body
2053 /// @param selectors list of selectors
2054 /// @param body the case body
2055 /// @returns the case statement pointer
2056 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
2057 ast::BlockStatement* body = nullptr) {
2058 return create<ast::CaseStatement>(std::move(selectors),
2059 body ? body : Block());
2060 }
2061
2062 /// Convenient overload that takes a single selector
2063 /// @param selector a single case selector
2064 /// @param body the case body
2065 /// @returns the case statement pointer
2066 ast::CaseStatement* Case(ast::IntLiteral* selector,
2067 ast::BlockStatement* body = nullptr) {
2068 return Case(ast::CaseSelectorList{selector}, body);
2069 }
2070
2071 /// Convenience function that creates a 'default' ast::CaseStatement
2072 /// @param body the case body
2073 /// @returns the case statement pointer
2074 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
2075 return Case(ast::CaseSelectorList{}, body);
2076 }
2077
James Price68f558f2021-04-06 15:51:47 +00002078 /// Creates an ast::BuiltinDecoration
2079 /// @param source the source information
2080 /// @param builtin the builtin value
2081 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002082 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00002083 return create<ast::BuiltinDecoration>(source, builtin);
2084 }
2085
2086 /// Creates an ast::BuiltinDecoration
2087 /// @param builtin the builtin value
2088 /// @returns the builtin decoration pointer
2089 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
2090 return create<ast::BuiltinDecoration>(source_, builtin);
2091 }
2092
James Price989a8e42021-06-28 23:04:43 +00002093 /// Creates an ast::InterpolateDecoration
2094 /// @param source the source information
2095 /// @param type the interpolation type
2096 /// @param sampling the interpolation sampling
2097 /// @returns the interpolate decoration pointer
2098 ast::InterpolateDecoration* Interpolate(const Source& source,
2099 ast::InterpolationType type,
2100 ast::InterpolationSampling sampling) {
2101 return create<ast::InterpolateDecoration>(source, type, sampling);
2102 }
2103
2104 /// Creates an ast::InterpolateDecoration
2105 /// @param type the interpolation type
2106 /// @param sampling the interpolation sampling
2107 /// @returns the interpolate decoration pointer
2108 ast::InterpolateDecoration* Interpolate(ast::InterpolationType type,
2109 ast::InterpolationSampling sampling) {
2110 return create<ast::InterpolateDecoration>(source_, type, sampling);
2111 }
2112
James Price508d2492021-07-12 12:12:32 +00002113 /// Creates an ast::InvariantDecoration
2114 /// @param source the source information
2115 /// @returns the invariant decoration pointer
2116 ast::InvariantDecoration* Invariant(const Source& source) {
2117 return create<ast::InvariantDecoration>(source);
2118 }
2119
2120 /// Creates an ast::InvariantDecoration
2121 /// @returns the invariant decoration pointer
2122 ast::InvariantDecoration* Invariant() {
2123 return create<ast::InvariantDecoration>(source_);
2124 }
2125
James Price68f558f2021-04-06 15:51:47 +00002126 /// Creates an ast::LocationDecoration
2127 /// @param source the source information
2128 /// @param location the location value
2129 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002130 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00002131 return create<ast::LocationDecoration>(source, location);
2132 }
2133
2134 /// Creates an ast::LocationDecoration
2135 /// @param location the location value
2136 /// @returns the location decoration pointer
2137 ast::LocationDecoration* Location(uint32_t location) {
2138 return create<ast::LocationDecoration>(source_, location);
2139 }
2140
James Pricef2f3bfc2021-05-13 20:32:32 +00002141 /// Creates an ast::OverrideDecoration with a specific constant ID
2142 /// @param source the source information
2143 /// @param id the id value
2144 /// @returns the override decoration pointer
2145 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
2146 return create<ast::OverrideDecoration>(source, id);
2147 }
2148
2149 /// Creates an ast::OverrideDecoration with a specific constant ID
2150 /// @param id the optional id value
2151 /// @returns the override decoration pointer
2152 ast::OverrideDecoration* Override(uint32_t id) {
2153 return Override(source_, id);
2154 }
2155
2156 /// Creates an ast::OverrideDecoration without a constant ID
2157 /// @param source the source information
2158 /// @returns the override decoration pointer
2159 ast::OverrideDecoration* Override(const Source& source) {
2160 return create<ast::OverrideDecoration>(source);
2161 }
2162
2163 /// Creates an ast::OverrideDecoration without a constant ID
2164 /// @returns the override decoration pointer
2165 ast::OverrideDecoration* Override() { return Override(source_); }
2166
James Price68f558f2021-04-06 15:51:47 +00002167 /// Creates an ast::StageDecoration
2168 /// @param source the source information
2169 /// @param stage the pipeline stage
2170 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002171 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00002172 return create<ast::StageDecoration>(source, stage);
2173 }
2174
2175 /// Creates an ast::StageDecoration
2176 /// @param stage the pipeline stage
2177 /// @returns the stage decoration pointer
2178 ast::StageDecoration* Stage(ast::PipelineStage stage) {
2179 return create<ast::StageDecoration>(source_, stage);
2180 }
2181
James Price70f80bb2021-05-19 13:40:08 +00002182 /// Creates an ast::WorkgroupDecoration
2183 /// @param x the x dimension expression
2184 /// @returns the workgroup decoration pointer
2185 template <typename EXPR_X>
2186 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
2187 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
2188 }
2189
2190 /// Creates an ast::WorkgroupDecoration
2191 /// @param x the x dimension expression
2192 /// @param y the y dimension expression
2193 /// @returns the workgroup decoration pointer
2194 template <typename EXPR_X, typename EXPR_Y>
2195 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
2196 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
2197 nullptr);
2198 }
2199
2200 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00002201 /// @param source the source information
2202 /// @param x the x dimension expression
2203 /// @param y the y dimension expression
2204 /// @param z the z dimension expression
2205 /// @returns the workgroup decoration pointer
2206 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2207 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
2208 EXPR_X&& x,
2209 EXPR_Y&& y,
2210 EXPR_Z&& z) {
2211 return create<ast::WorkgroupDecoration>(
2212 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2213 Expr(std::forward<EXPR_Z>(z)));
2214 }
2215
2216 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002217 /// @param x the x dimension expression
2218 /// @param y the y dimension expression
2219 /// @param z the z dimension expression
2220 /// @returns the workgroup decoration pointer
2221 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2222 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2223 return create<ast::WorkgroupDecoration>(
2224 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2225 Expr(std::forward<EXPR_Z>(z)));
2226 }
2227
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002228 /// Sets the current builder source to `src`
2229 /// @param src the Source used for future create() calls
2230 void SetSource(const Source& src) {
2231 AssertNotMoved();
2232 source_ = src;
2233 }
2234
2235 /// Sets the current builder source to `loc`
2236 /// @param loc the Source used for future create() calls
2237 void SetSource(const Source::Location& loc) {
2238 AssertNotMoved();
2239 source_ = Source(loc);
2240 }
2241
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002242 /// Marks that the given transform has been applied to this program.
2243 /// @param transform the transform that has been applied
2244 void SetTransformApplied(const CastableBase* transform) {
2245 transforms_applied_.emplace(&transform->TypeInfo());
2246 }
2247
2248 /// Marks that the given transform `T` has been applied to this program.
2249 template <typename T>
2250 void SetTransformApplied() {
2251 transforms_applied_.emplace(&TypeInfo::Of<T>());
2252 }
2253
2254 /// Marks that the transforms with the given TypeInfos have been applied to
2255 /// this program.
2256 /// @param transforms the set of transform TypeInfos that has been applied
2257 void SetTransformApplied(
2258 const std::unordered_set<const TypeInfo*>& transforms) {
2259 for (auto* transform : transforms) {
2260 transforms_applied_.emplace(transform);
2261 }
2262 }
2263
2264 /// @returns true if the transform of type `T` was applied.
2265 template <typename T>
2266 bool HasTransformApplied() {
2267 return transforms_applied_.count(&TypeInfo::Of<T>());
2268 }
2269
2270 /// @return the TypeInfo pointers of all transforms that have been applied to
2271 /// this program.
2272 std::unordered_set<const TypeInfo*> TransformsApplied() const {
2273 return transforms_applied_;
2274 }
2275
Ben Clayton33352542021-01-29 16:43:41 +00002276 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002277 /// @note As the Resolver is run when the Program is built, this will only be
2278 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002279 /// @param expr the AST expression
2280 /// @return the resolved semantic type for the expression, or nullptr if the
2281 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002282 sem::Type* TypeOf(const ast::Expression* expr) const;
2283
Ben Claytonfe0910f2021-05-17 15:51:47 +00002284 /// Helper for returning the resolved semantic type of the variable `var`.
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 var the AST variable
2288 /// @return the resolved semantic type for the variable, or nullptr if the
2289 /// variable has no resolved type.
2290 sem::Type* TypeOf(const ast::Variable* var) const;
2291
Ben Claytonfbec46f2021-04-30 20:20:19 +00002292 /// Helper for returning the resolved semantic type of the AST type `type`.
2293 /// @note As the Resolver is run when the Program is built, this will only be
2294 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002295 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002296 /// @return the resolved semantic type for the type, or nullptr if the type
2297 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002298 const sem::Type* TypeOf(const ast::Type* type) const;
2299
2300 /// Helper for returning the resolved semantic type of the AST type
2301 /// declaration `type_decl`.
2302 /// @note As the Resolver is run when the Program is built, this will only be
2303 /// useful for the Resolver itself and tests that use their own Resolver.
2304 /// @param type_decl the AST type declaration
2305 /// @return the resolved semantic type for the type declaration, or nullptr if
2306 /// the type declaration has no resolved type.
2307 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002308
Ben Clayton169512e2021-04-17 05:52:11 +00002309 /// Wraps the ast::Literal in a statement. This is used by tests that
2310 /// construct a partial AST and require the Resolver to reach these
2311 /// nodes.
2312 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2313 /// @return the ast::Statement that wraps the ast::Statement
2314 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002315 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002316 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002317 /// nodes.
2318 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2319 /// @return the ast::Statement that wraps the ast::Expression
2320 ast::Statement* WrapInStatement(ast::Expression* expr);
2321 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002322 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002323 /// these nodes.
2324 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2325 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2326 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2327 /// Returns the statement argument. Used as a passthrough-overload by
2328 /// WrapInFunction().
2329 /// @param stmt the ast::Statement
2330 /// @return `stmt`
2331 ast::Statement* WrapInStatement(ast::Statement* stmt);
2332 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002333 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002334 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002335 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002336 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002337 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002338 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002339 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002340 }
2341 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002342 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002343 /// @returns the function
2344 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002345
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002346 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002347 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002348
2349 protected:
2350 /// Asserts that the builder has not been moved.
2351 void AssertNotMoved() const;
2352
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002353 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002354 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002355 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002356 ASTNodeAllocator ast_nodes_;
2357 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002358 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002359 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002360 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002361 diag::List diagnostics_;
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002362 std::unordered_set<const TypeInfo*> transforms_applied_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002363
2364 /// The source to use when creating AST nodes without providing a Source as
2365 /// the first argument.
2366 Source source_;
2367
Ben Clayton5f0ea112021-03-09 10:54:37 +00002368 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002369 /// program when built.
2370 bool resolve_on_build_ = true;
2371
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002372 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2373 bool moved_ = false;
2374};
2375
2376//! @cond Doxygen_Suppress
2377// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2378template <>
2379struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002380 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002381 return t->i32();
2382 }
2383};
2384template <>
2385struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002386 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002387 return t->u32();
2388 }
2389};
2390template <>
2391struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002392 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002393 return t->f32();
2394 }
2395};
2396template <>
2397struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002398 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002399 return t->bool_();
2400 }
2401};
2402template <>
2403struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002404 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002405 return t->void_();
2406 }
2407};
2408//! @endcond
2409
Ben Clayton917b14b2021-04-19 16:50:23 +00002410/// @param builder the ProgramBuilder
2411/// @returns the ProgramID of the ProgramBuilder
2412inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2413 return builder->ID();
2414}
2415
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002416} // namespace tint
2417
2418#endif // SRC_PROGRAM_BUILDER_H_