blob: 4dac010736a606a445361bbdcf9af4637d3aa677 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +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_TINT_PROGRAM_BUILDER_H_
16#define SRC_TINT_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <unordered_set>
20#include <utility>
21
Ben Clayton9a6acc42022-07-27 20:50:40 +000022#include "tint/override_id.h"
23
Ryan Harrisondbc13af2022-02-21 15:19:07 +000024#include "src/tint/ast/alias.h"
25#include "src/tint/ast/array.h"
26#include "src/tint/ast/assignment_statement.h"
27#include "src/tint/ast/atomic.h"
28#include "src/tint/ast/binary_expression.h"
29#include "src/tint/ast/binding_attribute.h"
30#include "src/tint/ast/bitcast_expression.h"
31#include "src/tint/ast/bool.h"
32#include "src/tint/ast/bool_literal_expression.h"
dan sinclairb8b0c212022-10-20 22:45:50 +000033#include "src/tint/ast/break_if_statement.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000034#include "src/tint/ast/break_statement.h"
35#include "src/tint/ast/call_expression.h"
36#include "src/tint/ast/call_statement.h"
37#include "src/tint/ast/case_statement.h"
James Pricec022ff52022-03-31 22:30:10 +000038#include "src/tint/ast/compound_assignment_statement.h"
Ben Claytone3834c42022-06-25 23:21:39 +000039#include "src/tint/ast/const.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000040#include "src/tint/ast/continue_statement.h"
41#include "src/tint/ast/depth_multisampled_texture.h"
42#include "src/tint/ast/depth_texture.h"
43#include "src/tint/ast/disable_validation_attribute.h"
44#include "src/tint/ast/discard_statement.h"
Zhaoming Jiang7098d3d2022-04-27 02:27:52 +000045#include "src/tint/ast/enable.h"
Ben Clayton7f2b8cd2022-05-18 22:41:48 +000046#include "src/tint/ast/extension.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000047#include "src/tint/ast/external_texture.h"
Zhaoming Jiang62bfd312022-05-13 12:01:11 +000048#include "src/tint/ast/f16.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000049#include "src/tint/ast/f32.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000050#include "src/tint/ast/float_literal_expression.h"
51#include "src/tint/ast/for_loop_statement.h"
52#include "src/tint/ast/i32.h"
53#include "src/tint/ast/id_attribute.h"
54#include "src/tint/ast/if_statement.h"
James Priceebe97412022-04-07 13:42:45 +000055#include "src/tint/ast/increment_decrement_statement.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000056#include "src/tint/ast/index_accessor_expression.h"
dan sinclaird32fbe02022-10-19 00:43:41 +000057#include "src/tint/ast/int_literal_expression.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000058#include "src/tint/ast/interpolate_attribute.h"
59#include "src/tint/ast/invariant_attribute.h"
Ben Claytondcdf66e2022-06-17 12:48:51 +000060#include "src/tint/ast/let.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000061#include "src/tint/ast/loop_statement.h"
62#include "src/tint/ast/matrix.h"
63#include "src/tint/ast/member_accessor_expression.h"
64#include "src/tint/ast/module.h"
65#include "src/tint/ast/multisampled_texture.h"
Ben Claytondcdf66e2022-06-17 12:48:51 +000066#include "src/tint/ast/override.h"
67#include "src/tint/ast/parameter.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000068#include "src/tint/ast/phony_expression.h"
69#include "src/tint/ast/pointer.h"
70#include "src/tint/ast/return_statement.h"
71#include "src/tint/ast/sampled_texture.h"
72#include "src/tint/ast/sampler.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000073#include "src/tint/ast/stage_attribute.h"
Ben Claytonbfd1a812022-08-02 23:16:25 +000074#include "src/tint/ast/static_assert.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000075#include "src/tint/ast/storage_texture.h"
76#include "src/tint/ast/stride_attribute.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000077#include "src/tint/ast/struct_member_align_attribute.h"
78#include "src/tint/ast/struct_member_offset_attribute.h"
79#include "src/tint/ast/struct_member_size_attribute.h"
80#include "src/tint/ast/switch_statement.h"
81#include "src/tint/ast/type_name.h"
82#include "src/tint/ast/u32.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000083#include "src/tint/ast/unary_op_expression.h"
Ben Claytondcdf66e2022-06-17 12:48:51 +000084#include "src/tint/ast/var.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000085#include "src/tint/ast/variable_decl_statement.h"
86#include "src/tint/ast/vector.h"
87#include "src/tint/ast/void.h"
dan sinclair49d1a2d2022-06-16 12:01:27 +000088#include "src/tint/ast/while_statement.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000089#include "src/tint/ast/workgroup_attribute.h"
dan sinclair02b466f2022-12-15 21:56:32 +000090#include "src/tint/constant/composite.h"
91#include "src/tint/constant/splat.h"
dan sinclairb53b8cf2022-12-15 16:25:31 +000092#include "src/tint/constant/value.h"
Ben Clayton0ce9ab02022-05-05 20:23:40 +000093#include "src/tint/number.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000094#include "src/tint/program.h"
95#include "src/tint/program_id.h"
dan sinclair4b1d79e2022-12-01 23:45:18 +000096#include "src/tint/sem/array_count.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000097#include "src/tint/sem/struct.h"
dan sinclair946858a2022-12-08 22:21:24 +000098#include "src/tint/type/array.h"
dan sinclaird37ecf92022-12-08 16:39:59 +000099#include "src/tint/type/bool.h"
dan sinclair4595fb72022-12-08 14:14:10 +0000100#include "src/tint/type/depth_texture.h"
101#include "src/tint/type/external_texture.h"
dan sinclaird37ecf92022-12-08 16:39:59 +0000102#include "src/tint/type/f16.h"
103#include "src/tint/type/f32.h"
104#include "src/tint/type/i32.h"
dan sinclair0e780da2022-12-08 22:21:24 +0000105#include "src/tint/type/matrix.h"
dan sinclair4595fb72022-12-08 14:14:10 +0000106#include "src/tint/type/multisampled_texture.h"
dan sinclair4d56b482022-12-08 17:50:50 +0000107#include "src/tint/type/pointer.h"
dan sinclair4595fb72022-12-08 14:14:10 +0000108#include "src/tint/type/sampled_texture.h"
109#include "src/tint/type/storage_texture.h"
dan sinclaird37ecf92022-12-08 16:39:59 +0000110#include "src/tint/type/u32.h"
dan sinclair0e780da2022-12-08 22:21:24 +0000111#include "src/tint/type/vector.h"
dan sinclaird37ecf92022-12-08 16:39:59 +0000112#include "src/tint/type/void.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000113
Ben Clayton8fa6c252022-11-03 19:16:26 +0000114#ifdef CURRENTLY_IN_TINT_PUBLIC_HEADER
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000115#error "internal tint header being #included from tint.h"
116#endif
117
118// Forward declarations
Ben Claytona7230f02022-04-11 14:37:21 +0000119namespace tint {
120class CloneContext;
121} // namespace tint
dan sinclair8155b9d2022-04-07 19:10:25 +0000122namespace tint::ast {
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000123class VariableDeclStatement;
dan sinclair8155b9d2022-04-07 19:10:25 +0000124} // namespace tint::ast
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000125
126namespace tint {
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000127
Ben Clayton783b1692022-08-02 17:03:35 +0000128namespace detail {
129
130/// IsVectorLike<T>::value is true if T is a utils::Vector or utils::VectorRef.
131template <typename T>
132struct IsVectorLike {
133 /// Non-specialized form of IsVectorLike defaults to false
134 static constexpr bool value = false;
135};
136
137/// IsVectorLike specialization for utils::Vector
138template <typename T, size_t N>
139struct IsVectorLike<utils::Vector<T, N>> {
140 /// True for the IsVectorLike specialization of utils::Vector
141 static constexpr bool value = true;
142};
143
144/// IsVectorLike specialization for utils::VectorRef
145template <typename T>
146struct IsVectorLike<utils::VectorRef<T>> {
147 /// True for the IsVectorLike specialization of utils::VectorRef
148 static constexpr bool value = true;
149};
150} // namespace detail
151
Antonio Maioranodfa92a92022-12-23 17:56:56 +0000152// Forward declare metafunction that evaluates to true iff T can be wrapped in a statement.
153template <typename T, typename = void>
154struct CanWrapInStatement;
155
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000156/// ProgramBuilder is a mutable builder for a Program.
157/// To construct a Program, populate the builder and then `std::move` it to a
158/// Program.
159class ProgramBuilder {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000160 /// A helper used to disable overloads if the first type in `TYPES` is a
161 /// Source. Used to avoid ambiguities in overloads that take a Source as the
162 /// first parameter and those that perfectly-forward the first argument.
163 template <typename... TYPES>
164 using DisableIfSource =
165 traits::EnableIfIsNotType<traits::Decay<traits::NthTypeOf<0, TYPES..., void>>, Source>;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000166
Ben Clayton783b1692022-08-02 17:03:35 +0000167 /// A helper used to disable overloads if the first type in `TYPES` is a utils::Vector,
168 /// utils::VectorRef or utils::VectorRef.
169 template <typename... TYPES>
170 using DisableIfVectorLike = traits::EnableIf<
171 !detail::IsVectorLike<traits::Decay<traits::NthTypeOf<0, TYPES..., void>>>::value>;
172
Ben Clayton58794ae2022-08-19 17:28:53 +0000173 /// VarOptions is a helper for accepting an arbitrary number of order independent options for
174 /// constructing an ast::Var.
175 struct VarOptions {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000176 template <typename... ARGS>
Ben Clayton58794ae2022-08-19 17:28:53 +0000177 explicit VarOptions(ARGS&&... args) {
178 (Set(std::forward<ARGS>(args)), ...);
dan sinclair41e4d9a2022-05-01 14:40:55 +0000179 }
Ben Clayton58794ae2022-08-19 17:28:53 +0000180 ~VarOptions();
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000181
Ben Clayton58794ae2022-08-19 17:28:53 +0000182 const ast::Type* type = nullptr;
dan sinclairff7cf212022-10-03 14:05:23 +0000183 ast::AddressSpace address_space = ast::AddressSpace::kNone;
Ben Claytond2e0db32022-10-12 18:49:15 +0000184 ast::Access access = ast::Access::kUndefined;
dan sinclair6e77b472022-10-20 13:38:28 +0000185 const ast::Expression* initializer = nullptr;
Ben Clayton783b1692022-08-02 17:03:35 +0000186 utils::Vector<const ast::Attribute*, 4> attributes;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000187
dan sinclair41e4d9a2022-05-01 14:40:55 +0000188 private:
Ben Clayton58794ae2022-08-19 17:28:53 +0000189 void Set(const ast::Type* t) { type = t; }
dan sinclairff7cf212022-10-03 14:05:23 +0000190 void Set(ast::AddressSpace addr_space) { address_space = addr_space; }
dan sinclair41e4d9a2022-05-01 14:40:55 +0000191 void Set(ast::Access ac) { access = ac; }
dan sinclair6e77b472022-10-20 13:38:28 +0000192 void Set(const ast::Expression* c) { initializer = c; }
Ben Clayton783b1692022-08-02 17:03:35 +0000193 void Set(utils::VectorRef<const ast::Attribute*> l) { attributes = std::move(l); }
Ben Clayton58794ae2022-08-19 17:28:53 +0000194 void Set(const ast::Attribute* a) { attributes.Push(a); }
195 };
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000196
Ben Clayton58794ae2022-08-19 17:28:53 +0000197 /// LetOptions is a helper for accepting an arbitrary number of order independent options for
198 /// constructing an ast::Let.
199 struct LetOptions {
200 template <typename... ARGS>
201 explicit LetOptions(ARGS&&... args) {
202 static constexpr bool has_init =
203 (traits::IsTypeOrDerived<std::remove_pointer_t<std::remove_reference_t<ARGS>>,
204 ast::Expression> ||
205 ...);
206 static_assert(has_init, "Let() must be constructed with an initializer expression");
207 (Set(std::forward<ARGS>(args)), ...);
dan sinclair41e4d9a2022-05-01 14:40:55 +0000208 }
Ben Clayton58794ae2022-08-19 17:28:53 +0000209 ~LetOptions();
210
211 const ast::Type* type = nullptr;
dan sinclair6e77b472022-10-20 13:38:28 +0000212 const ast::Expression* initializer = nullptr;
Ben Clayton58794ae2022-08-19 17:28:53 +0000213 utils::Vector<const ast::Attribute*, 4> attributes;
214
215 private:
216 void Set(const ast::Type* t) { type = t; }
dan sinclair6e77b472022-10-20 13:38:28 +0000217 void Set(const ast::Expression* c) { initializer = c; }
Ben Clayton58794ae2022-08-19 17:28:53 +0000218 void Set(utils::VectorRef<const ast::Attribute*> l) { attributes = std::move(l); }
219 void Set(const ast::Attribute* a) { attributes.Push(a); }
220 };
221
222 /// ConstOptions is a helper for accepting an arbitrary number of order independent options for
223 /// constructing an ast::Const.
224 struct ConstOptions {
225 template <typename... ARGS>
226 explicit ConstOptions(ARGS&&... args) {
227 static constexpr bool has_init =
228 (traits::IsTypeOrDerived<std::remove_pointer_t<std::remove_reference_t<ARGS>>,
229 ast::Expression> ||
230 ...);
231 static_assert(has_init, "Const() must be constructed with an initializer expression");
232 (Set(std::forward<ARGS>(args)), ...);
233 }
234 ~ConstOptions();
235
236 const ast::Type* type = nullptr;
dan sinclair6e77b472022-10-20 13:38:28 +0000237 const ast::Expression* initializer = nullptr;
Ben Clayton58794ae2022-08-19 17:28:53 +0000238 utils::Vector<const ast::Attribute*, 4> attributes;
239
240 private:
241 void Set(const ast::Type* t) { type = t; }
dan sinclair6e77b472022-10-20 13:38:28 +0000242 void Set(const ast::Expression* c) { initializer = c; }
Ben Clayton58794ae2022-08-19 17:28:53 +0000243 void Set(utils::VectorRef<const ast::Attribute*> l) { attributes = std::move(l); }
244 void Set(const ast::Attribute* a) { attributes.Push(a); }
245 };
246
247 /// OverrideOptions is a helper for accepting an arbitrary number of order independent options
248 /// for constructing an ast::Override.
249 struct OverrideOptions {
250 template <typename... ARGS>
251 explicit OverrideOptions(ARGS&&... args) {
252 (Set(std::forward<ARGS>(args)), ...);
253 }
254 ~OverrideOptions();
255
256 const ast::Type* type = nullptr;
dan sinclair6e77b472022-10-20 13:38:28 +0000257 const ast::Expression* initializer = nullptr;
Ben Clayton58794ae2022-08-19 17:28:53 +0000258 utils::Vector<const ast::Attribute*, 4> attributes;
259
260 private:
261 void Set(const ast::Type* t) { type = t; }
dan sinclair6e77b472022-10-20 13:38:28 +0000262 void Set(const ast::Expression* c) { initializer = c; }
Ben Clayton58794ae2022-08-19 17:28:53 +0000263 void Set(utils::VectorRef<const ast::Attribute*> l) { attributes = std::move(l); }
264 void Set(const ast::Attribute* a) { attributes.Push(a); }
dan sinclair41e4d9a2022-05-01 14:40:55 +0000265 };
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000266
dan sinclair41e4d9a2022-05-01 14:40:55 +0000267 public:
268 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
269 using ASTNodeAllocator = utils::BlockAllocator<ast::Node>;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000270
dan sinclair41e4d9a2022-05-01 14:40:55 +0000271 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
272 using SemNodeAllocator = utils::BlockAllocator<sem::Node>;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000273
dan sinclairb53b8cf2022-12-15 16:25:31 +0000274 /// ConstantAllocator is an alias to BlockAllocator<constant::Value>
275 using ConstantAllocator = utils::BlockAllocator<constant::Value>;
Ben Claytonaa037ac2022-06-29 19:07:30 +0000276
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000277 /// Constructor
dan sinclair41e4d9a2022-05-01 14:40:55 +0000278 ProgramBuilder();
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000279
dan sinclair41e4d9a2022-05-01 14:40:55 +0000280 /// Move constructor
281 /// @param rhs the builder to move
282 ProgramBuilder(ProgramBuilder&& rhs);
283
284 /// Destructor
285 virtual ~ProgramBuilder();
286
287 /// Move assignment operator
288 /// @param rhs the builder to move
289 /// @return this builder
290 ProgramBuilder& operator=(ProgramBuilder&& rhs);
291
292 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
293 /// making a deep clone of the Program contents.
294 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
295 /// existing immutable program.
296 /// As the returned ProgramBuilder wraps `program`, `program` must not be
297 /// destructed or assigned while using the returned ProgramBuilder.
298 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
299 /// function. See crbug.com/tint/460.
300 /// @param program the immutable Program to wrap
301 /// @return the ProgramBuilder that wraps `program`
302 static ProgramBuilder Wrap(const Program* program);
303
304 /// @returns the unique identifier for this program
305 ProgramID ID() const { return id_; }
306
307 /// @returns a reference to the program's types
dan sinclair837b8042022-12-09 05:00:07 +0000308 type::Manager& Types() {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000309 AssertNotMoved();
310 return types_;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000311 }
312
dan sinclair41e4d9a2022-05-01 14:40:55 +0000313 /// @returns a reference to the program's types
dan sinclair837b8042022-12-09 05:00:07 +0000314 const type::Manager& Types() const {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000315 AssertNotMoved();
316 return types_;
317 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000318
dan sinclair41e4d9a2022-05-01 14:40:55 +0000319 /// @returns a reference to the program's AST nodes storage
320 ASTNodeAllocator& ASTNodes() {
321 AssertNotMoved();
322 return ast_nodes_;
323 }
324
325 /// @returns a reference to the program's AST nodes storage
326 const ASTNodeAllocator& ASTNodes() const {
327 AssertNotMoved();
328 return ast_nodes_;
329 }
330
331 /// @returns a reference to the program's semantic nodes storage
332 SemNodeAllocator& SemNodes() {
333 AssertNotMoved();
334 return sem_nodes_;
335 }
336
337 /// @returns a reference to the program's semantic nodes storage
338 const SemNodeAllocator& SemNodes() const {
339 AssertNotMoved();
340 return sem_nodes_;
341 }
342
Ben Claytonaa037ac2022-06-29 19:07:30 +0000343 /// @returns a reference to the program's semantic constant storage
344 ConstantAllocator& ConstantNodes() {
345 AssertNotMoved();
346 return constant_nodes_;
347 }
348
dan sinclair41e4d9a2022-05-01 14:40:55 +0000349 /// @returns a reference to the program's AST root Module
350 ast::Module& AST() {
351 AssertNotMoved();
352 return *ast_;
353 }
354
355 /// @returns a reference to the program's AST root Module
356 const ast::Module& AST() const {
357 AssertNotMoved();
358 return *ast_;
359 }
360
361 /// @returns a reference to the program's semantic info
362 sem::Info& Sem() {
363 AssertNotMoved();
364 return sem_;
365 }
366
367 /// @returns a reference to the program's semantic info
368 const sem::Info& Sem() const {
369 AssertNotMoved();
370 return sem_;
371 }
372
373 /// @returns a reference to the program's SymbolTable
374 SymbolTable& Symbols() {
375 AssertNotMoved();
376 return symbols_;
377 }
378
379 /// @returns a reference to the program's SymbolTable
380 const SymbolTable& Symbols() const {
381 AssertNotMoved();
382 return symbols_;
383 }
384
385 /// @returns a reference to the program's diagnostics
386 diag::List& Diagnostics() {
387 AssertNotMoved();
388 return diagnostics_;
389 }
390
391 /// @returns a reference to the program's diagnostics
392 const diag::List& Diagnostics() const {
393 AssertNotMoved();
394 return diagnostics_;
395 }
396
397 /// Controls whether the Resolver will be run on the program when it is built.
398 /// @param enable the new flag value (defaults to true)
399 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
400
401 /// @return true if the Resolver will be run on the program when it is
402 /// built.
403 bool ResolveOnBuild() const { return resolve_on_build_; }
404
405 /// @returns true if the program has no error diagnostics and is not missing
406 /// information
407 bool IsValid() const;
408
Ben Clayton4a92a3c2022-07-18 20:50:02 +0000409 /// @returns the last allocated (numerically highest) AST node identifier.
410 ast::NodeID LastAllocatedNodeID() const { return last_ast_node_id_; }
411
412 /// @returns the next sequentially unique node identifier.
413 ast::NodeID AllocateNodeID() {
414 auto out = ast::NodeID{last_ast_node_id_.value + 1};
415 last_ast_node_id_ = out;
416 return out;
417 }
418
dan sinclair41e4d9a2022-05-01 14:40:55 +0000419 /// Creates a new ast::Node owned by the ProgramBuilder. When the
420 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000421 /// @param source the Source of the node
dan sinclair41e4d9a2022-05-01 14:40:55 +0000422 /// @param args the arguments to pass to the type constructor
423 /// @returns the node pointer
424 template <typename T, typename... ARGS>
425 traits::EnableIfIsType<T, ast::Node>* create(const Source& source, ARGS&&... args) {
426 AssertNotMoved();
Ben Clayton4a92a3c2022-07-18 20:50:02 +0000427 return ast_nodes_.Create<T>(id_, AllocateNodeID(), source, std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000428 }
429
dan sinclair41e4d9a2022-05-01 14:40:55 +0000430 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
431 /// Source as set by the last call to SetSource() as the only argument to the
432 /// constructor.
433 /// When the ProgramBuilder is destructed, the ast::Node will also be
434 /// destructed.
435 /// @returns the node pointer
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000436 template <typename T>
dan sinclair41e4d9a2022-05-01 14:40:55 +0000437 traits::EnableIfIsType<T, ast::Node>* create() {
438 AssertNotMoved();
Ben Clayton4a92a3c2022-07-18 20:50:02 +0000439 return ast_nodes_.Create<T>(id_, AllocateNodeID(), source_);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000440 }
441
dan sinclair41e4d9a2022-05-01 14:40:55 +0000442 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
443 /// Source as set by the last call to SetSource() as the first argument to the
444 /// constructor.
445 /// When the ProgramBuilder is destructed, the ast::Node will also be
446 /// destructed.
447 /// @param arg0 the first arguments to pass to the type constructor
448 /// @param args the remaining arguments to pass to the type constructor
449 /// @returns the node pointer
450 template <typename T, typename ARG0, typename... ARGS>
451 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
452 traits::IsTypeOrDerived<T, ast::Node> &&
453 !traits::IsTypeOrDerived<ARG0, Source>,
454 T>*
455 create(ARG0&& arg0, ARGS&&... args) {
456 AssertNotMoved();
Ben Clayton4a92a3c2022-07-18 20:50:02 +0000457 return ast_nodes_.Create<T>(id_, AllocateNodeID(), source_, std::forward<ARG0>(arg0),
dan sinclair41e4d9a2022-05-01 14:40:55 +0000458 std::forward<ARGS>(args)...);
459 }
460
461 /// Creates a new sem::Node owned by the ProgramBuilder.
Ben Claytonaa037ac2022-06-29 19:07:30 +0000462 /// When the ProgramBuilder is destructed, the sem::Node will also be destructed.
463 /// @param args the arguments to pass to the constructor
dan sinclair41e4d9a2022-05-01 14:40:55 +0000464 /// @returns the node pointer
465 template <typename T, typename... ARGS>
466 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node> &&
dan sinclair5f764d82022-12-08 00:32:27 +0000467 !traits::IsTypeOrDerived<T, type::Node>,
dan sinclair41e4d9a2022-05-01 14:40:55 +0000468 T>*
469 create(ARGS&&... args) {
470 AssertNotMoved();
471 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
472 }
473
dan sinclairb53b8cf2022-12-15 16:25:31 +0000474 /// Creates a new constant::Value owned by the ProgramBuilder.
Ben Claytonaa037ac2022-06-29 19:07:30 +0000475 /// When the ProgramBuilder is destructed, the sem::Node will also be destructed.
476 /// @param args the arguments to pass to the constructor
477 /// @returns the node pointer
478 template <typename T, typename... ARGS>
dan sinclair02b466f2022-12-15 21:56:32 +0000479 traits::EnableIf<traits::IsTypeOrDerived<T, constant::Value> &&
480 !traits::IsTypeOrDerived<T, constant::Composite> &&
481 !traits::IsTypeOrDerived<T, constant::Splat>,
482 T>*
483 create(ARGS&&... args) {
Ben Claytonaa037ac2022-06-29 19:07:30 +0000484 AssertNotMoved();
485 return constant_nodes_.Create<T>(std::forward<ARGS>(args)...);
486 }
487
dan sinclair02b466f2022-12-15 21:56:32 +0000488 /// Constructs a constant of a vector, matrix or array type.
489 ///
490 /// Examines the element values and will return either a constant::Composite or a
491 /// constant::Splat, depending on the element types and values.
492 ///
493 /// @param type the composite type
494 /// @param elements the composite elements
495 /// @returns the node pointer
496 template <typename T>
497 traits::EnableIf<traits::IsTypeOrDerived<T, constant::Composite> ||
498 traits::IsTypeOrDerived<T, constant::Splat>,
499 const constant::Value>*
500 create(const type::Type* type, utils::VectorRef<const constant::Value*> elements) {
501 AssertNotMoved();
502 if (elements.IsEmpty()) {
503 return nullptr;
504 }
505
506 bool any_zero = false;
507 bool all_zero = true;
508 bool all_equal = true;
509 auto* first = elements.Front();
510 for (auto* el : elements) {
511 if (!el) {
512 return nullptr;
513 }
514 if (!any_zero && el->AnyZero()) {
515 any_zero = true;
516 }
517 if (all_zero && !el->AllZero()) {
518 all_zero = false;
519 }
520 if (all_equal && el != first) {
521 if (!el->Equal(first)) {
522 all_equal = false;
523 }
524 }
525 }
526 if (all_equal) {
527 return create<constant::Splat>(type, elements[0], elements.Length());
528 }
529
530 return constant_nodes_.Create<constant::Composite>(type, std::move(elements), all_zero,
531 any_zero);
532 }
533
534 /// Constructs a splat constant.
535 /// @param type the splat type
536 /// @param element the splat element
537 /// @param n the number of elements
538 /// @returns the node pointer
539 template <typename T>
540 traits::EnableIf<traits::IsTypeOrDerived<T, constant::Splat>, const constant::Splat>*
541 create(const type::Type* type, const constant::Value* element, size_t n) {
542 AssertNotMoved();
543 return constant_nodes_.Create<constant::Splat>(type, element, n);
544 }
545
Ben Clayton7c3e9a62022-12-16 15:31:19 +0000546 /// Creates a new type::Node owned by the ProgramBuilder.
547 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the returned node will also
548 /// be destructed. If T derives from type::UniqueNode, then the calling create() for the same
549 /// `T` and arguments will return the same pointer.
550 /// @param args the arguments to pass to the constructor
551 /// @returns the new, or existing node
dan sinclair41e4d9a2022-05-01 14:40:55 +0000552 template <typename T, typename... ARGS>
Ben Clayton7c3e9a62022-12-16 15:31:19 +0000553 traits::EnableIfIsType<T, type::Node>* create(ARGS&&... args) {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000554 AssertNotMoved();
555 return types_.Get<T>(std::forward<ARGS>(args)...);
556 }
dan sinclair5f764d82022-12-08 00:32:27 +0000557
dan sinclair41e4d9a2022-05-01 14:40:55 +0000558 /// Marks this builder as moved, preventing any further use of the builder.
559 void MarkAsMoved();
560
561 //////////////////////////////////////////////////////////////////////////////
562 // TypesBuilder
563 //////////////////////////////////////////////////////////////////////////////
564
565 /// TypesBuilder holds basic `tint` types and methods for constructing
566 /// complex types.
567 class TypesBuilder {
568 public:
569 /// Constructor
570 /// @param builder the program builder
571 explicit TypesBuilder(ProgramBuilder* builder);
572
573 /// @return the tint AST type for the C type `T`.
574 template <typename T>
575 const ast::Type* Of() const {
576 return CToAST<T>::get(this);
577 }
578
579 /// @returns a boolean type
580 const ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
581
582 /// @param source the Source of the node
583 /// @returns a boolean type
584 const ast::Bool* bool_(const Source& source) const {
585 return builder->create<ast::Bool>(source);
586 }
587
Zhaoming Jiang62bfd312022-05-13 12:01:11 +0000588 /// @returns a f16 type
589 const ast::F16* f16() const { return builder->create<ast::F16>(); }
590
591 /// @param source the Source of the node
592 /// @returns a f16 type
593 const ast::F16* f16(const Source& source) const {
594 return builder->create<ast::F16>(source);
595 }
596
dan sinclair41e4d9a2022-05-01 14:40:55 +0000597 /// @returns a f32 type
598 const ast::F32* f32() const { return builder->create<ast::F32>(); }
599
600 /// @param source the Source of the node
601 /// @returns a f32 type
602 const ast::F32* f32(const Source& source) const {
603 return builder->create<ast::F32>(source);
604 }
605
606 /// @returns a i32 type
607 const ast::I32* i32() const { return builder->create<ast::I32>(); }
608
609 /// @param source the Source of the node
610 /// @returns a i32 type
611 const ast::I32* i32(const Source& source) const {
612 return builder->create<ast::I32>(source);
613 }
614
615 /// @returns a u32 type
616 const ast::U32* u32() const { return builder->create<ast::U32>(); }
617
618 /// @param source the Source of the node
619 /// @returns a u32 type
620 const ast::U32* u32(const Source& source) const {
621 return builder->create<ast::U32>(source);
622 }
623
624 /// @returns a void type
625 const ast::Void* void_() const { return builder->create<ast::Void>(); }
626
627 /// @param source the Source of the node
628 /// @returns a void type
629 const ast::Void* void_(const Source& source) const {
630 return builder->create<ast::Void>(source);
631 }
632
633 /// @param type vector subtype
634 /// @param n vector width in elements
635 /// @return the tint AST type for a `n`-element vector of `type`.
636 const ast::Vector* vec(const ast::Type* type, uint32_t n) const {
637 return builder->create<ast::Vector>(type, n);
638 }
639
640 /// @param source the Source of the node
641 /// @param type vector subtype
642 /// @param n vector width in elements
643 /// @return the tint AST type for a `n`-element vector of `type`.
644 const ast::Vector* vec(const Source& source, const ast::Type* type, uint32_t n) const {
645 return builder->create<ast::Vector>(source, type, n);
646 }
647
648 /// @param type vector subtype
649 /// @return the tint AST type for a 2-element vector of `type`.
650 const ast::Vector* vec2(const ast::Type* type) const { return vec(type, 2u); }
651
Ben Clayton6315a272022-08-01 17:18:04 +0000652 /// @param source the vector source
653 /// @param type vector subtype
654 /// @return the tint AST type for a 2-element vector of `type`.
655 const ast::Vector* vec2(const Source& source, const ast::Type* type) const {
656 return vec(source, type, 2u);
657 }
658
dan sinclair41e4d9a2022-05-01 14:40:55 +0000659 /// @param type vector subtype
660 /// @return the tint AST type for a 3-element vector of `type`.
661 const ast::Vector* vec3(const ast::Type* type) const { return vec(type, 3u); }
662
Ben Clayton6315a272022-08-01 17:18:04 +0000663 /// @param source the vector source
664 /// @param type vector subtype
665 /// @return the tint AST type for a 3-element vector of `type`.
666 const ast::Vector* vec3(const Source& source, const ast::Type* type) const {
667 return vec(source, type, 3u);
668 }
669
dan sinclair41e4d9a2022-05-01 14:40:55 +0000670 /// @param type vector subtype
671 /// @return the tint AST type for a 4-element vector of `type`.
672 const ast::Vector* vec4(const ast::Type* type) const { return vec(type, 4u); }
673
Ben Clayton6315a272022-08-01 17:18:04 +0000674 /// @param source the vector source
675 /// @param type vector subtype
676 /// @return the tint AST type for a 4-element vector of `type`.
677 const ast::Vector* vec4(const Source& source, const ast::Type* type) const {
678 return vec(source, type, 4u);
679 }
680
dan sinclair41e4d9a2022-05-01 14:40:55 +0000681 /// @param n vector width in elements
682 /// @return the tint AST type for a `n`-element vector of `type`.
683 template <typename T>
684 const ast::Vector* vec(uint32_t n) const {
685 return vec(Of<T>(), n);
686 }
687
688 /// @return the tint AST type for a 2-element vector of the C type `T`.
689 template <typename T>
690 const ast::Vector* vec2() const {
691 return vec2(Of<T>());
692 }
693
Ben Clayton6315a272022-08-01 17:18:04 +0000694 /// @param source the Source of the node
695 /// @return the tint AST type for a 2-element vector of the C type `T`.
696 template <typename T>
697 const ast::Vector* vec2(const Source& source) const {
698 return vec2(source, Of<T>());
699 }
700
dan sinclair41e4d9a2022-05-01 14:40:55 +0000701 /// @return the tint AST type for a 3-element vector of the C type `T`.
702 template <typename T>
703 const ast::Vector* vec3() const {
704 return vec3(Of<T>());
705 }
706
Ben Clayton6315a272022-08-01 17:18:04 +0000707 /// @param source the Source of the node
708 /// @return the tint AST type for a 3-element vector of the C type `T`.
709 template <typename T>
710 const ast::Vector* vec3(const Source& source) const {
711 return vec3(source, Of<T>());
712 }
713
dan sinclair41e4d9a2022-05-01 14:40:55 +0000714 /// @return the tint AST type for a 4-element vector of the C type `T`.
715 template <typename T>
716 const ast::Vector* vec4() const {
717 return vec4(Of<T>());
718 }
719
Ben Clayton6315a272022-08-01 17:18:04 +0000720 /// @param source the Source of the node
721 /// @return the tint AST type for a 4-element vector of the C type `T`.
722 template <typename T>
723 const ast::Vector* vec4(const Source& source) const {
724 return vec4(source, Of<T>());
725 }
726
dan sinclair41e4d9a2022-05-01 14:40:55 +0000727 /// @param type matrix subtype
728 /// @param columns number of columns for the matrix
729 /// @param rows number of rows for the matrix
730 /// @return the tint AST type for a matrix of `type`
731 const ast::Matrix* mat(const ast::Type* type, uint32_t columns, uint32_t rows) const {
732 return builder->create<ast::Matrix>(type, rows, columns);
733 }
734
735 /// @param source the Source of the node
736 /// @param type matrix subtype
737 /// @param columns number of columns for the matrix
738 /// @param rows number of rows for the matrix
739 /// @return the tint AST type for a matrix of `type`
740 const ast::Matrix* mat(const Source& source,
741 const ast::Type* type,
742 uint32_t columns,
743 uint32_t rows) const {
744 return builder->create<ast::Matrix>(source, type, rows, columns);
745 }
746
747 /// @param type matrix subtype
748 /// @return the tint AST type for a 2x3 matrix of `type`.
749 const ast::Matrix* mat2x2(const ast::Type* type) const { return mat(type, 2u, 2u); }
750
751 /// @param type matrix subtype
752 /// @return the tint AST type for a 2x3 matrix of `type`.
753 const ast::Matrix* mat2x3(const ast::Type* type) const { return mat(type, 2u, 3u); }
754
755 /// @param type matrix subtype
756 /// @return the tint AST type for a 2x4 matrix of `type`.
757 const ast::Matrix* mat2x4(const ast::Type* type) const { return mat(type, 2u, 4u); }
758
759 /// @param type matrix subtype
760 /// @return the tint AST type for a 3x2 matrix of `type`.
761 const ast::Matrix* mat3x2(const ast::Type* type) const { return mat(type, 3u, 2u); }
762
763 /// @param type matrix subtype
764 /// @return the tint AST type for a 3x3 matrix of `type`.
765 const ast::Matrix* mat3x3(const ast::Type* type) const { return mat(type, 3u, 3u); }
766
767 /// @param type matrix subtype
768 /// @return the tint AST type for a 3x4 matrix of `type`.
769 const ast::Matrix* mat3x4(const ast::Type* type) const { return mat(type, 3u, 4u); }
770
771 /// @param type matrix subtype
772 /// @return the tint AST type for a 4x2 matrix of `type`.
773 const ast::Matrix* mat4x2(const ast::Type* type) const { return mat(type, 4u, 2u); }
774
775 /// @param type matrix subtype
776 /// @return the tint AST type for a 4x3 matrix of `type`.
777 const ast::Matrix* mat4x3(const ast::Type* type) const { return mat(type, 4u, 3u); }
778
779 /// @param type matrix subtype
780 /// @return the tint AST type for a 4x4 matrix of `type`.
781 const ast::Matrix* mat4x4(const ast::Type* type) const { return mat(type, 4u, 4u); }
782
783 /// @param columns number of columns for the matrix
784 /// @param rows number of rows for the matrix
785 /// @return the tint AST type for a matrix of `type`
786 template <typename T>
787 const ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
788 return mat(Of<T>(), columns, rows);
789 }
790
791 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
792 template <typename T>
793 const ast::Matrix* mat2x2() const {
794 return mat2x2(Of<T>());
795 }
796
797 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
798 template <typename T>
799 const ast::Matrix* mat2x3() const {
800 return mat2x3(Of<T>());
801 }
802
803 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
804 template <typename T>
805 const ast::Matrix* mat2x4() const {
806 return mat2x4(Of<T>());
807 }
808
809 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
810 template <typename T>
811 const ast::Matrix* mat3x2() const {
812 return mat3x2(Of<T>());
813 }
814
815 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
816 template <typename T>
817 const ast::Matrix* mat3x3() const {
818 return mat3x3(Of<T>());
819 }
820
821 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
822 template <typename T>
823 const ast::Matrix* mat3x4() const {
824 return mat3x4(Of<T>());
825 }
826
827 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
828 template <typename T>
829 const ast::Matrix* mat4x2() const {
830 return mat4x2(Of<T>());
831 }
832
833 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
834 template <typename T>
835 const ast::Matrix* mat4x3() const {
836 return mat4x3(Of<T>());
837 }
838
839 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
840 template <typename T>
841 const ast::Matrix* mat4x4() const {
842 return mat4x4(Of<T>());
843 }
844
845 /// @param subtype the array element type
846 /// @param n the array size. nullptr represents a runtime-array
847 /// @param attrs the optional attributes for the array
848 /// @return the tint AST type for a array of size `n` of type `T`
849 template <typename EXPR = ast::Expression*>
Ben Clayton783b1692022-08-02 17:03:35 +0000850 const ast::Array* array(
851 const ast::Type* subtype,
852 EXPR&& n = nullptr,
853 utils::VectorRef<const ast::Attribute*> attrs = utils::Empty) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000854 return builder->create<ast::Array>(subtype, builder->Expr(std::forward<EXPR>(n)),
Ben Clayton783b1692022-08-02 17:03:35 +0000855 std::move(attrs));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000856 }
857
858 /// @param source the Source of the node
859 /// @param subtype the array element type
860 /// @param n the array size. nullptr represents a runtime-array
861 /// @param attrs the optional attributes for the array
862 /// @return the tint AST type for a array of size `n` of type `T`
863 template <typename EXPR = ast::Expression*>
Ben Clayton783b1692022-08-02 17:03:35 +0000864 const ast::Array* array(
865 const Source& source,
866 const ast::Type* subtype,
867 EXPR&& n = nullptr,
868 utils::VectorRef<const ast::Attribute*> attrs = utils::Empty) const {
869 return builder->create<ast::Array>(
870 source, subtype, builder->Expr(std::forward<EXPR>(n)), std::move(attrs));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000871 }
872
873 /// @param subtype the array element type
874 /// @param n the array size. nullptr represents a runtime-array
875 /// @param stride the array stride. 0 represents implicit stride
876 /// @return the tint AST type for a array of size `n` of type `T`
877 template <typename EXPR>
878 const ast::Array* array(const ast::Type* subtype, EXPR&& n, uint32_t stride) const {
Ben Clayton783b1692022-08-02 17:03:35 +0000879 utils::Vector<const ast::Attribute*, 2> attrs;
dan sinclair41e4d9a2022-05-01 14:40:55 +0000880 if (stride) {
Ben Clayton783b1692022-08-02 17:03:35 +0000881 attrs.Push(builder->create<ast::StrideAttribute>(stride));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000882 }
883 return array(subtype, std::forward<EXPR>(n), std::move(attrs));
884 }
885
886 /// @param source the Source of the node
887 /// @param subtype the array element type
888 /// @param n the array size. nullptr represents a runtime-array
889 /// @param stride the array stride. 0 represents implicit stride
890 /// @return the tint AST type for a array of size `n` of type `T`
891 template <typename EXPR>
892 const ast::Array* array(const Source& source,
893 const ast::Type* subtype,
894 EXPR&& n,
895 uint32_t stride) const {
Ben Clayton783b1692022-08-02 17:03:35 +0000896 utils::Vector<const ast::Attribute*, 2> attrs;
dan sinclair41e4d9a2022-05-01 14:40:55 +0000897 if (stride) {
Ben Clayton783b1692022-08-02 17:03:35 +0000898 attrs.Push(builder->create<ast::StrideAttribute>(stride));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000899 }
900 return array(source, subtype, std::forward<EXPR>(n), std::move(attrs));
901 }
902
903 /// @return the tint AST type for a runtime-sized array of type `T`
904 template <typename T>
905 const ast::Array* array() const {
906 return array(Of<T>(), nullptr);
907 }
908
909 /// @return the tint AST type for an array of size `N` of type `T`
910 template <typename T, int N>
911 const ast::Array* array() const {
Ben Clayton0ce9ab02022-05-05 20:23:40 +0000912 return array(Of<T>(), builder->Expr(tint::u32(N)));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000913 }
914
915 /// @param stride the array stride
916 /// @return the tint AST type for a runtime-sized array of type `T`
917 template <typename T>
918 const ast::Array* array(uint32_t stride) const {
919 return array(Of<T>(), nullptr, stride);
920 }
921
922 /// @param stride the array stride
923 /// @return the tint AST type for an array of size `N` of type `T`
924 template <typename T, int N>
925 const ast::Array* array(uint32_t stride) const {
Ben Clayton0ce9ab02022-05-05 20:23:40 +0000926 return array(Of<T>(), builder->Expr(tint::u32(N)), stride);
dan sinclair41e4d9a2022-05-01 14:40:55 +0000927 }
928
929 /// Creates a type name
930 /// @param name the name
931 /// @returns the type name
932 template <typename NAME>
933 const ast::TypeName* type_name(NAME&& name) const {
934 return builder->create<ast::TypeName>(builder->Sym(std::forward<NAME>(name)));
935 }
936
937 /// Creates a type name
938 /// @param source the Source of the node
939 /// @param name the name
940 /// @returns the type name
941 template <typename NAME>
942 const ast::TypeName* type_name(const Source& source, NAME&& name) const {
943 return builder->create<ast::TypeName>(source, builder->Sym(std::forward<NAME>(name)));
944 }
945
946 /// Creates an alias type
947 /// @param name the alias name
948 /// @param type the alias type
949 /// @returns the alias pointer
950 template <typename NAME>
951 const ast::Alias* alias(NAME&& name, const ast::Type* type) const {
952 auto sym = builder->Sym(std::forward<NAME>(name));
953 return builder->create<ast::Alias>(sym, type);
954 }
955
956 /// Creates an alias type
957 /// @param source the Source of the node
958 /// @param name the alias name
959 /// @param type the alias type
960 /// @returns the alias pointer
961 template <typename NAME>
962 const ast::Alias* alias(const Source& source, NAME&& name, const ast::Type* type) const {
963 auto sym = builder->Sym(std::forward<NAME>(name));
964 return builder->create<ast::Alias>(source, sym, type);
965 }
966
967 /// @param type the type of the pointer
dan sinclairff7cf212022-10-03 14:05:23 +0000968 /// @param address_space the address space of the pointer
dan sinclair41e4d9a2022-05-01 14:40:55 +0000969 /// @param access the optional access control of the pointer
dan sinclairff7cf212022-10-03 14:05:23 +0000970 /// @return the pointer to `type` with the given ast::AddressSpace
dan sinclair41e4d9a2022-05-01 14:40:55 +0000971 const ast::Pointer* pointer(const ast::Type* type,
dan sinclairff7cf212022-10-03 14:05:23 +0000972 ast::AddressSpace address_space,
Ben Claytond2e0db32022-10-12 18:49:15 +0000973 ast::Access access = ast::Access::kUndefined) const {
dan sinclairff7cf212022-10-03 14:05:23 +0000974 return builder->create<ast::Pointer>(type, address_space, access);
dan sinclair41e4d9a2022-05-01 14:40:55 +0000975 }
976
977 /// @param source the Source of the node
978 /// @param type the type of the pointer
dan sinclairff7cf212022-10-03 14:05:23 +0000979 /// @param address_space the address space of the pointer
dan sinclair41e4d9a2022-05-01 14:40:55 +0000980 /// @param access the optional access control of the pointer
dan sinclairff7cf212022-10-03 14:05:23 +0000981 /// @return the pointer to `type` with the given ast::AddressSpace
dan sinclair41e4d9a2022-05-01 14:40:55 +0000982 const ast::Pointer* pointer(const Source& source,
983 const ast::Type* type,
dan sinclairff7cf212022-10-03 14:05:23 +0000984 ast::AddressSpace address_space,
Ben Claytond2e0db32022-10-12 18:49:15 +0000985 ast::Access access = ast::Access::kUndefined) const {
dan sinclairff7cf212022-10-03 14:05:23 +0000986 return builder->create<ast::Pointer>(source, type, address_space, access);
dan sinclair41e4d9a2022-05-01 14:40:55 +0000987 }
988
dan sinclairff7cf212022-10-03 14:05:23 +0000989 /// @param address_space the address space of the pointer
dan sinclair41e4d9a2022-05-01 14:40:55 +0000990 /// @param access the optional access control of the pointer
dan sinclairff7cf212022-10-03 14:05:23 +0000991 /// @return the pointer to type `T` with the given ast::AddressSpace.
dan sinclair41e4d9a2022-05-01 14:40:55 +0000992 template <typename T>
dan sinclairff7cf212022-10-03 14:05:23 +0000993 const ast::Pointer* pointer(ast::AddressSpace address_space,
Ben Claytond2e0db32022-10-12 18:49:15 +0000994 ast::Access access = ast::Access::kUndefined) const {
dan sinclairff7cf212022-10-03 14:05:23 +0000995 return pointer(Of<T>(), address_space, access);
dan sinclair41e4d9a2022-05-01 14:40:55 +0000996 }
997
998 /// @param source the Source of the node
dan sinclairff7cf212022-10-03 14:05:23 +0000999 /// @param address_space the address space of the pointer
Ben Claytoncfe07a12022-07-15 13:01:49 +00001000 /// @param access the optional access control of the pointer
dan sinclairff7cf212022-10-03 14:05:23 +00001001 /// @return the pointer to type `T` with the given ast::AddressSpace.
Ben Claytoncfe07a12022-07-15 13:01:49 +00001002 template <typename T>
1003 const ast::Pointer* pointer(const Source& source,
dan sinclairff7cf212022-10-03 14:05:23 +00001004 ast::AddressSpace address_space,
Ben Claytond2e0db32022-10-12 18:49:15 +00001005 ast::Access access = ast::Access::kUndefined) const {
dan sinclairff7cf212022-10-03 14:05:23 +00001006 return pointer(source, Of<T>(), address_space, access);
Ben Claytoncfe07a12022-07-15 13:01:49 +00001007 }
1008
1009 /// @param source the Source of the node
dan sinclair41e4d9a2022-05-01 14:40:55 +00001010 /// @param type the type of the atomic
1011 /// @return the atomic to `type`
1012 const ast::Atomic* atomic(const Source& source, const ast::Type* type) const {
1013 return builder->create<ast::Atomic>(source, type);
1014 }
1015
1016 /// @param type the type of the atomic
1017 /// @return the atomic to `type`
1018 const ast::Atomic* atomic(const ast::Type* type) const {
1019 return builder->create<ast::Atomic>(type);
1020 }
1021
1022 /// @return the atomic to type `T`
1023 template <typename T>
1024 const ast::Atomic* atomic() const {
1025 return atomic(Of<T>());
1026 }
1027
1028 /// @param kind the kind of sampler
1029 /// @returns the sampler
1030 const ast::Sampler* sampler(ast::SamplerKind kind) const {
1031 return builder->create<ast::Sampler>(kind);
1032 }
1033
1034 /// @param source the Source of the node
1035 /// @param kind the kind of sampler
1036 /// @returns the sampler
1037 const ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
1038 return builder->create<ast::Sampler>(source, kind);
1039 }
1040
1041 /// @param dims the dimensionality of the texture
1042 /// @returns the depth texture
1043 const ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
1044 return builder->create<ast::DepthTexture>(dims);
1045 }
1046
1047 /// @param source the Source of the node
1048 /// @param dims the dimensionality of the texture
1049 /// @returns the depth texture
1050 const ast::DepthTexture* depth_texture(const Source& source,
1051 ast::TextureDimension dims) const {
1052 return builder->create<ast::DepthTexture>(source, dims);
1053 }
1054
1055 /// @param dims the dimensionality of the texture
1056 /// @returns the multisampled depth texture
1057 const ast::DepthMultisampledTexture* depth_multisampled_texture(
1058 ast::TextureDimension dims) const {
1059 return builder->create<ast::DepthMultisampledTexture>(dims);
1060 }
1061
1062 /// @param source the Source of the node
1063 /// @param dims the dimensionality of the texture
1064 /// @returns the multisampled depth texture
1065 const ast::DepthMultisampledTexture* depth_multisampled_texture(
1066 const Source& source,
1067 ast::TextureDimension dims) const {
1068 return builder->create<ast::DepthMultisampledTexture>(source, dims);
1069 }
1070
1071 /// @param dims the dimensionality of the texture
1072 /// @param subtype the texture subtype.
1073 /// @returns the sampled texture
1074 const ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
1075 const ast::Type* subtype) const {
1076 return builder->create<ast::SampledTexture>(dims, subtype);
1077 }
1078
1079 /// @param source the Source of the node
1080 /// @param dims the dimensionality of the texture
1081 /// @param subtype the texture subtype.
1082 /// @returns the sampled texture
1083 const ast::SampledTexture* sampled_texture(const Source& source,
1084 ast::TextureDimension dims,
1085 const ast::Type* subtype) const {
1086 return builder->create<ast::SampledTexture>(source, dims, subtype);
1087 }
1088
1089 /// @param dims the dimensionality of the texture
1090 /// @param subtype the texture subtype.
1091 /// @returns the multisampled texture
1092 const ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
1093 const ast::Type* subtype) const {
1094 return builder->create<ast::MultisampledTexture>(dims, subtype);
1095 }
1096
1097 /// @param source the Source of the node
1098 /// @param dims the dimensionality of the texture
1099 /// @param subtype the texture subtype.
1100 /// @returns the multisampled texture
1101 const ast::MultisampledTexture* multisampled_texture(const Source& source,
1102 ast::TextureDimension dims,
1103 const ast::Type* subtype) const {
1104 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
1105 }
1106
1107 /// @param dims the dimensionality of the texture
1108 /// @param format the texel format of the texture
1109 /// @param access the access control of the texture
1110 /// @returns the storage texture
1111 const ast::StorageTexture* storage_texture(ast::TextureDimension dims,
1112 ast::TexelFormat format,
1113 ast::Access access) const {
1114 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
1115 return builder->create<ast::StorageTexture>(dims, format, subtype, access);
1116 }
1117
1118 /// @param source the Source of the node
1119 /// @param dims the dimensionality of the texture
1120 /// @param format the texel format of the texture
1121 /// @param access the access control of the texture
1122 /// @returns the storage texture
1123 const ast::StorageTexture* storage_texture(const Source& source,
1124 ast::TextureDimension dims,
1125 ast::TexelFormat format,
1126 ast::Access access) const {
1127 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
1128 return builder->create<ast::StorageTexture>(source, dims, format, subtype, access);
1129 }
1130
1131 /// @returns the external texture
1132 const ast::ExternalTexture* external_texture() const {
1133 return builder->create<ast::ExternalTexture>();
1134 }
1135
1136 /// @param source the Source of the node
1137 /// @returns the external texture
1138 const ast::ExternalTexture* external_texture(const Source& source) const {
1139 return builder->create<ast::ExternalTexture>(source);
1140 }
1141
1142 /// Constructs a TypeName for the type declaration.
1143 /// @param type the type
1144 /// @return either type or a pointer to a new ast::TypeName
1145 const ast::TypeName* Of(const ast::TypeDecl* type) const;
1146
1147 /// The ProgramBuilder
1148 ProgramBuilder* const builder;
1149
1150 private:
1151 /// CToAST<T> is specialized for various `T` types and each specialization
1152 /// contains a single static `get()` method for obtaining the corresponding
1153 /// AST type for the C type `T`.
1154 /// `get()` has the signature:
1155 /// `static const ast::Type* get(Types* t)`
1156 template <typename T>
1157 struct CToAST {};
1158 };
1159
1160 //////////////////////////////////////////////////////////////////////////////
1161 // AST helper methods
1162 //////////////////////////////////////////////////////////////////////////////
1163
1164 /// @return a new unnamed symbol
1165 Symbol Sym() { return Symbols().New(); }
1166
1167 /// @param name the symbol string
1168 /// @return a Symbol with the given name
1169 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
1170
1171 /// @param sym the symbol
1172 /// @return `sym`
1173 Symbol Sym(Symbol sym) { return sym; }
1174
1175 /// @param expr the expression
1176 /// @return expr
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001177 template <typename T>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001178 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
1179 return expr;
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001180 }
1181
dan sinclair41e4d9a2022-05-01 14:40:55 +00001182 /// Passthrough for nullptr
1183 /// @return nullptr
1184 const ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
1185
1186 /// @param source the source information
1187 /// @param symbol the identifier symbol
1188 /// @return an ast::IdentifierExpression with the given symbol
1189 const ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
1190 return create<ast::IdentifierExpression>(source, symbol);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001191 }
1192
dan sinclair41e4d9a2022-05-01 14:40:55 +00001193 /// @param symbol the identifier symbol
1194 /// @return an ast::IdentifierExpression with the given symbol
1195 const ast::IdentifierExpression* Expr(Symbol symbol) {
1196 return create<ast::IdentifierExpression>(symbol);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001197 }
1198
dan sinclair41e4d9a2022-05-01 14:40:55 +00001199 /// @param source the source information
1200 /// @param variable the AST variable
1201 /// @return an ast::IdentifierExpression with the variable's symbol
1202 const ast::IdentifierExpression* Expr(const Source& source, const ast::Variable* variable) {
1203 return create<ast::IdentifierExpression>(source, variable->symbol);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001204 }
1205
dan sinclair41e4d9a2022-05-01 14:40:55 +00001206 /// @param variable the AST variable
1207 /// @return an ast::IdentifierExpression with the variable's symbol
1208 const ast::IdentifierExpression* Expr(const ast::Variable* variable) {
1209 return create<ast::IdentifierExpression>(variable->symbol);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001210 }
1211
dan sinclair41e4d9a2022-05-01 14:40:55 +00001212 /// @param source the source information
1213 /// @param name the identifier name
1214 /// @return an ast::IdentifierExpression with the given name
1215 const ast::IdentifierExpression* Expr(const Source& source, const char* name) {
1216 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001217 }
1218
dan sinclair41e4d9a2022-05-01 14:40:55 +00001219 /// @param name the identifier name
1220 /// @return an ast::IdentifierExpression with the given name
1221 const ast::IdentifierExpression* Expr(const char* name) {
1222 return create<ast::IdentifierExpression>(Symbols().Register(name));
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001223 }
1224
dan sinclair41e4d9a2022-05-01 14:40:55 +00001225 /// @param source the source information
1226 /// @param name the identifier name
1227 /// @return an ast::IdentifierExpression with the given name
1228 const ast::IdentifierExpression* Expr(const Source& source, const std::string& name) {
1229 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001230 }
1231
dan sinclair41e4d9a2022-05-01 14:40:55 +00001232 /// @param name the identifier name
1233 /// @return an ast::IdentifierExpression with the given name
1234 const ast::IdentifierExpression* Expr(const std::string& name) {
1235 return create<ast::IdentifierExpression>(Symbols().Register(name));
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001236 }
1237
dan sinclair41e4d9a2022-05-01 14:40:55 +00001238 /// @param source the source information
1239 /// @param value the boolean value
1240 /// @return a Scalar constructor for the given value
Ben Clayton17fa42d2022-05-10 15:29:14 +00001241 template <typename BOOL>
1242 std::enable_if_t<std::is_same_v<BOOL, bool>, const ast::BoolLiteralExpression*> Expr(
1243 const Source& source,
1244 BOOL value) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00001245 return create<ast::BoolLiteralExpression>(source, value);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001246 }
1247
dan sinclair41e4d9a2022-05-01 14:40:55 +00001248 /// @param value the boolean value
1249 /// @return a Scalar constructor for the given value
Ben Clayton17fa42d2022-05-10 15:29:14 +00001250 template <typename BOOL>
1251 std::enable_if_t<std::is_same_v<BOOL, bool>, const ast::BoolLiteralExpression*> Expr(
1252 BOOL value) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00001253 return create<ast::BoolLiteralExpression>(value);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001254 }
1255
dan sinclair41e4d9a2022-05-01 14:40:55 +00001256 /// @param source the source information
1257 /// @param value the float value
Ben Clayton41285aa2022-05-10 14:55:34 +00001258 /// @return a 'f'-suffixed FloatLiteralExpression for the f32 value
1259 const ast::FloatLiteralExpression* Expr(const Source& source, f32 value) {
1260 return create<ast::FloatLiteralExpression>(source, static_cast<double>(value.value),
1261 ast::FloatLiteralExpression::Suffix::kF);
1262 }
1263
1264 /// @param value the float value
1265 /// @return a 'f'-suffixed FloatLiteralExpression for the f32 value
dan sinclair41e4d9a2022-05-01 14:40:55 +00001266 const ast::FloatLiteralExpression* Expr(f32 value) {
Ben Clayton41285aa2022-05-10 14:55:34 +00001267 return create<ast::FloatLiteralExpression>(static_cast<double>(value.value),
1268 ast::FloatLiteralExpression::Suffix::kF);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001269 }
1270
dan sinclair41e4d9a2022-05-01 14:40:55 +00001271 /// @param source the source information
Zhaoming Jiang62bfd312022-05-13 12:01:11 +00001272 /// @param value the float value
1273 /// @return a 'h'-suffixed FloatLiteralExpression for the f16 value
1274 const ast::FloatLiteralExpression* Expr(const Source& source, f16 value) {
1275 return create<ast::FloatLiteralExpression>(source, static_cast<double>(value.value),
1276 ast::FloatLiteralExpression::Suffix::kH);
1277 }
1278
1279 /// @param value the float value
1280 /// @return a 'h'-suffixed FloatLiteralExpression for the f16 value
1281 const ast::FloatLiteralExpression* Expr(f16 value) {
1282 return create<ast::FloatLiteralExpression>(static_cast<double>(value.value),
1283 ast::FloatLiteralExpression::Suffix::kH);
1284 }
1285
1286 /// @param source the source information
dan sinclair41e4d9a2022-05-01 14:40:55 +00001287 /// @param value the integer value
Ben Clayton508e4a52022-05-10 22:08:04 +00001288 /// @return an unsuffixed IntLiteralExpression for the AInt value
1289 const ast::IntLiteralExpression* Expr(const Source& source, AInt value) {
1290 return create<ast::IntLiteralExpression>(source, value,
1291 ast::IntLiteralExpression::Suffix::kNone);
1292 }
1293
1294 /// @param value the integer value
1295 /// @return an unsuffixed IntLiteralExpression for the AInt value
1296 const ast::IntLiteralExpression* Expr(AInt value) {
1297 return create<ast::IntLiteralExpression>(value, ast::IntLiteralExpression::Suffix::kNone);
1298 }
1299
1300 /// @param source the source information
1301 /// @param value the integer value
1302 /// @return an unsuffixed FloatLiteralExpression for the AFloat value
1303 const ast::FloatLiteralExpression* Expr(const Source& source, AFloat value) {
1304 return create<ast::FloatLiteralExpression>(source, value.value,
1305 ast::FloatLiteralExpression::Suffix::kNone);
1306 }
1307
1308 /// @param value the integer value
1309 /// @return an unsuffixed FloatLiteralExpression for the AFloat value
1310 const ast::FloatLiteralExpression* Expr(AFloat value) {
1311 return create<ast::FloatLiteralExpression>(value.value,
1312 ast::FloatLiteralExpression::Suffix::kNone);
1313 }
1314
1315 /// @param source the source information
1316 /// @param value the integer value
Ben Clayton41285aa2022-05-10 14:55:34 +00001317 /// @return a signed 'i'-suffixed IntLiteralExpression for the i32 value
Ben Clayton8822e292022-05-04 22:18:49 +00001318 const ast::IntLiteralExpression* Expr(const Source& source, i32 value) {
1319 return create<ast::IntLiteralExpression>(source, value,
Ben Clayton0ce9ab02022-05-05 20:23:40 +00001320 ast::IntLiteralExpression::Suffix::kI);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001321 }
1322
dan sinclair41e4d9a2022-05-01 14:40:55 +00001323 /// @param value the integer value
Ben Clayton41285aa2022-05-10 14:55:34 +00001324 /// @return a signed 'i'-suffixed IntLiteralExpression for the i32 value
Ben Clayton8822e292022-05-04 22:18:49 +00001325 const ast::IntLiteralExpression* Expr(i32 value) {
Ben Clayton0ce9ab02022-05-05 20:23:40 +00001326 return create<ast::IntLiteralExpression>(value, ast::IntLiteralExpression::Suffix::kI);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001327 }
1328
dan sinclair41e4d9a2022-05-01 14:40:55 +00001329 /// @param source the source information
1330 /// @param value the unsigned int value
Ben Clayton41285aa2022-05-10 14:55:34 +00001331 /// @return an unsigned 'u'-suffixed IntLiteralExpression for the u32 value
Ben Clayton8822e292022-05-04 22:18:49 +00001332 const ast::IntLiteralExpression* Expr(const Source& source, u32 value) {
1333 return create<ast::IntLiteralExpression>(source, value,
1334 ast::IntLiteralExpression::Suffix::kU);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001335 }
1336
dan sinclair41e4d9a2022-05-01 14:40:55 +00001337 /// @param value the unsigned int value
Ben Clayton41285aa2022-05-10 14:55:34 +00001338 /// @return an unsigned 'u'-suffixed IntLiteralExpression for the u32 value
Ben Clayton8822e292022-05-04 22:18:49 +00001339 const ast::IntLiteralExpression* Expr(u32 value) {
1340 return create<ast::IntLiteralExpression>(value, ast::IntLiteralExpression::Suffix::kU);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001341 }
1342
dan sinclair41e4d9a2022-05-01 14:40:55 +00001343 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1344 /// `list`.
1345 /// @param list the list to append too
1346 /// @param arg the arg to create
Ben Clayton783b1692022-08-02 17:03:35 +00001347 template <size_t N, typename ARG>
1348 void Append(utils::Vector<const ast::Expression*, N>& list, ARG&& arg) {
1349 list.Push(Expr(std::forward<ARG>(arg)));
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001350 }
1351
dan sinclair41e4d9a2022-05-01 14:40:55 +00001352 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1353 /// then appends them to `list`.
1354 /// @param list the list to append too
1355 /// @param arg0 the first argument
1356 /// @param args the rest of the arguments
Ben Clayton783b1692022-08-02 17:03:35 +00001357 template <size_t N, typename ARG0, typename... ARGS>
1358 void Append(utils::Vector<const ast::Expression*, N>& list, ARG0&& arg0, ARGS&&... args) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00001359 Append(list, std::forward<ARG0>(arg0));
1360 Append(list, std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001361 }
1362
Ben Clayton783b1692022-08-02 17:03:35 +00001363 /// @return utils::EmptyType
1364 utils::EmptyType ExprList() { return utils::Empty; }
dan sinclair41e4d9a2022-05-01 14:40:55 +00001365
1366 /// @param args the list of expressions
1367 /// @return the list of expressions converted to `ast::Expression`s using
1368 /// `Expr()`,
Ben Clayton783b1692022-08-02 17:03:35 +00001369 template <typename... ARGS, typename = DisableIfVectorLike<ARGS...>>
1370 auto ExprList(ARGS&&... args) {
1371 utils::Vector<const ast::Expression*, sizeof...(ARGS)> list;
dan sinclair41e4d9a2022-05-01 14:40:55 +00001372 Append(list, std::forward<ARGS>(args)...);
1373 return list;
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001374 }
1375
dan sinclair41e4d9a2022-05-01 14:40:55 +00001376 /// @param list the list of expressions
1377 /// @return `list`
Ben Clayton783b1692022-08-02 17:03:35 +00001378 template <typename T, size_t N>
1379 utils::Vector<T, N> ExprList(utils::Vector<T, N>&& list) {
1380 return std::move(list);
1381 }
1382
1383 /// @param list the list of expressions
1384 /// @return `list`
1385 utils::VectorRef<const ast::Expression*> ExprList(
1386 utils::VectorRef<const ast::Expression*> list) {
1387 return list;
1388 }
dan sinclair41e4d9a2022-05-01 14:40:55 +00001389
1390 /// @param args the arguments for the type constructor
1391 /// @return an `ast::CallExpression` of type `ty`, with the values
1392 /// of `args` converted to `ast::Expression`s using `Expr()`
1393 template <typename T, typename... ARGS>
1394 const ast::CallExpression* Construct(ARGS&&... args) {
1395 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001396 }
1397
dan sinclair41e4d9a2022-05-01 14:40:55 +00001398 /// @param type the type to construct
1399 /// @param args the arguments for the constructor
1400 /// @return an `ast::CallExpression` of `type` constructed with the
1401 /// values `args`.
1402 template <typename... ARGS>
1403 const ast::CallExpression* Construct(const ast::Type* type, ARGS&&... args) {
1404 return Construct(source_, type, std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001405 }
1406
dan sinclair41e4d9a2022-05-01 14:40:55 +00001407 /// @param source the source information
1408 /// @param type the type to construct
1409 /// @param args the arguments for the constructor
1410 /// @return an `ast::CallExpression` of `type` constructed with the
1411 /// values `args`.
1412 template <typename... ARGS>
1413 const ast::CallExpression* Construct(const Source& source,
1414 const ast::Type* type,
1415 ARGS&&... args) {
1416 return create<ast::CallExpression>(source, type, ExprList(std::forward<ARGS>(args)...));
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001417 }
1418
dan sinclair41e4d9a2022-05-01 14:40:55 +00001419 /// @param expr the expression for the bitcast
1420 /// @return an `ast::BitcastExpression` of type `ty`, with the values of
1421 /// `expr` converted to `ast::Expression`s using `Expr()`
1422 template <typename T, typename EXPR>
1423 const ast::BitcastExpression* Bitcast(EXPR&& expr) {
1424 return Bitcast(ty.Of<T>(), std::forward<EXPR>(expr));
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001425 }
1426
dan sinclair41e4d9a2022-05-01 14:40:55 +00001427 /// @param type the type to cast to
1428 /// @param expr the expression for the bitcast
1429 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1430 /// `expr`.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001431 template <typename EXPR>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001432 const ast::BitcastExpression* Bitcast(const ast::Type* type, EXPR&& expr) {
1433 return create<ast::BitcastExpression>(type, Expr(std::forward<EXPR>(expr)));
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001434 }
1435
dan sinclair41e4d9a2022-05-01 14:40:55 +00001436 /// @param source the source information
1437 /// @param type the type to cast to
1438 /// @param expr the expression for the bitcast
1439 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1440 /// `expr`.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001441 template <typename EXPR>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001442 const ast::BitcastExpression* Bitcast(const Source& source,
1443 const ast::Type* type,
1444 EXPR&& expr) {
1445 return create<ast::BitcastExpression>(source, type, Expr(std::forward<EXPR>(expr)));
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001446 }
1447
dan sinclair6e77b472022-10-20 13:38:28 +00001448 /// @param args the arguments for the vector initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001449 /// @param type the vector type
1450 /// @param size the vector size
1451 /// @return an `ast::CallExpression` of a `size`-element vector of
1452 /// type `type`, constructed with the values `args`.
1453 template <typename... ARGS>
1454 const ast::CallExpression* vec(const ast::Type* type, uint32_t size, ARGS&&... args) {
1455 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001456 }
1457
dan sinclair6e77b472022-10-20 13:38:28 +00001458 /// @param args the arguments for the vector initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001459 /// @return an `ast::CallExpression` of a 2-element vector of type
1460 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001461 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001462 const ast::CallExpression* vec2(ARGS&&... args) {
1463 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001464 }
1465
Ben Clayton6315a272022-08-01 17:18:04 +00001466 /// @param source the vector source
dan sinclair6e77b472022-10-20 13:38:28 +00001467 /// @param args the arguments for the vector initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001468 /// @return an `ast::CallExpression` of a 2-element vector of type
1469 /// `T`, constructed with the values `args`.
1470 template <typename T, typename... ARGS>
1471 const ast::CallExpression* vec2(const Source& source, ARGS&&... args) {
1472 return Construct(source, ty.vec2<T>(), std::forward<ARGS>(args)...);
1473 }
1474
dan sinclair6e77b472022-10-20 13:38:28 +00001475 /// @param args the arguments for the vector initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001476 /// @return an `ast::CallExpression` of a 3-element vector of type
1477 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001478 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001479 const ast::CallExpression* vec3(ARGS&&... args) {
1480 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001481 }
1482
Ben Clayton6315a272022-08-01 17:18:04 +00001483 /// @param source the vector source
dan sinclair6e77b472022-10-20 13:38:28 +00001484 /// @param args the arguments for the vector initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001485 /// @return an `ast::CallExpression` of a 3-element vector of type
1486 /// `T`, constructed with the values `args`.
1487 template <typename T, typename... ARGS>
1488 const ast::CallExpression* vec3(const Source& source, ARGS&&... args) {
1489 return Construct(source, ty.vec3<T>(), std::forward<ARGS>(args)...);
1490 }
1491
dan sinclair6e77b472022-10-20 13:38:28 +00001492 /// @param args the arguments for the vector initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001493 /// @return an `ast::CallExpression` of a 4-element vector of type
1494 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001495 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001496 const ast::CallExpression* vec4(ARGS&&... args) {
1497 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001498 }
1499
Ben Clayton6315a272022-08-01 17:18:04 +00001500 /// @param source the vector source
dan sinclair6e77b472022-10-20 13:38:28 +00001501 /// @param args the arguments for the vector initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001502 /// @return an `ast::CallExpression` of a 4-element vector of type
1503 /// `T`, constructed with the values `args`.
1504 template <typename T, typename... ARGS>
1505 const ast::CallExpression* vec4(const Source& source, ARGS&&... args) {
1506 return Construct(source, ty.vec4<T>(), std::forward<ARGS>(args)...);
1507 }
1508
dan sinclair6e77b472022-10-20 13:38:28 +00001509 /// @param args the arguments for the matrix initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001510 /// @return an `ast::CallExpression` of a 2x2 matrix of type
1511 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001512 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001513 const ast::CallExpression* mat2x2(ARGS&&... args) {
1514 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001515 }
1516
Ben Clayton6315a272022-08-01 17:18:04 +00001517 /// @param source the matrix source
dan sinclair6e77b472022-10-20 13:38:28 +00001518 /// @param args the arguments for the matrix initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001519 /// @return an `ast::CallExpression` of a 2x2 matrix of type
1520 /// `T`, constructed with the values `args`.
1521 template <typename T, typename... ARGS>
1522 const ast::CallExpression* mat2x2(const Source& source, ARGS&&... args) {
1523 return Construct(source, ty.mat2x2<T>(), std::forward<ARGS>(args)...);
1524 }
1525
dan sinclair6e77b472022-10-20 13:38:28 +00001526 /// @param args the arguments for the matrix initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001527 /// @return an `ast::CallExpression` of a 2x3 matrix of type
1528 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001529 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001530 const ast::CallExpression* mat2x3(ARGS&&... args) {
1531 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001532 }
1533
Ben Clayton6315a272022-08-01 17:18:04 +00001534 /// @param source the matrix source
dan sinclair6e77b472022-10-20 13:38:28 +00001535 /// @param args the arguments for the matrix initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001536 /// @return an `ast::CallExpression` of a 2x3 matrix of type
1537 /// `T`, constructed with the values `args`.
1538 template <typename T, typename... ARGS>
1539 const ast::CallExpression* mat2x3(const Source& source, ARGS&&... args) {
1540 return Construct(source, ty.mat2x3<T>(), std::forward<ARGS>(args)...);
1541 }
1542
dan sinclair6e77b472022-10-20 13:38:28 +00001543 /// @param args the arguments for the matrix initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001544 /// @return an `ast::CallExpression` of a 2x4 matrix of type
1545 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001546 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001547 const ast::CallExpression* mat2x4(ARGS&&... args) {
1548 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001549 }
1550
Ben Clayton6315a272022-08-01 17:18:04 +00001551 /// @param source the matrix source
dan sinclair6e77b472022-10-20 13:38:28 +00001552 /// @param args the arguments for the matrix initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001553 /// @return an `ast::CallExpression` of a 2x4 matrix of type
1554 /// `T`, constructed with the values `args`.
1555 template <typename T, typename... ARGS>
1556 const ast::CallExpression* mat2x4(const Source& source, ARGS&&... args) {
1557 return Construct(source, ty.mat2x4<T>(), std::forward<ARGS>(args)...);
1558 }
1559
dan sinclair6e77b472022-10-20 13:38:28 +00001560 /// @param args the arguments for the matrix initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001561 /// @return an `ast::CallExpression` of a 3x2 matrix of type
1562 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001563 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001564 const ast::CallExpression* mat3x2(ARGS&&... args) {
1565 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001566 }
1567
Ben Clayton6315a272022-08-01 17:18:04 +00001568 /// @param source the matrix source
dan sinclair6e77b472022-10-20 13:38:28 +00001569 /// @param args the arguments for the matrix initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001570 /// @return an `ast::CallExpression` of a 3x2 matrix of type
1571 /// `T`, constructed with the values `args`.
1572 template <typename T, typename... ARGS>
1573 const ast::CallExpression* mat3x2(const Source& source, ARGS&&... args) {
1574 return Construct(source, ty.mat3x2<T>(), std::forward<ARGS>(args)...);
1575 }
1576
dan sinclair6e77b472022-10-20 13:38:28 +00001577 /// @param args the arguments for the matrix initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001578 /// @return an `ast::CallExpression` of a 3x3 matrix of type
1579 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001580 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001581 const ast::CallExpression* mat3x3(ARGS&&... args) {
1582 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001583 }
1584
Ben Clayton6315a272022-08-01 17:18:04 +00001585 /// @param source the matrix source
dan sinclair6e77b472022-10-20 13:38:28 +00001586 /// @param args the arguments for the matrix initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001587 /// @return an `ast::CallExpression` of a 3x3 matrix of type
1588 /// `T`, constructed with the values `args`.
1589 template <typename T, typename... ARGS>
1590 const ast::CallExpression* mat3x3(const Source& source, ARGS&&... args) {
1591 return Construct(source, ty.mat3x3<T>(), std::forward<ARGS>(args)...);
1592 }
1593
dan sinclair6e77b472022-10-20 13:38:28 +00001594 /// @param args the arguments for the matrix initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001595 /// @return an `ast::CallExpression` of a 3x4 matrix of type
1596 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001597 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001598 const ast::CallExpression* mat3x4(ARGS&&... args) {
1599 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001600 }
1601
Ben Clayton6315a272022-08-01 17:18:04 +00001602 /// @param source the matrix source
dan sinclair6e77b472022-10-20 13:38:28 +00001603 /// @param args the arguments for the matrix initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001604 /// @return an `ast::CallExpression` of a 3x4 matrix of type
1605 /// `T`, constructed with the values `args`.
1606 template <typename T, typename... ARGS>
1607 const ast::CallExpression* mat3x4(const Source& source, ARGS&&... args) {
1608 return Construct(source, ty.mat3x4<T>(), std::forward<ARGS>(args)...);
1609 }
1610
dan sinclair6e77b472022-10-20 13:38:28 +00001611 /// @param args the arguments for the matrix initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001612 /// @return an `ast::CallExpression` of a 4x2 matrix of type
1613 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001614 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001615 const ast::CallExpression* mat4x2(ARGS&&... args) {
1616 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001617 }
1618
Ben Clayton6315a272022-08-01 17:18:04 +00001619 /// @param source the matrix source
dan sinclair6e77b472022-10-20 13:38:28 +00001620 /// @param args the arguments for the matrix initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001621 /// @return an `ast::CallExpression` of a 4x2 matrix of type
1622 /// `T`, constructed with the values `args`.
1623 template <typename T, typename... ARGS>
1624 const ast::CallExpression* mat4x2(const Source& source, ARGS&&... args) {
1625 return Construct(source, ty.mat4x2<T>(), std::forward<ARGS>(args)...);
1626 }
1627
dan sinclair6e77b472022-10-20 13:38:28 +00001628 /// @param args the arguments for the matrix initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001629 /// @return an `ast::CallExpression` of a 4x3 matrix of type
1630 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001631 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001632 const ast::CallExpression* mat4x3(ARGS&&... args) {
1633 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001634 }
1635
Ben Clayton6315a272022-08-01 17:18:04 +00001636 /// @param source the matrix source
dan sinclair6e77b472022-10-20 13:38:28 +00001637 /// @param args the arguments for the matrix initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001638 /// @return an `ast::CallExpression` of a 4x3 matrix of type
1639 /// `T`, constructed with the values `args`.
1640 template <typename T, typename... ARGS>
1641 const ast::CallExpression* mat4x3(const Source& source, ARGS&&... args) {
1642 return Construct(source, ty.mat4x3<T>(), std::forward<ARGS>(args)...);
1643 }
1644
dan sinclair6e77b472022-10-20 13:38:28 +00001645 /// @param args the arguments for the matrix initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001646 /// @return an `ast::CallExpression` of a 4x4 matrix of type
1647 /// `T`, constructed with the values `args`.
Ben Clayton6315a272022-08-01 17:18:04 +00001648 template <typename T, typename... ARGS, typename _ = DisableIfSource<ARGS...>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00001649 const ast::CallExpression* mat4x4(ARGS&&... args) {
1650 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001651 }
1652
Ben Clayton6315a272022-08-01 17:18:04 +00001653 /// @param source the matrix source
dan sinclair6e77b472022-10-20 13:38:28 +00001654 /// @param args the arguments for the matrix initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001655 /// @return an `ast::CallExpression` of a 4x4 matrix of type
1656 /// `T`, constructed with the values `args`.
1657 template <typename T, typename... ARGS>
1658 const ast::CallExpression* mat4x4(const Source& source, ARGS&&... args) {
1659 return Construct(source, ty.mat4x4<T>(), std::forward<ARGS>(args)...);
1660 }
1661
dan sinclair6e77b472022-10-20 13:38:28 +00001662 /// @param args the arguments for the array initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001663 /// @return an `ast::CallExpression` of an array with element type
1664 /// `T` and size `N`, constructed with the values `args`.
1665 template <typename T, int N, typename... ARGS>
1666 const ast::CallExpression* array(ARGS&&... args) {
1667 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001668 }
1669
Ben Clayton6315a272022-08-01 17:18:04 +00001670 /// @param source the array source
dan sinclair6e77b472022-10-20 13:38:28 +00001671 /// @param args the arguments for the array initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001672 /// @return an `ast::CallExpression` of an array with element type
1673 /// `T` and size `N`, constructed with the values `args`.
1674 template <typename T, int N, typename... ARGS>
1675 const ast::CallExpression* array(const Source& source, ARGS&&... args) {
1676 return Construct(source, ty.array<T, N>(), std::forward<ARGS>(args)...);
1677 }
1678
dan sinclair41e4d9a2022-05-01 14:40:55 +00001679 /// @param subtype the array element type
1680 /// @param n the array size. nullptr represents a runtime-array.
dan sinclair6e77b472022-10-20 13:38:28 +00001681 /// @param args the arguments for the array initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001682 /// @return an `ast::CallExpression` of an array with element type
1683 /// `subtype`, constructed with the values `args`.
1684 template <typename EXPR, typename... ARGS>
1685 const ast::CallExpression* array(const ast::Type* subtype, EXPR&& n, ARGS&&... args) {
1686 return Construct(ty.array(subtype, std::forward<EXPR>(n)), std::forward<ARGS>(args)...);
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001687 }
1688
Ben Clayton6315a272022-08-01 17:18:04 +00001689 /// @param source the array source
1690 /// @param subtype the array element type
1691 /// @param n the array size. nullptr represents a runtime-array.
dan sinclair6e77b472022-10-20 13:38:28 +00001692 /// @param args the arguments for the array initializer
Ben Clayton6315a272022-08-01 17:18:04 +00001693 /// @return an `ast::CallExpression` of an array with element type
1694 /// `subtype`, constructed with the values `args`.
1695 template <typename EXPR, typename... ARGS>
1696 const ast::CallExpression* array(const Source& source,
1697 const ast::Type* subtype,
1698 EXPR&& n,
1699 ARGS&&... args) {
1700 return Construct(source, ty.array(subtype, std::forward<EXPR>(n)),
1701 std::forward<ARGS>(args)...);
1702 }
1703
Ben Clayton7f2b8cd2022-05-18 22:41:48 +00001704 /// Adds the extension to the list of enable directives at the top of the module.
1705 /// @param ext the extension to enable
1706 /// @return an `ast::Enable` enabling the given extension.
1707 const ast::Enable* Enable(ast::Extension ext) {
1708 auto* enable = create<ast::Enable>(ext);
1709 AST().AddEnable(enable);
1710 return enable;
1711 }
1712
Ben Clayton1a567782022-10-14 13:38:27 +00001713 /// Adds the extension to the list of enable directives at the top of the module.
1714 /// @param source the enable source
1715 /// @param ext the extension to enable
1716 /// @return an `ast::Enable` enabling the given extension.
1717 const ast::Enable* Enable(const Source& source, ast::Extension ext) {
1718 auto* enable = create<ast::Enable>(source, ext);
1719 AST().AddEnable(enable);
1720 return enable;
1721 }
1722
dan sinclair41e4d9a2022-05-01 14:40:55 +00001723 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001724 /// @param options the extra options passed to the ast::Var initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001725 /// Can be any of the following, in any order:
Ben Clayton58794ae2022-08-19 17:28:53 +00001726 /// * ast::Type* - specifies the variable type
dan sinclairff7cf212022-10-03 14:05:23 +00001727 /// * ast::AddressSpace - specifies the variable address space
dan sinclair41e4d9a2022-05-01 14:40:55 +00001728 /// * ast::Access - specifies the variable's access control
1729 /// * ast::Expression* - specifies the variable's initializer expression
Ben Clayton58794ae2022-08-19 17:28:53 +00001730 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1731 /// Note that non-repeatable arguments of the same type will use the last argument's value.
Ben Claytondcdf66e2022-06-17 12:48:51 +00001732 /// @returns a `ast::Var` with the given name, type and additional
dan sinclair41e4d9a2022-05-01 14:40:55 +00001733 /// options
Ben Clayton58794ae2022-08-19 17:28:53 +00001734 template <typename NAME, typename... OPTIONS, typename = DisableIfSource<NAME>>
1735 const ast::Var* Var(NAME&& name, OPTIONS&&... options) {
1736 VarOptions opts(std::forward<OPTIONS>(options)...);
dan sinclairff7cf212022-10-03 14:05:23 +00001737 return create<ast::Var>(Sym(std::forward<NAME>(name)), opts.type, opts.address_space,
dan sinclair6e77b472022-10-20 13:38:28 +00001738 opts.access, opts.initializer, std::move(opts.attributes));
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001739 }
1740
dan sinclair41e4d9a2022-05-01 14:40:55 +00001741 /// @param source the variable source
1742 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001743 /// @param options the extra options passed to the ast::Var initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001744 /// Can be any of the following, in any order:
Ben Clayton58794ae2022-08-19 17:28:53 +00001745 /// * ast::Type* - specifies the variable type
dan sinclairff7cf212022-10-03 14:05:23 +00001746 /// * ast::AddressSpace - specifies the variable address space
dan sinclair41e4d9a2022-05-01 14:40:55 +00001747 /// * ast::Access - specifies the variable's access control
1748 /// * ast::Expression* - specifies the variable's initializer expression
Ben Clayton58794ae2022-08-19 17:28:53 +00001749 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1750 /// Note that non-repeatable arguments of the same type will use the last argument's value.
dan sinclairff7cf212022-10-03 14:05:23 +00001751 /// @returns a `ast::Var` with the given name, address_space and type
Ben Clayton58794ae2022-08-19 17:28:53 +00001752 template <typename NAME, typename... OPTIONS>
1753 const ast::Var* Var(const Source& source, NAME&& name, OPTIONS&&... options) {
1754 VarOptions opts(std::forward<OPTIONS>(options)...);
dan sinclairff7cf212022-10-03 14:05:23 +00001755 return create<ast::Var>(source, Sym(std::forward<NAME>(name)), opts.type,
dan sinclair6e77b472022-10-20 13:38:28 +00001756 opts.address_space, opts.access, opts.initializer,
dan sinclairff7cf212022-10-03 14:05:23 +00001757 std::move(opts.attributes));
dan sinclair41e4d9a2022-05-01 14:40:55 +00001758 }
1759
1760 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001761 /// @param options the extra options passed to the ast::Var initializer
Ben Clayton58794ae2022-08-19 17:28:53 +00001762 /// Can be any of the following, in any order:
1763 /// * ast::Expression* - specifies the variable's initializer expression (required)
1764 /// * ast::Type* - specifies the variable type
1765 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1766 /// Note that non-repeatable arguments of the same type will use the last argument's value.
1767 /// @returns an `ast::Const` with the given name, type and additional options
1768 template <typename NAME, typename... OPTIONS, typename = DisableIfSource<NAME>>
1769 const ast::Const* Const(NAME&& name, OPTIONS&&... options) {
1770 ConstOptions opts(std::forward<OPTIONS>(options)...);
dan sinclair6e77b472022-10-20 13:38:28 +00001771 return create<ast::Const>(Sym(std::forward<NAME>(name)), opts.type, opts.initializer,
Ben Clayton58794ae2022-08-19 17:28:53 +00001772 std::move(opts.attributes));
Ben Claytone3834c42022-06-25 23:21:39 +00001773 }
1774
1775 /// @param source the variable source
1776 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001777 /// @param options the extra options passed to the ast::Var initializer
Ben Clayton58794ae2022-08-19 17:28:53 +00001778 /// Can be any of the following, in any order:
1779 /// * ast::Expression* - specifies the variable's initializer expression (required)
1780 /// * ast::Type* - specifies the variable type
1781 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1782 /// Note that non-repeatable arguments of the same type will use the last argument's value.
1783 /// @returns an `ast::Const` with the given name, type and additional options
1784 template <typename NAME, typename... OPTIONS>
1785 const ast::Const* Const(const Source& source, NAME&& name, OPTIONS&&... options) {
1786 ConstOptions opts(std::forward<OPTIONS>(options)...);
1787 return create<ast::Const>(source, Sym(std::forward<NAME>(name)), opts.type,
dan sinclair6e77b472022-10-20 13:38:28 +00001788 opts.initializer, std::move(opts.attributes));
Ben Claytone3834c42022-06-25 23:21:39 +00001789 }
1790
1791 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001792 /// @param options the extra options passed to the ast::Var initializer
Ben Clayton58794ae2022-08-19 17:28:53 +00001793 /// Can be any of the following, in any order:
1794 /// * ast::Expression* - specifies the variable's initializer expression (required)
1795 /// * ast::Type* - specifies the variable type
1796 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1797 /// Note that non-repeatable arguments of the same type will use the last argument's value.
1798 /// @returns an `ast::Let` with the given name, type and additional options
1799 template <typename NAME, typename... OPTIONS, typename = DisableIfSource<NAME>>
1800 const ast::Let* Let(NAME&& name, OPTIONS&&... options) {
1801 LetOptions opts(std::forward<OPTIONS>(options)...);
dan sinclair6e77b472022-10-20 13:38:28 +00001802 return create<ast::Let>(Sym(std::forward<NAME>(name)), opts.type, opts.initializer,
Ben Clayton58794ae2022-08-19 17:28:53 +00001803 std::move(opts.attributes));
dan sinclair41e4d9a2022-05-01 14:40:55 +00001804 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001805
dan sinclair41e4d9a2022-05-01 14:40:55 +00001806 /// @param source the variable source
1807 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001808 /// @param options the extra options passed to the ast::Var initializer
Ben Clayton58794ae2022-08-19 17:28:53 +00001809 /// Can be any of the following, in any order:
1810 /// * ast::Expression* - specifies the variable's initializer expression (required)
1811 /// * ast::Type* - specifies the variable type
1812 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1813 /// Note that non-repeatable arguments of the same type will use the last argument's value.
1814 /// @returns an `ast::Let` with the given name, type and additional options
1815 template <typename NAME, typename... OPTIONS>
1816 const ast::Let* Let(const Source& source, NAME&& name, OPTIONS&&... options) {
1817 LetOptions opts(std::forward<OPTIONS>(options)...);
dan sinclair6e77b472022-10-20 13:38:28 +00001818 return create<ast::Let>(source, Sym(std::forward<NAME>(name)), opts.type, opts.initializer,
Ben Clayton58794ae2022-08-19 17:28:53 +00001819 std::move(opts.attributes));
dan sinclair41e4d9a2022-05-01 14:40:55 +00001820 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001821
dan sinclair41e4d9a2022-05-01 14:40:55 +00001822 /// @param name the parameter name
1823 /// @param type the parameter type
1824 /// @param attributes optional parameter attributes
Ben Claytondcdf66e2022-06-17 12:48:51 +00001825 /// @returns an `ast::Parameter` with the given name and type
dan sinclair41e4d9a2022-05-01 14:40:55 +00001826 template <typename NAME>
Ben Claytondcdf66e2022-06-17 12:48:51 +00001827 const ast::Parameter* Param(NAME&& name,
1828 const ast::Type* type,
Ben Clayton783b1692022-08-02 17:03:35 +00001829 utils::VectorRef<const ast::Attribute*> attributes = utils::Empty) {
Ben Claytondcdf66e2022-06-17 12:48:51 +00001830 return create<ast::Parameter>(Sym(std::forward<NAME>(name)), type, attributes);
dan sinclair41e4d9a2022-05-01 14:40:55 +00001831 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001832
dan sinclair41e4d9a2022-05-01 14:40:55 +00001833 /// @param source the parameter source
1834 /// @param name the parameter name
1835 /// @param type the parameter type
1836 /// @param attributes optional parameter attributes
Ben Claytondcdf66e2022-06-17 12:48:51 +00001837 /// @returns an `ast::Parameter` with the given name and type
dan sinclair41e4d9a2022-05-01 14:40:55 +00001838 template <typename NAME>
Ben Claytondcdf66e2022-06-17 12:48:51 +00001839 const ast::Parameter* Param(const Source& source,
1840 NAME&& name,
1841 const ast::Type* type,
Ben Clayton783b1692022-08-02 17:03:35 +00001842 utils::VectorRef<const ast::Attribute*> attributes = utils::Empty) {
Ben Claytondcdf66e2022-06-17 12:48:51 +00001843 return create<ast::Parameter>(source, Sym(std::forward<NAME>(name)), type, attributes);
dan sinclair41e4d9a2022-05-01 14:40:55 +00001844 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001845
dan sinclair41e4d9a2022-05-01 14:40:55 +00001846 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001847 /// @param options the extra options passed to the ast::Var initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001848 /// Can be any of the following, in any order:
Ben Clayton58794ae2022-08-19 17:28:53 +00001849 /// * ast::Type* - specifies the variable type
dan sinclairff7cf212022-10-03 14:05:23 +00001850 /// * ast::AddressSpace - specifies the variable address space
dan sinclair41e4d9a2022-05-01 14:40:55 +00001851 /// * ast::Access - specifies the variable's access control
1852 /// * ast::Expression* - specifies the variable's initializer expression
Ben Clayton58794ae2022-08-19 17:28:53 +00001853 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1854 /// Note that non-repeatable arguments of the same type will use the last argument's value.
Ben Claytondcdf66e2022-06-17 12:48:51 +00001855 /// @returns a new `ast::Var`, which is automatically registered as a global variable with the
1856 /// ast::Module.
Ben Clayton58794ae2022-08-19 17:28:53 +00001857 template <typename NAME, typename... OPTIONS, typename = DisableIfSource<NAME>>
1858 const ast::Var* GlobalVar(NAME&& name, OPTIONS&&... options) {
1859 auto* variable = Var(std::forward<NAME>(name), std::forward<OPTIONS>(options)...);
1860 AST().AddGlobalVariable(variable);
1861 return variable;
dan sinclair41e4d9a2022-05-01 14:40:55 +00001862 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001863
dan sinclair41e4d9a2022-05-01 14:40:55 +00001864 /// @param source the variable source
1865 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001866 /// @param options the extra options passed to the ast::Var initializer
dan sinclair41e4d9a2022-05-01 14:40:55 +00001867 /// Can be any of the following, in any order:
Ben Clayton58794ae2022-08-19 17:28:53 +00001868 /// * ast::Type* - specifies the variable type
dan sinclairff7cf212022-10-03 14:05:23 +00001869 /// * ast::AddressSpace - specifies the variable address space
dan sinclair41e4d9a2022-05-01 14:40:55 +00001870 /// * ast::Access - specifies the variable's access control
1871 /// * ast::Expression* - specifies the variable's initializer expression
Ben Clayton58794ae2022-08-19 17:28:53 +00001872 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1873 /// Note that non-repeatable arguments of the same type will use the last argument's value.
Ben Claytondcdf66e2022-06-17 12:48:51 +00001874 /// @returns a new `ast::Var`, which is automatically registered as a global variable with the
1875 /// ast::Module.
Ben Clayton58794ae2022-08-19 17:28:53 +00001876 template <typename NAME, typename... OPTIONS>
1877 const ast::Var* GlobalVar(const Source& source, NAME&& name, OPTIONS&&... options) {
1878 auto* variable = Var(source, std::forward<NAME>(name), std::forward<OPTIONS>(options)...);
1879 AST().AddGlobalVariable(variable);
1880 return variable;
dan sinclair41e4d9a2022-05-01 14:40:55 +00001881 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001882
dan sinclair41e4d9a2022-05-01 14:40:55 +00001883 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001884 /// @param options the extra options passed to the ast::Const initializer
Ben Clayton58794ae2022-08-19 17:28:53 +00001885 /// Can be any of the following, in any order:
1886 /// * ast::Expression* - specifies the variable's initializer expression (required)
1887 /// * ast::Type* - specifies the variable type
1888 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1889 /// Note that non-repeatable arguments of the same type will use the last argument's value.
1890 /// @returns an `ast::Const` with the given name, type and additional options, which is
1891 /// automatically registered as a global variable with the ast::Module.
1892 template <typename NAME, typename... OPTIONS, typename = DisableIfSource<NAME>>
1893 const ast::Const* GlobalConst(NAME&& name, OPTIONS&&... options) {
1894 auto* variable = Const(std::forward<NAME>(name), std::forward<OPTIONS>(options)...);
1895 AST().AddGlobalVariable(variable);
1896 return variable;
Ben Claytone3834c42022-06-25 23:21:39 +00001897 }
1898
1899 /// @param source the variable source
1900 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001901 /// @param options the extra options passed to the ast::Const initializer
Ben Clayton58794ae2022-08-19 17:28:53 +00001902 /// Can be any of the following, in any order:
1903 /// * ast::Expression* - specifies the variable's initializer expression (required)
1904 /// * ast::Type* - specifies the variable type
1905 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1906 /// Note that non-repeatable arguments of the same type will use the last argument's value.
1907 /// @returns an `ast::Const` with the given name, type and additional options, which is
1908 /// automatically registered as a global variable with the ast::Module.
1909 template <typename NAME, typename... OPTIONS>
1910 const ast::Const* GlobalConst(const Source& source, NAME&& name, OPTIONS&&... options) {
1911 auto* variable = Const(source, std::forward<NAME>(name), std::forward<OPTIONS>(options)...);
1912 AST().AddGlobalVariable(variable);
1913 return variable;
Ben Claytone3834c42022-06-25 23:21:39 +00001914 }
1915
1916 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001917 /// @param options the extra options passed to the ast::Override initializer
Ben Clayton58794ae2022-08-19 17:28:53 +00001918 /// Can be any of the following, in any order:
1919 /// * ast::Expression* - specifies the variable's initializer expression (required)
1920 /// * ast::Type* - specifies the variable type
1921 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1922 /// Note that non-repeatable arguments of the same type will use the last argument's value.
1923 /// @returns an `ast::Override` with the given name, type and additional options, which is
1924 /// automatically registered as a global variable with the ast::Module.
1925 template <typename NAME, typename... OPTIONS, typename = DisableIfSource<NAME>>
1926 const ast::Override* Override(NAME&& name, OPTIONS&&... options) {
1927 OverrideOptions opts(std::forward<OPTIONS>(options)...);
1928 auto* variable = create<ast::Override>(Sym(std::forward<NAME>(name)), opts.type,
dan sinclair6e77b472022-10-20 13:38:28 +00001929 opts.initializer, std::move(opts.attributes));
Ben Clayton58794ae2022-08-19 17:28:53 +00001930 AST().AddGlobalVariable(variable);
1931 return variable;
dan sinclair41e4d9a2022-05-01 14:40:55 +00001932 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001933
dan sinclair41e4d9a2022-05-01 14:40:55 +00001934 /// @param source the variable source
1935 /// @param name the variable name
dan sinclair6e77b472022-10-20 13:38:28 +00001936 /// @param options the extra options passed to the ast::Override initializer
Ben Clayton58794ae2022-08-19 17:28:53 +00001937 /// Can be any of the following, in any order:
1938 /// * ast::Expression* - specifies the variable's initializer expression (required)
1939 /// * ast::Type* - specifies the variable type
1940 /// * ast::Attribute* - specifies the variable's attributes (repeatable, or vector)
1941 /// Note that non-repeatable arguments of the same type will use the last argument's value.
1942 /// @returns an `ast::Override` with the given name, type and additional options, which is
1943 /// automatically registered as a global variable with the ast::Module.
1944 template <typename NAME, typename... OPTIONS>
1945 const ast::Override* Override(const Source& source, NAME&& name, OPTIONS&&... options) {
1946 OverrideOptions opts(std::forward<OPTIONS>(options)...);
1947 auto* variable = create<ast::Override>(source, Sym(std::forward<NAME>(name)), opts.type,
dan sinclair6e77b472022-10-20 13:38:28 +00001948 opts.initializer, std::move(opts.attributes));
Ben Clayton58794ae2022-08-19 17:28:53 +00001949 AST().AddGlobalVariable(variable);
1950 return variable;
dan sinclair41e4d9a2022-05-01 14:40:55 +00001951 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001952
dan sinclair41e4d9a2022-05-01 14:40:55 +00001953 /// @param source the source information
Ben Claytonbfd1a812022-08-02 23:16:25 +00001954 /// @param condition the assertion condition
1955 /// @returns a new `ast::StaticAssert`, which is automatically registered as a global statement
1956 /// with the ast::Module.
1957 template <typename EXPR>
1958 const ast::StaticAssert* GlobalStaticAssert(const Source& source, EXPR&& condition) {
1959 auto* sa = StaticAssert(source, std::forward<EXPR>(condition));
1960 AST().AddStaticAssert(sa);
1961 return sa;
1962 }
1963
1964 /// @param condition the assertion condition
1965 /// @returns a new `ast::StaticAssert`, which is automatically registered as a global statement
1966 /// with the ast::Module.
1967 template <typename EXPR, typename = DisableIfSource<EXPR>>
1968 const ast::StaticAssert* GlobalStaticAssert(EXPR&& condition) {
1969 auto* sa = StaticAssert(std::forward<EXPR>(condition));
1970 AST().AddStaticAssert(sa);
1971 return sa;
1972 }
1973
1974 /// @param source the source information
1975 /// @param condition the assertion condition
1976 /// @returns a new `ast::StaticAssert` with the given assertion condition
1977 template <typename EXPR>
1978 const ast::StaticAssert* StaticAssert(const Source& source, EXPR&& condition) {
1979 return create<ast::StaticAssert>(source, Expr(std::forward<EXPR>(condition)));
1980 }
1981
1982 /// @param condition the assertion condition
1983 /// @returns a new `ast::StaticAssert` with the given assertion condition
1984 template <typename EXPR, typename = DisableIfSource<EXPR>>
1985 const ast::StaticAssert* StaticAssert(EXPR&& condition) {
1986 return create<ast::StaticAssert>(Expr(std::forward<EXPR>(condition)));
1987 }
1988
1989 /// @param source the source information
dan sinclair41e4d9a2022-05-01 14:40:55 +00001990 /// @param expr the expression to take the address of
1991 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1992 template <typename EXPR>
1993 const ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1994 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1995 Expr(std::forward<EXPR>(expr)));
1996 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001997
dan sinclair41e4d9a2022-05-01 14:40:55 +00001998 /// @param expr the expression to take the address of
1999 /// @return an ast::UnaryOpExpression that takes the address of `expr`
2000 template <typename EXPR>
2001 const ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
2002 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
2003 Expr(std::forward<EXPR>(expr)));
2004 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002005
dan sinclair41e4d9a2022-05-01 14:40:55 +00002006 /// @param source the source information
2007 /// @param expr the expression to perform an indirection on
2008 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
2009 template <typename EXPR>
2010 const ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
2011 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
2012 Expr(std::forward<EXPR>(expr)));
2013 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002014
dan sinclair41e4d9a2022-05-01 14:40:55 +00002015 /// @param expr the expression to perform an indirection on
2016 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
2017 template <typename EXPR>
2018 const ast::UnaryOpExpression* Deref(EXPR&& expr) {
2019 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
2020 Expr(std::forward<EXPR>(expr)));
2021 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002022
dan sinclair41e4d9a2022-05-01 14:40:55 +00002023 /// @param expr the expression to perform a unary not on
2024 /// @return an ast::UnaryOpExpression that is the unary not of the input
2025 /// expression
2026 template <typename EXPR>
2027 const ast::UnaryOpExpression* Not(EXPR&& expr) {
2028 return create<ast::UnaryOpExpression>(ast::UnaryOp::kNot, Expr(std::forward<EXPR>(expr)));
2029 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002030
Antonio Maiorano28779af2022-12-09 21:28:51 +00002031 /// @param source the source information
2032 /// @param expr the expression to perform a unary not on
2033 /// @return an ast::UnaryOpExpression that is the unary not of the input
2034 /// expression
2035 template <typename EXPR>
2036 const ast::UnaryOpExpression* Not(const Source& source, EXPR&& expr) {
2037 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kNot,
2038 Expr(std::forward<EXPR>(expr)));
2039 }
2040
dan sinclair41e4d9a2022-05-01 14:40:55 +00002041 /// @param expr the expression to perform a unary complement on
2042 /// @return an ast::UnaryOpExpression that is the unary complement of the
2043 /// input expression
2044 template <typename EXPR>
2045 const ast::UnaryOpExpression* Complement(EXPR&& expr) {
2046 return create<ast::UnaryOpExpression>(ast::UnaryOp::kComplement,
2047 Expr(std::forward<EXPR>(expr)));
2048 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002049
Author: Shiyu Liub3aab092022-05-04 13:07:29 +00002050 /// @param expr the expression to perform a unary negation on
2051 /// @return an ast::UnaryOpExpression that is the unary negation of the
2052 /// input expression
2053 template <typename EXPR>
2054 const ast::UnaryOpExpression* Negation(EXPR&& expr) {
2055 return create<ast::UnaryOpExpression>(ast::UnaryOp::kNegation,
2056 Expr(std::forward<EXPR>(expr)));
2057 }
2058
dan sinclair41e4d9a2022-05-01 14:40:55 +00002059 /// @param source the source information
2060 /// @param func the function name
2061 /// @param args the function call arguments
2062 /// @returns a `ast::CallExpression` to the function `func`, with the
2063 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
2064 template <typename NAME, typename... ARGS>
2065 const ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
2066 return create<ast::CallExpression>(source, Expr(func),
2067 ExprList(std::forward<ARGS>(args)...));
2068 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002069
dan sinclair41e4d9a2022-05-01 14:40:55 +00002070 /// @param func the function name
2071 /// @param args the function call arguments
2072 /// @returns a `ast::CallExpression` to the function `func`, with the
2073 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
2074 template <typename NAME, typename... ARGS, typename = DisableIfSource<NAME>>
2075 const ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
2076 return create<ast::CallExpression>(Expr(func), ExprList(std::forward<ARGS>(args)...));
2077 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002078
dan sinclair41e4d9a2022-05-01 14:40:55 +00002079 /// @param source the source information
2080 /// @param call the call expression to wrap in a call statement
2081 /// @returns a `ast::CallStatement` for the given call expression
2082 const ast::CallStatement* CallStmt(const Source& source, const ast::CallExpression* call) {
2083 return create<ast::CallStatement>(source, call);
2084 }
James Pricec022ff52022-03-31 22:30:10 +00002085
dan sinclair41e4d9a2022-05-01 14:40:55 +00002086 /// @param call the call expression to wrap in a call statement
2087 /// @returns a `ast::CallStatement` for the given call expression
2088 const ast::CallStatement* CallStmt(const ast::CallExpression* call) {
2089 return create<ast::CallStatement>(call);
2090 }
James Pricec022ff52022-03-31 22:30:10 +00002091
dan sinclair41e4d9a2022-05-01 14:40:55 +00002092 /// @param source the source information
2093 /// @returns a `ast::PhonyExpression`
2094 const ast::PhonyExpression* Phony(const Source& source) {
2095 return create<ast::PhonyExpression>(source);
2096 }
James Priceebe97412022-04-07 13:42:45 +00002097
dan sinclair41e4d9a2022-05-01 14:40:55 +00002098 /// @returns a `ast::PhonyExpression`
2099 const ast::PhonyExpression* Phony() { return create<ast::PhonyExpression>(); }
James Priceebe97412022-04-07 13:42:45 +00002100
dan sinclair41e4d9a2022-05-01 14:40:55 +00002101 /// @param expr the expression to ignore
2102 /// @returns a `ast::AssignmentStatement` that assigns 'expr' to the phony
2103 /// (underscore) variable.
2104 template <typename EXPR>
2105 const ast::AssignmentStatement* Ignore(EXPR&& expr) {
2106 return create<ast::AssignmentStatement>(Phony(), Expr(expr));
2107 }
James Priceebe97412022-04-07 13:42:45 +00002108
dan sinclair41e4d9a2022-05-01 14:40:55 +00002109 /// @param lhs the left hand argument to the addition operation
2110 /// @param rhs the right hand argument to the addition operation
2111 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
2112 template <typename LHS, typename RHS>
2113 const ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
2114 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd, Expr(std::forward<LHS>(lhs)),
2115 Expr(std::forward<RHS>(rhs)));
2116 }
James Priceebe97412022-04-07 13:42:45 +00002117
Antonio Maiorano7058a172022-08-10 18:06:43 +00002118 /// @param source the source information
2119 /// @param lhs the left hand argument to the addition operation
2120 /// @param rhs the right hand argument to the addition operation
2121 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
2122 template <typename LHS, typename RHS>
2123 const ast::BinaryExpression* Add(const Source& source, LHS&& lhs, RHS&& rhs) {
2124 return create<ast::BinaryExpression>(source, ast::BinaryOp::kAdd,
2125 Expr(std::forward<LHS>(lhs)),
2126 Expr(std::forward<RHS>(rhs)));
2127 }
2128
dan sinclair41e4d9a2022-05-01 14:40:55 +00002129 /// @param lhs the left hand argument to the and operation
2130 /// @param rhs the right hand argument to the and operation
2131 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
2132 template <typename LHS, typename RHS>
2133 const ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
2134 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd, Expr(std::forward<LHS>(lhs)),
2135 Expr(std::forward<RHS>(rhs)));
2136 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002137
dan sinclair41e4d9a2022-05-01 14:40:55 +00002138 /// @param lhs the left hand argument to the or operation
2139 /// @param rhs the right hand argument to the or operation
2140 /// @returns a `ast::BinaryExpression` bitwise or-ing `lhs` and `rhs`
2141 template <typename LHS, typename RHS>
2142 const ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) {
2143 return create<ast::BinaryExpression>(ast::BinaryOp::kOr, Expr(std::forward<LHS>(lhs)),
2144 Expr(std::forward<RHS>(rhs)));
2145 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002146
dan sinclair41e4d9a2022-05-01 14:40:55 +00002147 /// @param lhs the left hand argument to the subtraction operation
2148 /// @param rhs the right hand argument to the subtraction operation
2149 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
2150 template <typename LHS, typename RHS>
2151 const ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
2152 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract, Expr(std::forward<LHS>(lhs)),
2153 Expr(std::forward<RHS>(rhs)));
2154 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002155
dan sinclair41e4d9a2022-05-01 14:40:55 +00002156 /// @param lhs the left hand argument to the multiplication operation
2157 /// @param rhs the right hand argument to the multiplication operation
2158 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
2159 template <typename LHS, typename RHS>
2160 const ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
2161 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply, Expr(std::forward<LHS>(lhs)),
2162 Expr(std::forward<RHS>(rhs)));
2163 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002164
dan sinclair41e4d9a2022-05-01 14:40:55 +00002165 /// @param source the source information
2166 /// @param lhs the left hand argument to the multiplication operation
2167 /// @param rhs the right hand argument to the multiplication operation
2168 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
2169 template <typename LHS, typename RHS>
2170 const ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
2171 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
2172 Expr(std::forward<LHS>(lhs)),
2173 Expr(std::forward<RHS>(rhs)));
2174 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002175
dan sinclair41e4d9a2022-05-01 14:40:55 +00002176 /// @param lhs the left hand argument to the division operation
2177 /// @param rhs the right hand argument to the division operation
2178 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
2179 template <typename LHS, typename RHS>
2180 const ast::BinaryExpression* Div(LHS&& lhs, RHS&& rhs) {
2181 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide, Expr(std::forward<LHS>(lhs)),
2182 Expr(std::forward<RHS>(rhs)));
2183 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002184
Antonio Maiorano28779af2022-12-09 21:28:51 +00002185 /// @param source the source information
2186 /// @param lhs the left hand argument to the division operation
2187 /// @param rhs the right hand argument to the division operation
2188 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
2189 template <typename LHS, typename RHS>
2190 const ast::BinaryExpression* Div(const Source& source, LHS&& lhs, RHS&& rhs) {
2191 return create<ast::BinaryExpression>(source, ast::BinaryOp::kDivide,
2192 Expr(std::forward<LHS>(lhs)),
2193 Expr(std::forward<RHS>(rhs)));
2194 }
2195
dan sinclair41e4d9a2022-05-01 14:40:55 +00002196 /// @param lhs the left hand argument to the modulo operation
2197 /// @param rhs the right hand argument to the modulo operation
2198 /// @returns a `ast::BinaryExpression` applying modulo of `lhs` by `rhs`
2199 template <typename LHS, typename RHS>
2200 const ast::BinaryExpression* Mod(LHS&& lhs, RHS&& rhs) {
2201 return create<ast::BinaryExpression>(ast::BinaryOp::kModulo, Expr(std::forward<LHS>(lhs)),
2202 Expr(std::forward<RHS>(rhs)));
2203 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002204
dan sinclair41e4d9a2022-05-01 14:40:55 +00002205 /// @param lhs the left hand argument to the bit shift right operation
2206 /// @param rhs the right hand argument to the bit shift right operation
2207 /// @returns a `ast::BinaryExpression` bit shifting right `lhs` by `rhs`
2208 template <typename LHS, typename RHS>
2209 const ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) {
2210 return create<ast::BinaryExpression>(
2211 ast::BinaryOp::kShiftRight, Expr(std::forward<LHS>(lhs)), Expr(std::forward<RHS>(rhs)));
2212 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002213
dan sinclair41e4d9a2022-05-01 14:40:55 +00002214 /// @param lhs the left hand argument to the bit shift left operation
2215 /// @param rhs the right hand argument to the bit shift left operation
2216 /// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs`
2217 template <typename LHS, typename RHS>
2218 const ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) {
2219 return create<ast::BinaryExpression>(
2220 ast::BinaryOp::kShiftLeft, Expr(std::forward<LHS>(lhs)), Expr(std::forward<RHS>(rhs)));
2221 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002222
Antonio Maiorano5f33fac2022-09-23 21:58:29 +00002223 /// @param source the source information
2224 /// @param lhs the left hand argument to the bit shift left operation
2225 /// @param rhs the right hand argument to the bit shift left operation
2226 /// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs`
2227 template <typename LHS, typename RHS>
2228 const ast::BinaryExpression* Shl(const Source& source, LHS&& lhs, RHS&& rhs) {
2229 return create<ast::BinaryExpression>(source, ast::BinaryOp::kShiftLeft,
2230 Expr(std::forward<LHS>(lhs)),
2231 Expr(std::forward<RHS>(rhs)));
2232 }
2233
dan sinclair41e4d9a2022-05-01 14:40:55 +00002234 /// @param lhs the left hand argument to the xor operation
2235 /// @param rhs the right hand argument to the xor operation
2236 /// @returns a `ast::BinaryExpression` bitwise xor-ing `lhs` and `rhs`
2237 template <typename LHS, typename RHS>
2238 const ast::BinaryExpression* Xor(LHS&& lhs, RHS&& rhs) {
2239 return create<ast::BinaryExpression>(ast::BinaryOp::kXor, Expr(std::forward<LHS>(lhs)),
2240 Expr(std::forward<RHS>(rhs)));
2241 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002242
dan sinclair41e4d9a2022-05-01 14:40:55 +00002243 /// @param lhs the left hand argument to the logical and operation
2244 /// @param rhs the right hand argument to the logical and operation
2245 /// @returns a `ast::BinaryExpression` of `lhs` && `rhs`
2246 template <typename LHS, typename RHS>
2247 const ast::BinaryExpression* LogicalAnd(LHS&& lhs, RHS&& rhs) {
2248 return create<ast::BinaryExpression>(
2249 ast::BinaryOp::kLogicalAnd, Expr(std::forward<LHS>(lhs)), Expr(std::forward<RHS>(rhs)));
2250 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002251
Antonio Maiorano28779af2022-12-09 21:28:51 +00002252 /// @param source the source information
2253 /// @param lhs the left hand argument to the logical and operation
2254 /// @param rhs the right hand argument to the logical and operation
2255 /// @returns a `ast::BinaryExpression` of `lhs` && `rhs`
2256 template <typename LHS, typename RHS>
2257 const ast::BinaryExpression* LogicalAnd(const Source& source, LHS&& lhs, RHS&& rhs) {
2258 return create<ast::BinaryExpression>(source, ast::BinaryOp::kLogicalAnd,
2259 Expr(std::forward<LHS>(lhs)),
2260 Expr(std::forward<RHS>(rhs)));
2261 }
2262
dan sinclair41e4d9a2022-05-01 14:40:55 +00002263 /// @param lhs the left hand argument to the logical or operation
2264 /// @param rhs the right hand argument to the logical or operation
2265 /// @returns a `ast::BinaryExpression` of `lhs` || `rhs`
2266 template <typename LHS, typename RHS>
2267 const ast::BinaryExpression* LogicalOr(LHS&& lhs, RHS&& rhs) {
2268 return create<ast::BinaryExpression>(
2269 ast::BinaryOp::kLogicalOr, Expr(std::forward<LHS>(lhs)), Expr(std::forward<RHS>(rhs)));
2270 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002271
Antonio Maiorano28779af2022-12-09 21:28:51 +00002272 /// @param source the source information
2273 /// @param lhs the left hand argument to the logical or operation
2274 /// @param rhs the right hand argument to the logical or operation
2275 /// @returns a `ast::BinaryExpression` of `lhs` || `rhs`
2276 template <typename LHS, typename RHS>
2277 const ast::BinaryExpression* LogicalOr(const Source& source, LHS&& lhs, RHS&& rhs) {
2278 return create<ast::BinaryExpression>(source, ast::BinaryOp::kLogicalOr,
2279 Expr(std::forward<LHS>(lhs)),
2280 Expr(std::forward<RHS>(rhs)));
2281 }
2282
dan sinclair41e4d9a2022-05-01 14:40:55 +00002283 /// @param lhs the left hand argument to the greater than operation
2284 /// @param rhs the right hand argument to the greater than operation
2285 /// @returns a `ast::BinaryExpression` of `lhs` > `rhs`
2286 template <typename LHS, typename RHS>
2287 const ast::BinaryExpression* GreaterThan(LHS&& lhs, RHS&& rhs) {
2288 return create<ast::BinaryExpression>(ast::BinaryOp::kGreaterThan,
2289 Expr(std::forward<LHS>(lhs)),
2290 Expr(std::forward<RHS>(rhs)));
2291 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002292
dan sinclair41e4d9a2022-05-01 14:40:55 +00002293 /// @param lhs the left hand argument to the greater than or equal operation
2294 /// @param rhs the right hand argument to the greater than or equal operation
2295 /// @returns a `ast::BinaryExpression` of `lhs` >= `rhs`
2296 template <typename LHS, typename RHS>
2297 const ast::BinaryExpression* GreaterThanEqual(LHS&& lhs, RHS&& rhs) {
2298 return create<ast::BinaryExpression>(ast::BinaryOp::kGreaterThanEqual,
2299 Expr(std::forward<LHS>(lhs)),
2300 Expr(std::forward<RHS>(rhs)));
2301 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002302
dan sinclair41e4d9a2022-05-01 14:40:55 +00002303 /// @param lhs the left hand argument to the less than operation
2304 /// @param rhs the right hand argument to the less than operation
2305 /// @returns a `ast::BinaryExpression` of `lhs` < `rhs`
2306 template <typename LHS, typename RHS>
2307 const ast::BinaryExpression* LessThan(LHS&& lhs, RHS&& rhs) {
2308 return create<ast::BinaryExpression>(ast::BinaryOp::kLessThan, Expr(std::forward<LHS>(lhs)),
2309 Expr(std::forward<RHS>(rhs)));
2310 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002311
dan sinclair41e4d9a2022-05-01 14:40:55 +00002312 /// @param lhs the left hand argument to the less than or equal operation
2313 /// @param rhs the right hand argument to the less than or equal operation
2314 /// @returns a `ast::BinaryExpression` of `lhs` <= `rhs`
2315 template <typename LHS, typename RHS>
2316 const ast::BinaryExpression* LessThanEqual(LHS&& lhs, RHS&& rhs) {
2317 return create<ast::BinaryExpression>(ast::BinaryOp::kLessThanEqual,
2318 Expr(std::forward<LHS>(lhs)),
2319 Expr(std::forward<RHS>(rhs)));
2320 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002321
dan sinclair41e4d9a2022-05-01 14:40:55 +00002322 /// @param lhs the left hand argument to the equal expression
2323 /// @param rhs the right hand argument to the equal expression
2324 /// @returns a `ast::BinaryExpression` comparing `lhs` equal to `rhs`
2325 template <typename LHS, typename RHS>
2326 const ast::BinaryExpression* Equal(LHS&& lhs, RHS&& rhs) {
2327 return create<ast::BinaryExpression>(ast::BinaryOp::kEqual, Expr(std::forward<LHS>(lhs)),
2328 Expr(std::forward<RHS>(rhs)));
2329 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002330
Antonio Maiorano28779af2022-12-09 21:28:51 +00002331 /// @param source the source information
2332 /// @param lhs the left hand argument to the equal expression
2333 /// @param rhs the right hand argument to the equal expression
2334 /// @returns a `ast::BinaryExpression` comparing `lhs` equal to `rhs`
2335 template <typename LHS, typename RHS>
2336 const ast::BinaryExpression* Equal(const Source& source, LHS&& lhs, RHS&& rhs) {
2337 return create<ast::BinaryExpression>(source, ast::BinaryOp::kEqual,
2338 Expr(std::forward<LHS>(lhs)),
2339 Expr(std::forward<RHS>(rhs)));
2340 }
2341
dan sinclair41e4d9a2022-05-01 14:40:55 +00002342 /// @param lhs the left hand argument to the not-equal expression
2343 /// @param rhs the right hand argument to the not-equal expression
2344 /// @returns a `ast::BinaryExpression` comparing `lhs` equal to `rhs` for
2345 /// disequality
2346 template <typename LHS, typename RHS>
2347 const ast::BinaryExpression* NotEqual(LHS&& lhs, RHS&& rhs) {
2348 return create<ast::BinaryExpression>(ast::BinaryOp::kNotEqual, Expr(std::forward<LHS>(lhs)),
2349 Expr(std::forward<RHS>(rhs)));
2350 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002351
dan sinclair41e4d9a2022-05-01 14:40:55 +00002352 /// @param source the source information
2353 /// @param obj the object for the index accessor expression
2354 /// @param idx the index argument for the index accessor expression
2355 /// @returns a `ast::IndexAccessorExpression` that indexes `arr` with `idx`
2356 template <typename OBJ, typename IDX>
2357 const ast::IndexAccessorExpression* IndexAccessor(const Source& source, OBJ&& obj, IDX&& idx) {
2358 return create<ast::IndexAccessorExpression>(source, Expr(std::forward<OBJ>(obj)),
2359 Expr(std::forward<IDX>(idx)));
2360 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002361
dan sinclair41e4d9a2022-05-01 14:40:55 +00002362 /// @param obj the object for the index accessor expression
2363 /// @param idx the index argument for the index accessor expression
2364 /// @returns a `ast::IndexAccessorExpression` that indexes `arr` with `idx`
2365 template <typename OBJ, typename IDX>
2366 const ast::IndexAccessorExpression* IndexAccessor(OBJ&& obj, IDX&& idx) {
2367 return create<ast::IndexAccessorExpression>(Expr(std::forward<OBJ>(obj)),
2368 Expr(std::forward<IDX>(idx)));
2369 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002370
dan sinclair41e4d9a2022-05-01 14:40:55 +00002371 /// @param source the source information
2372 /// @param obj the object for the member accessor expression
2373 /// @param idx the index argument for the member accessor expression
2374 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
2375 template <typename OBJ, typename IDX>
2376 const ast::MemberAccessorExpression* MemberAccessor(const Source& source,
2377 OBJ&& obj,
2378 IDX&& idx) {
2379 return create<ast::MemberAccessorExpression>(source, Expr(std::forward<OBJ>(obj)),
2380 Expr(std::forward<IDX>(idx)));
2381 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002382
dan sinclair41e4d9a2022-05-01 14:40:55 +00002383 /// @param obj the object for the member accessor expression
2384 /// @param idx the index argument for the member accessor expression
2385 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
2386 template <typename OBJ, typename IDX>
2387 const ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
2388 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
2389 Expr(std::forward<IDX>(idx)));
2390 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002391
dan sinclair41e4d9a2022-05-01 14:40:55 +00002392 /// Creates a ast::StructMemberOffsetAttribute
dan sinclair93df9672022-09-09 14:49:09 +00002393 /// @param val the offset expression
dan sinclair41e4d9a2022-05-01 14:40:55 +00002394 /// @returns the offset attribute pointer
dan sinclair93df9672022-09-09 14:49:09 +00002395 template <typename EXPR>
2396 const ast::StructMemberOffsetAttribute* MemberOffset(EXPR&& val) {
2397 return create<ast::StructMemberOffsetAttribute>(source_, Expr(std::forward<EXPR>(val)));
2398 }
2399
2400 /// Creates a ast::StructMemberOffsetAttribute
2401 /// @param source the source information
2402 /// @param val the offset expression
2403 /// @returns the offset attribute pointer
2404 template <typename EXPR>
2405 const ast::StructMemberOffsetAttribute* MemberOffset(const Source& source, EXPR&& val) {
2406 return create<ast::StructMemberOffsetAttribute>(source, Expr(std::forward<EXPR>(val)));
dan sinclair41e4d9a2022-05-01 14:40:55 +00002407 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002408
dan sinclair41e4d9a2022-05-01 14:40:55 +00002409 /// Creates a ast::StructMemberSizeAttribute
2410 /// @param source the source information
2411 /// @param val the size value
2412 /// @returns the size attribute pointer
dan sinclair93df9672022-09-09 14:49:09 +00002413 template <typename EXPR>
2414 const ast::StructMemberSizeAttribute* MemberSize(const Source& source, EXPR&& val) {
2415 return create<ast::StructMemberSizeAttribute>(source, Expr(std::forward<EXPR>(val)));
dan sinclair41e4d9a2022-05-01 14:40:55 +00002416 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002417
dan sinclair41e4d9a2022-05-01 14:40:55 +00002418 /// Creates a ast::StructMemberSizeAttribute
2419 /// @param val the size value
2420 /// @returns the size attribute pointer
dan sinclair93df9672022-09-09 14:49:09 +00002421 template <typename EXPR>
2422 const ast::StructMemberSizeAttribute* MemberSize(EXPR&& val) {
2423 return create<ast::StructMemberSizeAttribute>(source_, Expr(std::forward<EXPR>(val)));
dan sinclair41e4d9a2022-05-01 14:40:55 +00002424 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002425
dan sinclair41e4d9a2022-05-01 14:40:55 +00002426 /// Creates a ast::StructMemberAlignAttribute
2427 /// @param source the source information
dan sinclair4964d9b2022-08-23 13:28:44 +00002428 /// @param val the align value expression
dan sinclair41e4d9a2022-05-01 14:40:55 +00002429 /// @returns the align attribute pointer
dan sinclair4964d9b2022-08-23 13:28:44 +00002430 template <typename EXPR>
2431 const ast::StructMemberAlignAttribute* MemberAlign(const Source& source, EXPR&& val) {
2432 return create<ast::StructMemberAlignAttribute>(source, Expr(std::forward<EXPR>(val)));
dan sinclair41e4d9a2022-05-01 14:40:55 +00002433 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002434
dan sinclair41e4d9a2022-05-01 14:40:55 +00002435 /// Creates a ast::StructMemberAlignAttribute
dan sinclair4964d9b2022-08-23 13:28:44 +00002436 /// @param val the align value expression
dan sinclair41e4d9a2022-05-01 14:40:55 +00002437 /// @returns the align attribute pointer
dan sinclair4964d9b2022-08-23 13:28:44 +00002438 template <typename EXPR>
2439 const ast::StructMemberAlignAttribute* MemberAlign(EXPR&& val) {
2440 return create<ast::StructMemberAlignAttribute>(source_, Expr(std::forward<EXPR>(val)));
dan sinclair41e4d9a2022-05-01 14:40:55 +00002441 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002442
dan sinclair41e4d9a2022-05-01 14:40:55 +00002443 /// Creates the ast::GroupAttribute
dan sinclairbe4c9f42022-08-29 21:22:31 +00002444 /// @param value group attribute index expresion
dan sinclair41e4d9a2022-05-01 14:40:55 +00002445 /// @returns the group attribute pointer
dan sinclairbe4c9f42022-08-29 21:22:31 +00002446 template <typename EXPR>
2447 const ast::GroupAttribute* Group(EXPR&& value) {
2448 return create<ast::GroupAttribute>(Expr(std::forward<EXPR>(value)));
2449 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002450
dan sinclairf9b831c2022-08-29 21:13:00 +00002451 /// Creates the ast::GroupAttribute
2452 /// @param source the source
dan sinclairbe4c9f42022-08-29 21:22:31 +00002453 /// @param value group attribute index expression
dan sinclairf9b831c2022-08-29 21:13:00 +00002454 /// @returns the group attribute pointer
dan sinclairbe4c9f42022-08-29 21:22:31 +00002455 template <typename EXPR>
2456 const ast::GroupAttribute* Group(const Source& source, EXPR&& value) {
2457 return create<ast::GroupAttribute>(source, Expr(std::forward<EXPR>(value)));
dan sinclairf9b831c2022-08-29 21:13:00 +00002458 }
2459
dan sinclair41e4d9a2022-05-01 14:40:55 +00002460 /// Creates the ast::BindingAttribute
dan sinclairf9b831c2022-08-29 21:13:00 +00002461 /// @param value the binding index expression
dan sinclair41e4d9a2022-05-01 14:40:55 +00002462 /// @returns the binding deocration pointer
dan sinclairf9b831c2022-08-29 21:13:00 +00002463 template <typename EXPR>
2464 const ast::BindingAttribute* Binding(EXPR&& value) {
2465 return create<ast::BindingAttribute>(Expr(std::forward<EXPR>(value)));
2466 }
2467
2468 /// Creates the ast::BindingAttribute
2469 /// @param source the source
2470 /// @param value the binding index expression
2471 /// @returns the binding deocration pointer
2472 template <typename EXPR>
2473 const ast::BindingAttribute* Binding(const Source& source, EXPR&& value) {
2474 return create<ast::BindingAttribute>(source, Expr(std::forward<EXPR>(value)));
dan sinclair41e4d9a2022-05-01 14:40:55 +00002475 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002476
dan sinclair41e4d9a2022-05-01 14:40:55 +00002477 /// Creates an ast::Function and registers it with the ast::Module.
2478 /// @param source the source information
2479 /// @param name the function name
2480 /// @param params the function parameters
2481 /// @param type the function return type
2482 /// @param body the function body
2483 /// @param attributes the optional function attributes
2484 /// @param return_type_attributes the optional function return type
2485 /// attributes
2486 /// @returns the function pointer
2487 template <typename NAME>
Ben Clayton783b1692022-08-02 17:03:35 +00002488 const ast::Function* Func(
2489 const Source& source,
2490 NAME&& name,
2491 utils::VectorRef<const ast::Parameter*> params,
2492 const ast::Type* type,
2493 utils::VectorRef<const ast::Statement*> body,
2494 utils::VectorRef<const ast::Attribute*> attributes = utils::Empty,
2495 utils::VectorRef<const ast::Attribute*> return_type_attributes = utils::Empty) {
2496 auto* func =
2497 create<ast::Function>(source, Sym(std::forward<NAME>(name)), std::move(params), type,
2498 create<ast::BlockStatement>(std::move(body)),
2499 std::move(attributes), std::move(return_type_attributes));
dan sinclair41e4d9a2022-05-01 14:40:55 +00002500 AST().AddFunction(func);
2501 return func;
2502 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002503
dan sinclair41e4d9a2022-05-01 14:40:55 +00002504 /// Creates an ast::Function and registers it with the ast::Module.
2505 /// @param name the function name
2506 /// @param params the function parameters
2507 /// @param type the function return type
2508 /// @param body the function body
2509 /// @param attributes the optional function attributes
2510 /// @param return_type_attributes the optional function return type
2511 /// attributes
2512 /// @returns the function pointer
2513 template <typename NAME>
Ben Clayton783b1692022-08-02 17:03:35 +00002514 const ast::Function* Func(
2515 NAME&& name,
2516 utils::VectorRef<const ast::Parameter*> params,
2517 const ast::Type* type,
2518 utils::VectorRef<const ast::Statement*> body,
2519 utils::VectorRef<const ast::Attribute*> attributes = utils::Empty,
2520 utils::VectorRef<const ast::Attribute*> return_type_attributes = utils::Empty) {
2521 auto* func =
2522 create<ast::Function>(Sym(std::forward<NAME>(name)), std::move(params), type,
2523 create<ast::BlockStatement>(std::move(body)),
2524 std::move(attributes), std::move(return_type_attributes));
dan sinclair41e4d9a2022-05-01 14:40:55 +00002525 AST().AddFunction(func);
2526 return func;
2527 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002528
dan sinclair41e4d9a2022-05-01 14:40:55 +00002529 /// Creates an ast::BreakStatement
2530 /// @param source the source information
2531 /// @returns the break statement pointer
2532 const ast::BreakStatement* Break(const Source& source) {
2533 return create<ast::BreakStatement>(source);
2534 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002535
dan sinclair41e4d9a2022-05-01 14:40:55 +00002536 /// Creates an ast::BreakStatement
2537 /// @returns the break statement pointer
2538 const ast::BreakStatement* Break() { return create<ast::BreakStatement>(); }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002539
dan sinclairb8b0c212022-10-20 22:45:50 +00002540 /// Creates a ast::BreakIfStatement with input condition
2541 /// @param source the source information for the if statement
2542 /// @param condition the if statement condition expression
2543 /// @returns the break-if statement pointer
2544 template <typename CONDITION>
2545 const ast::BreakIfStatement* BreakIf(const Source& source, CONDITION&& condition) {
2546 return create<ast::BreakIfStatement>(source, Expr(std::forward<CONDITION>(condition)));
2547 }
2548
2549 /// Creates a ast::BreakIfStatement with input condition
2550 /// @param condition the if statement condition expression
2551 /// @returns the break-if statement pointer
2552 template <typename CONDITION>
2553 const ast::BreakIfStatement* BreakIf(CONDITION&& condition) {
2554 return create<ast::BreakIfStatement>(Expr(std::forward<CONDITION>(condition)));
2555 }
2556
dan sinclair41e4d9a2022-05-01 14:40:55 +00002557 /// Creates an ast::ContinueStatement
2558 /// @param source the source information
2559 /// @returns the continue statement pointer
2560 const ast::ContinueStatement* Continue(const Source& source) {
2561 return create<ast::ContinueStatement>(source);
2562 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002563
dan sinclair41e4d9a2022-05-01 14:40:55 +00002564 /// Creates an ast::ContinueStatement
2565 /// @returns the continue statement pointer
2566 const ast::ContinueStatement* Continue() { return create<ast::ContinueStatement>(); }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002567
dan sinclair41e4d9a2022-05-01 14:40:55 +00002568 /// Creates an ast::ReturnStatement with no return value
2569 /// @param source the source information
2570 /// @returns the return statement pointer
2571 const ast::ReturnStatement* Return(const Source& source) {
2572 return create<ast::ReturnStatement>(source);
2573 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002574
dan sinclair41e4d9a2022-05-01 14:40:55 +00002575 /// Creates an ast::ReturnStatement with no return value
2576 /// @returns the return statement pointer
2577 const ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002578
dan sinclair41e4d9a2022-05-01 14:40:55 +00002579 /// Creates an ast::ReturnStatement with the given return value
2580 /// @param source the source information
2581 /// @param val the return value
2582 /// @returns the return statement pointer
2583 template <typename EXPR>
2584 const ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
2585 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
2586 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002587
dan sinclair41e4d9a2022-05-01 14:40:55 +00002588 /// Creates an ast::ReturnStatement with the given return value
2589 /// @param val the return value
2590 /// @returns the return statement pointer
2591 template <typename EXPR, typename = DisableIfSource<EXPR>>
2592 const ast::ReturnStatement* Return(EXPR&& val) {
2593 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
2594 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002595
dan sinclair41e4d9a2022-05-01 14:40:55 +00002596 /// Creates an ast::DiscardStatement
2597 /// @param source the source information
2598 /// @returns the discard statement pointer
2599 const ast::DiscardStatement* Discard(const Source& source) {
2600 return create<ast::DiscardStatement>(source);
2601 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002602
dan sinclair41e4d9a2022-05-01 14:40:55 +00002603 /// Creates an ast::DiscardStatement
2604 /// @returns the discard statement pointer
2605 const ast::DiscardStatement* Discard() { return create<ast::DiscardStatement>(); }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002606
dan sinclair41e4d9a2022-05-01 14:40:55 +00002607 /// Creates a ast::Alias registering it with the AST().TypeDecls().
2608 /// @param source the source information
2609 /// @param name the alias name
2610 /// @param type the alias target type
2611 /// @returns the alias type
2612 template <typename NAME>
2613 const ast::Alias* Alias(const Source& source, NAME&& name, const ast::Type* type) {
2614 auto* out = ty.alias(source, std::forward<NAME>(name), type);
2615 AST().AddTypeDecl(out);
2616 return out;
2617 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002618
dan sinclair41e4d9a2022-05-01 14:40:55 +00002619 /// Creates a ast::Alias registering it with the AST().TypeDecls().
2620 /// @param name the alias name
2621 /// @param type the alias target type
2622 /// @returns the alias type
2623 template <typename NAME>
2624 const ast::Alias* Alias(NAME&& name, const ast::Type* type) {
2625 auto* out = ty.alias(std::forward<NAME>(name), type);
2626 AST().AddTypeDecl(out);
2627 return out;
2628 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002629
dan sinclair41e4d9a2022-05-01 14:40:55 +00002630 /// Creates a ast::Struct registering it with the AST().TypeDecls().
2631 /// @param source the source information
2632 /// @param name the struct name
2633 /// @param members the struct members
2634 /// @returns the struct type
2635 template <typename NAME>
Ben Clayton783b1692022-08-02 17:03:35 +00002636 const ast::Struct* Structure(const Source& source,
2637 NAME&& name,
2638 utils::VectorRef<const ast::StructMember*> members) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00002639 auto sym = Sym(std::forward<NAME>(name));
Ben Clayton783b1692022-08-02 17:03:35 +00002640 auto* type = create<ast::Struct>(source, sym, std::move(members), utils::Empty);
dan sinclair41e4d9a2022-05-01 14:40:55 +00002641 AST().AddTypeDecl(type);
2642 return type;
2643 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002644
dan sinclair41e4d9a2022-05-01 14:40:55 +00002645 /// Creates a ast::Struct registering it with the AST().TypeDecls().
2646 /// @param name the struct name
2647 /// @param members the struct members
2648 /// @returns the struct type
2649 template <typename NAME>
Ben Clayton783b1692022-08-02 17:03:35 +00002650 const ast::Struct* Structure(NAME&& name, utils::VectorRef<const ast::StructMember*> members) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00002651 auto sym = Sym(std::forward<NAME>(name));
Ben Clayton783b1692022-08-02 17:03:35 +00002652 auto* type = create<ast::Struct>(sym, std::move(members), utils::Empty);
dan sinclair41e4d9a2022-05-01 14:40:55 +00002653 AST().AddTypeDecl(type);
2654 return type;
2655 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002656
dan sinclair41e4d9a2022-05-01 14:40:55 +00002657 /// Creates a ast::StructMember
2658 /// @param source the source information
2659 /// @param name the struct member name
2660 /// @param type the struct member type
2661 /// @param attributes the optional struct member attributes
2662 /// @returns the struct member pointer
2663 template <typename NAME>
Ben Clayton783b1692022-08-02 17:03:35 +00002664 const ast::StructMember* Member(
2665 const Source& source,
2666 NAME&& name,
2667 const ast::Type* type,
2668 utils::VectorRef<const ast::Attribute*> attributes = utils::Empty) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00002669 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)), type,
2670 std::move(attributes));
2671 }
2672
2673 /// Creates a ast::StructMember
2674 /// @param name the struct member name
2675 /// @param type the struct member type
2676 /// @param attributes the optional struct member attributes
2677 /// @returns the struct member pointer
2678 template <typename NAME>
Ben Clayton783b1692022-08-02 17:03:35 +00002679 const ast::StructMember* Member(
2680 NAME&& name,
2681 const ast::Type* type,
2682 utils::VectorRef<const ast::Attribute*> attributes = utils::Empty) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00002683 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)), type,
2684 std::move(attributes));
2685 }
2686
2687 /// Creates a ast::StructMember with the given byte offset
Ben Clayton783b1692022-08-02 17:03:35 +00002688 /// @param offset the offset to use in the StructMemberOffsetAttribute
dan sinclair41e4d9a2022-05-01 14:40:55 +00002689 /// @param name the struct member name
2690 /// @param type the struct member type
2691 /// @returns the struct member pointer
2692 template <typename NAME>
2693 const ast::StructMember* Member(uint32_t offset, NAME&& name, const ast::Type* type) {
2694 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)), type,
Ben Clayton783b1692022-08-02 17:03:35 +00002695 utils::Vector<const ast::Attribute*, 1>{
dan sinclair93df9672022-09-09 14:49:09 +00002696 MemberOffset(AInt(offset)),
dan sinclair41e4d9a2022-05-01 14:40:55 +00002697 });
2698 }
2699
2700 /// Creates a ast::BlockStatement with input statements
2701 /// @param source the source information for the block
2702 /// @param statements statements of block
2703 /// @returns the block statement pointer
2704 template <typename... Statements>
2705 const ast::BlockStatement* Block(const Source& source, Statements&&... statements) {
2706 return create<ast::BlockStatement>(
Ben Clayton783b1692022-08-02 17:03:35 +00002707 source, utils::Vector<const ast::Statement*, sizeof...(statements)>{
2708 std::forward<Statements>(statements)...,
2709 });
dan sinclair41e4d9a2022-05-01 14:40:55 +00002710 }
2711
2712 /// Creates a ast::BlockStatement with input statements
2713 /// @param statements statements of block
2714 /// @returns the block statement pointer
2715 template <typename... STATEMENTS, typename = DisableIfSource<STATEMENTS...>>
2716 const ast::BlockStatement* Block(STATEMENTS&&... statements) {
2717 return create<ast::BlockStatement>(
Ben Clayton783b1692022-08-02 17:03:35 +00002718 utils::Vector<const ast::Statement*, sizeof...(statements)>{
2719 std::forward<STATEMENTS>(statements)...,
2720 });
dan sinclair41e4d9a2022-05-01 14:40:55 +00002721 }
2722
James Price8aff0ed2022-05-02 14:53:36 +00002723 /// A wrapper type for the Else statement used to create If statements.
2724 struct ElseStmt {
2725 /// Default constructor - no else statement.
2726 ElseStmt() : stmt(nullptr) {}
2727 /// Constructor
2728 /// @param s The else statement
2729 explicit ElseStmt(const ast::Statement* s) : stmt(s) {}
2730 /// The else statement, or nullptr.
2731 const ast::Statement* stmt;
2732 };
2733
dan sinclair41e4d9a2022-05-01 14:40:55 +00002734 /// Creates a ast::IfStatement with input condition, body, and optional
2735 /// else statement
2736 /// @param source the source information for the if statement
2737 /// @param condition the if statement condition expression
2738 /// @param body the if statement body
2739 /// @param else_stmt optional else statement
2740 /// @returns the if statement pointer
2741 template <typename CONDITION>
2742 const ast::IfStatement* If(const Source& source,
2743 CONDITION&& condition,
2744 const ast::BlockStatement* body,
James Price8aff0ed2022-05-02 14:53:36 +00002745 const ElseStmt else_stmt = ElseStmt()) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00002746 return create<ast::IfStatement>(source, Expr(std::forward<CONDITION>(condition)), body,
James Price8aff0ed2022-05-02 14:53:36 +00002747 else_stmt.stmt);
dan sinclair41e4d9a2022-05-01 14:40:55 +00002748 }
2749
2750 /// Creates a ast::IfStatement with input condition, body, and optional
2751 /// else statement
2752 /// @param condition the if statement condition expression
2753 /// @param body the if statement body
2754 /// @param else_stmt optional else statement
2755 /// @returns the if statement pointer
2756 template <typename CONDITION>
2757 const ast::IfStatement* If(CONDITION&& condition,
2758 const ast::BlockStatement* body,
James Price8aff0ed2022-05-02 14:53:36 +00002759 const ElseStmt else_stmt = ElseStmt()) {
2760 return create<ast::IfStatement>(Expr(std::forward<CONDITION>(condition)), body,
2761 else_stmt.stmt);
dan sinclair41e4d9a2022-05-01 14:40:55 +00002762 }
2763
James Price8aff0ed2022-05-02 14:53:36 +00002764 /// Creates an Else object.
2765 /// @param stmt else statement
2766 /// @returns the Else object
2767 ElseStmt Else(const ast::Statement* stmt) { return ElseStmt(stmt); }
2768
dan sinclair41e4d9a2022-05-01 14:40:55 +00002769 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
2770 /// @param source the source information
2771 /// @param lhs the left hand side expression initializer
2772 /// @param rhs the right hand side expression initializer
2773 /// @returns the assignment statement pointer
2774 template <typename LhsExpressionInit, typename RhsExpressionInit>
2775 const ast::AssignmentStatement* Assign(const Source& source,
2776 LhsExpressionInit&& lhs,
2777 RhsExpressionInit&& rhs) {
2778 return create<ast::AssignmentStatement>(source, Expr(std::forward<LhsExpressionInit>(lhs)),
2779 Expr(std::forward<RhsExpressionInit>(rhs)));
2780 }
2781
2782 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
2783 /// @param lhs the left hand side expression initializer
2784 /// @param rhs the right hand side expression initializer
2785 /// @returns the assignment statement pointer
2786 template <typename LhsExpressionInit, typename RhsExpressionInit>
2787 const ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs, RhsExpressionInit&& rhs) {
2788 return create<ast::AssignmentStatement>(Expr(std::forward<LhsExpressionInit>(lhs)),
2789 Expr(std::forward<RhsExpressionInit>(rhs)));
2790 }
2791
2792 /// Creates a ast::CompoundAssignmentStatement with input lhs and rhs
2793 /// expressions, and a binary operator.
2794 /// @param source the source information
2795 /// @param lhs the left hand side expression initializer
2796 /// @param rhs the right hand side expression initializer
2797 /// @param op the binary operator
2798 /// @returns the compound assignment statement pointer
2799 template <typename LhsExpressionInit, typename RhsExpressionInit>
2800 const ast::CompoundAssignmentStatement* CompoundAssign(const Source& source,
2801 LhsExpressionInit&& lhs,
2802 RhsExpressionInit&& rhs,
2803 ast::BinaryOp op) {
2804 return create<ast::CompoundAssignmentStatement>(
2805 source, Expr(std::forward<LhsExpressionInit>(lhs)),
2806 Expr(std::forward<RhsExpressionInit>(rhs)), op);
2807 }
2808
2809 /// Creates a ast::CompoundAssignmentStatement with input lhs and rhs
2810 /// expressions, and a binary operator.
2811 /// @param lhs the left hand side expression initializer
2812 /// @param rhs the right hand side expression initializer
2813 /// @param op the binary operator
2814 /// @returns the compound assignment statement pointer
2815 template <typename LhsExpressionInit, typename RhsExpressionInit>
2816 const ast::CompoundAssignmentStatement* CompoundAssign(LhsExpressionInit&& lhs,
2817 RhsExpressionInit&& rhs,
2818 ast::BinaryOp op) {
2819 return create<ast::CompoundAssignmentStatement>(Expr(std::forward<LhsExpressionInit>(lhs)),
2820 Expr(std::forward<RhsExpressionInit>(rhs)),
2821 op);
2822 }
2823
2824 /// Creates an ast::IncrementDecrementStatement with input lhs.
2825 /// @param source the source information
2826 /// @param lhs the left hand side expression initializer
2827 /// @returns the increment decrement statement pointer
2828 template <typename LhsExpressionInit>
2829 const ast::IncrementDecrementStatement* Increment(const Source& source,
2830 LhsExpressionInit&& lhs) {
2831 return create<ast::IncrementDecrementStatement>(
2832 source, Expr(std::forward<LhsExpressionInit>(lhs)), true);
2833 }
2834
2835 /// Creates a ast::IncrementDecrementStatement with input lhs.
2836 /// @param lhs the left hand side expression initializer
2837 /// @returns the increment decrement statement pointer
2838 template <typename LhsExpressionInit>
2839 const ast::IncrementDecrementStatement* Increment(LhsExpressionInit&& lhs) {
2840 return create<ast::IncrementDecrementStatement>(Expr(std::forward<LhsExpressionInit>(lhs)),
2841 true);
2842 }
2843
2844 /// Creates an ast::IncrementDecrementStatement with input lhs.
2845 /// @param source the source information
2846 /// @param lhs the left hand side expression initializer
2847 /// @returns the increment decrement statement pointer
2848 template <typename LhsExpressionInit>
2849 const ast::IncrementDecrementStatement* Decrement(const Source& source,
2850 LhsExpressionInit&& lhs) {
2851 return create<ast::IncrementDecrementStatement>(
2852 source, Expr(std::forward<LhsExpressionInit>(lhs)), false);
2853 }
2854
2855 /// Creates a ast::IncrementDecrementStatement with input lhs.
2856 /// @param lhs the left hand side expression initializer
2857 /// @returns the increment decrement statement pointer
2858 template <typename LhsExpressionInit>
2859 const ast::IncrementDecrementStatement* Decrement(LhsExpressionInit&& lhs) {
2860 return create<ast::IncrementDecrementStatement>(Expr(std::forward<LhsExpressionInit>(lhs)),
2861 false);
2862 }
2863
2864 /// Creates a ast::LoopStatement with input body and optional continuing
2865 /// @param source the source information
2866 /// @param body the loop body
2867 /// @param continuing the optional continuing block
2868 /// @returns the loop statement pointer
2869 const ast::LoopStatement* Loop(const Source& source,
2870 const ast::BlockStatement* body,
2871 const ast::BlockStatement* continuing = nullptr) {
2872 return create<ast::LoopStatement>(source, body, continuing);
2873 }
2874
2875 /// Creates a ast::LoopStatement with input body and optional continuing
2876 /// @param body the loop body
2877 /// @param continuing the optional continuing block
2878 /// @returns the loop statement pointer
2879 const ast::LoopStatement* Loop(const ast::BlockStatement* body,
2880 const ast::BlockStatement* continuing = nullptr) {
2881 return create<ast::LoopStatement>(body, continuing);
2882 }
2883
2884 /// Creates a ast::ForLoopStatement with input body and optional initializer,
2885 /// condition and continuing.
2886 /// @param source the source information
2887 /// @param init the optional loop initializer
2888 /// @param cond the optional loop condition
2889 /// @param cont the optional loop continuing
2890 /// @param body the loop body
2891 /// @returns the for loop statement pointer
2892 template <typename COND>
2893 const ast::ForLoopStatement* For(const Source& source,
2894 const ast::Statement* init,
2895 COND&& cond,
2896 const ast::Statement* cont,
2897 const ast::BlockStatement* body) {
2898 return create<ast::ForLoopStatement>(source, init, Expr(std::forward<COND>(cond)), cont,
2899 body);
2900 }
2901
2902 /// Creates a ast::ForLoopStatement with input body and optional initializer,
2903 /// condition and continuing.
2904 /// @param init the optional loop initializer
2905 /// @param cond the optional loop condition
2906 /// @param cont the optional loop continuing
2907 /// @param body the loop body
2908 /// @returns the for loop statement pointer
2909 template <typename COND>
2910 const ast::ForLoopStatement* For(const ast::Statement* init,
2911 COND&& cond,
2912 const ast::Statement* cont,
2913 const ast::BlockStatement* body) {
2914 return create<ast::ForLoopStatement>(init, Expr(std::forward<COND>(cond)), cont, body);
2915 }
2916
dan sinclair49d1a2d2022-06-16 12:01:27 +00002917 /// Creates a ast::WhileStatement with input body and condition.
2918 /// @param source the source information
2919 /// @param cond the loop condition
2920 /// @param body the loop body
2921 /// @returns the while statement pointer
2922 template <typename COND>
2923 const ast::WhileStatement* While(const Source& source,
2924 COND&& cond,
2925 const ast::BlockStatement* body) {
2926 return create<ast::WhileStatement>(source, Expr(std::forward<COND>(cond)), body);
2927 }
2928
2929 /// Creates a ast::WhileStatement with given condition and body.
2930 /// @param cond the condition
2931 /// @param body the loop body
2932 /// @returns the while loop statement pointer
2933 template <typename COND>
2934 const ast::WhileStatement* While(COND&& cond, const ast::BlockStatement* body) {
2935 return create<ast::WhileStatement>(Expr(std::forward<COND>(cond)), body);
2936 }
2937
dan sinclair41e4d9a2022-05-01 14:40:55 +00002938 /// Creates a ast::VariableDeclStatement for the input variable
2939 /// @param source the source information
2940 /// @param var the variable to wrap in a decl statement
2941 /// @returns the variable decl statement pointer
2942 const ast::VariableDeclStatement* Decl(const Source& source, const ast::Variable* var) {
2943 return create<ast::VariableDeclStatement>(source, var);
2944 }
2945
2946 /// Creates a ast::VariableDeclStatement for the input variable
2947 /// @param var the variable to wrap in a decl statement
2948 /// @returns the variable decl statement pointer
2949 const ast::VariableDeclStatement* Decl(const ast::Variable* var) {
2950 return create<ast::VariableDeclStatement>(var);
2951 }
2952
2953 /// Creates a ast::SwitchStatement with input expression and cases
2954 /// @param source the source information
2955 /// @param condition the condition expression initializer
2956 /// @param cases case statements
2957 /// @returns the switch statement pointer
2958 template <typename ExpressionInit, typename... Cases>
2959 const ast::SwitchStatement* Switch(const Source& source,
2960 ExpressionInit&& condition,
2961 Cases&&... cases) {
Ben Clayton783b1692022-08-02 17:03:35 +00002962 return create<ast::SwitchStatement>(
2963 source, Expr(std::forward<ExpressionInit>(condition)),
2964 utils::Vector<const ast::CaseStatement*, sizeof...(cases)>{
2965 std::forward<Cases>(cases)...});
dan sinclair41e4d9a2022-05-01 14:40:55 +00002966 }
2967
2968 /// Creates a ast::SwitchStatement with input expression and cases
2969 /// @param condition the condition expression initializer
2970 /// @param cases case statements
2971 /// @returns the switch statement pointer
2972 template <typename ExpressionInit,
2973 typename... Cases,
2974 typename = DisableIfSource<ExpressionInit>>
2975 const ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
Ben Clayton783b1692022-08-02 17:03:35 +00002976 return create<ast::SwitchStatement>(
2977 Expr(std::forward<ExpressionInit>(condition)),
2978 utils::Vector<const ast::CaseStatement*, sizeof...(cases)>{
2979 std::forward<Cases>(cases)...});
dan sinclair41e4d9a2022-05-01 14:40:55 +00002980 }
2981
2982 /// Creates a ast::CaseStatement with input list of selectors, and body
2983 /// @param source the source information
2984 /// @param selectors list of selectors
2985 /// @param body the case body
2986 /// @returns the case statement pointer
2987 const ast::CaseStatement* Case(const Source& source,
dan sinclairf148f082022-10-19 15:55:02 +00002988 utils::VectorRef<const ast::CaseSelector*> selectors,
dan sinclair41e4d9a2022-05-01 14:40:55 +00002989 const ast::BlockStatement* body = nullptr) {
2990 return create<ast::CaseStatement>(source, std::move(selectors), body ? body : Block());
2991 }
2992
2993 /// Creates a ast::CaseStatement with input list of selectors, and body
2994 /// @param selectors list of selectors
2995 /// @param body the case body
2996 /// @returns the case statement pointer
dan sinclairf148f082022-10-19 15:55:02 +00002997 const ast::CaseStatement* Case(utils::VectorRef<const ast::CaseSelector*> selectors,
dan sinclair41e4d9a2022-05-01 14:40:55 +00002998 const ast::BlockStatement* body = nullptr) {
2999 return create<ast::CaseStatement>(std::move(selectors), body ? body : Block());
3000 }
3001
3002 /// Convenient overload that takes a single selector
3003 /// @param selector a single case selector
3004 /// @param body the case body
3005 /// @returns the case statement pointer
dan sinclairf148f082022-10-19 15:55:02 +00003006 const ast::CaseStatement* Case(const ast::CaseSelector* selector,
dan sinclair41e4d9a2022-05-01 14:40:55 +00003007 const ast::BlockStatement* body = nullptr) {
Ben Clayton783b1692022-08-02 17:03:35 +00003008 return Case(utils::Vector{selector}, body);
dan sinclair41e4d9a2022-05-01 14:40:55 +00003009 }
3010
3011 /// Convenience function that creates a 'default' ast::CaseStatement
3012 /// @param source the source information
3013 /// @param body the case body
3014 /// @returns the case statement pointer
3015 const ast::CaseStatement* DefaultCase(const Source& source,
3016 const ast::BlockStatement* body = nullptr) {
dan sinclairf148f082022-10-19 15:55:02 +00003017 return Case(source, utils::Vector{DefaultCaseSelector(source)}, body);
dan sinclair41e4d9a2022-05-01 14:40:55 +00003018 }
3019
3020 /// Convenience function that creates a 'default' ast::CaseStatement
3021 /// @param body the case body
3022 /// @returns the case statement pointer
3023 const ast::CaseStatement* DefaultCase(const ast::BlockStatement* body = nullptr) {
dan sinclairf148f082022-10-19 15:55:02 +00003024 return Case(utils::Vector{DefaultCaseSelector()}, body);
dan sinclair41e4d9a2022-05-01 14:40:55 +00003025 }
3026
dan sinclairf148f082022-10-19 15:55:02 +00003027 /// Convenience function that creates a case selector
3028 /// @param source the source information
3029 /// @param expr the selector expression
3030 /// @returns the selector pointer
3031 template <typename EXPR>
3032 const ast::CaseSelector* CaseSelector(const Source& source, EXPR&& expr) {
3033 return create<ast::CaseSelector>(source, Expr(std::forward<EXPR>(expr)));
3034 }
3035
3036 /// Convenience function that creates a case selector
3037 /// @param expr the selector expression
3038 /// @returns the selector pointer
3039 template <typename EXPR>
3040 const ast::CaseSelector* CaseSelector(EXPR&& expr) {
3041 return create<ast::CaseSelector>(source_, Expr(std::forward<EXPR>(expr)));
3042 }
3043
3044 /// Convenience function that creates a default case selector
3045 /// @param source the source information
3046 /// @returns the selector pointer
3047 const ast::CaseSelector* DefaultCaseSelector(const Source& source) {
3048 return create<ast::CaseSelector>(source, nullptr);
3049 }
3050
3051 /// Convenience function that creates a default case selector
3052 /// @returns the selector pointer
3053 const ast::CaseSelector* DefaultCaseSelector() { return create<ast::CaseSelector>(nullptr); }
3054
dan sinclair41e4d9a2022-05-01 14:40:55 +00003055 /// Creates an ast::BuiltinAttribute
3056 /// @param source the source information
3057 /// @param builtin the builtin value
3058 /// @returns the builtin attribute pointer
Ben Claytonf3302292022-07-27 18:48:06 +00003059 const ast::BuiltinAttribute* Builtin(const Source& source, ast::BuiltinValue builtin) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00003060 return create<ast::BuiltinAttribute>(source, builtin);
3061 }
3062
3063 /// Creates an ast::BuiltinAttribute
3064 /// @param builtin the builtin value
3065 /// @returns the builtin attribute pointer
Ben Claytonf3302292022-07-27 18:48:06 +00003066 const ast::BuiltinAttribute* Builtin(ast::BuiltinValue builtin) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00003067 return create<ast::BuiltinAttribute>(source_, builtin);
3068 }
3069
3070 /// Creates an ast::InterpolateAttribute
3071 /// @param source the source information
3072 /// @param type the interpolation type
3073 /// @param sampling the interpolation sampling
3074 /// @returns the interpolate attribute pointer
3075 const ast::InterpolateAttribute* Interpolate(
3076 const Source& source,
3077 ast::InterpolationType type,
Ben Claytond2e0db32022-10-12 18:49:15 +00003078 ast::InterpolationSampling sampling = ast::InterpolationSampling::kUndefined) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00003079 return create<ast::InterpolateAttribute>(source, type, sampling);
3080 }
3081
3082 /// Creates an ast::InterpolateAttribute
3083 /// @param type the interpolation type
3084 /// @param sampling the interpolation sampling
3085 /// @returns the interpolate attribute pointer
3086 const ast::InterpolateAttribute* Interpolate(
3087 ast::InterpolationType type,
Ben Claytond2e0db32022-10-12 18:49:15 +00003088 ast::InterpolationSampling sampling = ast::InterpolationSampling::kUndefined) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00003089 return create<ast::InterpolateAttribute>(source_, type, sampling);
3090 }
3091
3092 /// Creates an ast::InterpolateAttribute using flat interpolation
3093 /// @param source the source information
3094 /// @returns the interpolate attribute pointer
3095 const ast::InterpolateAttribute* Flat(const Source& source) {
3096 return Interpolate(source, ast::InterpolationType::kFlat);
3097 }
3098
3099 /// Creates an ast::InterpolateAttribute using flat interpolation
3100 /// @returns the interpolate attribute pointer
3101 const ast::InterpolateAttribute* Flat() { return Interpolate(ast::InterpolationType::kFlat); }
3102
3103 /// Creates an ast::InvariantAttribute
3104 /// @param source the source information
3105 /// @returns the invariant attribute pointer
3106 const ast::InvariantAttribute* Invariant(const Source& source) {
3107 return create<ast::InvariantAttribute>(source);
3108 }
3109
3110 /// Creates an ast::InvariantAttribute
3111 /// @returns the invariant attribute pointer
3112 const ast::InvariantAttribute* Invariant() { return create<ast::InvariantAttribute>(source_); }
3113
3114 /// Creates an ast::LocationAttribute
3115 /// @param source the source information
dan sinclairf9eeed62022-09-07 22:25:24 +00003116 /// @param location the location value expression
dan sinclair41e4d9a2022-05-01 14:40:55 +00003117 /// @returns the location attribute pointer
dan sinclairf9eeed62022-09-07 22:25:24 +00003118 template <typename EXPR>
3119 const ast::LocationAttribute* Location(const Source& source, EXPR&& location) {
3120 return create<ast::LocationAttribute>(source, Expr(std::forward<EXPR>(location)));
dan sinclair41e4d9a2022-05-01 14:40:55 +00003121 }
3122
3123 /// Creates an ast::LocationAttribute
dan sinclairf9eeed62022-09-07 22:25:24 +00003124 /// @param location the location value expression
dan sinclair41e4d9a2022-05-01 14:40:55 +00003125 /// @returns the location attribute pointer
dan sinclairf9eeed62022-09-07 22:25:24 +00003126 template <typename EXPR>
3127 const ast::LocationAttribute* Location(EXPR&& location) {
3128 return create<ast::LocationAttribute>(source_, Expr(std::forward<EXPR>(location)));
dan sinclair41e4d9a2022-05-01 14:40:55 +00003129 }
3130
3131 /// Creates an ast::IdAttribute
3132 /// @param source the source information
3133 /// @param id the id value
3134 /// @returns the override attribute pointer
Ben Clayton9a6acc42022-07-27 20:50:40 +00003135 const ast::IdAttribute* Id(const Source& source, OverrideId id) {
dan sinclair5361d9e2022-08-31 13:39:48 +00003136 return create<ast::IdAttribute>(source, Expr(AInt(id.value)));
Ben Clayton9a6acc42022-07-27 20:50:40 +00003137 }
3138
3139 /// Creates an ast::IdAttribute with an override identifier
3140 /// @param id the optional id value
3141 /// @returns the override attribute pointer
dan sinclair5361d9e2022-08-31 13:39:48 +00003142 const ast::IdAttribute* Id(OverrideId id) {
3143 return create<ast::IdAttribute>(Expr(AInt(id.value)));
3144 }
Ben Clayton9a6acc42022-07-27 20:50:40 +00003145
3146 /// Creates an ast::IdAttribute
3147 /// @param source the source information
dan sinclair5361d9e2022-08-31 13:39:48 +00003148 /// @param id the id value expression
Ben Clayton9a6acc42022-07-27 20:50:40 +00003149 /// @returns the override attribute pointer
dan sinclair5361d9e2022-08-31 13:39:48 +00003150 template <typename EXPR>
3151 const ast::IdAttribute* Id(const Source& source, EXPR&& id) {
3152 return create<ast::IdAttribute>(source, Expr(std::forward<EXPR>(id)));
dan sinclair41e4d9a2022-05-01 14:40:55 +00003153 }
3154
Ben Clayton9a6acc42022-07-27 20:50:40 +00003155 /// Creates an ast::IdAttribute with an override identifier
dan sinclair5361d9e2022-08-31 13:39:48 +00003156 /// @param id the optional id value expression
dan sinclair41e4d9a2022-05-01 14:40:55 +00003157 /// @returns the override attribute pointer
dan sinclair5361d9e2022-08-31 13:39:48 +00003158 template <typename EXPR>
3159 const ast::IdAttribute* Id(EXPR&& id) {
3160 return create<ast::IdAttribute>(Expr(std::forward<EXPR>(id)));
3161 }
dan sinclair41e4d9a2022-05-01 14:40:55 +00003162
3163 /// Creates an ast::StageAttribute
3164 /// @param source the source information
3165 /// @param stage the pipeline stage
3166 /// @returns the stage attribute pointer
3167 const ast::StageAttribute* Stage(const Source& source, ast::PipelineStage stage) {
3168 return create<ast::StageAttribute>(source, stage);
3169 }
3170
3171 /// Creates an ast::StageAttribute
3172 /// @param stage the pipeline stage
3173 /// @returns the stage attribute pointer
3174 const ast::StageAttribute* Stage(ast::PipelineStage stage) {
3175 return create<ast::StageAttribute>(source_, stage);
3176 }
3177
3178 /// Creates an ast::WorkgroupAttribute
3179 /// @param x the x dimension expression
3180 /// @returns the workgroup attribute pointer
3181 template <typename EXPR_X>
3182 const ast::WorkgroupAttribute* WorkgroupSize(EXPR_X&& x) {
3183 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
3184 }
3185
3186 /// Creates an ast::WorkgroupAttribute
Ben Claytonb8ac9332022-05-28 10:34:06 +00003187 /// @param source the source information
dan sinclair41e4d9a2022-05-01 14:40:55 +00003188 /// @param x the x dimension expression
dan sinclair7517e212022-08-24 21:31:45 +00003189 /// @returns the workgroup attribute pointer
3190 template <typename EXPR_X>
3191 const ast::WorkgroupAttribute* WorkgroupSize(const Source& source, EXPR_X&& x) {
3192 return WorkgroupSize(source, std::forward<EXPR_X>(x), nullptr, nullptr);
3193 }
3194
3195 /// Creates an ast::WorkgroupAttribute
3196 /// @param source the source information
3197 /// @param x the x dimension expression
dan sinclair41e4d9a2022-05-01 14:40:55 +00003198 /// @param y the y dimension expression
3199 /// @returns the workgroup attribute pointer
3200 template <typename EXPR_X, typename EXPR_Y>
Ben Claytonb8ac9332022-05-28 10:34:06 +00003201 const ast::WorkgroupAttribute* WorkgroupSize(const Source& source, EXPR_X&& x, EXPR_Y&& y) {
3202 return WorkgroupSize(source, std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y), nullptr);
3203 }
3204
3205 /// Creates an ast::WorkgroupAttribute
3206 /// @param x the x dimension expression
3207 /// @param y the y dimension expression
3208 /// @returns the workgroup attribute pointer
3209 template <typename EXPR_X, typename EXPR_Y, typename = DisableIfSource<EXPR_X>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00003210 const ast::WorkgroupAttribute* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
3211 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y), nullptr);
3212 }
3213
3214 /// Creates an ast::WorkgroupAttribute
3215 /// @param source the source information
3216 /// @param x the x dimension expression
3217 /// @param y the y dimension expression
3218 /// @param z the z dimension expression
3219 /// @returns the workgroup attribute pointer
3220 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
3221 const ast::WorkgroupAttribute* WorkgroupSize(const Source& source,
3222 EXPR_X&& x,
3223 EXPR_Y&& y,
3224 EXPR_Z&& z) {
3225 return create<ast::WorkgroupAttribute>(source, Expr(std::forward<EXPR_X>(x)),
3226 Expr(std::forward<EXPR_Y>(y)),
3227 Expr(std::forward<EXPR_Z>(z)));
3228 }
3229
3230 /// Creates an ast::WorkgroupAttribute
3231 /// @param x the x dimension expression
3232 /// @param y the y dimension expression
3233 /// @param z the z dimension expression
3234 /// @returns the workgroup attribute pointer
Ben Claytonb8ac9332022-05-28 10:34:06 +00003235 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z, typename = DisableIfSource<EXPR_X>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00003236 const ast::WorkgroupAttribute* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
3237 return create<ast::WorkgroupAttribute>(source_, Expr(std::forward<EXPR_X>(x)),
3238 Expr(std::forward<EXPR_Y>(y)),
3239 Expr(std::forward<EXPR_Z>(z)));
3240 }
3241
3242 /// Creates an ast::DisableValidationAttribute
3243 /// @param validation the validation to disable
3244 /// @returns the disable validation attribute pointer
3245 const ast::DisableValidationAttribute* Disable(ast::DisabledValidation validation) {
Ben Clayton4a92a3c2022-07-18 20:50:02 +00003246 return ASTNodes().Create<ast::DisableValidationAttribute>(ID(), AllocateNodeID(),
3247 validation);
dan sinclair41e4d9a2022-05-01 14:40:55 +00003248 }
3249
3250 /// Sets the current builder source to `src`
3251 /// @param src the Source used for future create() calls
3252 void SetSource(const Source& src) {
3253 AssertNotMoved();
3254 source_ = src;
3255 }
3256
3257 /// Sets the current builder source to `loc`
3258 /// @param loc the Source used for future create() calls
3259 void SetSource(const Source::Location& loc) {
3260 AssertNotMoved();
3261 source_ = Source(loc);
3262 }
3263
3264 /// Helper for returning the resolved semantic type of the expression `expr`.
3265 /// @note As the Resolver is run when the Program is built, this will only be
3266 /// useful for the Resolver itself and tests that use their own Resolver.
3267 /// @param expr the AST expression
3268 /// @return the resolved semantic type for the expression, or nullptr if the
3269 /// expression has no resolved type.
dan sinclair5f764d82022-12-08 00:32:27 +00003270 const type::Type* TypeOf(const ast::Expression* expr) const;
dan sinclair41e4d9a2022-05-01 14:40:55 +00003271
3272 /// Helper for returning the resolved semantic type of the variable `var`.
3273 /// @note As the Resolver is run when the Program is built, this will only be
3274 /// useful for the Resolver itself and tests that use their own Resolver.
3275 /// @param var the AST variable
3276 /// @return the resolved semantic type for the variable, or nullptr if the
3277 /// variable has no resolved type.
dan sinclair5f764d82022-12-08 00:32:27 +00003278 const type::Type* TypeOf(const ast::Variable* var) const;
dan sinclair41e4d9a2022-05-01 14:40:55 +00003279
3280 /// Helper for returning the resolved semantic type of the AST type `type`.
3281 /// @note As the Resolver is run when the Program is built, this will only be
3282 /// useful for the Resolver itself and tests that use their own Resolver.
3283 /// @param type the AST type
3284 /// @return the resolved semantic type for the type, or nullptr if the type
3285 /// has no resolved type.
dan sinclair5f764d82022-12-08 00:32:27 +00003286 const type::Type* TypeOf(const ast::Type* type) const;
dan sinclair41e4d9a2022-05-01 14:40:55 +00003287
3288 /// Helper for returning the resolved semantic type of the AST type
3289 /// declaration `type_decl`.
3290 /// @note As the Resolver is run when the Program is built, this will only be
3291 /// useful for the Resolver itself and tests that use their own Resolver.
3292 /// @param type_decl the AST type declaration
3293 /// @return the resolved semantic type for the type declaration, or nullptr if
3294 /// the type declaration has no resolved type.
dan sinclair5f764d82022-12-08 00:32:27 +00003295 const type::Type* TypeOf(const ast::TypeDecl* type_decl) const;
dan sinclair41e4d9a2022-05-01 14:40:55 +00003296
Ben Clayton1b35e3f2022-05-19 21:55:29 +00003297 /// @param type a type
3298 /// @returns the name for `type` that closely resembles how it would be
3299 /// declared in WGSL.
Ben Claytond7d71882022-09-05 20:51:23 +00003300 std::string FriendlyName(const ast::Type* type) const;
Ben Clayton1b35e3f2022-05-19 21:55:29 +00003301
3302 /// @param type a type
3303 /// @returns the name for `type` that closely resembles how it would be
3304 /// declared in WGSL.
dan sinclair5f764d82022-12-08 00:32:27 +00003305 std::string FriendlyName(const type::Type* type) const;
Ben Clayton1b35e3f2022-05-19 21:55:29 +00003306
3307 /// Overload of FriendlyName, which removes an ambiguity when passing nullptr.
3308 /// Simplifies test code.
3309 /// @returns "<null>"
Ben Claytond7d71882022-09-05 20:51:23 +00003310 std::string FriendlyName(std::nullptr_t) const;
Ben Clayton1b35e3f2022-05-19 21:55:29 +00003311
dan sinclair41e4d9a2022-05-01 14:40:55 +00003312 /// Wraps the ast::Expression in a statement. This is used by tests that
3313 /// construct a partial AST and require the Resolver to reach these
3314 /// nodes.
3315 /// @param expr the ast::Expression to be wrapped by an ast::Statement
3316 /// @return the ast::Statement that wraps the ast::Expression
3317 const ast::Statement* WrapInStatement(const ast::Expression* expr);
3318 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
3319 /// tests that construct a partial AST and require the Resolver to reach
3320 /// these nodes.
3321 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
3322 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
3323 const ast::VariableDeclStatement* WrapInStatement(const ast::Variable* v);
3324 /// Returns the statement argument. Used as a passthrough-overload by
3325 /// WrapInFunction().
3326 /// @param stmt the ast::Statement
3327 /// @return `stmt`
3328 const ast::Statement* WrapInStatement(const ast::Statement* stmt);
3329 /// Wraps the list of arguments in a simple function so that each is reachable
3330 /// by the Resolver.
3331 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
3332 /// @returns the function
Antonio Maioranodfa92a92022-12-23 17:56:56 +00003333 template <typename... ARGS,
3334 typename = traits::EnableIf<(CanWrapInStatement<ARGS>::value && ...)>>
dan sinclair41e4d9a2022-05-01 14:40:55 +00003335 const ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton783b1692022-08-02 17:03:35 +00003336 utils::Vector stmts{
3337 WrapInStatement(std::forward<ARGS>(args))...,
3338 };
Antonio Maioranodfa92a92022-12-23 17:56:56 +00003339 return WrapInFunction(std::move(stmts));
dan sinclair41e4d9a2022-05-01 14:40:55 +00003340 }
3341 /// @param stmts a list of ast::Statement that will be wrapped by a function,
3342 /// so that each statement is reachable by the Resolver.
3343 /// @returns the function
Ben Clayton783b1692022-08-02 17:03:35 +00003344 const ast::Function* WrapInFunction(utils::VectorRef<const ast::Statement*> stmts);
dan sinclair41e4d9a2022-05-01 14:40:55 +00003345
3346 /// The builder types
3347 TypesBuilder const ty{this};
3348
3349 protected:
3350 /// Asserts that the builder has not been moved.
3351 void AssertNotMoved() const;
3352
3353 private:
3354 ProgramID id_;
Ben Clayton4a92a3c2022-07-18 20:50:02 +00003355 ast::NodeID last_ast_node_id_ = ast::NodeID{static_cast<decltype(ast::NodeID::value)>(0) - 1};
dan sinclair837b8042022-12-09 05:00:07 +00003356 type::Manager types_;
dan sinclair41e4d9a2022-05-01 14:40:55 +00003357 ASTNodeAllocator ast_nodes_;
3358 SemNodeAllocator sem_nodes_;
Ben Claytonaa037ac2022-06-29 19:07:30 +00003359 ConstantAllocator constant_nodes_;
dan sinclair41e4d9a2022-05-01 14:40:55 +00003360 ast::Module* ast_;
3361 sem::Info sem_;
3362 SymbolTable symbols_{id_};
3363 diag::List diagnostics_;
3364
3365 /// The source to use when creating AST nodes without providing a Source as
3366 /// the first argument.
3367 Source source_;
3368
3369 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
3370 /// program when built.
3371 bool resolve_on_build_ = true;
3372
3373 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
3374 bool moved_ = false;
Ryan Harrisondbc13af2022-02-21 15:19:07 +00003375};
3376
3377//! @cond Doxygen_Suppress
3378// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
3379template <>
Ben Clayton46ee6392022-11-09 22:04:11 +00003380struct ProgramBuilder::TypesBuilder::CToAST<AInt> {
3381 static const ast::Type* get(const ProgramBuilder::TypesBuilder*) { return nullptr; }
3382};
3383template <>
3384struct ProgramBuilder::TypesBuilder::CToAST<AFloat> {
3385 static const ast::Type* get(const ProgramBuilder::TypesBuilder*) { return nullptr; }
3386};
3387template <>
Ben Clayton0ce9ab02022-05-05 20:23:40 +00003388struct ProgramBuilder::TypesBuilder::CToAST<i32> {
dan sinclair41e4d9a2022-05-01 14:40:55 +00003389 static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) { return t->i32(); }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00003390};
3391template <>
Ben Clayton0ce9ab02022-05-05 20:23:40 +00003392struct ProgramBuilder::TypesBuilder::CToAST<u32> {
dan sinclair41e4d9a2022-05-01 14:40:55 +00003393 static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) { return t->u32(); }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00003394};
3395template <>
Ben Clayton0ce9ab02022-05-05 20:23:40 +00003396struct ProgramBuilder::TypesBuilder::CToAST<f32> {
dan sinclair41e4d9a2022-05-01 14:40:55 +00003397 static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) { return t->f32(); }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00003398};
3399template <>
Zhaoming Jiang62bfd312022-05-13 12:01:11 +00003400struct ProgramBuilder::TypesBuilder::CToAST<f16> {
3401 static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) { return t->f16(); }
3402};
3403template <>
Ryan Harrisondbc13af2022-02-21 15:19:07 +00003404struct ProgramBuilder::TypesBuilder::CToAST<bool> {
dan sinclair41e4d9a2022-05-01 14:40:55 +00003405 static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) { return t->bool_(); }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00003406};
3407template <>
3408struct ProgramBuilder::TypesBuilder::CToAST<void> {
dan sinclair41e4d9a2022-05-01 14:40:55 +00003409 static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) { return t->void_(); }
Ryan Harrisondbc13af2022-02-21 15:19:07 +00003410};
3411//! @endcond
3412
3413/// @param builder the ProgramBuilder
3414/// @returns the ProgramID of the ProgramBuilder
3415inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
dan sinclair41e4d9a2022-05-01 14:40:55 +00003416 return builder->ID();
Ryan Harrisondbc13af2022-02-21 15:19:07 +00003417}
3418
Antonio Maioranodfa92a92022-12-23 17:56:56 +00003419// Primary template for metafunction that evaluates to true iff T can be wrapped in a statement.
3420template <typename T, typename /* = void */>
3421struct CanWrapInStatement : std::false_type {};
3422
3423// Specialization of CanWrapInStatement
3424template <typename T>
3425struct CanWrapInStatement<
3426 T,
3427 std::void_t<decltype(std::declval<ProgramBuilder>().WrapInStatement(std::declval<T>()))>>
3428 : std::true_type {};
3429
Ryan Harrisondbc13af2022-02-21 15:19:07 +00003430} // namespace tint
3431
3432#endif // SRC_TINT_PROGRAM_BUILDER_H_